如何将整个内联块级元素作为超链接。

4
这里是CodePen链接。我想让整个立方体(div)作为超链接。我可以通过在其周围添加标签来实现此目的。但是,由于立方体具有display: inline-block;属性,因此超链接实际上也会被包含在立方体下面。如果你悬停在立方体下面,你会发现它。
我该怎么做才能去掉底部的链接,同时保持立方体为inline-block
以下是代码:
<a href="example.com" target="_blank">
    <div class="cube">
        <div class="flippety">
            <h1>Flippity</h1>
        </div>
        <div class="flop">
            <h2>Flop</h2>
        </div>
    </div>
</a>

/* Set-up */
body {
    color: rgb(6, 106, 117);
    text-transform: uppercase;
    font-family: sans-serif;
    font-size: 100%;
    background: #F4F6F8;
    padding: 3em 0 0 0;
    line-height: 62px;
    -webkit-perspective: 1000px; 
}

/* Container box to set the sides relative to */
.cube {
    display: inline-block;
    width: 30%;
    text-align: center;
    margin: 0 auto;
    height: 200px;

    -webkit-transition: -webkit-transform .33s;
    transition: transform .33s; /* Animate the transform properties */

    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d; /* <-NB */
}

/* The two faces of the cube */
.flippety,.flop {
    background: rgb(247, 247, 247);
    border: 1px solid rgba(147, 184, 189, .8);

    -webkit-border-radius: 5px;
    border-radius: 5px;

    -webkit-box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
    box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
    height: 200px;
}

/* Position the faces */
.flippety {
    -webkit-transform: translateZ(100px);
    transform: translateZ(100px);
}

.flop {
    -webkit-transform: rotateX(-90deg) translateZ(-100px);
    transform: rotateX(-90deg) translateZ(-100px);
}

/* Rotate the cube */
.cube:hover {
    -webkit-transform: rotateX(90deg);
    transform: rotateX(90deg); /* Text bleed at 90º */
}
3个回答

1
将 inline-block 和 width 属性移动到 a 元素中。

/* Set-up */

body {
  color: rgb(6, 106, 117);
  text-transform: uppercase;
  font-family: sans-serif;
  font-size: 100%;
  background: #F4F6F8;
  padding: 3em 0 0 0;
  line-height: 62px;
  perspective: 1000px;
}

a {
  display: inline-block;
  width: 30%;
}


/* Container box to set the sides relative to */

.cube {
  text-align: center;
  margin: 0 auto;
  height: 200px;
  transition: transform .33s;
  transform-style: preserve-3d;
}


/* The two faces of the cube */

.flippety,
.flop {
  background: rgb(247, 247, 247);
  border: 1px solid rgba(147, 184, 189, .8);
  border-radius: 5px;
  box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
  height: 200px;
}


/* Position the faces */

.flippety {
  transform: translateZ(100px);
}

.flop {
  transform: rotateX(-90deg) translateZ(-100px);
}


/* Rotate the cube */

.cube:hover {
  transform: rotateX(90deg);
}
<a href="example.com" target="_blank">
  <div class="cube">
    <div class="flippety">
      <h1>Flippity</h1>
    </div>
    <div class="flop">
      <h2>Flop</h2>
    </div>
  </div>
</a>

或者将立方体作为您的a元素,避免使用额外的元素:

/* Set-up */

body {
  color: rgb(6, 106, 117);
  text-transform: uppercase;
  font-family: sans-serif;
  font-size: 100%;
  background: #F4F6F8;
  padding: 3em 0 0 0;
  line-height: 62px;
  perspective: 1000px;
}

a.cube {
  display: inline-block;
  width: 30%;
  text-align: center;
  margin: 0 auto;
  height: 200px;
  transition: transform .33s;
  transform-style: preserve-3d;
}


/* The two faces of the cube */

.flippety,
.flop {
  background: rgb(247, 247, 247);
  border: 1px solid rgba(147, 184, 189, .8);
  border-radius: 5px;
  box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
  height: 200px;
}


/* Position the faces */

.flippety {
  transform: translateZ(100px);
}

.flop {
  transform: rotateX(-90deg) translateZ(-100px);
}


/* Rotate the cube */

.cube:hover {
  transform: rotateX(90deg);
}
<a href="example.com" target="_blank" class="cube">
    <div class="flippety">
      <h1>Flippity</h1>
    </div>
    <div class="flop">
      <h2>Flop</h2>
    </div>
</a>


这似乎是最优雅的解决方案。谢谢! - Shawn Zhang

0

1
这是我的一个问题之一。现在,您可以看到超链接水平扩展了。 - Shawn Zhang

-1
也许可以考虑使用JavaScript在立方体上创建一个点击事件,并从方程式中删除锚点:

document.getElementsByClassName("cube")[0].addEventListener("click", function() {
  window.open('https://stackoverflow.com', '_blank');
}, false);
/* Set-up */

body {
  color: rgb(6, 106, 117);
  text-transform: uppercase;
  font-family: sans-serif;
  font-size: 100%;
  background: #F4F6F8;
  padding: 3em 0 0 0;
  line-height: 62px;
  -webkit-perspective: 1000px;
}


/* Container box to set the sides relative to */

.cube {
  cursor: pointer;
  display: inline-block;
  width: 30%;
  text-align: center;
  margin: 0 auto;
  height: 200px;
  -webkit-transition: -webkit-transform .33s;
  transition: transform .33s;
  /* Animate the transform properties */
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  /* <-NB */
}


/* The two faces of the cube */

.flippety,
.flop {
  background: rgb(247, 247, 247);
  border: 1px solid rgba(147, 184, 189, .8);
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -webkit-box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
  box-shadow: 0 5px 20px rgba(105, 108, 109, .3), 0 0 8px 5px rgba(208, 223, 226, .4) inset;
  height: 200px;
}


/* Position the faces */

.flippety {
  -webkit-transform: translateZ(100px);
  transform: translateZ(100px);
}

.flop {
  -webkit-transform: rotateX(-90deg) translateZ(-100px);
  transform: rotateX(-90deg) translateZ(-100px);
}


/* Rotate the cube */

.cube:hover {
  -webkit-transform: rotateX(90deg);
  transform: rotateX(90deg);
  /* Text bleed at 90º */
}
<!-- <a href="example.com" target="_blank"> -->
<div class="cube">
  <div class="flippety">
    <h1>Flippity</h1>
  </div>
  <div class="flop">
    <h2>Flop</h2>
  </div>
</div>
<!-- </a> -->


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接