使用水平css菜单填充表格单元格

时间:2014-12-02 13:16:42

标签: html css

我有一个css菜单(我下载的源代码,因为我不知道关于css的深蹲)包含在div中

<div id='cssmenu' style='float:right;'>
  <ul>
    <li id='index'><a href='index.php'><span>Home</span></a></li>
    <li id='aboutus'><a href='aboutus.php'><span>About Us</span></a></li>
    <li id='gallery'><a href='gallery.php'><span>Galley</span></a></li>
    <li id='videos'><a href='videos.php'><span>Videos</span></a></li>
    <li id='links'><a href='links.php'><span>Links</span></a></li>
    <li id='contact'><a href='contact.php'><span>Contact</span></a></li>
  </ul>
</div>

我在<td>内部有一个固定宽度(为简单起见,假设为500px)。我调整了css中的填充以使宽度适合...至少在Firefox中。当我在Chrome中查看它时,最后一个菜单项溢出表格单元格的宽度,最终低于菜单的其余部分。

我的问题是,如何在菜单宽度上均匀分布菜单项?将填充设置为0px会使一切看起来很小并且压扁,我仍然需要垂直填充。

这是我的CSS(对不起,我知道它很长)

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#cssmenu {
  width: 100%;
  font-family: Helvetica, Arial, sans-serif;
  color: #ffffff;
  padding:0px;
  display:table;
}
#cssmenu.align-right ul li {
  float: right;
  border-right: 0;
  border-left: 1px solid rgba(0, 0, 0, 0.22);
}
#cssmenu.align-right ul li a {
  border-right: 0;
  border-left: 1px solid rgba(255, 255, 255, 0.15);
}
#cssmenu ul {
  background: #222222;
  /* Old browsers */

  background: -moz-linear-gradient(bottom, #000000 0%, #222222 50%, #3c3c3c 51%, #393939 78%, #888888 100%);
  background: -webkit-linear-gradient(bottom, #000000 0%, #222222 50%, #3c3c3c 51%, #393939 78%, #888888 100%);
  background: -o-linear-gradient(bottom, #000000 0%, #222222 50%, #3c3c3c 51%, #393939 78%, #888888 100%);
  background: -ms-linear-gradient(bottom, #000000 0%, #222222 50%, #3c3c3c 51%, #393939 78%, #888888 100%);
  background: linear-gradient(to top, #000000 0%, #222222 50%, #3c3c3c 51%, #393939 78%, #888888 100%);
  width: 100%;
  display:table-row-group;
}
#cssmenu:after
#cssmenu ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
#cssmenu ul li {
  float: left;
  display: table-cell;
  vertical-align: middle;
  /*width: auto; */
  border-right: 1px solid rgba(0, 0, 0, 0.22);
  z-index: 1;
}
#cssmenu ul li::after {
  content: "";
  width: 100%;
  height: 8px;
  position: absolute;
  border-top-left-radius: 50% 4px;
  border-top-right-radius: 50% 4px;
  background: -webkit-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #222222 61%, #222222 100%);
  background: -o-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #222222 61%, #222222 100%);
  background: -ms-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #222222 61%, #222222 100%);
  background: linear-gradient(to top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #222222 61%, #222222 100%);
  background: -moz-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0));
  z-index: 2;
  bottom: 10px;
}
#cssmenu ul li a {
  display: block;
  padding: 10px 11px;
  text-decoration: none;
  font-size: 11px;
  text-transform: uppercase;
  color: #ffffff;
  border-right: 1px solid rgba(255, 255, 255, 0.15);
  z-index: 3;
}
#cssmenu ul li a:hover,
#cssmenu ul li.active a {
  color: #ffffff;
}
#cssmenu ul li:hover,
#cssmenu ul li.active {
  background: #1275ae;
  /* Old browsers */

  background: -moz-linear-gradient(bottom, #0b4669 0%, #1275ae 50%, #1794dc 51%, #1691d8 78%, #98d2f4 100%);
  background: -webkit-linear-gradient(bottom, #0b4669 0%, #1275ae 50%, #1794dc 51%, #1691d8 78%, #98d2f4 100%);
  background: -o-linear-gradient(bottom, #0b4669 0%, #1275ae 50%, #1794dc 51%, #1691d8 78%, #98d2f4 100%);
  background: -ms-linear-gradient(bottom, #0b4669 0%, #1275ae 50%, #1794dc 51%, #1691d8 78%, #98d2f4 100%);
  background: linear-gradient(to top, #0b4669 0%, #1275ae 50%, #1794dc 51%, #1691d8 78%, #98d2f4 100%);
}
#cssmenu ul li:hover::after,
#cssmenu ul li.active::after {
  background: -webkit-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #1275ae 61%, #1275ae 100%);
  background: -o-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #1275ae 61%, #1275ae 100%);
  background: -ms-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #1275ae 61%, #1275ae 100%);
  background: linear-gradient(to top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 60%, #1275ae 61%, #1275ae 100%);
  background: -moz-linear-gradient(bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0));
}

2 个答案:

答案 0 :(得分:1)

如果你正在使用表格会很容易,但是这里你有ul所以我建议你使用calc()函数,如果你能用它来安慰它。或等待另一种解决方案。我用你的代码测试了它,如width:calc(100%/ 6);而且效果很好。

我已将此规则添加到#cssmenu ul li

答案 1 :(得分:1)

试试这个

ul{
display:table;
table-layout: fixed;
width: 100%;
}
li{
    display:table-cell;
    display: table-cell;
    float: none;
}

http://jsfiddle.net/upj6oe0z/

我希望它有效。