如何在具有 children div 不同大小的 parent div 中居中文本(flexbox)
How to center text within a parent div that has children divs of different sizes (flexbox)
我正在使用 Flexbox 创建一个站点(我对它的使用相当陌生)。我想将网站名称和菜单下拉按钮放在同一行。我能够使用 Flexbox 成功地做到这一点,但是标题并不在页面本身的中心。我知道这是因为菜单按钮的 flex-grow 为 1,而标题的 flex-grow 为 8。我这样做是为了留出间距,但现在标题并未居中整个页面。
我试过将标题 div 留空,只在 div 之外写一个标题,但这会将文本放在 parent 容器之前。
<body>
<div class="parent container">
<!--Navigational Bar-->
<div class="Nav">
<div id="Nav_Button">
<p>Button</p>
</div>
<div id="Nav_Title">
<h3>Super Snack Stadium</h3>
</div>
</div>
</div>
</body>
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
如果可能的话,我希望能够使用 Flexbox 使整个页面的标题居中(而不仅仅是在其所在的容器内)。
所以如果我没理解错的话,你已经在正确的轨道上了,但你没有指定它应该居中。
#Nav_Button {
display: flex;
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
align-items: center;
justify-content: center;
}
看到这里你使用了 div 而这个 div 有默认显示:块;
https://www.w3schools.com/cssref/css_default_values.asp
这里是 align-items 和 justify-content
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
第一步:设置Nav
position:relative;
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position:relative;
}
第 2 步: 使 h3
绝对化。
h3 {
position:absolute;
}
现在,h3
脱离了文档流,但可以根据最近的relative
元素定位。
在这种情况下,我们将.Nav
设为relative
。因此,h3
将使用 .Nav
作为参考。由于 .Nav
占据了整个屏幕宽度,我们只需要使用 left
和 transform
将 h3
定位到 .Nav
的中心
第 3 步:
h3 {
position:absolute;
left:50%;
transform:translateX(-50%);
}
决赛:
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position:relative;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
h3 {
position:absolute;
left:50%;
transform:translateX(-50%);
}
<div class="Nav">
<div id="Nav_Button"><p>Button</p></div>
<div id="Nav_Title"><h3>Super Snack Stadium</h3></div>
</div>
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position: relative;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
width: 300px;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
position: absolute;
width: 100%;
}
#Nav_Title h3 {
margin: 14px;
}```
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position: relative;
}
h3 {
position: absolute;
width: 100%;
left: 0;
top: -5px;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
<div class="parent container">
<!--Navigational Bar-->
<div class="Nav">
<div id="Nav_Button">
<p>Button</p>
</div>
<div id="Nav_Title">
<h3>Super Snack Stadium</h3>
</div>
</div>
</div>
我正在使用 Flexbox 创建一个站点(我对它的使用相当陌生)。我想将网站名称和菜单下拉按钮放在同一行。我能够使用 Flexbox 成功地做到这一点,但是标题并不在页面本身的中心。我知道这是因为菜单按钮的 flex-grow 为 1,而标题的 flex-grow 为 8。我这样做是为了留出间距,但现在标题并未居中整个页面。
我试过将标题 div 留空,只在 div 之外写一个标题,但这会将文本放在 parent 容器之前。
<body>
<div class="parent container">
<!--Navigational Bar-->
<div class="Nav">
<div id="Nav_Button">
<p>Button</p>
</div>
<div id="Nav_Title">
<h3>Super Snack Stadium</h3>
</div>
</div>
</div>
</body>
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
如果可能的话,我希望能够使用 Flexbox 使整个页面的标题居中(而不仅仅是在其所在的容器内)。
所以如果我没理解错的话,你已经在正确的轨道上了,但你没有指定它应该居中。
#Nav_Button {
display: flex;
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
align-items: center;
justify-content: center;
}
看到这里你使用了 div 而这个 div 有默认显示:块; https://www.w3schools.com/cssref/css_default_values.asp
这里是 align-items 和 justify-content https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
第一步:设置Nav
position:relative;
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position:relative;
}
第 2 步: 使 h3
绝对化。
h3 {
position:absolute;
}
现在,h3
脱离了文档流,但可以根据最近的relative
元素定位。
在这种情况下,我们将.Nav
设为relative
。因此,h3
将使用 .Nav
作为参考。由于 .Nav
占据了整个屏幕宽度,我们只需要使用 left
和 transform
h3
定位到 .Nav
的中心
第 3 步:
h3 {
position:absolute;
left:50%;
transform:translateX(-50%);
}
决赛:
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position:relative;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
h3 {
position:absolute;
left:50%;
transform:translateX(-50%);
}
<div class="Nav">
<div id="Nav_Button"><p>Button</p></div>
<div id="Nav_Title"><h3>Super Snack Stadium</h3></div>
</div>
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order:1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position: relative;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
width: 300px;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
position: absolute;
width: 100%;
}
#Nav_Title h3 {
margin: 14px;
}```
.parent_container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
}
/*Containers within ".parent container"*/
.Nav {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 1;
/*Flex Parent Code*/
display: flex;
flex-direction: row;
position: relative;
}
h3 {
position: absolute;
width: 100%;
left: 0;
top: -5px;
}
/*--------Children of .Nav ---------*/
#Nav_Button {
border: 1px solid black;
/*Flex Child Code*/
order: 1;
flex-grow: 1;
}
#Nav_Title {
border: 1px solid black;
text-align: center;
/*Flex Child Code*/
order: 2;
flex-grow: 8;
}
<div class="parent container">
<!--Navigational Bar-->
<div class="Nav">
<div id="Nav_Button">
<p>Button</p>
</div>
<div id="Nav_Title">
<h3>Super Snack Stadium</h3>
</div>
</div>
</div>