我正在尝试使用 CSS 和 display: inline; 创建一个水平导航栏。但我对此并不满意。使用 <ul><li>
,我得到的是垂直列表而不是水平列表。我已经在网上搜索了 SO,但找不到解决方法。
#menu ul {
height: 15px;
padding: 8px 0px;
margin: 0px;
}
#menu li {
text-decoration: none;
display: inline-block;
padding: 20px;
float: left;
}
.logo {
font-family: 'Mr De Haviland', cursive;
font-size: 32px;
}
.logo a {
color: black;
text-decoration: none;
}
#nav a {
color: black;
text-decoration: none;
}
#nav {
max-width: 600px;
width: auto;
height: 35px;
font-size: 16px;
font-family: Helvetica, Geneva, sans-serif;
text-align: center;
text-shadow: 3px 2px 3px #666666;
background-color: #999999;
border-radius: 8px;
list-style-type: none;
}
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Mr+De+Haviland' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Anthony Jones</title>
</head>
<body>
<div id="main">
<div class="logo">
<p align="center">
<a class="logo" href="index.html">Anthony Jones</a>
</p>
</div>
<div id="nav">
<ul style="list-style-type: none;">
<a href="about.html">
<li>About</li>
</a>
<a href="cv.html">
<li>Curriculum Vitae</li>
</a>
<a href="services.html">
<li>Services</li>
</a>
<a href="random.html">
<li>Random</li>
</a>
<a href="contact.html">
<li>Contact Me</li>
</a>
</ul>
</div>
</div>
</body>
ul style="list-style-type: none;"
在<style>
标签在那里是因为我无法删除 index.css 文件中的元素符号点,这是我能找到的唯一解决方案。
我尝试了很多代码变体,重新排列、删除并重新开始,但仍然没有任何乐趣。有什么想法吗?
最佳答案
1:您不能将a
标记包裹在li
周围。 a
标签必须在 li
内。
像这样:
<div id="nav">
<ul style="list-style-type: none;">
<li><a href="about.html">About</a></li>
<li><a href="cv.html">Curriculum Vitae</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="random.html">Random</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
</div>
2:设置li
为display: inline
像这样:
#nav li {
display: inline;
}
关于html - 显示:内联即使使用 float 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672093/