css水平居中,竖直居中技巧(一)

css水平居中,竖直居中技巧(一)
===
### 1.效果



### 2.代码
#### 2.1.index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
 
    <link rel="stylesheet/less" type="text/css" href="test.less"/>
    <script type="text/javascript" charset="utf-8"
            src="http://lib.jxt189.com/otherlib/less/2.6.1/dist/less.min.js"></script>
</head>
<body>
<h2>原始的效果</h2>
<div class="container">
    <div class="inner-container">
        <div>aaa</div>
        <div>bbbbbb</div>
        <div>ccccccccc</div>
    </div>
</div>
<hr>
<h2>居中后的效果</h2>
<div class="container container-01">
    <div class="inner-container container-02">
        <div>aaa</div>
        <div>bbbbbb</div>
        <div>ccccccccc</div>
    </div>
</div>
</body>
</html>
#### 2.2.test.less
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
.container {
  width: 500px;
  height: 300px;
  background: red;
}
 
.inner-container {
  background: yellow;
}
 
//核心代码
.container-01 {
  position: relative;
 
  .container-02 {
    @width: 150px;
    @height: 100px;
 
    width: @width;
    height: @height;
 
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -1*(@height / 2);
    margin-left: -1*(@width / 2);
  }
}
###3.说明
a.使用这种方式实现水平居中和垂直居中,需要对`.container-02`的容器设置宽度和高度
posted @   泪已凝  阅读(518)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示