html怎么垂直对其,html – 垂直对齐方法

以下是3种非常好的技术,用于将容器中的子项垂直居中 – 即使您不知道子元素的高度.前2个来自

this CSS-tricks article

标记(适用于所有方法):

Some text

解决方案#1:CSS表方法(FIDDLE)

CSS:

/* This parent can be any width and height */

.block {

display: table;

width: 100%;

background: orange;

height: 300px;

}

.centered {

display: table-cell;

text-align: center;

vertical-align: middle;

background: pink;

}

解决方案#2:伪(‘Ghost’)元素方法(FIDDLE)

CSS:

/* This parent can be any width and height */

.block {

text-align: center;

background: orange;

height: 300px;

}

/* The ghost, nudged to maintain perfect centering */

.block:before {

content: '';

display: inline-block;

height: 100%;

vertical-align: middle;

margin-right: -0.25em; /* Adjusts for spacing */

}

/* The element to be centered, can

also be of any width and height */

.centered {

display: inline-block;

vertical-align: middle;

width: 300px;

background: pink;

}

解决方案#3:Flexbox(FIDDLE)

(相关)CSS:

.block {

background: orange;

height: 300px;

display: flex;

align-items: center;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值