简体   繁体   English

在CSS3中平均分配菜单选项卡

[英]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%

demo 演示

<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% ). 有第一在leftleft: 0 ),第三个在right ,并且在第二个50% - (31.2% / 2) = 50% - 15.6% = 34.%从任一leftrightleft: 34.4% )。

demo 演示

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中有这个:


ul.tabsmenu li:last-child a{ float:right; margin:0 0 0 0;}
If your container is wider than the 3 tabs with the margins then it will show a larger gap on this one only. 如果您的容器宽于3个带边距的标签,则仅在此标签上会显示较大的间隙。 Do not float this to the right - instead keep them all left and either work out the box model size of these and do the Math or use box-sizing:border-box and 33% widths for the tabs. 不要将其向右浮动-而是将它们全部留在左边,然后计算出它们的框模型大小并进行数学运算,或者对选项卡使用box-sizing:border-box和33%的宽度。 NB be careful though as margins are not included in box-sizing only borders and paddings so reduce down to say 30% to be safe. 注意:由于框大小中不包括边距,因此请注意将其减少到30%以确保安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM