Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
css如何设置垂直居中_创想鸟

css如何设置垂直居中

css设置垂直居中的方法:1、使用line-height属性让文字垂直居中;2、使用CSS3 flex布局让文字垂直居中;3、使用绝对定位和transform让块状元素垂直居中;4、使用flex布局让块状元素垂直居中。

css如何设置垂直居中

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。

css设置文本文字垂直居中

1、line-height 使文字垂直居中

css 垂直居中.box{    width: 300px;    height: 300px;    background: paleturquoise;    line-height:300px;}
css 垂直居中了--文本文字

效果图:

立即学习“前端免费学习笔记(深入)”;

1.png

这样就能让div中的文字水平垂直居中了

2、CSS3的flex布局 使文字垂直居中

css 垂直居中.box{   width: 300px;   height: 200px;   background: paleturquoise;   /*设置为伸缩容器*/   display: -webkit-box;   display: -moz-box;   display: -ms-flexbox;   display: -webkit-flex;   display: flex;   /*垂直居中*/   -webkit-box-align: center;/*旧版本*/   -moz-box-align: center;/*旧版本*/   -ms-flex-align: center;/*混合版本*/   -webkit-align-items: center;/*新版本*/   align-items: center;/*新版本*/}
css 垂直居中--文本文字(弹性布局)

效果图:

立即学习“前端免费学习笔记(深入)”;

2.png

css设置块状元素垂直居中

1、使用绝对定位和transform(未知元素高度)

如果我们不知道元素的高度,那么就需要先将元素定位到容器的中心位置,然后使用 transform 的 translate 属性,将元素的中心和父容器的中心重合,从而实现垂直居中:

css 垂直居中.box{width: 300px;    height: 300px;    background: #ddd;    position: relative;}.child{background: #93BC49;    position: absolute;    top: 50%;    transform: translate(0, -50%);}
css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中

效果图:

立即学习“前端免费学习笔记(深入)”;

3.png

2、使用flex布局

css 垂直居中.box{width: 300px;    height: 300px;    background: #ddd;    display: flex;    flex-direction: column;    justify-content: center;}.child{width: 300px;    height: 100px;    background: #08BC67;    line-height: 100px;}
css 垂直居中了--弹性布局

效果图:

立即学习“前端免费学习笔记(深入)”;

4.png

(学习视频分享:css视频教程)

以上就是css如何设置垂直居中的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1619569.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
css如何实现鼠标经过样式改变
上一篇 2025年12月24日 06:46:04
css如何设置滚动条宽度
下一篇 2025年12月24日 06:46:14

相关推荐

发表回复

登录后才能评论
关注微信