显示:内联即使使用浮动也不起作用
display: inline not working even with float
我正在尝试使用 CSS 创建水平导航栏并显示:内联;但我对此并不满意。使用 <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>
<style>
标签中的 ul style="list-style-type: none;"
在那里,因为我无法删除 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;
}
这样使用
<!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>
<style>
#menu ul {
height: 15px;
padding: 8px 0px;
margin: 0px;
}
#nav li {
text-decoration: none;
display: inline;
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;
}
</style>
</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;">
<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>
</div>
</body>
#nav li {
display: inline-block;
}
您的列表项设置为显示为 list-item
,而不是 inline block
。
这些样式对您没有任何作用:
#menu li {
text-decoration: none;
display: inline-block;
padding: 20px;
float: left;
}
您的标记中不存在 ID 为 #menu
的元素。
.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;
}
#nav li {
display: inline-block;
}
#nav ul {
padding: 0px;
}
<!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;">
<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>
</div>
</body>
- 正确缩进您的标记,以便于阅读,必要时也便于调试。
- 避免用
inline
元素(如 <a>
)包装 block
元素(如 <li>
),这是无效标记(参见:Is it sound to wrap a list item in an anchor?)
我正在尝试使用 CSS 创建水平导航栏并显示:内联;但我对此并不满意。使用 <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>
<style>
标签中的 ul style="list-style-type: none;"
在那里,因为我无法删除 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;
}
这样使用
<!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>
<style>
#menu ul {
height: 15px;
padding: 8px 0px;
margin: 0px;
}
#nav li {
text-decoration: none;
display: inline;
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;
}
</style>
</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;">
<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>
</div>
</body>
#nav li {
display: inline-block;
}
您的列表项设置为显示为 list-item
,而不是 inline block
。
这些样式对您没有任何作用:
#menu li {
text-decoration: none;
display: inline-block;
padding: 20px;
float: left;
}
您的标记中不存在 ID 为 #menu
的元素。
.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;
}
#nav li {
display: inline-block;
}
#nav ul {
padding: 0px;
}
<!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;">
<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>
</div>
</body>
- 正确缩进您的标记,以便于阅读,必要时也便于调试。
- 避免用
inline
元素(如<a>
)包装block
元素(如<li>
),这是无效标记(参见:Is it sound to wrap a list item in an anchor?)