2

I have images that are centered within their parent divs and I would only like for the images to be clickable links instead of the whole divs themselves. I'm not quite sure what the problem is, so any help would be greatly appreciated.

Here is my code:

HTML:

    <div class="banner_column">
        <a href="#">
        <img src="images/image1.png" alt="Image 1" >
        </a>
        <h1>Check Out Link Above</h1>
        <p>Some text here</p>
    </div>

CSS:

.banner_column {
   width:25%;
   display:inline-block;
   float:left;
   margin-bottom:60px;
}

.banner_column img {
   display:block;
   width:300px;
   height:450px;
   position:relative;
   margin-top:30px;
   margin-left:auto;
   margin-right:auto;
}

The problem is the whole column div is a clickable link instead of just the centered image itself.

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
Jeff P.
  • 2,754
  • 5
  • 19
  • 41

5 Answers5

2

target the <a> tag not the <img> tag. also small note remember to put /> at the end of your <img> tag.

   .banner_column {
       width:25%;
       display:inline-block;
       float:left;
    }

.banner_column a{
    display:inline-block;
    margin:30px auto 60px auto;
    width:300px;
    height:450px;
}

.banner_column img {
    width:100%;
    /*don't worry about height it will auto size*/
}


 <div class="banner_column">
    <a href="#">
        <img src="images/image1.png" alt="Image 1" />
    </a>
    <h1>Check Out Link Above</h1>
    <p>Some text here</p>
</div>

display:block will take up the full width of the parent element every time. if you want it to be inline then use display:inline if you want to have padding and margin and force the element in a block like element to be inline use display:inline-block;.

Patrick W. McMahon
  • 3,488
  • 1
  • 20
  • 31
1

Perhaps you may style your a to center instead of image, then you will not have left and right margins that triggers link.

.banner_column a {
   display:block;
   width:300px;
   height:450px;
   position:relative;
   margin:30px auto 0 auto;
}
Sergiy T.
  • 1,433
  • 1
  • 23
  • 25
0

Edit : Use Sergiy's solution, add in the style for the a tag.

.banner_column a {
   display:block;
   width:300px;
   height:450px;
   position:relative;
    margin:30px auto 0 auto;
}
.banner_column {
  width: 25%;
  display: inline-block;
  float: left;
  margin-bottom: 60px;
}
.banner_column img {
  display: block;
  width: 300px;
  height: 450px;
  position: relative;
  margin-top: 30px;
  margin-left: auto;
  margin-right: auto;
}
<div class="banner_column">
  <a href="#">
    <img src="image.png" alt="Image 1"/>
  </a>
  <h1>Check Out Link Above</h1>
  <p>Some text here</p>
</div>

https://jsfiddle.net/d3euvpr9/

< Obsolete >

I just tried it and your code works perfectly fine when run on chrome. Do you actually have a fiddle where it fails? Because as BlackHatSamurai commented too, this code works fine. Try closing the img tag maybe?

< /Obsolete >

Khaldor
  • 294
  • 1
  • 16
0

I had a similar problem and fixed it with the following CSS targeted at the problem DIV :

#projects {pointer-events: none;} /* prevents the whole div from containing links */

#projects img {pointer-events: all;} /* restores links on just images within the div  */
Gav
  • 1
  • 2
-1
 <img href="your href id" src="images/image1.png" alt="Image 1" > and remove the img in <a>
Tremmillicious
  • 454
  • 4
  • 14
  • 2
    Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long but it is expected. – peterh May 01 '15 at 01:52