css让一个div绝对居中

本文详细介绍了六种使用CSS让div绝对居中的方法,包括:1) 利用父相子绝定位;2) 使用margin: auto; 3) 弹性盒子布局;4) CSS3的transform属性;5) 文本居中方法;6) 通过JS计算定位。部分方法在低版本IE浏览器中不兼容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

css让一个div绝对居中

   <!-- html页面 -->
<body>
  <div>
  </div>
</body>

方法1

  /* css */
   html,body{
            height: 100%;
        }
        body{
            margin: 0;
            background-color: bisque;
            position: relative;
        }
        div{
            background-color: aqua;
            width: 80px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -40px;
            margin-top: -100px;
        }

该方法利用css中父相子绝,让div根据body来进行定位(不加 position: relative;相对定位的话,默认也会相对body来进行定位). left: 50%; top: 50%;会使该div左上角在页面中间.加上 margin-left: -40px; margin-top: -100px;就形成了绝对居中. 该方法适用于宽高都确定的情况下

方法2

  /* css */
        html,
        body {
            height: 100%;
        }

        body {
            margin: 0;
            background-color: bisque;
            position: relative;
        }

        div {
            background-color: aqua;
            width: 80px;
            height: 200px;
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

该方法是将元素完全脱离文档流使用margin: auto;将div上下,左右都完全居中;

方法3

  /* css */
        html,
        body {
            height: 100%;
            
        }

        body {
            margin: 0;
            background-color: bisque;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        div {
            background-color: aqua;
            width: 80px;
            height: 200px;
        }

该方法利用的css3的弹性盒子使横轴以及纵轴都达到了居中的效果 IE10及其以下版本都不兼容

方法4

   /* css */
        html,
        body {
            height: 100%;
        }

        body {
            margin: 0;
            background-color: bisque;
            position: relative;
        }

        div {
            background-color: aqua;
            width: 80px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

该方法利用的css3中 transform属性中translate(-50%, -50%)进行的一个2D的转换,达到了居中的效果
IE10及其以下版本都不兼容

方法5

 /* css */
 body {
           margin: 0;
           background-color: bisque;
           width: 2000px;
           height: 1000px;
           display: table-cell;
           text-align: center;
           vertical-align: middle;
        }

        div {
            background-color: aqua;
            width: 80px;
            height: 200px;
            display: inline-block;
        }
        

该方法利用文本居中,(不常用).而且必须要有绝对的宽和高百分比的宽高也不可以

方法6

     //  基本样式的设定
    // box代表div
     box.style.width = '80px'
     box.style.height = '200px'
     box.style.background = ' aqua'
     document.body.style.position = 'relative'
     document.body.style.background = 'bisque'

     let htm = document.documentElement
     let htmWidth = htm.clientWidth
     let htmHeight = htm.clientHeight

     let boxW = box.clientWidth
     let boxH = box.clientHeight

     box.style.position = 'absolute'
     box.style.left = (htmWidth-boxW)/2 +'px'
     box.style.top =( htmHeight-boxH)/2 +'px'

该方法也是用的绝对定位,利用js算出left,和top的精确值,绝对定位到页面的中间

以上所有方法都可以得到以下效果

CSS中,有多种方法可以让一个`div`元素居中显示。以下是几种常用的方法: ### 方法一:使用Flexbox Flexbox是一种强大的布局模块,可以轻松实现居中对齐。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Flexbox居中</title> <style> .container { display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */ height: 100vh; /* 容器高度为视口高度 */ } .box { width: 200px; height: 200px; background-color: lightblue; } </style> </head> <body> <div class="container"> <div class="box"></div> </div> </body> </html> ``` ### 方法二:使用Grid布局 CSS Grid也是一种现代的布局方式,可以轻松实现居中对齐。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Grid居中</title> <style> .container { display: grid; place-items: center; /* 同时水平和垂直居中 */ height: 100vh; /* 容器高度为视口高度 */ } .box { width: 200px; height: 200px; background-color: lightgreen; } </style> </head> <body> <div class="container"> <div class="box"></div> </div> </body> </html> ``` ### 方法三:使用绝对定位和变换 这种方法适用于需要兼容旧版浏览器的情况。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>绝对定位居中</title> <style> .container { position: relative; height: 100vh; /* 容器高度为视口高度 */ } .box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 200px; height: 200px; background-color: lightcoral; } </style> </head> <body> <div class="container"> <div class="box"></div> </div> </body> </html> ``` ### 方法四:使用Margin自动 这种方法适用于已知元素宽度和高度的情况。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Margin自动居中</title> <style> .container { position: relative; height: 100vh; /* 容器高度为视口高度 */ } .box { width: 200px; height: 200px; margin: 0 auto; /* 水平居中 */ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: lightyellow; } </style> </head> <body> <div class="container"> <div class="box"></div> </div> </body> </html> ``` 以上方法各有优缺点,可以根据具体需求选择合适的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值