2025年3月14日 星期五 甲辰(龙)年 月十三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Html+Div+Css(前端)

CSS的20种对齐代码!左对齐、右对齐、中间对齐、底部对齐、两端对齐等,代码简单(最全)

时间:02-02来源:作者:点击数:18

前言

CSS 中的右很多对齐的方式,今天主要来分享 20 个常用的对齐方式。

来一起看看吧。

1. 水平居中

1.1 水平居中对齐块元素

  • .box {
  •   width1180px;
  •   height100px;
  •   border1px solid #0f0;
  •   margin10px auto;
  • }

比如,tb的这个顶部导航和中间内容区块,都是居中的:

图片

1.2 水平居中对齐图像

  • img {
  •   display: block;
  •   width50%;
  •   margin10px auto;
  • }

1.3 水平居中对齐文本

  • .box {
  •   width600px;
  •   height100px;
  •   text-align: center;
  •   border1px solid #333;
  • }

2. 垂直居中

2.1 垂直居中对齐块元素

  • .box {
  •   width400px;
  •   height100px;
  •   background#0f0;
  •   padding20px 0px;
  • }

比如,wyy右侧这块,上下都居中,可以用 padding 或者相同的 margin,都能实现。

2.2 垂直居中对齐文本

  • .box {
  •   width400px;
  •   height100px;
  •   background#0f0;
  •   line-height100px;
  • }

比如,wyy音乐导航这里,文字就是垂直居中。

图片

2.3 垂直居中对齐元素之flexbox(CSS3)

  • .box {
  •   width400px;
  •   height100px;
  •   background#0f0;
  •   display: flex;
  •   align-items: center;
  • }

3. 水平垂直居中

3.2 使用 padding

  • .box {
  •   padding20px;
  • }

3.2 使用 position 和 transform

  • .box {
  •   width400px;
  •   height100px;
  •   background#0f0;
  •   position: absolute;
  •   left50%;
  •   top50%;
  •   transformtranslate(-50%, -50%);
  • }

比如dy的登录弹窗。

图片

3.3 使用 flexbox

  • .box {
  •   width400px;
  •   height100px;
  •   background#0f0;
  •   display: flex;
  •   justify-content: center;
  •   align-items: center;
  • }

4. 居左/居右对齐

4.1 float 左对齐

  • .box {
  •   width400px;
  •   background#0f0;
  •   float: left;
  • }

注意:使用时候,记得给父级增加 clearfix,防止浮动导致父级高度塌陷。

  • /* 清除浮动样式 */
  • .clearfix::after {
  •   content'';
  •   display: table;
  •   clear: both;
  • }

4.2 float 右对齐

  • .box {
  •   width400px;
  •   background#0f0;
  •   float: right;
  • }

注意:使用时候,记得给父级增加 clearfix,防止浮动导致父级高度塌陷。

  • /* 清除浮动样式 */
  • .clearfix::after {
  •   content'';
  •   display: table;
  •   clear: both;
  • }

4.3 让文本居左

  • .box {
  •   text-align: left;
  • }

4.4 让文本居右

  • .box {
  •   text-align: right;
  • }

4.5 使用定位让元素居左

  • .box {
  •   width400px;
  •   background: pink;
  •   position: absolute;
  •   left0;
  • }

4.6 使用定位让元素居右

  • .box {
  •   width400px;
  •   background#00f;
  •   position: absolute;
  •   right10px;
  • }

比如,tb右侧客服这块。

图片

5. 两端对齐

5.1 两端对齐多个元素之 float

  • .box-left {
  •   width800px;
  •   background#00f;
  •   float: left;
  • }
  • .box-right {
  •   width300px;
  •   background#0f0;
  •   float: right;
  • }

注意:使用时候,记得给父级增加 clearfix,防止浮动导致父级高度塌陷。

  • /* 清除浮动样式 */
  • .clearfix::after {
  •   content'';
  •   display: table;
  •   clear: both;
  • }

比如,淘宝这块。

5.2 两端对齐多个元素之 flexbox(CSS3)

  • .box {
  •   width400px;
  •   background#0f0;
  •   display: flex;
  •   justify-content: space-between;
  • }

5.3 两端对齐文本

  • .box {
  •   width400px;
  •   background#0f0;
  •   text-align: justify;
  • }

6. 底部对齐

6.1 底部对齐元素

  • .box {
  •   width400px;
  •   height400px;
  •   background#0f0;
  •   display: flex;
  •   align-items: flex-end;
  •   justify-content: flex-start;
  • }

7. 顶部对齐

7.1 顶部对齐元素

  • .box {
  •   width400px;
  •   height400px;
  •   background#0f0;
  •   display: flex;
  •   align-items: flex-start; /* 不让子元素拉伸至整个父级高度 */
  •   justify-content: flex-start; /* 水平顶端对齐 */
  • }

OK,本文完。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