html - 无法正确证明 flexbox 容器中的内容

标签 html css flexbox

我在设置 flexbox 中元素的对齐时遇到了一些问题。

我在容器类中设置了这个属性,但不知何故它不会影响任何东西?我无法弄清楚,感谢您的帮助。

jsfiddle-> https://fiddle.jshell.net/n7ts68se/4/

(嵌入式编辑器用填充弄乱了一些东西,所以 jsfiddle)

我想让元素相互粘在一起,所以我设置了'center'值,但它们之间仍然留有空间,怎么了?

<nav class="main-header">                       
<a id="logo" class="main-button header-logo">                           
    <h4>test logo here..</h4>
</a>
<a id="t1" class="main-button">                         
    <h4> najnowsze standardy.</h4>
</a>
<a id="t2" class="main-button">
    <h4> profesjonalna wspólpraca.</h4>
</a>
<a id="t3"  class="main-button" href="kontakt.php">
    <h4>kontakt</h4>
</a>                                                
</nav>

CSS:

.main-header {
display: flex;
justify-content:     center;
margin: 0 0 3em 0;
}
.main-button {
background: rgba(70, 73, 79,0.6);   
margin: 0 auto;
padding: 0 1em;
color: darkblue;
font-size: 110%;
box-shadow: 1px 2px 1px 0px lightgray;
}

最佳答案

您需要从 .main-button 中删除 left/right 上的 margin: auto,这就是使元素表现得像您在 flex 容器上设置了 justify-content: space-around

.main-header {
  display: flex;
  justify-content: center;
  margin: 0 0 3em 0;
}
.main-button {
  background: rgba(70, 73, 79, 0.6);
  padding: 0 1em;
  margin: 0;
  color: darkblue;
  font-size: 110%;
  box-shadow: 1px 2px 1px 0px lightgray;
}
<nav class="main-header">
  <a id="logo" class="main-button">
    <h4>test logo here..</h4>
  </a>
  <a id="t1" class="main-button">
    <h4> first. </h4>
  </a>
  <a id="t2" class="main-button">
    <h4> last. </h4>
  </a>
  <a id="t3" class="main-button" href="kontakt.php">
    <h4> after. </h4>
  </a>
</nav>

关于html - 无法正确证明 flexbox 容器中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120417/

相关文章:

javascript - 提交一个输入框或一个选择框(不能同时提交)

jquery - 设计 DIV 以缩小垂直间隙

javascript - Jssor限制 slider 移动方向

javascript - 如何在 jQuery 中控制我的悬停事件?

css - 如何将文本与其他居中文本对齐

html - 一个 flex/网格元素设置 sibling 的大小限制

html - 为什么具有相同百分比宽度的 div 不均匀呈现

保存txt文件的javascript代码

html - 文本宽度限制,带换行

html - 如何创建这个简单的布局?