简体   繁体   English

li 元素堆叠而不对齐

[英]li elements stacking and not aligning

Hi so I'm trying to align my navigation menu where the links align on one line with each other.嗨,我正在尝试对齐我的导航菜单,其中链接在一行上相互对齐。 But instead of aligning they are stacking.但不是对齐,而是堆叠。 I'm currently coding in dreamweaver.我目前正在使用 Dreamweaver 进行编码。 I don't know what's going on.我不知道发生了什么。

Here is my css这是我的 css

body, html {
   margin: auto;
   width: 100%;
   height: 100%;
}

.header {
   width: 100%;
   height: 100%;
   background: url("") no-repeat;
   display: block;
}

.header > .nav-container {
   width: 100%;
   height: 50px;
   padding-top: 0px;
   display: block;
}

.header > .nav-container > .logo {
   width: 100%;
   max-width: 196px;
   display: inline-block;
   margin-left: 20px;
   background: #000;
}

.header > .nav-container > .navigation {
   display: inline-block;
   width: 60%;
   background: #000;
}

.nav-container > ul {
   list-style-type: none;
   margin: 0;
   padding: 0;
   overflow: hidden;
   background-color: #333;
}

.nav-container > li {
   float: left;
   display:inline-block;
}

.nav-container > li a {
   display: inline-block;
   color: white;
   text-align: center;
   padding: 14px 16px;
   text-decoration: none;
}

.nav-container > li a:hover {
   background-color: #111;
}

.nav-container > .active {
   background-color: #4CAF50;
}

and the html和 html

<div class="header">
    <div class="nav-container">
    <div class="logo">
        <img src="C:\Users\Terrell\Documents\Designs\GetVersed\site\versedlogo.png">
    </div>

<!-- Naviagation -->
    <div class="navigation">
        <ul>
            <li><a class="active" href="#home">Home</a></li>
            <li><a href="#news">News</a></li>
            <li><a href="#contact">Contact</a></li>
       </ul>
    </div> 
<!-- End Navigation -->

</div>

Can anyone explain to me what I am doing wrong.谁能向我解释我做错了什么。

I think this is the simplest way that I can break down what i'm asking for this site is asking me to write more because it's so much code here so this is just exrta writing.我认为这是我可以分解我对这个网站的要求的最简单的方法是要求我写更多,因为这里的代码太多,所以这只是额外的写作。

Add the block below to your css将下面的块添加到您的 css 中

.navigation ul {
  display: flex;
}

.navigation li {
  padding: 5px;
}

 .navigation ul { display: flex; } .navigation li { padding: 5px; } body, html { margin: auto; width: 100%; height: 100%; } .header { width: 100%; height: 100%; background: url("") no-repeat; display: block; } .header>.nav-container { width: 100%; height: 50px; padding-top: 0px; display: block; } .header>.nav-container>.logo { width: 100%; max-width: 196px; display: inline-block; margin-left: 20px; background: #000; } .header>.nav-container>.navigation { display: inline-block; width: 60%; background: #000; } .nav-container>ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } .nav-container>li { float: left; display: inline-block; } .nav-container>li a { display: inline-block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } .nav-container>li a:hover { background-color: #111; } .nav-container>.active { background-color: #4CAF50; }
 <div class="header"> <div class="nav-container"> <div class="logo"> <img src="C:\\Users\\Terrell\\Documents\\Designs\\GetVersed\\site\\versedlogo.png"> </div> <!-- Naviagation --> <div class="navigation"> <ul> <li><a class="active" href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> </ul> </div> <!-- End Navigation --> </div>

You need to set display property of each List item as Inline.您需要将每个列表项的显示属性设置为内联。

ul li{
 display: inline;
}
ul{
  list-style: none;
position: absolute;
left: 0px;

}

li>a{
  display:inline;
}

li{
  display:inline-block;
}

implement these css properties , you should get it inline实现这些css属性,你应该让它内联

working fiddle工作小提琴

You should follow proper way to apply css您应该按照正确的方式应用 css

body, html {
    margin:auto;
    width:100%;
    height:100%;
    }

    .header{
    width:100%;
    height:100%;
    background:url("") no-repeat;
    display:block;
    }

    .header > .nav-container{
    width:100%;
    height:50px;
    padding-top:0px;
    display:block;
    }

    .header > .nav-container > .logo{
    width:100%;
    max-width:196px;
    display:inline-block;
    margin-left:20px;
    background:#000;

    }

    .header > .nav-container > .navigation{
    display:inline-block;
    width:60%;
    background:#000;
    vertical-align: middle
    }

    .navigation > ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
    }

    .navigation ul > li {
    float: left;
     display:inline-block;
    }

    .navigation ul > li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    }

    .navigation ul > li a:hover {
     background-color: #111;
     }

     .navigation .active {
      background-color: #4CAF50;
      }

Remove the greater sign in css in between the element删除元素之间的css中的更大符号

ex:前任:

.nav-container > li {
float: left;
 display:inline-block;
}

write like this像这样写

.nav-container li {
float: left;
 display:inline-block;
}

The greater than sign (>) selector in CSS is used to select the element with a specific parent. CSS 中的大于号 (>) 选择器用于选择具有特定父元素的元素。 It is called as element > element selector.它被称为元素 > 元素选择器。 It is also known as the child combinator selector which means that it selects only those elements which are direct children of a parent.它也被称为子组合器选择器,这意味着它只选择那些是父元素的直接子元素的元素。 It looks only one level down the markup structure and not further deep down.它看起来只是标记结构的下一级,而不是更深的层次。 Elements which are not the direct child of the specified parent is not selected.不选择不是指定父项的直接子项的元素。

https://www.geeksforgeeks.org/what-is-greater-than-sign-selector-in-css/ https://www.geeksforgeeks.org/what-is-greater-than-sign-selector-in-css/

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

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