怎么将html中纵向拉开,css 实现常用的6种垂直剧中方式

d73abaf00655b1977c8e333ab08f0d02.png

1、绝对定位,利用负边距

html 部分

复制代码

css 部分

.box1{

display: table-cell;

vertical-align: middle;

text-align: center;

height: 100px;

width: 100%;

background-color: pink;

position: relative;

}

.content1 {

background-color: burlywood;

width: 200px;

height: 50px;

position: absolute;

left: 50%;

top: 50%;

margin-top: -25px;

margin-left: -100px;

overflow: auto;

}

复制代码

2、绝对定位,margin:auto

html 部分

复制代码

css 部分

.box2{

display: table-cell;

vertical-align: middle;

text-align: center;

height: 100px;

width: 100%;

background-color: pink;

position: relative;

margin-top: 10px;

}

.content2 {

background-color: burlywood;

width: 200px;

height: 50px;

position: absolute;

left: 0;

top: 0;

right: 0;

bottom: 0;

margin: auto;

overflow: auto;

}

复制代码

3、flex布局

html 部分

复制代码

css 部分

.box3{

display: flex;

height: 100px;

width: 100%;

align-items: center;

justify-content: center;

background-color: pink;

margin-top: 10px;

}

.content3 {

background-color: burlywood;

width: 200px;

height: 50px;

overflow: auto;

line-height: 50px;

text-align: center;

}

复制代码

4、line-height = 元素高度

html 部分

垂直剧中 line-height

复制代码

css 部分

.box4{

height: 100px;

width: 100%;

background-color: pink;

margin-top: 10px;

line-height: 100px;

text-align: center;

}

复制代码

5、transform

html 部分

复制代码

css 部分

.box5{

height: 100px;

width: 100%;

background-color: pink;

margin-top: 10px;

position: relative;

}

.content5 {

position: absolute;

background-color: burlywood;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

-webkit-transform: translate(-50%, -50%);

}

复制代码

6、table布局

html 部分

垂直剧中 Table

垂直剧中 Table

复制代码

css 部分

.box6{

height: 100px;

width: 100%;

background-color: pink;

margin-top: 10px;

display: table;

}

.content6 {

text-align: center;

vertical-align: middle;

display: table-cell;

background-color: burlywood;

}

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值