Does UL have default margin or padding [duplicate]
Asked Answered
S

2

29

This might seem like a dumb question, but I've added an UL to a basic page and the list seems to be off-centered. There's nothing special about the list. No specific css added: just a list. When I load live it's slightly off center.
Is there a default margin or padding on the left side?

<h3>Title Heading</h3>
   <ul id="listItems">
       <li>itemOne</li>
       <li>itemTwo</li>
       <li>itemThree</li>
   </ul>

The main body has all the css code for centering, aligning, float, etc. The 'Title Header' align perfectly. Just list is a little off.

Thank you.

Oh, don't know if this is important, but I added the 'id' cause... wanted to use 'first-of-type' to give 1st item em(bold).

Sanderling answered 24/5, 2015 at 13:48 Comment(2)
add ul - padding: 0;Francesfrancesca
The W3C standard says ul should have a 40px left padding, but browsers have their own defaults.Reflector
R
24

The problem is that by default, browsers have custom css - in chrome for example:

ul, menu, dir {
   display: block;
   list-style-type: disc;
   -webkit-margin-before: 1em;
   -webkit-margin-after: 1em;
   -webkit-margin-start: 0px;
   -webkit-margin-end: 0px;
   -webkit-padding-start: 40px;
}

You'll have to use a custom rule for your ul:

element.style {
    margin-left: 0px;
    /* set to 0 if your not using a list-style-type */
    padding-left: 20px;
}
Rider answered 24/5, 2015 at 14:9 Comment(2)
OK, the padding seems to work. I just feel strange adding padding to a default setting that I have no control over or that I can't recognize in my code. But I guess, whatever works.Sanderling
Interesting but here in 2024 the default for UL in chrome is display: block; list-style-type: disc; margin-block-start: 1em; margin-block-end: 1em; margin-inline-start: 0px; margin-inline-end: 0px; padding-inline-start: 40px;Homologize
P
12

Lists will always align so the text remains in line with the edge of the parent element. This means the bullet points will by default sit outside (to the left) of the element. You can force the alignment to happen to the bullet point, not the text like so:

ul {
  list-style-position: inside; }

Alternatively you can just add your own margin to push the entire list element like so:

ul {
  margin-left: 20px; }
Paganini answered 24/5, 2015 at 13:56 Comment(3)
Ok, I tried the suggestion two ways. I added the code to css under the selector #itemList. I also added a section around the ul entitled id="bodyList" (I was trying not to mess with my specific code to change f-o-t). Nothing happen. I would use the margin-left, but not sure how it will affect float during resize; additionally there aren't any bullets.Sanderling
@Sanderling well it looks like your problem there is applying the CSS to #itemList when it's meant to be #listItemsPaganini
Thanks, this worked, but only when I used the following syntax in my Wordpress website: <ul style="list-style-position: inside">Hennessy

© 2022 - 2024 — McMap. All rights reserved.