简体   繁体   English

CSS列表项宽度/高度不起作用

[英]CSS list item width/height does not work

I tried to make a navigation inline list. 我试图制作导航内联列表。 You can find it here: http://www.luukratief-design.nl/dump/parallax/para.html 你可以在这里找到它: http//www.luukratief-design.nl/dump/parallax/para.html

For some reason it does not display the width and height of the LI. 由于某种原因,它不显示LI的宽度和高度。 Here is the snippet. 这是片段。 What is wrong with this? 这有什么问题?

.navcontainer-top li {
    font-family: "Verdana", sans-serif;
    margin: 0;
    padding: 0;
    font-size: 1em;
    text-align: center;
    display: inline;
    list-style-type: none;<br>
    width: 117px;
    height: 26px;
}


.navcontainer-top li a {
    background: url("../images/nav-button.png") no-repeat;
    color: #FFFFFF;
    text-decoration: none;
    width: 117px;
    height: 26px;
    margin-left: 2px;
    margin-right: 2px;
}
.navcontainer-top li a:hover {
    background: url("../images/nav-button-hover.png") no-repeat;
    color: #dedede;
}

Declare the a element as display: inline-block and drop the width and height from the li element. a元素声明为display: inline-block并从li元素中删除宽度和高度。

Alternatively, apply a float: left to the li element and use display: block on the a element. 或者,将float: left应用于li元素并在a元素上使用display: block This is a bit more cross browser compatible, as display: inline-block is not supported in Firefox <= 2 for example. 这与浏览器兼容得多,因为display: inline-block例如,Firefox <= 2不支持display: inline-block

The first method allows you to have a dynamically centered list if you give the ul element a width of 100% (so that it spans from left to right edge) and then apply text-align: center . 第一种方法允许你有一个动态居中的列表,如果你给ul元素的宽度为100%(使它从左边缘到右边缘),然后应用text-align: center

Use line-height to control the text's Y-position inside the element. 使用line-height控制文本在元素内的Y位置。

Inline items cannot have a width. 内联项目不能有宽度。 You have to use display: block or display:inline-block , but the latter is not supported everywhere. 你必须使用display: blockdisplay:inline-block ,但后者不受支持。

I think the problem is, that you're trying to set width to an inline element which I'm not sure is possible. 我认为问题是,你试图将宽度设置为内联元素,我不确定是否可行。 In general Li is block and this would work. 一般来说,李是块,这将是有效的。

Using width/height on inline elements is not always a good idea. 在内联元素上使用宽度/高度并不总是一个好主意。 You can use display: inline-block instead 您可以使用display: inline-block代替

.navcontainer-top li样式中删除<br>

I had a similar issue trying to fix the item size to fit the background image width. 我有一个类似的问题,试图修复项目大小以适应背景图像宽度。 This worked (at least with Firefox 35) for me : 这对我有用(至少在Firefox 35中):

.navcontainer-top li
{
  display: inline-block;
  background: url("../images/nav-button.png") no-repeat;
  width: 117px;
  height: 26px;
}

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

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