为什么标题容器的子级不水平对齐?

时间:2018-10-02 02:05:30

标签: html css html5 css3

我有一个名为“标头容器”的主容器和两个名为“徽标容器”和“导航容器”的子容器。这是代码:`

.header-container {
}

.header-container figure nav{
    float: left;
}

.header-container .cisd-logo-container {
    margin-left: 50%;
    height: 100px;
    width: 100px
}

.header-container img {
    max-height: 100%;
}

.navigation-container {
}`
<header class="header-container">
  <figure class="logo-container">
    <img src="img/logo.png" alt="logo.png">
  </figure>

  <nav class="navigation-container">
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
  </nav>
</header>

两个子容器彼此不对齐,请问谁能解释为什么?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

即使NguyễnThanhTú是正确的,我们也已进入2018年,所以您不应使用float属性,因为它只会带来更多麻烦。而是使用flex。 https://css-tricks.com/snippets/css/a-guide-to-flexbox/

您的情况说明:

.header-container {
    display: flex; //align children next to each other
    align-items: center; // vertically align children to center
    justify-content: space-between; // horizontally align children to sides of container
}

.header-container {
  display: flex; 
  align-items: center;
  justify-content: space-between; 
}

.header-container .cisd-logo-container {
    margin-left: 50%;
    height: 100px;
    width: 100px
}

.header-container img {
    max-height: 100%;
}

.navigation-container {
}
<header class="header-container">
  <figure class="logo-container">
    <img src="img/logo.png" alt="logo.png">
  </figure>

  <nav class="navigation-container">
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
  </nav>
</header>

答案 1 :(得分:1)

.header-container {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    padding: 20px 0;
    justify-content: space-between;
    align-items: center;
}
.logo-container {
    display: inline-block;
}
.navigation-container {
    display: inline-block;
}
.navigation-container a {
    text-decoration: none;
    padding: 0 20px;
    font-size: 24px;
    font-weight: 500;
    color: #000000;
}
.navigation-container a:hover { 
  color: #f00;
}
<header class="header-container">
  <figure class="logo-container">
    <img src="https://image.ibb.co/n80Drz/slash_logo.png" alt="logo.png">
  </figure>

  <nav class="navigation-container">
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
    <a href="#">Hello</a>
  </nav>
</header>

答案 2 :(得分:0)

  

两个子容器彼此不对齐,谁能   请解释为什么会这样?

因为div元素是块级元素,所以它将占据一行,而不是彼此相邻。

为了使这些容器彼此对齐,请尝试以下操作:

    .navigation-container {
        float: left;
    }
    .logo-container{
        float: left;
    }

这是演示:

.header-container {
}

.header-container figure nav{
    float: left;
}

.header-container .cisd-logo-container {
    margin-left: 50%;
    height: 100px;
    width: 100px
}

.header-container img {
    max-height: 100%;
}


.navigation-container {
    float: left;
}
.logo-container{
    float: left;
}
<header class="header-container">
    <figure class="logo-container">
        <img src="img/logo.png" alt="logo.png">
    </figure>

    <nav class="navigation-container">
        <a href="#">Hello</a>
        <a href="#">Hello</a>
        <a href="#">Hello</a>
        <a href="#">Hello</a>
        <a href="#">Hello</a>
        <a href="#">Hello</a>
    </nav>
</header>