17

I have a basic menu, made from a horizontally aligned list (<li>), each containing an icon image and some text:

Menu bar

One of the <li> contains an extra image with display: none; so that the icon can be toggled (from a green to a red pepper, in this example. The problem is that it doesn't align correctly on some browsers, as shown in the above image. My understanding was that in contrast to visibilty: hidden;, an element with display: none; should not affect the position of any other element and should render as if it's not there?

The browsers where it doesn't render correctly are Google Chrome and Safari - but only on MacOS(!?) and IE7 (I know, I know...) on Windows. Every other browser / OS combination I've tested works fine.

Here's the HTML:

<ul class="menu">
    <li><img alt="Green Pepper" src="/green.png">li</li>
    <li><img alt="Green Pepper" src="/green.png">li</li>
    <li><img alt="Green Pepper" src="/green.png">li</li>
    <li id="change">
        <img alt="Red Pepper" src="/red.png" style="display: none;">
        <img alt="Green Pepper" src="/green.png"> 
        li
    </li>
    <li><img alt="Green Pepper" src="/green.png">li</li>
</ul>

Here's the CSS:

.menu li {
    cursor: default;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10pt;
    list-style-type: none; 
    position: relative; 
    text-align:center; 
    margin: 0 0 0 -25px; 
    padding: 8px 0 0 0;
    width: 144px;
    height: 35px;
    display: inline-block;
    background-image: url(../bct-white.png);
    background-repeat: no-repeat;
    color: #0091c1;
}

And for the icon images:

.menu img {
    display: inline;
    vertical-align: -25%;
    padding-right: 6px;
}

I've also had to include a browser hack for IE7 because it doesn't recognise inline-block, coming from a separate stylesheet based on a conditional import (<!--[if lte IE 7]>):

.menu li {
    zoom: 1;
    display: inline;
}

Although, obviously that style isn't loaded on Chrome and Safari regardless of OS, so can't be causing my issue on Macs.

I know the quickest solution would be to refactor the HTML and the JavaScript manipulation of the show / hide of the icons, but I'd really like to know what causes this issue and how to resolve it.


Update

I've tracked the cause down. Basically, the element style of display: none; on the <img> element overrides the inline from the .menu img rule. Removing that, then toggling between block and inline allows you to reproduce the issue. This is obviously a browser bug, and while the element is not displayed being an in-line or block element should have no effect on the layout.


jsFiddles

Issue with Chrome and Safari on Macs only

Issue with extra CSS for IE7 only

Note! For me, the Fiddle page didn't load properly using IE7, but the direct link for the result iFrame is http://fiddle.jshell.net/z4dU7/3/show/

Bounty update!!!

I've posted one fix below, but it actually introduces the same layout problem in IE9! Please feel free to evolve or improve on my answer - or come to the table with something completely different! :)

Michael
  • 7,210
  • 8
  • 47
  • 84
  • I think we need to see this in action in order to be able to help you. I did a quick fiddle and could not reproduce this behaviour — display:none removed the image from the layout (as it should). Maybe `#change` has some other styles applied to it? – David Hellsing Jul 11 '13 at 12:10
  • This fiddle demo's it in IE7: http://jsfiddle.net/z4dU7/3/ To see the issue on the Mac, just remove the last style rule (that's pulled in from an IE specific style-sheet on my system). – Michael Jul 11 '13 at 14:07
  • @David I've updated my question with links to both scenarios. – Michael Jul 11 '13 at 14:14
  • The first example looks OK in my chrome/mac: http://imgur.com/nYgBIG0 or am I missing something? – David Hellsing Jul 11 '13 at 14:26
  • 4
    You might want to consider using background images anyway, that way they will never affect the visual flow. – David Hellsing Jul 11 '13 at 14:28
  • @David Strange - that's exactly as it should display. Perfectly in-line with the deliberate overlap. Does it render correctly in Safari for you too? – Michael Jul 11 '13 at 14:29
  • In my actual environment, the menu buttons each have the same background, but the icons differ for each one and have a negative to contrast with the blue background. – Michael Jul 11 '13 at 14:32
  • Safari: same (it looks right) – David Hellsing Jul 11 '13 at 14:44
  • I would agree with David that background images would be a better solution. Not only will it not impact your layout, but it is also semantically right: You don't want to show an image but a kind of decoration. – KFleischer Jul 16 '13 at 19:35
  • I agree with David too, putting the images to a background is a better solution. – LinkinTED Jul 17 '13 at 07:23
  • @Downvoter Please explain what's wrong with my question? – Michael Jul 19 '13 at 11:49
  • @Mikaveli I've updated my background image method to more closely resemble wheat you were shooting for, see update – apaul Jul 21 '13 at 23:24
  • @Mikaveli Your fiddle seems to be rendering fine in Chrome/Safari on OSX for me, any details about browser version? – godfrzero Jul 22 '13 at 03:10
  • 1
    Sorry it might have been suggested yet, but i am lazy and i don't want to read all the answers.If your point is to toggle the image , i assume there is some sort of a user action which is firing it. Why not just change the image's src attribute once this action is performed with some javascript ? Since there would be no more hidden image, the problem should be gone am i right ? – nubinub Jul 23 '13 at 13:25

