第六届字节跳动青训营第一课CSS居中方法总结 | 豆包MarsCode AI 刷题

159 阅读3分钟

一、行内元素

(1)水平居中

   1.通过text-align:center

<div class="parent" >
  <span class="child">我是行内元素</span>
</div>
.parent {
    background-color: red;
    text-align: center;
 }

居中text.png

 2.通过fit-content

给父元素的宽度加上width: fit-content;让父元素的宽度适应子元素的宽度

<div class="parent" >
   <span class="child">我是行内元素</span>
</div>

.parent {
        background-color: red;
        width: fit-content;
 }

微信截图_20241011182419.png

然后给父元素加上margin: auto;实现居中

微信截图_20241011182813.png

(2)垂直居中

1.line-height(只针对单行文本)

<div class="parent" >
    <span class="child">我是行内元素</span>
</div>
.parent {
        background-color: red;
        height: 200px;
        
}

微信截图_20241011183751.png

然后加入line-height: 200px;
.parent {
        background-color: red;
        height: 200px;
        line-height: 200px;
}

微信截图_20241011183922.png

二、块级元素

(1)水平居中

1.通过margin:0 auto;

<div class="parent">
    <div class="child">我是块级元素</div>
</div> 

.parent {
        background-color: red;
}
.child {
        width: 100px;
        background-color: blue;
}

微信截图_20241011184758.png

然后给子元素加入margin:0 auto;
.parent {
        background-color: red;
}
.child {
        width: 100px;
        background-color: blue;
        margin: 0 auto;
}

微信截图_20241011184940.png

(2)水平垂直居中

1.定位

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  background: blue;

}

微信截图_20241011185934.png

然后给子元素加入 left: 50%; top: 50%;margin-top: -50px; margin-left: -50px;

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  background: blue;
  left: 50%;
  top: 50%;
  margin-top: -50px;
  margin-left: -50px;
}

微信截图_20241011190202.png

2.定位+transform

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  background: blue;

}

微信截图_20241011185934.png

然后给子元素加入left: 50%;top: 50%;transform: translate(-50%, -50%);

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  position: absolute;
  background: blue;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

微信截图_20241011190618.png

3.定位 + margin

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  background: blue;

}

微信截图_20241011185934.png

然后加入left: 0;top: 0;right: 0;bottom: 0;margin: auto;

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background: blue;
}

微信截图_20241011190852.png

4.Padding

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  background-color: red;
}
.child {
  width: 200px;
  background: blue;

}

微信截图_20241011191220.png

然后给父元素加入padding: 20px;

.parent {
  padding: 20px;
  background-color: red;
}
.child {
  height: 200px;
  background: blue;
}

微信截图_20241011191359.png

5.flex

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  background-color: blue;
}

微信截图_20241011191557.png

6. 伪元素

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  height: 200px;
  text-align: center;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  display: inline-block;
  vertical-align: middle;
  background-color: blue;
}
.parent::before {
  content: "";
  width: 20px;
  height: 200px;
  display: inline-block;
  vertical-align: middle;
}

微信截图_20241011191854.png

7.calc(宽高相等)

<div class="parent">
   <div class="child">我是块级元素</div>
</div>

.parent {
  width: 600px;
  height: 600px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  padding: calc((100% - 100px) / 2);
  background-clip: content-box;
  background-color: blue;
}

微信截图_20241011192047.png