添加 link 到 HTML、CSS 手风琴

Add a link to HTML, CSS accordion

我想在 HTML/CSS 手风琴上的图像中添加单独的 links - 但是,每次我将 img-source 包装在标签中时,图像块的格式是受影响。

没有标签的代码如下:

https://codepen.io/knyttneve/pen/YgZbLO

.container {
  display: flex;
  width: 100%;
  padding: 4% 2%;
  box-sizing: border-box;
  height: 100vh;
}

.box {
  flex: 1;
  overflow: hidden;
  transition: 0.5s;
  margin: 0 2%;
  box-shadow: 0 20px 30px rgba(0, 0, 0, 0.1);
  line-height: 0;
}

.box>img {
  width: 200%;
  height: calc(100% - 10vh);
  -o-object-fit: cover;
  object-fit: cover;
  transition: 0.5s;
}

.box>span {
  font-size: 3.8vh;
  display: block;
  text-align: center;
  height: 10vh;
  line-height: 2.6;
}

.box:hover {
  flex: 1 1 50%;
}

.box:hover>img {
  width: 100%;
  height: 100%;
}
<h1>Accordion Slider</h1>

<div class="accordion">

  <!-- BOX 1 -->
  <div class="box a1">
    <div class="image_1">
      <div class="text">
        <h2>Lorem Ipsum</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis nisl sed dictum aliquam.
        </p>
      </div>
    </div>
  </div>

  <!-- BOX 2 -->
  <div class="box a2">
    <div class="image_2">
      <div class="text">
        <h2>Lorem Ipsum</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis nisl sed dictum aliquam.
        </p>
      </div>
    </div>
  </div>

  <!-- BOX 3 -->
  <div class="box a3">
    <div class="image_3">
      <div class="text">
        <h2>Lorem Ipsum</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis nisl sed dictum aliquam.
        </p>
      </div>
    </div>
  </div>

  <!-- BOX 1 -->
  <div class="box a4">
    <div class="image_4">
      <div class="text">
        <h2>Lorem Ipsum</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis nisl sed dictum aliquam.
        </p>
      </div>
    </div>
  </div>

  <!-- BOX 5 -->
  <div class="box a5">
    <div class="image_5">
      <div class="text">
        <h2>Lorem Ipsum</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis nisl sed dictum aliquam.
        </p>
      </div>
    </div>
  </div>

</div>

这是带有标签的样子: Accordion Error

有什么方法可以在不影响block格式的情况下,给图片源添加一个link?

.box > img 属性转到包装锚标记。而.box:hover > img也得改成.box:hover > a。仅包含您 CSS.

的更改部分
<div class="container">
  <div class="box">
    <a href="#">
      <img src="https://source.unsplash.com/1000x800">
    </a>

    <span>CSS</span>
  </div>
  <div class="box">
    <a href="#">
      <img src="https://source.unsplash.com/1000x802">
    </a>

    <span>Image</span>
  </div>
  <div class="box">
    <a href="#"><img src="https://source.unsplash.com/1000x804"></a>

    <span>Hover</span>
  </div>
  <div class="box">
    <a href="#"> <img src="https://source.unsplash.com/1000x806"></a>

    <span>Effect</span>
  </div>
</div>
.box > a {
  display: inline-block;
  width: 200%;
  height: calc(100% - 10vh);
  transition: .5s;
}

.box > a > img {
  width: 100%;
  height: 100%;
  object-fit: cover; 
  transition: .5s;
}

.box:hover > a {
  width: 100%;
  height: 100%;
}

与其将您的盒子变形为 <div>,不如用 <a> 标签包裹它并向其添加 box class,这应该可行。