Flexbox 样式不适用于元素
Flexbox styling not applying to elements
无论我如何设置元素样式,我应用的 none Flexbox 样式都有效。我到处搜索解决方案,但找不到任何解决方案(如果这是重复的,我深表歉意,因为我找不到问题的答案)。
我创建了一个CodePen here。
HTML:
<div class="test">
<div class="test2">
</div>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
.test {
height: 10em;
width: 10em;
background: #333;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
text-align: center;
}
在此先感谢您提供的任何帮助。
您需要将这些 CSS 规则添加到父元素。
当您在一个元素上设置 display: flex
时,它的直接子元素将成为 flexbox 项目。在您的示例中,.test2
元素没有任何子元素,因此我假设您可能想在父元素上添加 display: flex
。
.test {
height: 10em;
width: 10em;
background: #333;
display: flex;
align-items: center;
justify-content: center;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
}
<div class="test">
<div class="test2"></div>
</div>
无论我如何设置元素样式,我应用的 none Flexbox 样式都有效。我到处搜索解决方案,但找不到任何解决方案(如果这是重复的,我深表歉意,因为我找不到问题的答案)。
我创建了一个CodePen here。
HTML:
<div class="test">
<div class="test2">
</div>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
.test {
height: 10em;
width: 10em;
background: #333;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
text-align: center;
}
在此先感谢您提供的任何帮助。
您需要将这些 CSS 规则添加到父元素。
当您在一个元素上设置 display: flex
时,它的直接子元素将成为 flexbox 项目。在您的示例中,.test2
元素没有任何子元素,因此我假设您可能想在父元素上添加 display: flex
。
.test {
height: 10em;
width: 10em;
background: #333;
display: flex;
align-items: center;
justify-content: center;
}
.test2 {
height: 2.5em;
width: 2.5em;
background: #ff0000;
}
<div class="test">
<div class="test2"></div>
</div>