我知道我可以在父 div 上使用 text-align: center;
使其所有子元素居中,但如果我不想让所有子元素居中怎么办?比如说,我只想要一个相对于父元素居中的子元素。
假设我在包装器 div 中拥有所有内容。一些标题、表格等。但在底部,我想把一小行文字放在中间,以说明一些事情。如果它的层次结构是 body > div.wrapper > div.credit,我将如何实现?
最佳答案
那么为什么要使 CSS 复杂化呢?只需将该文本包装在一个 div 中,然后为该特定的 div 使用 text-align: center;
即可完成
如果您想以 CSS 层次结构方式声明,只需使用此
body div:last-child {
text-align: center;
}
Note: Be sure the text you want to be centered is the last div you are using else don't use last-child selector, better declare a class
关于html - 如何使用 CSS 将文本相对于其父 div 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883747/