我有一个头,作为表,我试图浮动最后一个李到右边。
我试过将第五行的右边填充设置为在结尾出现,但显然这并不理想,因为在其他屏幕上它会导致问题。我也试过把它漂到右边,但是它弄乱了垂直线。有什么建议吗?
html - 如何在CSS中将<li> float 到右侧?-LMLPHP

#header ul {
    width: 100%;
    float: left;
    margin: 0;
    margin-top: 2px;
    padding: 0;
    list-style-type: none;
    list-style-image: none;
}

#header ul li {
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

/* Logo */
#header li:nth-child(1) {
    font-size: 40px;
    font-family: serif;
    font-weight: bold;
    padding-right: 4%;
}

/* Back & Forward buttons */
#header li:nth-child(2) {
    padding-right: 8.2%;
}

/* Search box */
#header li:nth-child(3) {
    padding-right: 4.5%;
}

/* Break line */
#header li:nth-child(4) {
    padding-right: 2%;
}

/* Icons */
#header li:nth-child(5) {}

/* Icons */
#header li:nth-child(6) {
    border: 1px dotted red;
}

<div id="header">
    <ul>
        <!-- Logo -->
        <li width="10%"><div>B</div></li>
        <!-- Back / forward buttons -->
        <li>
            <i class="material-icons md-38 back-forward icn-hvr">chevron_left</i>
            <i class="material-icons md-38 back-forward icn-hvr">chevron_right</i>
        </li>
        <!-- Search box -->
        <li>
            <input type="search" id="search" size="30" placeholder="What are you looking for?" />
        </li>
        <!-- Break line -->
        <li>
            <span class="hdr-break"></span>
        </li>
        <!-- Icons -->
        <li>
            <i class="material-icons md-26 icn-lft icn-stamp">add_location</i>
            <i class="material-icons md-26 icn-lft icn-map">map</i>
            <i class="material-icons md-26 icn-lft icn-hvr">business_center</i>
            <i class="material-icons md-26 icn-lft icn-hvr">notifications</i>
        </li>
        <!-- This should float right -->
        <li>
            <p>x</p>
        </li>
    </ul>
</div>

最佳答案

使用现有代码,您需要做的就是删除float:left;并添加display: table

#header ul {
        width: 100%;
        display: table;
        ....

这是你的代码片段。
#header ul {
    width: 100%;
    display: table;
    margin-top: 2px;
    padding: 0;
    list-style-type: none;
    list-style-image: none;
}

#header ul li {
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

/* Logo */
#header li:nth-child(1) {
    font-size: 40px;
    font-family: serif;
    font-weight: bold;
    padding-right: 4%;
}

/* Back & Forward buttons */
#header li:nth-child(2) {
    padding-right: 8.2%;
}

/* Search box */
#header li:nth-child(3) {
    padding-right: 4.5%;
}

/* Break line */
#header li:nth-child(4) {
    padding-right: 2%;
}

/* Icons */
#header li:nth-child(5) {}

/* Icons */
#header li:nth-child(6) {
    border: 1px dotted red;
}

<div id="header">
    <ul>
        <!-- Logo -->
        <li width="10%"><div>B</div></li>
        <!-- Back / forward buttons -->
        <li>
            <i class="material-icons md-38 back-forward icn-hvr">chevron_left</i>
            <i class="material-icons md-38 back-forward icn-hvr">chevron_right</i>
        </li>
        <!-- Search box -->
        <li>
            <input type="search" id="search" size="30" placeholder="What are you looking for?" />
        </li>
        <!-- Break line -->
        <li>
            <span class="hdr-break"></span>
        </li>
        <!-- Icons -->
        <li>
            <i class="material-icons md-26 icn-lft icn-stamp">add_location</i>
            <i class="material-icons md-26 icn-lft icn-map">map</i>
            <i class="material-icons md-26 icn-lft icn-hvr">business_center</i>
            <i class="material-icons md-26 icn-lft icn-hvr">notifications</i>
        </li>
        <!-- This should float right -->
        <li>
            <p>x</p>
        </li>
    </ul>
</div>

10-04 20:10