【问题标题】:Fill table cell with horizontal css menu用水平 css 菜单填充表格单元格
【发布时间】:2014-12-02 13:16:42
【问题描述】:

我有一个包含在 div 中的 css 菜单(我下载的源代码,因为我对 css 不了解)

<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>

我在 &lt;td&gt; 内部有一个固定宽度(为简单起见,假设为 500 像素)。我调整了css中的填充以使宽度很好地适应……至少在Firefox中。当我在 Chrome 中查看它时,最后一个菜单项超出了表格单元格的宽度,最终位于菜单其余部分的下方。

我的问题是,如何使菜单项在表格宽度上等距分布?将 padding 设置为 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));
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    如果你使用 table 会很容易,但是这里你有 ul 所以我建议你使用 calc() 函数,如果你能接受的话。或等待其他解决方案。我已经用你的代码测试了它,比如 width: calc(100%/ 6);而且效果很好。

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

    【讨论】:

    • 如果他把这个菜单放在桌子上,为什么不把男人也做成桌子。
    • 我添加了宽度:calc(500px/6);到#cssmenu ul li a {},这似乎有效。我赞成你的答案为我指明了正确的方向,但没有将其标记为已接受,因为它不是完整的解决方案。
    【解决方案2】:

    试试这个

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

    http://jsfiddle.net/upj6oe0z/

    我希望它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2015-01-03
      • 2011-04-28
      • 2010-10-13
      • 2018-03-12
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多