欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

让div内容在HTML中居中的两种方法:方法二

最编程 2024-08-08 19:46:53
...

只用padding属性,通过计算求得居中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="author" type="Nancle from CAU CS101"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>如何让img标签在div里左右上下居中</title>
    <script type="text/javascript">
    </script>
    <style type="text/css">
        #container {
            /*  300*150*/
            width: 300px;
            height: 150px;
            background-color: cyan;
        }

        #container1 {
            /*  300*150*/
            width: 300px;
            height: 150px;
            background-color: cyan;
            /*水平居中*/
            padding-left: 150px; /*div宽度-img宽度*/
        }

        #container2 {
            /*  300*150*/
            width: 300px;
            height: 150px;
            background-color: cyan;
            /*水平居中*/
            padding-left: 150px; /*div宽度-img宽度*/
            /*垂直居中*/
            padding-top: 50px; /*div高度-img高度*/
        }
    </style>
</head>
<body>
<div id="container"><!--150*100-->
    <img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<br/>
<br/>
<br/>
<div id="container1"><!--150*100-->
    <img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<br/>
<br/>
<br/>
<div id="container2"><!--150*100-->
    <img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<p>
</p>
</body>
</html> 

方法三:

利用图片的margin属性将图片水平居中,利用DIV的padding属性将图片垂直居中。

Img是内联元素,要设置其margin属性使其居中,就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="author" type="Nancle from CAU CS101"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>如何让img标签在div里左右上下居中</title>
    <script type="text/javascript">
    </script>
    <style type="text/css">
        .div1 {
            /*  300*150*/
            width: 300px;
            height: 150px;
            background-color: cyan;
            /*垂直居中*/
            padding-top: 50px;
        }

        /* Img是内联元素,要设置其margin属性使其居中,
        就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中*/
        img {
            /*background-color: red;*/
            display: block;
            /*水平居中*/
            margin: 0 auto;
        }
    </style>
</head>

<body>
<div class="div1">
    <img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
</body>
</html> 

参考:

如何让图片在div中居中显示?

二、div盒子 居中的方法:

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div居于页面正中间</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            background-color: #EAEAEA;
        }

        div {
            width: 200px;
            height: 200px;
            background-color: #1E90FF;
            /*--------------------------文字居中方法--------------------------*/
            text-align: center; /*水平居中*/
            line-height: 200px; /*垂直居中:div的全高度*/

            /*--------------------------div居中方法--------------------------*/
            /*使div脱离文档流*/
            position: absolute;
            /*左上角位置*/
            top: 50%; /*div下移父容器的一半高度*/
            left: 50%; /*div右移父容器的一半宽度*/
            /*中心矫正:上移div自身一半高度,左移div自身一半宽度*/
            -webkit-transform: translate(-50%, -50%);
            -moz-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            -o-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }
    </style>

</head>

<body>
<div>
    我在中间
    <!--<img src="../share_icon_sina_weibo.png" width="150" height="100"/>-->
</div>
<!--
使文字处于div中心方法:
先设置一个div的宽高,使用text-align水平居中;
再使用line-height上下居中即可(line-height设置的高度需跟div的高度一致)
-->
</body>

</html>

方法二:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div居中方法</title>
    <style>
        /*先要设置div元素的祖先元素html和body的高度为100%(因为他们默认是为0的),
        并且清除默认样式,即把margin和padding设置为0(如果不清除默认样式的话,浏览器就会出现滚动条*/
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }

        div {
            width: 300px;
            height: 300px;
            background: orange;
            position: relative; /*相对父容器*/
            /*-----------------------div居中-----------------------*/
            /*水平居中*/
            margin: 0 auto; /*水平居中*/
            /*垂直居中*/
            top: 50%; /*垂直偏移:父元素的一半高度*/
            margin-top: -150px; /*垂直矫正:上移自身一半高度*/
            /*---------------------文字居div中间----------------------*/
            text-align: center;
            line-height: 300px;
        }
    </style>
</head>

<body>
<div>我在中间</div>
</body>
</html>

推荐阅读