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

怎样才能让一个div在页面上既能上下对齐又能左右对齐?

最编程 2024-01-20 08:30:11
...

这是我参与「4月日新计划更文活动」的第9天。

我们今天讲一下如何让一个div垂直水平居中。

我知道实现的方法一共有5种。

他们分别是下面的几种方法。

方式1position: absolute;margin: auto;toprightbottomleft属性设置为0
方式2position: absolute;设置50% + translaste属性
方式3:弹性布局水平垂直居中
方式4:网格布局
方式5:文本水平对齐和行高

position: absolute;margin: auto;top、right、bottom、left属性设置为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>垂直居中</title>
  </head>
  <style>
  .container{
    position: absolute;
    margin: auto;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 200px;
    height: 200px;
    background: #000;
  }
  </style>
  <body>
    <div class="container"></div>
  </body>
</html>

这是通过position定位的一种方式。

方式2:position: absolute;设置50% + translaste属性

实现的代码如下:

<!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>垂直居中</title>
  </head>
  <style>
  .container{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 200px;
    height: 200px;
    background: #000;
  }
  </style>
  <body>
    <div class="container"></div>
  </body>
</html>

这里利用了定位和translate的特殊特性实现居中。

方式3:弹性布局水平垂直居中

实现的代码如下:

<!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>垂直居中</title>
  </head>
  <style>
  .container{
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .box{
    width: 200px;
    height: 200px;
    background: #000;
  }
  </style>
  <body>
    <div class="container">
      <div class="box"></div>
    </div>
  </body>
</html>

方式4:网格布局

实现的代码如下:

<!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>垂直居中</title>
  </head>
  <style>
  .container{
    display: grid;
    justify-content: center;
    align-items: center;
  }
  .box{
    width: 200px;
    height: 200px;
    background: #000;
  }
  </style>
  <body>
    <div class="container">
      <div class="box"></div>
    </div>
  </body>
</html>

方式5:文本水平对齐和行高

实现的代码如下:

<!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>垂直居中</title>
  </head>
  <style>
  .container{
    width: 1000px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    border: 1px solid red;
  }
  </style>
  <body>
    <div class="container">3333333</div>
  </body>
</html>

这种方式适用于行内元素的水平垂直居中。

看到掘金上的好文章,如果对你有帮助,顺手点个赞,或者把文章收藏。不用担心找不到了,以后也能经常收到类似好回答,我会持续进行更新。