How to keep indent for second line in ordered lists via CSS?
Update
This answer is outdated. You can do this a lot more simply, as pointed out in another answer below:
ul {
list-style-position: outside;
}
See https://www.w3schools.com/cssref/pr_list-style-position.asp
Original Answer
I'm surprised to see this hasn't been solved yet. You can make use of the browser's table layout algorithm (without using tables) like this:
ol {
counter-reset: foo;
display: table;
}
ol > li {
counter-increment: foo;
display: table-row;
}
ol > li::before {
content: counter(foo) ".";
display: table-cell; /* aha! */
text-align: right;
}
Demo: http://jsfiddle.net/4rnNK/1/
To make it work in IE8, use the legacy :before
notation with one colon.
Second line in li starts under the bullet after CSS-reset
The li
tag has a property called list-style-position
. This makes your bullets inside or outside the list. On default, it’s set to inside
. That makes your text wrap around it. If you set it to outside
, the text of your li
tags will be aligned.
The downside of that is that your bullets won't be aligned with the text outside the ul
. If you want to align it with the other text you can use a margin.
ul li {
/*
* We want the bullets outside of the list,
* so the text is aligned. Now the actual bullet
* is outside of the list’s container
*/
list-style-position: outside;
/*
* Because the bullet is outside of the list’s
* container, indent the list entirely
*/
margin-left: 1em;
}
Edit 15th of March, 2014
Seeing people are still coming in from Google, I felt like the original answer could use some improvement
- Changed the code block to provide just the solution
- Changed the indentation unit to
em
’s - Each property is applied to the
ul
element - Good comments :)
Get second line of bullet item to indent as first part - not underneath the bullet?
My take on this would be to include the bullet in another div and then wrap both divs in a container div.
.row2 { padding-left: 20px; overflow: hidden; max-width: 500px; }.red-square-5 { position:absolute; width:5px; height:5px; margin-top: 0.5em; background:#f00; }
<div class="container-div"> <div class="red-square-5"></div> <div class="row2"> Long long long long long long long long long long long long long long long long long long long long long long long long long title </div></div>
Li item on two lines. Second line has no margin
This is because the tick is inline content so when the text wraps it will continue to flow as usual.
You can stop this behaviour by taking advantage of text-indent
:
The text-indent property specifies how much horizontal space should be left before the beginning of the first line of the text content of an element.
text-indent (https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent)
By supplying a negative text-indent
you can tell the first line to shift a desired amount to the left. If you then specify a positive padding-left
you can cancel this offset out.
In the following example a value of 1.28571429em
is used because it is the width
set on the .fa-fw
by font-awesome.
ul { width: 300px;}li { padding-left: 1.28571429em; text-indent: -1.28571429em;}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/><ul class="fa-ul custom-list"> <li><i class="fa fa-check fa-fw"></i>List item on 1 line</li> <li><i class="fa fa-check fa-fw"></i>List item on 1 line</li> <li><i class="fa fa-check fa-fw"></i>This is a list item that actually takes up 2 lines. Looks ugly</li></ul>
Text, when is in new line, has no left padding
There are more ways, for example set padding to whole LI
and position: absolute
to counter (:before
pseudoelement).
.listPo {
list-style: none;
counter-reset: item;
padding-left: 0.2em;
}
.listPo>li {
counter-increment: item;
margin-bottom: 8px;
font-size: 15px;
padding-left: calc(1.5em + 10px);
position: relative;
}
.listPo>li:before {
margin-right: 10px;
content: counter(item);
background: #0e4b78;
color: white;
width: 1.5em;
text-align: center;
display: inline-block;
padding-left: 1px;
padding-right: 2px;
position: absolute;
top: 0;
left: 0;
}
<ol class="listPo">
<li>
<a href="h#">
gfsdgsdfgsdf
</a>
,Chemica ActSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-Resetaaaaa
</li>
<li>
<a href="h#">
gfsdgsdfgsdf
</a>
,Chemica ActSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-ResetSecond Line in Li Starts Under the Bullet After CSS-Resetaaaaa
</li>
</ol>
Related Topics
How to Submit a Post Form Using the <A Href="..."> Tag
Why Use Protocol-Relative Urls at All
CSS Data Attribute New Line Character & Pseudo-Element Content Value
Center Align Container and Left Align Child Elements
What Is the Performance Impact of the Universal Selector
Border Style Do Not Work with Sticky Position Element
Div 100% Height Works on Firefox But Not in Ie
Text Not Centered with Justify-Content: Center
Flex Items Not Respecting Margins and Box-Sizing: Border-Box
HTML Links Not Working (Using Base Href)
Hiding Elements in Responsive Layout
Why Does CSS Work with Fake Elements
What Does -Webkit-Transform: Translate3D(0,0,0); Exactly Do? Apply to Body