Li item on two lines. Second line has no margin

2019-01-19 11:01发布

I'm currently working on an unordered list containing list items with taglines. I'm having a problem concerning one list item, which is long enough to take up two lines (See image)

enter image description here

I want it so that the second line is aligned with the first line. This is the HTML code i'm using. I used fontAwesome for the check images.

ul {
  width: 300px;
}
<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>

I already tried to enter multiple &nbsp; in between '2' and 'lines' but that seems like a really bad practice to me. I hope someone can help me with this problem.

6条回答
太酷不给撩
2楼-- · 2019-01-19 11:33

You can use "display:table" as style for li, and inside create a span with "display:table-cell" styling. Or create corresponding class. Like this:

<li style="display:table"><span style="display:table-cell">text with 
all lines indented</span></li>
查看更多
地球回转人心会变
3楼-- · 2019-01-19 11:42

you can just add this to your ul:

ul {
text-indent:-20px; 
margin-left:20px;
}

JSFIDDLE

查看更多
祖国的老花朵
4楼-- · 2019-01-19 11:43

I found this does not work reliably across all browsers and devices.

This code does:

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;
}

Original answer

查看更多
冷血范
5楼-- · 2019-01-19 11:52

you can put your text in a div and give float:left to your inner li and div.

查看更多
叛逆
6楼-- · 2019-01-19 11:54

You could remove the check out of the page flow by setting them with position:absolute

ul {
  width: 300px;
}
.fa-fw {
  position: absolute;
  left: -22px;
}
<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>

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-19 11:56

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>

查看更多
登录 后发表回答