css一个盒子水平居中和隐藏的方法

1.盒子水平居中

(1)margin:0 auto;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
            
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
             margin: 0 auto;
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

(2)transform:translate();

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
            
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
             transform: translate(90px,0);
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

(3)display:flex;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
             display: flex;
             justify-content: center;
            
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
             margin-top: 0;
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

(4)position:relative;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
             margin-top: 0;
             position:relative;
             left: 90px;
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

2.隐藏元素

(1)transform:scale(  ,);占据屏幕空间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
             transform: scale(0,0);
          
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

(2)visibility:hidden; 占据屏幕空间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
         }
         p{
             background-color: blue;
             width: 120px;
             height: 90px;
            visibility: hidden;
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

(3)overflow:hidden; 不占据原先位置

当子元素浮动时,父元素背景不显示,如图一所示

图1

给父元素清除浮动,背景显示

(4) opacity:0;占据屏幕空间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         div{
             background-color: rgb(23, 31, 31);
             width: 300px;
             height: 160px;
         }
         p{
            background-color: royalblue;
             margin: 0;
             width: 120px;
             height: 90px;
             opacity: 0;
         }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>

 (5)伪元素选择器

  ::after   选中之后不存在的元素

  ::before  选中之前不存在的元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值