CSSzxx日报

CSS行内、块级、行内块、浮动、定位,水平垂直居中方法

1.行内元素

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .one {
            width: 600px;
            height: 400px;
            background-color: red;
            
			margin: auto;            /红盒子居中
            
            text-align: center;          /文字(行内)水平居中
            line-height: 400px;          /文字(行内)垂直居中
        }
        .two {
        }
    </style>
·</head>
<body>
<div class="one">
    <span class="two">span</span>
</div>
</body>
</html>

2.块级元素

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .one {
            width: 600px;
            height: 400px;
            background-color: red;

            margin: auto;               /红盒子居中
            padding-top: 1px;           /防止顶部塌陷
        }
        .two {
            width: 60px;
            height: 60px;
            background-color: gold;

            text-align: center;         /文字水平居中
            line-height: 60px;          /文字垂直居中

            margin: 200px auto 0;           /黄盒子(块级),水平居中,垂直距顶部200px
            transform: translateY(-50%);    /黄盒子(块级),位移自身一半高度(30px),使它垂直居中
        }
    </style>
</head>
<body>
<div class="one">
    <div class="two">div</div>
</div>
</body>
</html>

3.行内块元素

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .one {
            width: 600px;
            height: 400px;
            background-color: red;

            margin: auto;               /红盒子居中
            
            text-align: center;             /黄盒子(行内块)水平居中,文字也居中
        }
        .two {
            width: 60px;
            height: 60px;
            background-color: gold;
            display: inline-block;
            
            line-height: 60px;          /文字垂直居中

            margin-top: 200px;              /黄盒子(行内块),垂直距顶部200px
            transform: translateY(-50%);    /黄盒子(行内块),位移自身一半高度(30px),使它垂直居中

        }
    </style>
</head>
<body>
<div class="one">
    <div class="two">行内块</div>
</div>
</body>
</html>

4.浮动元素

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .one {
            width: 600px;
            height: 400px;
            background-color: red;
            
            margin: auto;                /红盒子居中
        }
        .fl {
            width: 60px;
            height: 60px;
            background-color: gold;
            float: left;
                        
            text-align: center;          /文字水平居中
            line-height: 60px;           /文字垂直居中
            
            margin-left: 50%;                  /黄盒子(浮动),水平距左边300px
            margin-top: 200px;                 /黄盒子(浮动),垂直距顶部200px
            transform: translate(-50%,-50%);   /位移自身一半宽(30px)(30px),使它水平垂直居中
        }
    </style>
</head>
<body>
<div class="one">
    <div class="fl">浮动</div>
</div>
</body>
</html>

5.定位元素

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .one {
            width: 600px;
            height: 400px;
            background-color: red;
            position: relative;
            
            margin: auto;             /红盒子居中
        }
        .two {
            width: 60px;
            height: 60px;
            background-color: gold;
            position: absolute;
            
            text-align: center;       /文字水平居中
            line-height: 60px;        /文字垂直居中
            
            left: 50%;                           /黄盒子(定位),水平距左边300px
            top: 50%;                            /黄盒子(定位),垂直距顶部200px
            transform: translate(-50%,-50%);     /位移自身一半宽(30px)(30px),使它水平垂直居中
        }
    </style>
</head>
<body>
<div class="one">
    <div class="two">定位</div>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值