HTML& CSS - 导航栏不会使用左浮动水平浮动

时间:2016-07-28 15:26:10

标签: html css web

尝试使导航栏与左侧的徽标保持水平,但文本不会移动。任何人都知道为什么不呢?

https://jsfiddle.net/herxforn/1/

CSS

body {
    width 100%;
    max-width: 960px;
    margin: 0 auto;
}

nav {
    width: 100%;
    margin: 20px 0;
}

nav ul {
    width: 100%;
    list-style: none;
    margin: 0;
    padding: 0;
}

nav ul li {
    width: 20%;
    float: left;

}

nav ul li a {
    display: block;
    width: 100%;
}

5 个答案:

答案 0 :(得分:1)

您可以使用flexboxes:



body {
  width 100%;
  max-width: 960px;
  margin: 0 auto;
}

nav {
  width: 100%;
  margin: 20px 0;
  display: flex;
}

nav img{
  max-width: 50px;
  max-heigth: 50px;
}
nav ul {
  flex: 1;
  width: 100%;
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  height: 50px;
}

nav ul li {
  flex: 1;
}

nav ul li a {
  display: block;
  width: 100%;
}

<!DOCTYPE html>



<title>URBANFITNESSNI</title>
<body>
  <nav>
  <img src="https://upload.wikimedia.org/wikipedia/commons/a/ab/Logo_TV_2015.png" alt="logo" height="50px" width="50px">
    <ul>
      <li>
        <a href="/home">Home</a>
      </li>
      <li>
        <a href="/about">About Us</a>
      </li>
      <li>
        <a href="/membership">Membership Details</a>
      </li>
      <li>
        <a href="/facilities">Facilities</a>
      </li>
      <li>
        <a href="/faq">FAQ</a>
      </li>
    </ul>
  </nav>

</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

添加了演示

body {
  width 100%;
  max-width: 960px;
  margin: 0 auto;
}

nav {
  width: 100%;
  margin: 20px 0;
}

nav ul {
  width: 100%;
  list-style: none;
  margin: 0;
  padding: 0;
}

nav ul li,
nav ul li a {
  display: inline-block;
}
<!DOCTYPE html>



<title>URBANFITNESSNI</title>

<body>


  <img src="C:\Users\Ross\Documents\UrbanFitnessNi\assets\logo.png" alt="logo">


  <nav>
    <ul>
      <li>
        <a href="/home">Home</a>
      </li>
      <li>
        <a href="/about">About Us</a>
      </li>
      <li>
        <a href="/membership">Membership Details</a>
      </li>
      <li>
        <a href="/facilities">Facilities</a>
      </li>
      <li>
        <a href="/faq">FAQ</a>
      </li>
    </ul>
  </nav>

</body>

删除

nav ul li {
    width: 20%;
    float: left;

}

nav ul li a {
    display: block;
    width: 100%;
}

并为此进行更改:

nav ul li,
nav ul li a {
  display: inline-block;
}

答案 2 :(得分:0)

看看这个。

https://jsfiddle.net/herxforn/5/

&#13;
&#13;
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333333;
}

li {
    float: left;
}

li a {
    display: block;
    color: white;
    text-align: center;
    padding: 16px;
    text-decoration: none;
}
&#13;
<!DOCTYPE html>



<title>URBANFITNESSNI</title>

<body>


  <img src="C:\Users\Ross\Documents\UrbanFitnessNi\assets\logo.png" alt="logo">


 
    <ul>
      <li>
        <a href="/home">Home</a>
      </li>
      <li>
        <a href="/about">About Us</a>
      </li>
      <li>
        <a href="/membership">Membership Details</a>
      </li>
      <li>
        <a href="/facilities">Facilities</a>
      </li>
      <li>
        <a href="/faq">FAQ</a>
      </li>
    </ul>
  

</body>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您需要为图像添加一个类,并将其浮动到左侧。

CSS

body {
  width 100%;
  max-width: 960px;
  margin: 0 auto;
}

nav {
  width: 100%;
  margin: 20px 0;
}

nav ul {
  width: 100%;
  list-style: none;
  margin: 0;
  padding: 0;
}

nav ul li,
nav ul li a {
  display: inline-block;
}

.logo {
  float: left;
}

HTML

<!DOCTYPE html>



<title>URBANFITNESSNI</title>

<body>


  <img src="C:\Users\Ross\Documents\UrbanFitnessNi\assets\logo.png" alt="logo" class="logo">


  <nav>
    <ul>
      <li>
        <a href="/home">Home</a>
      </li>
      <li>
        <a href="/about">About Us</a>
      </li>
      <li>
        <a href="/membership">Membership Details</a>
      </li>
      <li>
        <a href="/facilities">Facilities</a>
      </li>
      <li>
        <a href="/faq">FAQ</a>
      </li>
    </ul>
  </nav>

</body>

答案 4 :(得分:0)

  

横向显示左侧徽标的导航栏

将徽标放在导航中浮动图像和Activity

ul
body {
  width 100%;
  max-width: 960px;
  margin: 0 auto;
}
nav {
  margin: 20px 0;
  background: black;
  float: left;
  width: 100%;
}
nav img {
  float: left;
}
nav ul {
  float: left;
  list-style: none;
  margin: 0;
  padding: 0;
}
nav ul li {
  float: left;
  padding: 1em;
}
nav ul li a {
  display: block;
  color: white;
  text-decoration: none;
  text-align: center;
}