6 Answers6

11

Scrap Approach and Use Background Images

http://jsfiddle.net/P5CKC/2/

<ul class="menu">
    <li><span>Li</span></li>
    <li><span>Li</span></li>
    <li><span>Li</span></li>
    <li class="change"><span>Li</span></li>
    <li><span>Li</span></li>
</ul>

CSS

ul.menu {
    overlflow: hidden;
}
ul.menu li {
    float: left;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10pt;
    text-align:center; 
    margin: 0 0 0 -25px; 
    width: 152px;
    line-height: 35px;
    height: 35px;
    background: url(../bct-white.png) no-repeat;
    color: #0091c1;
}
ul.menu li span {
    background: url(/green.png) no-repeat 5px 6px;
    display: block;
}
ul.menu li.change span {
    background-image: url(/red.png);
}

CSS2.0 and browser compatibility

The code application I have provided is Css2.0 and should easily work in IE7 and above.

  1. Removed img tags and implemmented aesthetics (images) as backgrounds
  2. Extra span had to be added because CSS2 allows only 1 background image per element
  3. Li tag holds the arrow background; span tag holds the pepper background
  4. Updated id="change" to class="change". UNLESS you are 100% certain that you will only have one #change element, use a class. This is purely styling and it prevents you from having two menu lists on the same page.
  5. Tweaked your CSS styling a bit as follows:

Removed top padding and increased the height. So your li elements are the same height BUT then added line-height: 35px -> this is the best way to vertically center text. Utlizing top padding works but it is prone to poor browser inconsistency.

Change li elements to floats. Floated elements are the most IE7 friendly method! Even IE6 will not bug out but I don't have that old version to test your webpage in. FYI - ul.menu has to have overflow: hidden to clear the floats.

position: relative; 
cursor: default;

Unless you changed the defaults, you can keep these two properties out. cursor should be default. Position: relative is unnecessary - you aren't using absolute positioning or anything that warrants its need. Now, you can keep these in your declaration. I just like code to be as "slim" as possible.

final words:

Take a look at my CSS. Notice how I used ul.menu in all my declaration. You may want to get in the habit of doign the same; this provides the developer some insight on what the HTML looks like and more importantly - your css will not get overrided if you decide to add <div class=menu> later on. Specfically .menu img would apply to any image tag within the menu div.

Okay - that's it. Let me know if there are any clarfications.

FYI - seeing as this question has a bounty, if you provide me with the background images I can polish my code to suit your needs 100% - perhaps upload them in an edit of your answer.

