Flexbox margin-right:如果我在一个元素中使用display:none,则自动无效

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flexbox margin-right:如果我在一个元素中使用display:none,则自动无效相关的知识,希望对你有一定的参考价值。

我想用display:none删除flexbox中的一个项目,我想在margin-right: auto类中将对齐(使用logo)设置为nav类项目。但如果我使用display:none,那么margin-right: auto无法正常工作。怎么了?也许是另一种解决方案而不是display:none

* {
    margin: 0;
    padding: 0;
}

body {
    background: white;
}

/*
---------------------------
----------Flexbox----------
---------------------------
*/

.container {
    border: 1px solid red;
}

.header {
    display: flex;
    flex-direction: row;
    height: 80px;
}

.menubtn {
    align-self: center;
    margin-left: 20px;
    font-size: 20px;
    margin-right: auto;
}

.logo {
    align-self: center;
    margin-right: 40px;
}

.nav {
    background: #000;
    color: #FFF;
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 0%;
    opacity: 0;
    text-align: center;
    overflow-x: hidden;
    list-style: none;
    transition: .5s;
    border: 1px solid blue;
}

.closebtn {
    text-align: left;
    margin-left: 30px;
    margin-top: 30px;
}

.nav-item {
    font-size: 1.8em;
}

.nav-item:nth-child(1n+2) {
    margin-top: 3em;
}



/*
---------------------------
--------Responsive---------
---------------------------
*/

