在Flexbox中的div之间添加空间

时间:2018-06-28 17:40:20

标签: html css css3 flexbox

我想问一下在具有flexbox的两个div之间留出间距的好方法。例如:

enter image description here

header {
  background-image: radial-gradient(circle, #72d6c9, #54d1ed, #7ac5ff, #bcb2fe, #f29cd9);
  height: 80px;
}

.menu-section {
  display: flex;
}

.nav-logo {
  color: #e00986;
  font-size: 25px;
  margin: 0;
  padding-left: 15px;
  line-height: 80px;
}

nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  text-align: center;
}

nav ul li {
  display: inline-block;
  width: 150px;
  position: relative;
}

nav ul li a {
  text-decoration: none;
  line-height: 80px;
  padding: 0 10px;
  display: block;
  color: #e00986;
}

nav ul li a:hover {
  color: #FFF;
  transition-duration: 2s;
}
<header>
  <nav class="menu-section">
    <h1 class="nav-logo">Love &#9825 Cookies</h1>
    <ul>
      <li><a href="#">Strona główna</a></li>
      <li><a href="#">Ciastka</a>
        <ul>
          <li><a href="#">Torty</a></li>
          <li><a href="#">Babeczki</a></li>
          <li><a href="#">Bezy</a></li>
          <li><a href="#">Ciasta</a></li>
        </ul>
      </li>
      <li><a href="#">Kontakt</a></li>
    </ul>
  </nav>
</header>

我当然可以给左边距:550px给nav ul,这没关系,但是我不认为这是个好习惯。您对此主题有何看法?

3 个答案:

答案 0 :(得分:4)

您所要做的就是使这两个div(左徽标和右菜单项)的父级显示灵活,并像这样显示justify-content:space-between或justify-content:space-around

.parent-div {
  display:flex;
  justify-content: space-between; //or space-around
}

.child-left {//doesn't matter what you put in here just so long as its a div}

.child-right {//doesn't matter what you put in here just so long as its a div}

如果您还有其他问题,如果我需要帮助弄清楚如何使用flex,这是我去的地方:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

答案 1 :(得分:1)

我想您需要的是将导航菜单向右移动,阅读评论。

因此,解决方案是在导航的左侧留出空间(菜单链接)。是的,您绝对可以使用margin-left,但不需要指定确切的像素。

最好提供margin-left:auto,以便将剩余空间留在导航的左侧

解决方案:

header {
  background-image: radial-gradient(circle, #72d6c9, #54d1ed, #7ac5ff, #bcb2fe, #f29cd9);
  height: 80px;
}

.menu-section {
  display: flex;
}

.nav-logo {
  color: #e00986;
  font-size: 25px;
  margin: 0;
  padding-left: 15px;
  line-height: 80px;
}

nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  text-align: center;
  margin-left:auto; /*ADDED HERE - THIS IS THE TRICK :)*/
}

nav ul li {
  display: inline-block;
  width: 150px;
  position: relative;
}

nav ul li a {
  text-decoration: none;
  line-height: 80px;
  padding: 0 10px;
  display: block;
  color: #e00986;
}

nav ul li a:hover {
  color: #FFF;
  transition-duration: 2s;
}
<header>
  <nav class="menu-section">
    <h1 class="nav-logo">Love &#9825 Cookies</h1>
    <ul>
      <li><a href="#">Strona główna</a></li>
      <li><a href="#">Ciastka</a>
        <ul>
          <li><a href="#">Torty</a></li>
          <li><a href="#">Babeczki</a></li>
          <li><a href="#">Bezy</a></li>
          <li><a href="#">Ciasta</a></li>
        </ul>
      </li>
      <li><a href="#">Kontakt</a></li>
    </ul>
  </nav>
</header>

希望这对您有所帮助。

答案 2 :(得分:0)

.nav-logo
{
    color: #e00986;
    font-size: 25px;
    margin-right: 100px;
    padding-left: 15px;
    line-height: 80px;
}

为外部间距使用边距:)!