简体   繁体   English

CSS菜单:均等地填充总面积,垂直居中的文本

[英]CSS Menu: Fill Total Area Equally, Vertically-Centered text

I'm trying to create a simple CSS menu. 我正在尝试创建一个简单的CSS菜单。 Here are the constraints: 这里是约束:

  1. Fill 100% of the width of the parent container 填充父容器宽度的100%
  2. The parent container has a percentage-based width 父容器具有基于百分比的宽度
  3. Have each button be equal-width 使每个按钮的宽度相等
  4. Have the entire button be clickable (ie the anchor tag expands to fill the entire li tag) 整个按钮是否都可以单击(即,锚标签扩展为填充整个li标签)
  5. Have button width dynamically generated. 具有动态生成的按钮宽度。
  6. have the text vertically aligned in the center of the menu 使文本在菜单中心垂直对齐
  7. We can safely assume there are only 3 menu items. 我们可以安全地假设只有3个菜单项。 (it would be nice to have it work for N items, but not necessary). (最好将它用于N个项目,但这不是必需的)。

Here is the html: 这是html:

<ul>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li> 
</ul>

Here are my attempts, each has its own failing. 这是我的尝试,每一次都有自己的失败。

  1. http://jsfiddle.net/QzYAr/266/ (widths aren't expressed as a %) http://jsfiddle.net/QzYAr/266/ (宽度未表示为%)

     ul { width: 600px; display: table; table-layout: fixed; background: #EEE; } li { float: left; border: 1px dotted red; } a { display: table-cell; height: 50px; vertical-align: middle; text-align: center; width: 150px; border: 1px dotted green; } 
  2. http://jsfiddle.net/RzeK2/ (Anchor tag doesn't fill height) http://jsfiddle.net/RzeK2/ (锚标记未填充高度)

     ul { width: 80%; display: table; table-layout: fixed; background: #EEE; } li { float: left; border: 1px dotted red; } a { display: table-cell; height: 50px; vertical-align: middle; text-align: center; width: 150px; /*this is the part that needs work?*/ border: 1px dotted green; } 
  3. http://jsfiddle.net/XsLHY/ (Anchor tag text isn't vertically centered) http://jsfiddle.net/XsLHY/ (锚标记文本未垂直居中)

     ul { width: 80%; display: table; table-layout: fixed; background: #EEE; } li { display: table-cell; width: 33%; height: 50px; vertical-align: middle; text-align: center; border: 1px dotted red; } a { display: block; border: 1px dotted green; height: 50px; } 
  4. http://jsfiddle.net/w55Lg/ [suggested by two answers below] (Creates ugly button if text wraps, and also other buttons aren't full height now) http://jsfiddle.net/w55Lg/ [由以下两个答案建议](如果文本自动换行,则创建难看的按钮,并且其他按钮现在还没有全高)

     same as three, plus: a { line-height: 50px; } 

Try this: 尝试这个:

ul {
    width: 80%;
    background: #EEE;

}
li {
    width:33.33333333%;
    float:left;
}
a{
    display:block;
    text-align:center;
    height:50px;
    line-height:50px;
}
a:hover {
    background:red;
}

DEMO: http://jsfiddle.net/QzYAr/267/ 演示: http : //jsfiddle.net/QzYAr/267/

EDIT: If you have longer text, you can wrap it inside a <span> tag and add this CSS: 编辑:如果您有更长的文本,您可以将其包装在<span>标记内并添加以下CSS:

ul span{
    display:inline-block; vertical-align:middle;
    line-height:14px;
}

Using fiddle #2, updated version: http://jsfiddle.net/XsLHY/1/ 使用小提琴#2,更新版本: http//jsfiddle.net/XsLHY/1/

You just need to add a matching line-height to the li as the height of the a . 你只需要一个匹配的line-height添加到li的高度a

CSS CSS

    ul {
        width: 80%;
        display: table;
        table-layout: fixed; 
        background: #EEE;

    }

    li {
        display: table-cell;
        width: 33%;
        height: 50px;
        vertical-align: middle;
        text-align: center;
        border: 1px dotted red;
        line-height: 50px;
    }

    a {
        display: block;
        border: 1px dotted green;
        height: 50px;
    }

This could be done using: 可以使用以下方法完成:

display: flex;

http://jsfiddle.net/XsLHY/3/ http://jsfiddle.net/XsLHY/3/

http://jsfiddle.net/LD2w4/ http://jsfiddle.net/LD2w4/

To fix the anchor problem, you can switch the order, so the <a> wraps the <li> instead of the other way around: 要解决定位问题,可以切换顺序,因此<a>包装<li>而不是采用其他方式:

<ul>
    <a href="#"><li>Item 1</li></a>
    <a href="#"><li>Item 2</li></a>
    <a href="#"><li>Item 3</li></a> 
</ul>

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

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