这篇文章给大家介绍怎么在CSS中设置元素垂直居中,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1.容器里面的内容只有一行文字<!DOCTYPE html><html> &nb
这篇文章给大家介绍怎么在CSS中设置元素垂直居中,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1.容器里面的内容只有一行文字
<!DOCTYPE html><html> <style> * { padding: 0; margin: 0; } div { height: 60px; background-color: #1888fa; color: white; } span { line-height: 60px; } </style> <body> <div> <span>测试</span> </div> </body></html>
2.容器自然高度
CSS中最简单的垂直居中方法是给容器相等的上下内边距,让容器和内容自行决定自己的高度。看下面的例子, 通过设置padding-top
和padding-bottom
相等的值,让内容在父容器垂直居中。
<!DOCTYPE html><html> <style> * { padding: 0; margin: 0; } div { padding-top: 20px; padding-bottom: 20px; background-color: #1888FA; color: white; } </style> <body> <div> <span>测试</span> </div> </body></html>
3.使用 FlexBox
<!DOCTYPE html><html> <style> * { padding: 0; margin: 0; } div { height: 60px; display: flex; align-items: center; justify-content: center; background-color: #1888fa; color: white; } </style> <body> <div> <span>测试</span> </div> </body></html>
关于怎么在CSS中设置元素垂直居中就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
--结束END--
本文标题: 怎么在CSS中设置元素垂直居中
本文链接: https://www.lsjlt.com/news/269997.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0