[英]how to centre align div horizontally when using flex direction column
我有具有表和与所述一个div的容器display
设定为flex
与flex-direction
为column
。
我想将div中心水平对齐。 这是代码
<style>
.container {
display: flex;
flex-direction: column;
border: 1px solid red;
}
.mytable {
width: 100%
}
table,
th,
td {
border: 1px solid black;
}
.container2 {
margin: 2.5em 0 0;
display: flex;
max-width: 30%;
text-align: center;
}
.question {
padding: 5px 20px;
line-height: 20px;
font-size: 14px;
min-width: 50%;
background-color: green;
border: 1px solid rgba(27, 31, 35, 0.2);
border-top-left-radius: 18px;
border-bottom-left-radius: 18px;
color: white;
}
.answer {
background-color: blue;
padding: 5px 20px;
line-height: 20px;
font-size: 14px;
min-width: 50%;
border: 1px solid rgba(27, 31, 35, 0.2);
border-top-right-radius: 18px;
border-bottom-right-radius: 18px;
color: white;
}
</style>
<div class="container">
<div>
<table class="mytable">
<tr>
<th>ABC</th>
<th>DEF</th>
</tr>
</table>
</div>
<div class="container2">
<span class="question">What is your Name</span>
<span class="answer">Sunil Garg</span>
</div>
</div>
我已经在container2
类上尝试了justify-content
和align-items
CSS规则。 但是这些规则没有得到应用。 有什么问题
由于将text-align:center
设置为container2
类,但是其类别answer
子div仍未显示居中对齐的文本。 可能是什么问题?
这是小提琴https://jsfiddle.net/er_markar/nznddv4k/
谢谢!!
要在flexbox
对齐子flexbox
,请将以下项设置为父级:
1- justify-content
沿着主/主轴线,即垂直轴如果父母有对准孩子flex-direction: column
和水平轴如果父具有flex-direction: row
。
2 align-items
沿交叉/辅助轴对齐子align-items
,即,如果父级具有flex-direction: column
,则水平轴对齐;如果父级具有flex-direction: row
,则使垂直轴对齐。
对于您的情况,请尝试:
.container {
display: flex;
flex-direction: column;
border: 1px solid red;
justify-content: center; /* align children centered vertically */
align-items: center; /* align children centered horizontally */
}
并从用来对齐子级的子级中删除margin
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.