Is a bulleted list item always indented exactly 1.8em? - css

I made a css-only dropdown menu. The requirement was to have a horizontal bar of items that can each drop down a vertical menu. Furthermore, those items should not drop a tertiary menu, but instead just show bulleted lists. My html has three nested ul and the menu is working perfectly in all modern browsers. It looks like this:
However, I did not like how the darker box behind the link is starting right of the bullet and does not stretch over the whole menu width, so I played around a bit and finally came to this tweak:
#nav li ul li ul li a {
padding-left:1.8em;
margin-left:-1.8em;
}
Now the bulleted menu item looks just like I wanted:
And due to the nature of em beeing relative to the font size, it works independently of the font size, like shown with a larger font size here:
I tested this on Internet Explorer 8+9+10(developer preview), Firefox 3+7, latest Chrome, Opera and Safari and it works like a charm.
However, I just dont understand why it is exactly 1.8em that does the job. How come every browser indents the bullet items exactly this far? I searched the internet on this topic, but I did not find anything helpful. Can I be sure this works on future browsers? Are those 1.8em specified in the HTML standard?
Thanks in advance for any hint!
Edit:
To DisgruntledGoat's answer: It would not work if I used 1em/-1em or 20px/-20px. With this style:
#nav li ul li ul li a {
padding-left:20px;
margin-left:-20px;
}
I get this (obviously not scaling with the font size) result for different font sizes:
Similarly, 1em/-1em is also off and looks like on the right in the picture above but scaling with the font size. It still looks like 1.8em is the magic distance for some reason...

Given your code, you've set up your ul such that it has no margin or padding. However, you've set up your li's such that they have margin-left: 1.8em:
#nav li ul li ul li {
display: list-item;
margin-left:1.8em;
list-style:disc outside none;
}
#nav li ul li ul li {
margin-left: 1.8em;
padding-left: 0;
}
And there it is.

You should definitely do a CSS reset and then set the properties the way you need them. Never trust browsers to be consistent. It adds a bit of coding but at the same time future proofs your code.

Based on many years of inconsistent browsers - I'd say you can't trust them to ever be consistent. The best option is to forcibly control it yourself.
You can use that by simultaneously setting the padding-left and margin-left of an li. eg:
li {
margin-left: 1.8em;
padding-left: 0;
}
Apparently some (mainly older) browsers use padding and some margin - so be sure to set both.

To answer the question and your comment: your solution works because you negate the padding with the exact same size margin. But the spacing to the left of the list is larger with the larger font size. You would get the same result with 1em padding and -1em margin or 20px and -20px.
As I mentioned in the comment, the actual default padding for lists is 40px. To make things even more confusing, on checking the user agent stylesheets (in Chrome Dev Tool and Firebug for Firefox) they report unique CSS properties: -webkit-padding-start or -moz-padding-start respectively.
I assume that these special properties are used in place of regular padding due to lists being a special case in HTML - they have hanging bullets/numbers that don't count in the padding.

Related

Why does nesting an inline within a block create a 1px gap at the bottom?

I have a simple structure
<nav>
<span>
A
B
C
<span>
<nav>
and I go Godzilla on the css, e.g.
margin:0px; padding:0px; vertical-align:bottom; box-sizing:border-box;
But no matter what I try there is always this really cool 1px gap at the bottom of the element. Why is it there and how would you make it go away if you were me?
fiddle
One possible solution would be to make the <span> element inline-block. This will remove the gap
EXAMPLE HERE
nav > span {
display: inline-block;
}
Alternatively, a display of block works too. The point is that it is no longer a pure inline element.
I see what you mean, it does appear (on Firefox) without zooming in closely. I believe the problem is the default line-height given to text in browsers, which results in a bit of extra space between text lines.
With some experimentation, I found that adding these styles works:
nav {
font-size:16px; /* Default font size in Firefox, but specify just in case */
line-height:18px;
}
Here's an updated JSFiddle to demonstrate it. Seems to work properly in Firefox and Chrome. Let me know if you have any problems, though. Hope this helps!

Simple spacing CSS

I have designed a navbar for my website; you can see it at http://jsfiddle.net/ToddStyles/D2tZH/.
What CSS I would use to change the spacing between them? I used an unordered list to make it and changed the display to inline.
When you are displaying elements inline, HTML will parse any whitespace as a spacebar character. So, you can either comment out the space between list items, i.e.:
<li>...</li><!--
--><li>...</li>
Or, display them as block level elements and float them:
ul {
overflow: hidden; /* To prevent collapse */
}
ul li {
display: block;
float: left;
}
With regards to the Fiddle you have posted, you have used the <img> tag incorrectly. It is self-closing, and it should not be used to nest anything.
p/s: And please, don't use link shortener just because SO prevents you from posting a Fiddle link due to a lack of code. The rule is there for a reason — providing us code gives your question context.
If you’re looking to increase the width, you can apply a left margin to consecutive <li>s easily:
#navbar li + li {
margin-left: 0.2em;
}
Here’s your fiddle back. I fixed it to use inline-block to make the layout break less when it inevitably wraps on a smaller screen. And do take into account that we can’t see images on your computer across the internet.

