Unordered List (
    ) default indent

前端 未结 8 1567
忘了有多久
忘了有多久 2020-12-03 10:15

Can anyone tell me why an unordered list has a default indent/padding applied to it? Can this be solved with a CSS Browser Reset?

8条回答
  •  伪装坚强ぢ
    2020-12-03 10:24

    When reseting the "Indent" of the list you have to keep in mind that browsers might have different defaults. To make life a lot easier always start off with a "Normalize" file.

    The purpose of using these CSS "Normalize" files is to set everything to a know set of values and not relying on what the browser's defaults. Chrome might have a different set of defaults to say FireFox. This way you know that your pages will always display the same no matter what browser you are using, and you know the values to "Reset" your elements.

    Now if you are only concerned about lists in particular I would not simply set the padding to 0, this will put your bullets "Outside" of the list not inside like you would expect.

    Another thing to keep in mind is not to use the "px" or pixel unit of measurement, you want to use the "em" unit instead. The "em" unit is based on font-size, this way no matter what the font-size is you are guaranteed that the bullet will be on the inside of the list, if you use a pixel offset then on larger font sizes the bullets will be on the outside of the list.

    So here is a snippet of the "Normalize" script I use. First set everything to a known value so you know what to set it back to.

    ul{
       margin: 0;
       padding: 1em; /* Set the distance from the list edge to 1x the font size */
       list-style-type: disc;
       list-style-position: outside;
       list-style-image: none;
    }
    

提交回复
热议问题