如何在 Flexbox 中的两个 div 之间添加边距/空格 [重复]

Posted

技术标签:

【中文标题】如何在 Flexbox 中的两个 div 之间添加边距/空格 [重复]【英文标题】:How do I add a Margin/Space between two divs in Flexbox [duplicate] 【发布时间】:2020-01-31 14:08:05 【问题描述】:

我有一个左列和一个右列。右列 Div 元素的 Display Value 为 Flex,但我无法在其两个子 Div 元素之间添加边距或空格。

我找到了关于堆栈溢出的答案,建议我使用 Justify-content: space-around

但这似乎不起作用。我正在使用码笔平台练习。

body 
  margin: 0 auto;
  width: 100%;
  max-width: 2500px;
  padding: 0;


* 
  box-sizing: border-box;


header 
  background: blue;
  width: 100%;
  height: 100px;


a 
  text-decoration: none;
  text-align: center;
  color: #ffffff;
  font-size: 1.15em;


h1 
  position: absolute;
  top: 15px;
  left: 10px;
  float: left;
  left-margin: 6%;
  margin-top: 10px;
  padding: 3px;


ul 
  float: right;
  margin: auto;
  margin-top: 37px;


li 
  display: inline-block;
  padding-left: 20px;
  padding-right: 10px;


.lastlist 
  padding-right: 30px;


.wrapper 
  width: 100%;
  margin: 0 auto;
  padding: 0;


.leftcolumn 
  padding: 30px;
  float: left;
  width: 70%;
  background: grey;
  height: 2000px;
  display: grid;
  grid-template-rows: 200px 200px 200px;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 20px;
  border-radius: 1px;


.box1 
  grid-column: 1/3;


.rightcolumn 
  padding: 30px;
  float: left;
  width: 30%;
  background: pink;
  height: 2000px;
  border-radius: 1px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;


.rightbox1 
  flex: 1 30px;
  margin-left: 2%;


.rightbox2 
  flex: 1 30px;


div 
  background: white;
  border-radius: 5px;
  padding: 10px;
<header>
  <h1>Daniel Savva</h1>
  <nav>
    <ul>
      <li> <a href="#"> Home</a> </li>
      <li> <a href="#"> Portfolio</a> </li>
      <li> <a href="#"> Gallery</a> </li>
      <li> <a href="#"> Contact</a> </li>
      <li class="lastlist"> <a href="#"> Services</a> </li>
    </ul>
  </nav>
</header>
<div class="wrapper">
  <div class="leftcolumn">
    <div class="box1">hi</div>
    <div>hi</div>
    <div>hi</div>
    <div>hi</div>
    <div>hi</div>

  </div>
  <div class="rightcolumn">
    <div class="rightbox1">hello</div>
    <div class="rightbox2">hello</div>
  </div>
</div>

【问题讨论】:

只有第一个框有左边距,所以它们并排粘在一起,因为你没有设置边距。 感谢 G-Cyr 现在你向我解释了它看起来像一个非常愚蠢的错误。感谢您的帮助。 【参考方案1】:

请用给定的 css 替换“rightcolumn”子 css。

.rightbox1 
    flex: 1 1 auto;
    margin: 5px;

.rightbox2 
    flex: 1 1 auto;
    margin: 5px;

【讨论】:

【参考方案2】:

试试这个:

.rightbox1 
    flex: 1 30px;
    margin: 0 2%;


.rightbox2 
    flex: 1 30px;
    margin: 0 2%;

【讨论】:

以上是关于如何在 Flexbox 中的两个 div 之间添加边距/空格 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

在flexbox中的两个兄弟之间居中元素[重复]

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

如何使 flexbox 响应式?

如何使用 flexbox 垂直居中 div [重复]

如何在使用flexbox时在句子的中间添加?

Flexbox Div 不包含 Safari 中的所有元素,但在 Chrome 中运行良好