display:即使使用float也不能工作

时间:2016-06-07 06:35:41

标签: html css css-float display

我正在尝试使用CSS创建一个水平导航栏并显示: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 文件中的项目符号点,这是我能找到的唯一解决方案

我在代码上尝试了很多变化,重新排列,删除并重新开始,但仍然没有快乐。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:0)

像这样使用

<!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>

答案 2 :(得分:0)

#nav li {
    display: inline-block;
}

您的列表项设置为list-item,而不是inline block

这些风格对你无动于衷:

#menu li {
text-decoration: none;
display: inline-block;
padding: 20px;
float: left;
}

标记中不存在标识为#menu的元素。

&#13;
&#13;
.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;
}
&#13;
<!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>
&#13;
&#13;
&#13;

  1. 正确缩进标记,以便于阅读,并在必要时进行调试。
  2. 避免将block个元素(例如<li>)与inline个元素(如<a>)包装在一起,这是无效的标记(请参阅:Is it sound to wrap a list item in an anchor?