浮动和固定宽度li的组合

时间:2013-11-23 13:37:40

标签: html css css3 responsive-design

我的HTML                                                                

                    </div><!-- /end .topbar -->

                    <ul class="nav">
                            <li class="first"><a href="#">Time added</a></li>
                            <li class="first"><a href="#">Price</a></li>
                            <li class="last"><a href="#">Discount</a></li>
                            <li class="delete"><a href="#"></a></li>
                    </ul><!-- /end .nav -->
                    <div class="done hidden">

                        <a href="#" class="button">done</a>
                    </div>
                </div><!-- /end .mast -->
        </div><!-- /end .inner -->
</div><!-- /end #page -->

我的CSS:

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

ul.nav{
    height:45px;
    background:#36d6be;
    position:fixed;
    top:0;
    width:100%;

}

ul.nav li.first{
    width:33.333333%;
    float:left;
    border-right:solid 2px #3cefd4;
    height:45px;
}

ul.nav li.last{
    width:26.04166666666667%;
    float:left;
    border-right:solid 2px #3cefd4;
    height:45px;
}

ul.nav li.delete{
    float:left;
    width:56px;
    height:45px;
}

http://jsfiddle.net/q3LGW/

我有4个li,其中前两个大小相等33%,第三个是(33% - 56px)上一个删除垃圾桶图标。但是我无法创建一个固定宽度的最后一个li,它是左浮动的。

2 个答案:

答案 0 :(得分:1)

您的前三个列表项具有2px右边框,最多可添加6px。这意味着您需要再扣除最后一个列表项目的6 px frm:

http://jsfiddle.net/q3LGW/2/

ul.nav li.delete {
    float: left;
    width: 50px;
    height: 45px;
}

可替换地:

您可以更改框大小属性,以便框的宽度中包含边框。我建议作为更好的解决方案。您需要做的就是在CSS文件的顶部添加:

*,
*:after,
*:before {
    -webkit-box-sizing: border-box; 
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}

如果使用此方法,还可以将最后一个列表项的宽度增加到58px。

http://jsfiddle.net/q3LGW/4/

答案 1 :(得分:0)

你必须使用display:table <ul> 并显示:table-cell to <li> 查看fiddle

ul.nav{
    height:45px;
    background:#36d6be;
    position:fixed;
    top:0;
    width:100%;
    display:table;

}

ul.nav li.first{
    width:33.333333%;
    display:table-cell;
    border-right:solid 2px #3cefd4;
    height:45px;
}

ul.nav li.last{
    width:26.04166666666667%;
    display:table-cell;
    border-right:solid 2px #3cefd4;
    height:45px;
}

ul.nav li.delete{
    display:table-cell;
    width:56px;
    height:45px;
}