Hi. I just started learning html and css in the past couple of weeks so please bear with me
I have been practicing creating a horizontal nav menu and got it working ok until i add an image to one of the menu items.
When the image is the same height as the text it looks ok. However if i resize the image height over 18px my text is no longer centered vertically on the menu items
(CSS line 54 is the image)
.nav-img {
margin-right: 20px;
height: 18px;
float:left;
}
Also when i resize the image height to over 32px, the grey borders do not extend to the bottom of the nav bar.
Iām sure thereās a very simple explanation of why this is happening, but i would like to know how to do this properly so that whenever i create a navbar (or anything else) and it has an image of any size i can be sure the text always aligns vertically and borders look right.
This happens because the height of your nav is determined by the biggest item it contains.
While the text is the biggest thing, its height the sum of font size and top/bottom padding, the text is centred vertically with the grey borders full height.
But when you make your image bigger than the text, the text does not magically get any bigger as a result of the image being bigger.
So the fix may be to also make the text bigger so the enlarged image is still not the biggest element.
its height the sum of font size and top/bottom padding
So increasing either or both of these can make the text element bigger than the image.
In this fork the image is 36px high, but the text has its padding put up to 18px.
Another way to stop the image interfering with the size would be to make it a background.
As @SamA74 explained, the image takes too much height.
There are several options to shrink the height it claims, one is to use negative margins top and bottom.
To, despite the negative margins, vertical centre the image on the text line, you can display it as an inline-block and use vertical align. This requires that itās not floated.
Great! thank you for the help. Is there a way to adjust only the padding for the text? I noticed on your fork adjusting the padding to 18px also pushed the image down?
Yes that pushes the image down, off centre. To have separate padding, you would need to put them in separate elements, maybe a <span> around the text.
Or you could do as @Erik_J suggests and use a negative margin to nudge it up a bit, or use relative positioning instead.
Any of these will work, but not be 100% robust if people alter the font size in their browsers. Using em units over px may overcome that, but one step at a time for now.
Youāre right, it does seem to handle font zooming. I was thinking that all those magic numbers and negative margins would be pretty fragile, but it appears to work.
And if we follow @SamA74ās background option, we could move the image from html foreground to css background and use the CSS3 ābackground resizingā property for browsers that support it and ems for horizontal space.
Have you tried both snippits of code in your browser? Did you try zooming the text size in Firefox to simulate different user preferences/browser font-size settings?
Did you not see a difference in the behavior of the icon in the snippits?
I just tried them both in chrome and FF. In the 2nd example the icon is slightly larger at 100%, but i see no difference in behaviour in zoom for either snippet
Let me ask you to copy the code that I posted in #12 to a file and open it in your browser and perform the same Zoom test. It is meant to be a standalone demo. Compare the behavior of the icon and text in that demo. I think it might be better behaved that Samās and Erikās, although the image is still in the foreground and not in the background. Semantically, background would probably be more appropriate, but just look at the behavior of the demo and see if that is close to you wish. The code with the image in the background can be tweaked.
ok yes i see it scales much better in the code you posted. thanks again ill see if i can play around with yours and get the image in the background tomorrow