我的样式表中有一个名为gdiv
的类,它引用一般的div
条件。我可以从gdiv
更改文本对齐方式。但无法更改div
中所有内容的对齐方式,例如div
。如何对齐所有内容?
.gdiv {
display: flex; /* just to set it to flex box */
justify-content: center; /* to align the items horizontally in the div */
align-items: center; /* to align the items vertically in the div */
}
如果要按DIV的父级对齐DIV本身,则可以在父级中使用此代码;
如果您是初学者,我强烈建议您学习CSS中的Flexbox和网格!如果要对齐的是文本,则可以使用:
.gdiv { text-align: center; }
display: flex;
align-items: center;
justify-content: center;