ProfileTwist
  • 1,423
  • 1
  • 12
  • 18
  • +1 I like this solution and it's been explained well. Using the internal spans avoids the issue of needing multiple background images nicely and isn't a big modification to the HTML. – Michael Jul 19 '13 at 11:07
  • 2
    +1. [The right approach](http://stackoverflow.com/questions/492809/when-to-use-img-vs-css-background-image). To me, using ``s for layout is like using ``s for layout.
    – Artyom Jul 23 '13 at 08:53
3

There is whitespace between your elements. None of your other list items have that whitespace. Try removing all whitespace between those elements and see if that fixes your problem.

If it does, it just means that your HTML is parsing the content of your <li> as having a line break - which would be why you are seeing an issue. To solve this, wrap the text in your <li> with a <span>. When your browser parses HTML it will automatically trim whitespace between HTML tags and automatically fix the issue (without losing your formatting)

Matthew R.
  • 4,152
  • 1
  • 20
  • 39
2

This is an odd one but I could recreate the issue in Firefox v22. The really odd part was that in the jsFiddle if I just hit "Tidy Up" and then hit "Run" and the problem was solved. As Matthew R mentioned this is likely due to the additional white space being removed.

Broken: enter image description here

Tidy Up, Run, and Fixed enter image description here

Here's an alternate method using background images:

Working Example

.menu li {
  cursor: default;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 10pt;
  list-style-type: none;
  position: relative;
  text-align: center;
  margin: 0 0 0 -25px;
  padding: 8px 0 0 10px;
  width: 118px;
  height: 20px;
  display: inline-block;
  background-image: url(http://modmyi.com/attachments/forums/iphone-4-4s-new-skins-themes-launches/465481d1282224308-classified-hd-cydia-released-goldborder.png), url(http://i.stack.imgur.com/dmHl0.png);
  background-position: 0% 0%, 44% 75%;
  background-size: 128px, 18px;
  background-repeat: no-repeat;
  color: #0091c1;
}

.menu img {
  display: inline;
  vertical-align: -25%;
  padding-right: 6px;
}

#change:hover {
  background-image: url(http://modmyi.com/attachments/forums/iphone-4-4s-new-skins-themes-launches/465481d1282224308-classified-hd-cydia-released-goldborder.png), url(http://www.chiletownhotsauce.com/images/stories/Red%20bell%20pepper.jpg);
  background-repeat: no-repeat;
  background-position: 0% 0%, 44% 75%;
  background-size: 128px, 18px;
}
<ul class="menu">
  <li>li</li>
  <li>li</li>
  <li>li</li>
  <li id="change">li</li>
  <li>li</li>
</ul>
Community
  • 1
  • 1
apaul
  • 15,557
  • 8
  • 44
  • 76
1

I think your issue is with the box model and display:inline-block. When I use display:inline-block in horizontal menus, I also use a clear fix for the parent and float:left; with the children.
In this example of the css, I put some values in the .menu that can be inherited and I used overflow:hidden; to address the clear fix. Then I used float:left; in the li to force them tight and avoid any weird spacing caused by the browsers defaults. And then I changed the vertical-align value to middle for the image. When toggled display:none; with inline, it all looked correct to me. I did not test in all the browsers listed. I also used the first piece of CSS there for the css reset, which may be redundant but I always find it useful on JSFiddle.

* {
    margin:0;
    padding:0;
}
.menu {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10pt;
    list-style-type: none;
    color: #0091c1;
    overflow:hidden;
}
.menu li {
    cursor: default; 
    position: relative; 
    text-align:center; 
    /* margin: 0 0 0 -25px; */
    padding: 8px 0 0 0;
    width: 118px;
    height: 20px;
    display: inline-block;
    float:left;
    background-image: url(http://modmyi.com/attachments/forums/iphone-4-4s-new-skins-themes-launches/465481d1282224308-classified-hd-cydia-released-goldborder.png);
    background-repeat: no-repeat;
}

.menu img {
    display: inline;
    /* vertical-align: -25%; */
    padding-right: 6px;
    vertical-align:middle;
}
Jason Lydon
  • 6,536
  • 1
  • 30
  • 41
0

To resolve the (appearance of) the issue, change the vertical alignment of the icon images contained in the menu:

.menu img {
    display: inline;
    vertical-align: top;
    margin-top: 2px;
    padding-right: 6px;
}

On the affected browsers, the additional image (with display: none;) was altering the height of the containing element. Setting the <img> to display: none; meant that the display: inline; setting no longer applied and was positioned underneath the first image.

Just having the extra <img> displaying in-line fixes the vertical alignment, but obviously doesn't fix the issue as the image needs to be hidden without changing the position of the text.

This fix changes the vertical alignment from a percentage (-25%) to a fixed position, avoiding any (spurious) differences in the height of the containing element. I've then set the top margin of the image to get the desired spacing from the top edge of the element.

Update

This fix breaks the display in IE9 - same appearance as IE7 without this fix.

Michael
  • 7,210
  • 8
  • 47
  • 84
  • 1
    you could try to set the width and height of the hidden images to say 1 or 0. that way even if they somehow affect the layout, it's not visible. – Ruben Verschueren Jul 17 '13 at 08:14
0

You made a mistake with the line-height instead of 35px give 23px

<ul class="menu">
  <li><span>Li</span></li>
  <li><span>Li</span></li>
  <li><span>Li</span></li>
  <li class="change"><span>Li</span></li>
  <li><span>Li</span></li>
</ul>
ul.menu {
  overlflow: hidden;
}
ul.menu li {
  float: left;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 10pt;
  text-align: center;
  margin: 0 0 0 -25px;
  width: 152px;
  line-height: 23px;
  list-style: none;
  height: 35px;
  background: #ccc;
  color: #0091c1;
}
ul.menu li span {
  background: url(http://i.stack.imgur.com/dmHl0.png) no-repeat 5px 6px;
  display: block;
}
ul.menu li.change span {
  background-image: url(/red.png);
}

check the output

jsfiddle-link

enter image description here

Igor Ivancha
  • 3,231
  • 4
  • 28
  • 39
GaneshKumar
  • 159
  • 1
  • 11