首页 > 其他分享 >CSS居中方法总结

CSS居中方法总结

时间:2024-10-11 19:21:47浏览次数:15  
标签:居中 总结 parent color 100px height red background CSS

一、行内元素

(1)水平居中

   1.通过text-align:center

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

 2.通过fit-content

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

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

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

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

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

(2)垂直居中

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

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

然后加入line-height: 200px;

.parent {
        background-color: red;
        height: 200px;
        line-height: 200px;
}

二、块级元素

(1)水平居中

1.通过margin:0 auto;

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

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

然后给子元素加入margin:0 auto;

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

(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;

}

 然后给子元素加入 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;
}

 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;

}

 然后给子元素加入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%);
}

 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;

}

 然后加入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;
}

 

 4.Padding

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

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

}

 

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

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

 

 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;
}

 

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;
}

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;
}

 

标签:居中,总结,parent,color,100px,height,red,background,CSS
From: https://blog.csdn.net/Emnational/article/details/142860446

相关文章

  • 2024红队必备工具列表总结_railgun工具
    一、信息收集1、AppInfoScanner一款适用于以HVV行动/红队/渗透测试团队为场景的移动端(Android、iOS、WEB、H5、静态网站)信息收集扫描工具,可以帮助渗透测试工程师、红队成员快速收集到移动端或者静态WEB站点中关键的资产信息并提供基本的信息输出,如:Title、Domain、CDN、......
  • Visual Studio的实用调试技巧总结
        对于很多学习编程的老铁们来说,是不是也像下面这张图一样写代码呢?    那当我们这样编写代码的时候遇到了问题?大家又是怎么排查问题的呢?是不是也像下面这张图一样,毫无目的的一遍遍尝试呢?    这篇文章我就以 VisualStudio2022编译器为例,带大家......
  • 2024.10.11总结
    本文于github博客同步更新最简单但挂分最惨的一集。唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了......
  • html面试题总结
    文章目录1.什么是DOCTYPE,有何作用2.H5有哪些新的元素和新特性3.cookie、sessionStorage和localStorage的区别4.script、scriptasync和scriptdefer的区别5.行内元素有哪些?块级元素有哪些?空(void)元素有哪些?6.页面导入样式时,使用link和@import有什么区别7.title和h1......