@media (min-width: 640px) {

    .closebtn {
        display: none;
    }

    .menubtn {
        display: none;
    }

    .nav {
        transition: 0;
        background: none;
        color: #000;
        display: flex;
        flex-direction: row;
        align-items: center;
        position: static;
        opacity: 1;
        font-size: 0.8em;
        width: auto;
        margin-right: auto;
    }

    .nav-item:nth-child(1n+2) {
        margin-left: 10px;
    }

    .nav-item:nth-child(1n+2) {
        margin-top: 0;
    }
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Mineral Fever</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="container">
        <header>
            <div class="header">
                <p class="menubtn" onclick="openNav()">&#9776;</p>
                <nav>
                    <ul id="nav" class="nav">
                        <li class="nav-item closebtn" onclick="closeNav()">&times;</li>
                        <li class="nav-item">Termékek</li>
                        <li class="nav-item">Kapcsolat</li>
                        <li class="nav-item">Szállítás</li>
                    </ul>
                </nav>
                <h1 class="logo">Mineral Fever</h1>
            </div>
        </header>
    </div>
</body>
<script src="main.js"></script>
</html>
答案

你似乎倒退了。如果要将元素向右推,请给它一个自动左边距(反之亦然)。它可能会帮助您以这种方式来考虑它:边缘(不包括负面边缘)会远离某些东西。在这种情况下,您希望将元素推离其父元素的左边缘。自动边距会根据需要增加以占用任何可用空间(只要宽度不计算为“自动”)。

或者正如kukkuz建议的那样,你可以将自动右边距放在<nav>元素上。

另一答案

既然你已经在display: flex;上使用.header了,也许它就像在那里使用justify-content一样简单......

.header {
    display: flex;
    flex-direction: row;
    height: 80px;
    justify-content: space-between;
}

* {
    margin: 0;
    padding: 0;
}

body {
    background: white;
}

/*
---------------------------
----------Flexbox----------
---------------------------
*/

.container {
    border: 1px solid red;
}

.header {
    display: flex;
    flex-direction: row;
    height: 80px;
    justify-content: space-between;

}

.menubtn {
    align-self: center;
    margin-left: 20px;
    font-size: 20px;
    margin-right: auto;
}

.logo {
    align-self: center;
    margin-right: 40px;
}

.nav {
    background: #000;
    color: #FFF;
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 0%;
    opacity: 0;
    text-align: center;
    overflow-x: hidden;
    list-style: none;
    transition: .5s;
    border: 1px solid blue;
}

.closebtn {
    text-align: left;
    margin-left: 30px;
    margin-top: 30px;
}

.nav-item {
    font-size: 1.8em;
}

.nav-item:nth-child(1n+2) {
    margin-top: 3em;
}



/*
---------------------------
--------Responsive---------
---------------------------
*/

@media (min-width: 640px) {

    .closebtn {
        display: none;
    }

    .menubtn {
        display: none;
    }

    .nav {
        transition: 0;
        background: none;
        color: #000;
        display: flex;
        flex-direction: row;
        align-items: center;
        position: static;
        opacity: 1;
        font-size: 0.8em;
        width: auto;
        margin-right: auto;
    }
  

    .nav-item:nth-child(1n+2) {
        margin-left: 10px;
    }

    .nav-item:nth-child(1n+2) {
        margin-top: 0;
    }
}
<div class="container">
        <header>
            <div class="header">
                <p class="menubtn" onclick="openNav()">&#9776;</p>
                <nav>
                    <ul id="nav" class="nav">
                        <li class="nav-item closebtn" onclick="closeNav()">&times;</li>
                        <li class="nav-item">Termékek</li>
                        <li class="nav-item">Kapcsolat</li>
                        <li class="nav-item">Szállítás</li>
                    </ul>
                </nav>
                <h1 class="logo">Mineral Fever</h1>
            </div>
        </header>
    </div>
另一答案

您可以将margin-right: auto添加到nav元素 - 请参阅下面的演示:

* {
  margin: 0;
  padding: 0;
}

body {
  background: white;
}


/*
---------------------------
----------Flexbox----------
---------------------------
*/

.container {
  border: 1px solid red;
}

.header {
  display: flex;
  flex-direction: row;
  height: 80px;
}

.menubtn {
  align-self: center;
  margin-left: 20px;
  font-size: 20px;
  margin-right: auto;
}

.logo {
  align-self: center;
  margin-right: 40px;
}

nav {
  margin-right: auto; /* added */
}

.nav {
  background: #000;
  color: #FFF;
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 0%;
  opacity: 0;
  text-align: center;
  overflow-x: hidden;
  list-style: none;
  transition: .5s;
  border: 1px solid blue;
}

.closebtn {
  text-align: left;
  margin-left: 30px;
  margin-top: 30px;
}

.nav-item {
  font-size: 1.8em;
}

.nav-item:nth-child(1n+2) {
  margin-top: 3em;
}


/*
---------------------------
--------Responsive---------
---------------------------
*/

@media (min-width: 640px) {
  .closebtn {
    display: none;
  }
  .menubtn {
    display: none;
  }
  .nav {
    transition: 0;
    background: none;
    color: #000;
    display: flex;
    flex-direction: row;
    align-items: center;
    position: static;
    opacity: 1;
    font-size: 0.8em;
    width: auto;
    margin-right: auto;
  }
  .nav-item:nth-child(1n+2) {
    margin-left: 10px;
  }
  .nav-item:nth-child(1n+2) {
    margin-top: 0;
  }
}
<div class="container">
  <header>
    <div class="header">
      <p class="menubtn" onclick="openNav()">&#9776;</p>
      <nav>
        <ul id="nav" class="nav">
          <li class="nav-item closebtn" onclick="closeNav()">&times;</li>
          <li class="nav-item">Termékek</li>
          <li class="nav-item">Kapcsolat</li>
          <li class="nav-item">Szállítás</li>
        </ul>
      </nav>
      <h1 class="logo">Mineral Fever</h1>
    </div>
  </header>
</div>

以上是关于Flexbox margin-right:如果我在一个元素中使用display:none,则自动无效的主要内容,如果未能解决你的问题,请参考以下文章

使用 flexbox 左对齐和居中对齐元素

如何使用 flexbox 使 flex 容器的子 div 具有相同的间距? [复制]

使用百分比值填充 flexbox 项

Firefox 中的 Flexbox 溢出问题 [重复]

CSS flexbox中的Angular CDK拖放问题

flexbox 列和 IE