Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make an anchor element cover an entire div element?

I want when clicking anywhere on the div.parent to act as a link without including the tag in the entire div.des

I try to many times but unable to do it.

div.parent {
  background-color: gray;
  padding: 5px;
}

a {
  width: 100px;
  height: 100px;
  display: block;
  background-color: white
}
<div style="display: flex;justify-content: center;gap: 20px">
  <div class="parent">
    <a href="#"></a>
    <div class="des">
      <p>Title</p>
      <i>Price</i>
    </div>
  </div>

  <div class="parent">
    <a href="#"></a>
    <div class="des">
      <p>Title</p>
      <i>Price</i>
    </div>
  </div>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you don’t want to just wrap your div with the anchor we can learn from Bootstrap’s stretched links and use an absolutely-positioned pseudo-element on the anchor.

div.parent {
  background-color: gray;
  padding: 5px;
  position: relative;
}

a {
  width: 100px;
  height: 100px;
  display: block;
  background-color: white
}

a::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<div style="display: flex;justify-content: center;gap: 20px">
  <div class="parent">
    <a href="#"></a>
    <div class="des">
      <p>Title</p>
      <i>Price</i>
    </div>
  </div>

  <div class="parent">
    <a href="#"></a>
    <div class="des">
      <p>Title</p>
      <i>Price</i>
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading