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

CSS居中方法总结

时间:2024-10-11 19:21:47浏览次数:3  
标签:居中 总结 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

相关文章

  • 10.9总结
    今天,没啥课,就上了数据结构和统一建模,对于统一建模,我上了三节课,总感觉模模糊糊的,可能是还没有做具体任务作业的原因吧,对于数据结构,我们继续向后学习了算法栈和队列,了解了共享栈,入栈,出栈的顺序栈的实现还有链式栈的实现入栈:Statuspush(Sqstack&s,inte){if(s.top-s.base==s.......
  • 2024红队必备工具列表总结_railgun工具
    一、信息收集1、AppInfoScanner一款适用于以HVV行动/红队/渗透测试团队为场景的移动端(Android、iOS、WEB、H5、静态网站)信息收集扫描工具,可以帮助渗透测试工程师、红队成员快速收集到移动端或者静态WEB站点中关键的资产信息并提供基本的信息输出,如:Title、Domain、CDN、......
  • [CSS] 伪元素和伪类,::before 和 :before 区别
     标签:CSS特点伪类伪元素用途选择元素的状态或基于结构选择元素创建虚拟的内容,操作元素的某些部分语法使用单个冒号(......
  • CSS 中的常用尺寸单位
    像素(px):最常用的单位,相对于显示器的分辨率。1px等于屏幕上的一个点。例如:width:100px;表示宽度为100像素。百分比(%):相对于父元素的尺寸。例如,如果一个元素的宽度设置为50%,那么它将占据其父元素宽度的一半。例如:width:50%;表示宽度为父元素宽度的50%。视口宽度(vw)......
  • Visual Studio的实用调试技巧总结
        对于很多学习编程的老铁们来说,是不是也像下面这张图一样写代码呢?    那当我们这样编写代码的时候遇到了问题?大家又是怎么排查问题的呢?是不是也像下面这张图一样,毫无目的的一遍遍尝试呢?    这篇文章我就以 VisualStudio2022编译器为例,带大家......
  • CSS 隐藏元素的方法
    display:none;通过设置元素CSS的displays属性为none来使元素消失,该方法隐藏的元素不占据页面空间oapcity:0;oapcity属性的作用是控制元素的透明度,当值为0的时候该元素完全不可见,但仍然存在页面上,占据页面空间visibility:hidden;visibility属性旨在控制元素的可见性,设置为hid......
  • 2024.10.11总结
    本文于github博客同步更新最简单但挂分最惨的一集。唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了......
  • Flutter布局(5):对齐与居中布局(Align、Center)
    在Flutter中,布局是构建用户界面的重要组成部分。Align和Center是两个常用的布局组件,它们都用于在父组件中对子组件进行对齐和居中。本篇博客将详细介绍Align和Center的用法、属性和适用场景,帮助你更好地理解和运用它们。一、Align:精准对齐,掌握位置Align组件用于将子组......
  • html面试题总结
    文章目录1.什么是DOCTYPE,有何作用2.H5有哪些新的元素和新特性3.cookie、sessionStorage和localStorage的区别4.script、scriptasync和scriptdefer的区别5.行内元素有哪些?块级元素有哪些?空(void)元素有哪些?6.页面导入样式时,使用link和@import有什么区别7.title和h1......
  • [总结]AD9154设计注意事项
    **AD9154为4通道**,分辨率**16**位,最高采样率**2.4G**Hz的**DA芯片**,支持JESD204B,可选1×,2×,4×,8×插值滤波器,内部有两个可独立控制的NCO,最高通信速率可达8×10.96Gb/s,可满足高速复杂雷达信号的产生。 功能框图如图1所示:图1AD9154功能框图从功能框图可以看出,AD9154......