英文:
Html css justify-content: space-between doesnt work correctly when parent is justify content center is active
问题
我现在有以下布局
现在我真的希望所有的h2标题都并排,之间有间隔
有人告诉我要将这段代码添加到我的CSS中
justify-content: space-between;
我希望实现类似于这样的结果,但是当我添加
justify-content: space-between;
时,什么也不发生
这是我使用的代码
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言: lang-css -->
.navbar {
position: fixed;
text-align: center;
top: 0;
left: 0;
width: 100%;
height: auto;
background-color: #003959;
color: white;
justify-content: center;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
}
<!-- 语言: lang-html -->
<div class="navbar">
<h1>test</h1>
<div style="justify-content: space-between;">
<h2>test</h2>
<h2>test</h2>
<h2>test</h2>
</div>
</div>
<!-- 结束代码片段 -->
我认为问题可能是由父级div引起的,但我非常不确定如何解决这个问题。
<details>
<summary>英文:</summary>
I have the following laylout right now
[![Sample image][1]][1]
Now id really like to have all of the h2s with test next to each other with space between
I was told to add this to my css
justify-content: space-between;
[![Example of desired result][2]][2]
[1]: https://i.stack.imgur.com/sfPLl.png
[2]: https://i.stack.imgur.com/OUd3W.png
Id like to achieve a result similar to this however when I add
justify-content: space-between;
Nothing happens
here's the code I used
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.navbar {
position: fixed;
text-align: center;
top: 0;
left: 0;
width: 100%;
height: auto;
background-color: #003959;
color: white;
justify-content: center;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
}
<!-- language: lang-html -->
<div class="navbar">
<h1>test</h1>
<div style="justify-content: space-between;">
<h2>test</h2>
<h2>test</h2>
<h2>test</h2>
</div>
</div>
<!-- end snippet -->
I assume the issue is caused by the div parent but I'm very unsure how to solve this
</details>
# 答案1
**得分**: 1
You need to set the `display` to `flex` for `space-between` to work:
```css
.navbar {
position: fixed;
text-align: center;
top: 0;
left: 0;
width: 100%;
height: auto;
background-color: #003959;
color: white;
justify-content: center;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
}
<div class="navbar">
<h1>test</h1>
<div style="justify-content: space-between; display: flex;">
<h2>test</h2>
<h2>test</h2>
<h2>test</h2>
</div>
</div>
If that spaces the test elements too far apart, try center
instead of space-between
and then add some left/right margins to them.
英文:
You need to set the display
to flex
for space-between
to work:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.navbar {
position: fixed;
text-align: center;
top: 0;
left: 0;
width: 100%;
height: auto;
background-color: #003959;
color: white;
justify-content: center;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
}
<!-- language: lang-html -->
<div class="navbar">
<h1>test</h1>
<div style="justify-content: space-between;display:flex;">
<h2>test</h2>
<h2>test</h2>
<h2>test</h2>
</div>
</div>
<!-- end snippet -->
If that spaces the test elements too far apart, try center
instead of space-between
and then add some left/right margins to them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论