[英]Distribute menu tab evenly in CSS3
How to distribute menu tab evenly in CSS3. 如何在CSS3中平均分配菜单选项卡。 There is three tabs and one tab with obvious wider gap between tab two and three, than tab one and two?
有三个选项卡,一个选项卡在第二和第三选项卡之间的间隙明显大于第一和第二选项卡?
align top http://www.kerrydeaf.com/tabs_uneven.png 对齐顶部http://www.kerrydeaf.com/tabs_uneven.png
ul.tabsmenu{ padding:5px 0 0 0; position:relative; bottom:-1px;}
ul.tabsmenu li a{ width:31.2%; float:left; padding:12px 0 12px 0; margin:0 2% 0 0;color:#000000;text-align:center;
-moz-border-radius-topleft:5px;-webkit-border-top-left-radius:5px;-khtml-border-top-left-radius:5px;
-moz-border-radius-topright:5px;-webkit-border-top-right-radius:5px;-khtml-border-top-right-radius:5px;
background:-moz-linear-gradient(top, #ececec 0%, #ffffff 100%);background:
-webkit gradient(linear, 0% 0%, 0% 100%, from(#ececec), to(#ffffff)); }
ul.tabsmenu li:last-child a{ float:right; margin:0 0 0 0;}
ul.tabsmenu li.active a{border-bottom-color:#fff; color:#48c4d2;
font-weight:bold; background: -moz-linear-gradient(top, #e4f6f8 0%, #ffffff 100%);background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#e4f6f8), to(#ffffff)); }
.tabcontent{ padding:5% 4%; clear:both; border:1px #bfeaf0 solid; margin:0 0 15px 0; /* ddd darker grey. border:1px #bfeaf0 solid; */
-moz-border-radius-bottomleft:5px;-webkit-border-bottom-left-radius:5px;-khtml-border-bottom-left-radius:5px;
-moz-border-radius-bottomright:5px;-webkit-border-bottom-right-radius:5px;-khtml-border-bottom-right-radius:5px;
} }
Using inline-block
for the list items and set a left margin on all but the first one. 对列表项使用
inline-block
,并在除第一个项之外的所有项上设置左边界。 The left margin will have a value of (100% - 3*31.2%)/2 = (100% - 93.6%)/2 = 6.4%/2 = 3.2%
左边距的值为
(100% - 3*31.2%)/2 = (100% - 93.6%)/2 = 6.4%/2 = 3.2%
<ul class='tabsmenu'>
<li><a href='#'>Tab one</a></li>
<li><a href='#'>Tab two</a></li>
<li class='active'><a href='#'>Tab three</a></li>
</ul>
<ul class='tabcontent'>
<li><!-- content--></li>
<li><!-- content--></li>
<li><!-- content--></li>
</ul>
CSS : CSS :
.tabsmenu{
padding: 0;
margin: 0 0 -1px;
list-style: none;
text-align: center;
}
.tabsmenu li {
box-sizing: border-box;
display: inline-block;
width: 31.2%;
border-radius: 5px 5px 0 0;
background: linear-gradient(#ececec, #fff);
}
.tabsmenu li:not(:first-child) { margin-left: 3.2%; }
.tabsmenu li.active {
border: solid 1px #bfeaf0;
border-bottom: solid 1px #fff;
background: linear-gradient(#e4f6f8, #fff);
font-weight: bold;
}
.tabsmenu a {
display: block;
padding: 12px 0 12px 0;
color: #000;
text-decoration: none;
}
.tabsmenu li.active a { color:#48c4d2; }
.tabcontent{
padding: 5% 4%;
border: solid 1px #bfeaf0;
border-radius: 0 0 5px 5px;
margin: 0 0 15px 0;
}
Or floating the list items. 或浮动列表项。 Not much changes:
变化不大:
.tabsmenu{
overflow: hidden;
padding: 0;
margin: 0 0 -1px;
list-style: none;
text-align: center;
}
.tabsmenu li {
box-sizing: border-box;
float: left;
width: 31.2%;
border-radius: 5px 5px 0 0;
background: linear-gradient(#ececec, #fff);
}
Or using absolute positioning. 或使用绝对定位。 Since you have the widths for each, then you can easily absolutely position them.
由于每个都有宽度,因此可以轻松地绝对定位它们。 Have the first at the
left
( left: 0
), the third at the right
, and the second one at 50% - (31.2% / 2) = 50% - 15.6% = 34.%
from either left
or right
( left: 34.4%
). 有第一在
left
( left: 0
),第三个在right
,并且在第二个50% - (31.2% / 2) = 50% - 15.6% = 34.%
从任一left
或right
( left: 34.4%
)。
CSS changes wrt the first method: CSS更改第一种方法:
.tabsmenu{
position: relative;
height: 28px;
list-style: none;
text-align: center;
}
.tabsmenu li {
position: absolute;
width: 31.2%;
border-radius: 5px 5px 0 0;
background: linear-gradient(#ececec, #fff);
}
.tabsmenu li:first-child { left: 0; }
.tabsmenu li:nth-child(2) { left: 34.4%; }
.tabsmenu li:last-child { right: 0; }
You have this in your CSS: 您在CSS中有这个:
If your container is wider than the 3 tabs with the margins then it will show a larger gap on this one only.
ul.tabsmenu li:last-child a{ float:right; margin:0 0 0 0;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.