简体   繁体   English

HTML中的UL LI标签会创建空白

[英]UL LI tags in html create blank space

So I have this ul's li's and nav is changed in css to inline but for some reason when i put those few lines on the top of the page they create blank area like 3-4 lines down which I can't get rid of 所以我有这个ul的li's,并且nav在CSS中更改为内联,但是由于某些原因,当我将那几行放在页面顶部时,它们会创建空白区域(例如3-4行),我无法摆脱它

<ul id="nav">
<li><span style="position: relative; top: -19px;"><a href="champions.php">Champions</a></span></li>
<li><span style="position: relative; top: -19px;"><a href="items.php">Items</a></span></li>
<li><span style="position: relative; top: -19px;"><a href="changes.php">Changes</a></span></li>
</ul>

First rule of UL/LIs. UL / LI的第一条规则。 Get rid of the default formatting: 摆脱默认格式:

#nav.ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

JSFiddle to mess around with: http://jsfiddle.net/TrueBlueAussie/4ctUx/ JSFiddle搞砸了: http : //jsfiddle.net/TrueBlueAussie/4ctUx/

Inline content is whitespace-sensitive, so you will have spaces between the element. 内联内容对空格敏感,因此元素之间将留有空格。 This is typically why people will use float rather than inline when they want elements to line up next to each other. 这就是为什么人们在希望元素彼此inline时会使用float而不是inline原因。

Here is a simplified example demonstrating what I think your problem is: 这是一个简化的示例,说明我认为您的问题是:

http://jsfiddle.net/rH24P/ http://jsfiddle.net/rH24P/

I would change this to use float: left instead: 我将其更改为使用float: left代替:

http://jsfiddle.net/rH24P/1/ http://jsfiddle.net/rH24P/1/

Updated CSS: 更新的CSS:

ul {
    list-style: none;
}
li {
    background: #ccc;
    float: left;
}

I think what you're looking for is something like this http://jsfiddle.net/4QR2v/ 我认为您正在寻找的是这样的http://jsfiddle.net/4QR2v/

Remove your inline styling and use the following CSS instead: 删除内联样式,而改用以下CSS:

#nav{
    margin:0;
    padding:0;
    list-style: none;
}

#nav li{
    display:inline;
}

The margin:0 and padding:0 removes the default formatting on lists (which may be the gap you're referring to). margin:0padding:0删除列表上的默认格式(这可能是您指的空白)。 And the display:inline puts everything on one line. display:inline将所有内容都放在一行上。

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

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