Edit/replace Wordpress TwentyTwelve default menu

I'm trying to edit the default menu for Wordpress TwentyTwelve theme. So far I have made the sub-menus horizontal but they don't align the same in Firefox than Chrome.
In Ff it looks as I want, but in Chrome, the sub menu align with the Menu item previously clicked, NOT to the far left of the main menu.
basically, I want a horizontal two-lines menu. I can' t get the "position:"" properly.
Here's how it looks in both browsers:
Here's how it looks in both browser:
Chrome:
http://imageshack.us/photo/my-images/248/cssmenuchrome.jpg/
I can't post more links because I need 10 reputation but the second image (menu in Firefox) in there too.
And here's a fiddle of my code so far:
http://jsfiddle.net/ZN9my/
.main-navigation li ul ul {
top: 0;
left: 100%;
}
.main-navigation .menu-item li {
display: none;
margin-right: 14px !important;
}
Your problem, as you say, is that both browsers seem to be dealing with your position:absolute; differently. position:absolute should be calculated in regards to the most recent parent element with an explicitly set position, which means that it's actually Chrome which is interpreting it right.
In this case, you've given .main-navigation li a position:relative, which means that Chrome is positioning the submenu, li.sub-menu, relative to it. If you remove position-relative from the CSS for .main-navigation li and add it to ul#menu-main, then li.sub-menu will be positioned relative to the main navigation ul, and should behave as you want it to across browsers. You'll probably want to change .main-navigation li ul's top from 100% to something like 37px so it still sits in the right place.
I've made the changes to your jsfiddle as well: http://jsfiddle.net/ZN9my/1/.

Strange CSS behaviour, cannot see what is taking the space up

My website header is where I have my menu. Except I am having to put a negative margin on it and I don't want to do that, but for some reason, there is something taking up space between the hgroup and the nav element. I thought it might be the form element for the search box, but when I do display:block it makes no difference. The site is here.
Another strange thing is when I hover over a submenu (e.g. recycle) the parent element goes white. I can't for the life of me work out why. See below for strange hover behaviour.
Make your body background longer?
The design changed right after I refreshed your website. As for now, all I see is that the navigation is still 1px off the header. Maybe if you'd reduce the margin-top from 42px to 41px, that problem will be solved.
EDIT:
So in Chrome, there's this 1px issue, but in Firefox, it looks fine.
The same 1px offset in Safari and Opera. But again, in IE8, it looks fine.
I think this problem has occurred due to the defined height for hgroup, if you have done that.
[ the first image is a screenshot from Chrome, the second is from Firefox ]
EDIT 2:
The offset is due to the fixed height of the background image. Other than that, everything is working just fine. Obviously, there are variations in both the images, as you can see, the chrome having the 1px offset while it is absent in Firefox. I, personally don't like to give a defined height to elements that is likely to have results like above.
One good solution would be to make a header container with 100% width and the given background image, while all the header content will go within the container. That way, no matter how high the contents within the container are, the background will remain the way it is meant to, and your navigation will not get an offset.
Yea h2 description is taking space from your header and about your menu, try splitting your rules in mainnav.css, seems like you have :hover grouped with actives, that's why your classes are messing up
This is the rule causing the problem in your css
#access #menu-menu li.current_page_item a, #access #menu-menu li.current_page_item, #access #menu-menu li.current_page_item a:hover, #access #menu-menu li.current_page_item:hover {
background-color: #FAFAF0;
box-shadow: none;
color: #0F0D0C;
<h2 id="site-description"></h2>
If you use Firebug/Inspect element, you'll see it there, just after your h1, taking up a lot of space.
#site-description {
color: #7A7A7A;
font-size: 14px;
margin: 0 270px 3.65625em 0;
}
For the White hover in the nav, make the following change:
#access ul ul {
background-color:red;
}

Slight gap between list items in an unordered list

I am using an unordered list where the actual list items have a rounded rectangular background image.
My problem is, there seems to be a slight gap between the list items in Firefox but in Google Chrome, there is no gap.
If I change the CSS line-height value, it corrects in Firefox but then breaks Google Chrome.
What do I need to do? I think I am doing something wrong to ensure that between each list item, there is no gap in both Firefox and Google Chrome.
I came across the problem a few minutes ago and found an easy fix that hadn't yet been posted here. I tried changing the margin and the padding and that didn't work, so I floated all the LI element's left, and it worked perfectly.
The best bet is to reset all padding/margins on your <li> and <ul> and then add padding back in as needed for the background image:
ul, li {
padding: 0;
margin: 0;
}
Different browsers apply different default paddings/margins to list, so you need to strip it off for consistency.
Have you tried a negative margin?
li { margin-bottom: -2px; }
These cross-browser pixel imperfections will always exist, this margin trick usually saves a lot of time when tweaking the layout.
A negative margin value as a safety factor will solve it for you...
ul li {
margin: -2px;
}

Resources