亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

css中關于元素的水平居中和豎直居中

標簽:
Html/CSS

在web前端实际项目中,我们通常需要让元素居中:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中;4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元素竖直居中,7. 居中其他方法。

  1. 单行文本的水平居中,使用 text-align: center; (edge,firefix,chrome,opera,ie测试通过)
<!DOCTYPE html>
<html>
<head>
    <title>文字水平居中</title>   
    <meta charset="utf-8">
    <style>
    .text-center {
        width: 200px;
        height: 100px;
        text-align: center;
        background-color: #f54;
    }
    </style>
</head>
<body>
    <div class="text-center">水平居中</div>
</body>
</html>

文字水平居中

2 . 让图片水平居中,让父元素使用 text-align: center; (edge,firefix,chrome,opera,ie测试通过):

<!DOCTYPE html>
<html>
<head>
    <title>图片水平居中</title>   
    <meta charset="utf-8">
    <style>
    .text-center {
        width: 300px;
        height: 150px;
        text-align: center;
        background-color: #f54;
    }
    </style>
</head>
<body>
    <div class="text-center">
        <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="baidu.jpg" alt="baidu">
    </div>
</body>
</html>

图片水平居中

  1. 块级元素水平居中,使用 margin-left: auto; margin-right: auto;(edge,firefix,chrome,opera,ie测试通过):
<!DOCTYPE html>
<html>
<head>
    <title>块级元素居中</title>   
    <meta charset="utf-8">
    <style>
    .parent-box {
        width: 250px;
        height: 150px;
        background-color: #f98;
    }
    .child-box {
        width: 200px;
        height: 100px;
        background-color: #f00;
        margin-left: auto;
        margin-right: auto;
    }
    </style>
</head>
<body>
    <div class="parent-box">
        <div class="child-box">块级元素水平居中</div>
    </div>
</body>
</html>

图片描述

  1. 单行文本的竖直居中,使用 line-height: ...; (edge,firefix,chrome,opera,ie测试通过):
<!DOCTYPE html>
<html>
<head>
    <title>单行文本竖直居中</title> 
    <meta charset="utf-8">
    <style>
    .line-height {
        width: 250px;
        height: 100px;
        line-height: 100px;
        background-color: #f00;
    }
    </style>
</head>
    <div class="line-height">单行文本竖直居中</div>
</body>
</html>

单行文本竖直居中

  1. 不确定高度的一段文本竖直居中,这里不适用高度,使用 padding-top: ...; padding-bottom: ...;padding-top和padding-bottom值相同(edge,firefix,chrome,opera,ie测试通过):
<!DOCTYPE html>
<html>
<head>
    <title>不确定高度的一段文本竖直居中</title>   
    <meta charset="utf-8">
    <style>
    .padding {
        width: 150px;
        background-color: #f00;
        padding-top: 30px;
        padding-bottom: 30px;
    }
    </style>
</head>
    <div class="padding">不确定高度的一段文本竖直居中</div>
</body>
</html>

不确定高度的一段文本竖直居中

  1. 确定高度的块级元素竖直居中,使用 position: absolute; top: 50%; margin-top: ...;(margin-top的值为自身高度的值的一半的负值);(edge,firefix,chrome,opera,ie测试通过):
<!DOCTYPE html>
<html>
<head>
    <title>确定高度的块级元素竖直居中</title>    
    <meta charset="utf-8">
    <style>
    .parent-box {
        position: relative;
        width: 250px;
        height: 150px;
        background-color: #f00;
    }
    .child-box {
        position: absolute;
        top: 50%;
        width: 200px;
        height: 100px;
        margin-top: -50px;
        background-color: #f54;
    }
    </style>
</head>
    <div class="parent-box">
      <div class="child-box">确定高度的块级元素竖直居中</div>
    </div>
</body>
</html>

确定高度的块级元素竖直居中

  1. 竖直居中的其他办法:通过使用 transform: translateY(-50%); 添加厂商前缀(edge,firefix,chrome,opera,ie测试通过)
<!DOCTYPE html>
<html>
    <head>
        <title>verticalmiddle</title>
        <meta charset="utf-8">
        <style>
            .parent-box{
                width: 200px;
                height: 200px;
                background-color: #f00;
            }
            .child-box{
                position: relative;
                top: 50%;
                width: 100px;
                height: 100px;
                background-color: #0f0;
                -webkit-transform: translateY(-50%); 
                     -o-transform: translateY(-50%); 
                        transform: translateY(-50%);
            }
        </style>
    </head>
    <body>
        <div class="parent-box">
            <div class="child-box"></div>
        </div>
    </body>
</html>

竖直居中其他办法

  1. 居中其他方法,使用 position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto;(edge,firefix,chrome,opera,ie测试通过):
<!DOCTYPE html>
<html>
<head>
    <title>居中其他方法</title>   
    <meta charset="utf-8">
    <style>
    .parent-box {
        position: relative;
        width: 250px;
        height: 150px;
        background-color: #f00;
    }
    .child-box {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 200px;
        height: 100px;
        margin: auto;
        background-color: #f54;
    }
    </style>
</head>
    <div class="parent-box">
      <div class="child-box">居中其他方法</div>
    </div>
</body>
</html>

居中其他办法

點擊查看更多內容
18人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
9
獲贊與收藏
271

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消