1

I'm having an issue getting Chrome and Safari to correctly display the rollover div above my container image. What I have done works sometimes, but when resizing the page, you can sometimes see a little gap of 1 or 2 pixels between the rollover div and the image container.

I made a demo here: http://jsfiddle.net/eJNsS/9/

Screenshot enter image description here

HTML

<div id="related_projects">
    <a href=#  ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image"  /></div><h2 class="transparent_font">IMG1</h2></div></a>          
    <a href=#  ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image"  /></div><h2 class="transparent_font">IMG2</h2></div></a>          
    <a href=#  ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image"  /></div><h2 class="transparent_font">IMG3</h2></div></a>          
    <a href=#  ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image" /></div><h2 class="transparent_font">IMG4</h2></div></a>           
    <a href=# ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image" /></div><h2 class="transparent_font">IMG5</h2></div></a>            
    <a href=#  ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image" /></div><h2 class="transparent_font">IMG6</h2></div></a>           
    <a href=# ><div class="thumbnail_image_related_project"><div id="image"><img width="300" height="173" src="http://www.gulfster.com/Lessern/Gulfsterhat.jpg" class="attachment-post-thumbnail wp-post-image" /></div><h2 class="transparent_font">IMG7</h2></div></a>            

CSS

#related_projects {
    background-color :#FFFF44;
    margin-bottom: 20px;
    position: relative;
    top: 0;
    left: 0;
    overflow: hidden;
    margin-bottom: 20px;
    display: block;
    margin-top: 2%;
}
#related_projects .thumbnail_image_related_project {
    width: 23%;
    position: relative;
    float: left;
    top: 0;
    left: 0;
    margin-left: 1%;
    margin-right: 1%;
    margin-top: 0%;
    margin-bottom: 2%;
    overflow: hidden;
    line-height: 0;
}
#related_projects img {
    width: 100% !important;
    height: auto;
}
#related_projects p, #categories_container #left p {
    text-align: left;
    color: black;
    font-family: HermL, Arial, sans-serif;
    font-size: 17px;
    line-height: 23px;
    margin-bottom: 20px;
}
#link_description {
    float: left;
    width: 98%;
    margin-left: 1%;
    margin-right: 1%;
}
#link_description #link {
    width: 50%;
    float: right;
    color: black;
    font-family: HermL, Arial, sans-serif;
    font-size: 17px;
    line-height: 23px;
}
#link_description #link a {
    color: #666666;
    font-family: HermL, Arial, sans-serif;
    font-size: 17px;
}
#link_description #link a:hover {
    color: #22B573;
    font-family: HermL, Arial, sans-serif;
    font-size: 17px;
}

/* THUMBNAIL PICTURES
-------------------------------------------------------------- */

.thumbnail_image_related_project .transparent_font {
    line-height: 1.25em;
    position: absolute;
    top: 0;
    left: 0;
    color: black;
    background-color:white; 
    width: 92%;
    padding-right: 3%;
    height: 100%;
    padding-left: 5%;
    opacity:1;
    filter: alpha(opacity=0);
}
.thumbnail_image_related_project h2 {
    padding-top: 30px;
    text-align: left;
    color: black;
    font-family: HermL, Arial, sans-serif;
    font-size: 17px;
    line-height: 23px;
}
h2 {
    margin:0px;
}

jQuery

$("#slider").hover(
    function(){
        $(".caption").stop().animate({'opacity' : '0.9'}, 200);
    },
    function(){
        $(".caption").stop().animate({'opacity' : '0'}, 100);
    }
);

$(".thumbnail_image_related_project").hover(
    function(){
        $(this).children(".transparent_font").stop()
               .animate({'opacity' : '0.7', 'padding-top': '10px'}, 300);
    },
    function(){
        $(this).children(".transparent_font").stop()
               .animate({'opacity' : '0', 'padding-top': '30px'}, 300);
    }
);

Thanks for helping

Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
Romain
  • 1,903
  • 2
  • 18
  • 23
  • Probably sub-pixel problem. – Damonsson Mar 21 '13 at 21:23
  • 1
    @Damonsson what is a sub-pixel problem? – dezman Mar 21 '13 at 21:24
  • Browser badly divided by percentages. Google turned off? – Damonsson Mar 21 '13 at 21:25
  • 2
    It seems fine in Chrome, or I don't understand your issue? – dezman Mar 21 '13 at 21:26
  • Does any one have any ideas how can I bypass this problem? Useful links, example of working rollover etc. Thanks – Romain Mar 22 '13 at 09:38
  • I'm using a mac, but, when you resize the window, some times we have this pixel gap. – Romain May 02 '13 at 16:08
  • 1
    This definitely looks like a pixel rounding issue. If I change the width on `#related_projects .thumbnail_image_related_project` form a percentage to a pixel value (say 200px) the issue goes away. – j08691 May 02 '13 at 16:30
  • Yes, it is a percentage problem. Use pixels. The web does not do half % pixels, so when you expand, it doesn't know which whole number to round up to. I don't understand why you are doing it this way anyways. It seems that there's an easier way – ntgCleaner May 02 '13 at 16:35

