首页 建站教程 WEB前端 正文

元素垂直居中

在写css样式时,水平居中比较简单,这里主要介绍垂直居中的方法,代码如下:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style&g...

在写css样式时,水平居中比较简单,这里主要介绍垂直居中的方法,代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .wrap{
            width: 400px;
            height: 400px;
            margin: 0 auto;
            background: rgba(0,0,0,0.5);
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .main{
            width: 400px;
            height: 400px;
            margin: 20px auto;
            position: relative;
            background: rgba(0,0,0,0.5);
            color: #fff;
        }
        .main p{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body>
    <div>
        <p>对对对对对对对对对对对对对对对<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是</p>
    </div>
    <div>
        <p>对对对对对对对对对对对对对对对<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是<br>猜猜猜猜猜错错错错错错错错错错错<br>试试事实上是是是</p>
    </div>
</body>
</html>


打赏
海报

本文由优美主题原创或收集发布

转载请注明本文地址:https://www.umtheme.com/web/31.html

相关推荐

css中position:fixed如何实现相对于父级定位

css中position:fixed如何实现相对于父级定位

在项目开发中我们经常会用到position:fixed属性,它常常应用的场合是,当下拉滚动条时固定导航栏到顶部,将广告固定在页面两侧或浏览器中间。当我们使用position:fixed定位属性时,它生...
WEB前端 2023.02.12 0 705
发布评论

ainiaobaibaibaibaobaobeishangbishibizuichiguachijingchongjingdahaqiandaliandangaodw_dogedw_erhadw_miaodw_tuzidw_xiongmaodw_zhutouganbeigeiliguiguolaiguzhanghahahahashoushihaixiuhanheixianhenghorse2huaixiaohuatonghuaxinhufenjiayoujiyankeaikeliankouzhaokukuloukunkuxiaolandelinileimuliwulxhainiolxhlikelxhqiuguanzhulxhtouxiaolxhwahahalxhzanningwennonuokpinganqianqiaoqinqinquantouruoshayanshengbingshiwangshuaishuijiaosikaostar0star2star3taikaixintanshoutianpingtouxiaotuwabiweifengweiquweiwuweixiaowenhaowoshouwuxiangjixianhuaxiaoerbuyuxiaokuxiaoxinxinxinxinsuixixixuyeyinxianyinyueyouhenghengyuebingyueliangyunzanzhajizhongguozanzhoumazhuakuangzuohenghengzuoyi
感谢您的支持