Home > Software design >  Add a link to HTML, CSS accordion
Add a link to HTML, CSS accordion

Time:01-14

I am looking to add individual links to the images on a HTML/CSS accordion - however, each time I wrap the img-source in an tag, the formatting of the image block is affected.

The code without the tag is below:

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

<style>
.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%;
}
</style>
 <h1>Accordion Slider</h1>

<div >

    <!-- BOX 1 -->
    <div >
        <div >
            <div >
                <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 >
        <div >
            <div >
                <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 >
        <div >
            <div >
                <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 >
        <div >
            <div >
                <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 >
        <div >
            <div >
                <h2>Lorem Ipsum</h2>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                    Vestibulum iaculis nisl sed dictum aliquam.
                </p>
            </div>
        </div>
    </div>

</div>

This is what it looks like with an tag: Accordion Error

What is the way to add a link to the image source without affecting the formatting of the block?

CodePudding user response:

.box > img properties go to the wrapping anchor tag. And .box:hover > img has to be changed to .box:hover > a. Only included the changed parts of your CSS.

<div >
  <div >
    <a href="#">
      <img src="https://source.unsplash.com/1000x800">
    </a>

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

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

    <span>Hover</span>
  </div>
  <div >
    <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%;
}

CodePudding user response:

instead of warping your box as a <div> wrap it with the <a> tag and add the box class to it, this should work.

  •  Tags:  
  • Related