2 Answers2

1

I changed your CSS a little bit and moved some CSS properties to focus your links containers within the #related_projects. The links didn't have 100% width and height of their contents the way you had it.

This code works for me, let me know if that's what you were looking for. You would have to fix your margins, though.

Also instead of putting the padding on the h2 tag, I changed your markup and put it on spans within the h2.

updated jsFiddle

Edit: the issues is fixed in Chrome, however you can still see it in Safari.

Edit2: Updated jsFiddle, I can't see the problem on my end both in Chrome and Safari

Stefan
  • 1,096
  • 8
  • 10
  • Thanks for your answer, otherwise, there are still gaps. (when, I resize the screen, we can see plenty of them). Maybe, I need to find a different way to do it... – Romain May 02 '13 at 16:34
  • Updated fiddle, see if it makes a difference. – Stefan May 02 '13 at 16:36
  • Still the same on chrome and safari on mac and pc. +1 for your help, more later – Romain May 02 '13 at 16:38
1

Pixel rounding errors

The h2 tag is not always entirely covering the image beneath it. The black line is the part of the image that's not covered. This is due to pixel rounding errors that occur when the content is scaled.

There are 2 places where a noticeable pixel rounding error is occuring:

  1. The absolute positioning of the h2 tag. Even when using top:0; left:0, this can still introduce a rounding error. When this happens, the h2 is displayed at a slight offset, so that it's not lined up consistently with the img tag.
  2. Splitting up the total width of the h2 tag into 8% for the combined horizontal padding and 92% for the interior content. In effect, the combined % values do not always add up to 100% when scaled.

Either one of these by themselves causes inconsistent rounding errors to occur between the h2 tag and the img tag. And inconsistencies between the 2 elements tend to be noticeable visually.

Solutions

Updated demo

The key concept for avoiding this inconsistency is to ensure that the h2 tag has the exact same rounding errors as the img tag. There's no practical way to entirely avoid rounding errors of this sort, but as long as they're consistent between the 2 elements, they won't be noticeable in this case.

To fix #1, add position:relative; to the img tag. Any rounding errors in the relative positioning of the img tag will be consistent with the rounding errors in the absolute positioniong of the h2 tag (since their positioning is relative to the same element). This essentially cancels out the rounding errors caused by the absolute positioning.

CSS added

#related_projects img {
    position: relative;
    ...
}

To fix #2, use one of the following approaches:

  • Change the width of the h2 tag to 100% and add box-sizing: border-box; (as well as the -moz- and -webkit- versions). This won't work for IE7 or earlier. Demo using box-sizing.
  • Remove the horizontal padding from the h2 tag, and add a text-indent to simulate the left padding (if that's adequate). Demo using text-indent.
  • Remove the horizontal padding from the h2 tag, and add a wrapper element inside or outside of the h2 tag, with the outer element having width:%100; position:absolute;, and the inner element having padding: 0 3% 0 5%;. Typically this would be done by adding a span inside the h2 tag.

Any of the above approaches prevent this particular rounding error from occurring. The updated demo mentioned above uses box-sizing. As long as support for IE7 or earlier isn't needed, this is probably the most elegant fix for #2, and it doesn't require adding additional HTML elements.

CSS removed

.thumbnail_image_related_project .transparent_font {
    width: 92%;
    ...
}

CSS added

.thumbnail_image_related_project .transparent_font {
    width: 100%;
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
    ...
}

If support for IE7 or earlier is required, and text-indent isn't adequate, and editing the static HTML isn't an option, then use JavaScript or jQuery to inject a wrapper element.

Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
  • Thanks a lot for your detail answer. This fixed my problem. Note: I had a new div inside the h2 tag to indent the text as you said. Thanks again. Your bounty is coming in 4 hours =) – Romain May 03 '13 at 11:34
  • @Romain: I overlooked a simpler solution for issue #2: Using `width: 100%` for the `h2` tag and adding `box-sizing: border-box` (not supported by IE7 or earlier). This doesn't require adding any additional HTML elements, and it doesn't require changing the padding. I updated the answer and the demo to address this. – Matt Coughlin May 03 '13 at 15:39
  • Both of your solutions are perfectly working, thanks a lot and nice job – Romain May 03 '13 at 16:13