html盒子如何左对齐,css怎么让盒子居中对齐?

css怎么让盒子居中对齐?下面本篇文章就来给大家使用CSS让盒子居中对齐的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5deb13f76ab7d662.jpg

一、盒子垂直居中的方法

1、先让盒子的上下边缘和父盒子的水平中心线重叠,,然后再让子盒子往回移动自身一半的距离

/* 结构 */

/* 通过 transform 属性来移动*/

.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 0 auto;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

margin-top: 50%; /* 向下移动父盒子的一半 */

transform: translateY(-50%); /* 向上移动自身盒子的一半 */

}

/* 通过 定位来移动*/

.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 0 auto;

position: relative;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

position: absolute;

top: 50%; /* 先向下移动父盒子的一半 */

margin-top: -100px; /* 再向上移动自身盒子的一半 */

}

2、使用表格的 vertical-align :middle 属性来实现盒子垂直居中.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

display: table-cell; /* 显示形式为表格 */

vertical-align: middle; /* 里面内容为居中对齐 */

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

}

3、知道父盒子的高度,可以使用 margin 计算盒子的上下边距,来使盒子居中.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

margin-top: 149px; /* 根据父盒子的高度指定 margin-top 即可 */

}

二、盒子水平居中的方法

1、使用 margin: 0 auto;.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

margin: 0 auto; /* 让盒子左右自动适应,想当于 left:auto; right:auto */

}

2、通过计算 margin 左右边距来实现居中.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

margin-left: 149px; /*父盒子的定宽的,指定 margin-left 即可*/

}

3、先让盒子左右边缘和父盒子垂直的中心线垂直,然后把子盒子往回移动自身宽度的一半/* 通过 transform 实现*/

.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

margin-left: 50%; /*先移动父盒子的一半 */

transform: translateX(-50%); /*再移动自身盒子一半 */

}

/*通过 定位实现*/

.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

position: relative;

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

position: absolute;

left: 50%; /* 向右移动父盒子一半*/

margin-left: -100px; /* 向左移动自身盒子一半*/

/* transform: translateX(-50%); */ /*向左移动自身盒子一半*/

}

4、把盒子转成 行内块,然后用 text-align 属性使盒子水平居中.father {

width: 500px;

height: 500px;

background-color: skyblue;

border: 1px solid #000;

margin: 50px auto;

text-align: center; /*让父盒子设置水平居中*/

}

.son {

width: 200px;

height: 200px;

background-color: pink;

border: 1px solid #000;

display: inline-block; /* 让子盒子显示为行内块模式*/

}

更多CSS相关知识,可访问 CSS教程 !!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值