当前位置: 首页 > news >正文

css实现水平居中

代码示例

<div class="box"><div class="box1"></div>
</div>

1.弹性布局:(推荐)

display:flex;

 这些要添加在父级的,是父级的属性

 //父级添加display:flex;

 //父级添加justify-content:center;

.box{width: 500px;height: 300px;background-color: aquamarine;display: flex;/*主轴-x轴:居中*/justify-content: center;
}
.box1{width: 200px;height: 100px;background-color: lightpink;
}

效果图: 

在这里插入图片描述

2.添加margin值auto

外边距可以让块级盒子水平居中,但是必须满足两个条件:

①盒子必须指定了宽度

②盒子左右的外边距都设置为auto

 .header{

      width:960px;

      margin:0 auto;

}

以上方法是让块级元素水平居中,行内元素或者行内块元素水平居中给其父元素添加text-ali gn:center;

 .box{width: 500px;height: 300px;background-color: aquamarine;}
.box1{width: 200px;height: 100px;background-color: lightpink;margin: 0 auto;
}

 3.text-align:center+行内块元素 

.box{width: 500px;height: 300px;background-color: aquamarine;text-align: center;   //父级添加text-align: center;
} 
.box1{width: 200px;height: 100px;background-color: lightpink;display: inline-block;  //把div变为行内块元素
}

http://www.lryc.cn/news/104001.html

相关文章:

  • c刷题(一)
  • webpack
  • 反复 Failed to connect to github.com port 443 after xxx ms
  • ARM裸机-11
  • centos7升级glibc
  • 【OnnxRuntime】在linux下编译并安装C++版本的onnx-runtime
  • C#基于OpenCv(OpenCvSharp) 的 fftshift, ifftshift 函数的实现
  • 【SpringBoot】笔记2
  • Spring事务传播机制详细讲解
  • kubernetes 集群搭建(kubeadm 方式)
  • 基于ARM+FPGA的驱控一体机器人控制器设计
  • docker 安装 字体文件
  • Vue.js与ASP.NET的结合,实现企业级应用的开发和部署
  • Uncaught SyntaxError: ‘‘ string literal contains an unescaped line break
  • Vue3+Vite+TypeScript常用项目模块详解
  • 数字电路(一)
  • Oracle也有回收站
  • 投稿注意!APA格式超全示例详解,原本28天能录用,可能要拖延2个月
  • 【Python】Web学习笔记_flask(1)——模拟登录
  • css单行文本省略号多行文本省略号
  • 信号槽中的函数重载
  • 计算机视觉(五)深度学习基础
  • ES6学习-Generator
  • Flowable-服务-微服务任务
  • opencv03-补充-vector的操作
  • 二叉树(C语言)
  • 介绍下Django中的表单(forms)模块中的类forms.CharField
  • 元宇宙与数字孪生的异同?
  • 智慧农业:科技赋能农村发展
  • x86架构ubuntu22下运行WILL模拟器dophin