html - 如何使用 CSS 将文本相对于其父 div 居中?

标签 html css

我知道我可以在父 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/

相关文章:

html - firefox 不接受@font-family 声明

java - Spring 异常评估 SpringEL 表达式

css - 使用 CSS 指向图像中的区域并在悬停时显示一些文本?

javascript - 为什么删除某些代码行会出现错误?

javascript - 切换元素时如何进行转换?

javascript - Jquery 附加到 div

html - 你如何延迟 z-index 转换?

html - Bootstrap 列的高度不同

jquery - 如何根据jquery中出现的css类名添加类

css - 如何强制浏览器在 CSS 中打印背景图像?