首页前端开发CSScss如何设置垂直居中

css如何设置垂直居中

时间2024-01-28 04:12:02发布访客分类CSS浏览396
导读:收集整理的这篇文章主要介绍了css如何设置垂直居中,觉得挺不错的,现在分享给大家,也给大家做个参考。css设置垂直居中的方法:1、使用line-height属性让文字垂直居中;2、使用CSS3 flex布局让文字垂直居中;3、使用绝对定位和...
收集整理的这篇文章主要介绍了css如何设置垂直居中,觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

css设置文本文字垂直居中

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

!DOCTYPE html>
    html>
    head>
    meta charset="UTF-8">
    tITle>
    css 垂直居中/title>
    style>
.box{
        width: 300px;
        height: 300px;
        background: paleturquoise;
        line-height:300px;
}
    /style>
    /head>
    body>
    div class="box">
    css 垂直居中了--文本文字/div>
    /body>
    /html>
    

效果图:

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

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

!DOCTYPE html>
    html>
    head>
    meta charset="UTF-8">
    title>
    css 垂直居中/title>
    style>
.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;
/*新版本*/}
    /style>
    /head>
    body>
    div class="box">
    css 垂直居中--文本文字(弹性布局)/div>
    /body>
    /html>
    

效果图:

css设置块状元素垂直居中

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

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

!DOCTYPE html>
    html>
    head>
    meta charset="UTF-8">
    title>
    css 垂直居中/title>
    style>
.box{
    width: 300px;
        height: 300px;
        background: #ddd;
        position: relative;
}
.child{
    background: #93BC49;
        position: absolute;
        top: 50%;
        transform: translate(0, -50%);
}
    /style>
    /head>
    body>
    div class="box">
        div class="child">
    css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中/div>
    /div>
    /body>
    /html>
    

效果图:

2、使用flex布局

!DOCTYPE html>
    html>
    head>
    meta charset="UTF-8">
    title>
    css 垂直居中/title>
    style>
.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;
}
    /style>
    /head>
    body>
    div class="box">
        div class="child">
    css 垂直居中了--弹性布局/div>
    /div>
    /body>
    /html>
    

效果图:

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

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

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: css如何实现鼠标经过样式改变下一篇:css如何设置滚动条宽度猜你在找的CSS相关文章 css怎么控制按钮不可用2022-05-17css3中transform属性实现的4种功能2022-04-13纯CSS3实现div按照顺序出入效果2022-04-13CSS实现隐藏搜索框功能(动画正反向序列)2022-04-13使用CSS3实现按钮悬停闪烁动态特效代码2022-04-13CSS3 Tab动画实例之背景切换动态效果2022-04-13CSS实现两列布局的N种方法2022-04-13CSS 实现Chrome标签栏的技巧2022-04-13css实现两栏布局左侧固定宽右侧自适应的多种方法2022-04-13从QQtabBar看css命名规范BEM的详细介绍2022-04-13 其他相关热搜词更多phpjavapython程序员

若转载请注明出处: css如何设置垂直居中
本文地址: https://pptw.com/jishu/589144.html
css怎么设置边框的宽度 css如何实现鼠标经过样式改变

游客 回复需填写必要信息