accp7.0html基础居中,html 元素垂直水平居中

一、 不定宽高元素水平垂直居中

1、transform: translate()

水平垂直居中

.wrapper{

background-color: #eee;

height:200px;

}

.center{

position: relative;

width: 300px;

padding: 10px 20px;

background-color: #2c3e50;

color: #fff;

left: 50%;

right: 50%;

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

}

缺点:IE9以下不兼容

2、flex 布局,利用justify-content和align-items 实现居中

水平垂直居中

.wrapper{

height:200px;

background-color: #eee;

display: flex;

justify-content: center;

align-items: center;

}

.center2 {

position: relative;

width: 300px;

padding: 10px 20px;

background-color: #2c3e50;

color: #fff;

}

3、使用table+table-cell实现垂直居中,display:inline-block;或margin: auto;实现水平居中

水平垂直居中

.wrapper{

background-color: #eee;

height: 200px;

width: 100%;

display: table;

}

.content {

display: table-cell;

text-align: center;

vertical-align: middle;

}

.center3 {

display: inline-block;

width: 300px;

padding: 10px 20px;

background-color: #2c3e50;

color: #fff;

}

4、伪元素:after, :before 使用inline-block+ vertical-align:middle 对齐伪元素

水平垂直居中

.wrapper {

background-color: #eee;

height: 200px;

width: 100%;

position: relative;

}

.wrapper:after {

content: '';

display: inline-block;

vertical-align: middle;

height: 100%;

}

.center4 {

background-color:#2c3e50;

padding: 10px 20px;

color:#fff;

display: inline-block;

}

5、writing-mode: 改变文字的显示方向

水平垂直居中

.wrapper {

background-color:#eee;

width: 100%;

height: 200px;

writing-mode: vertical-lr;

}

.content {

writing-mode: horizontal-tb;

display: inline-block;

width: 100%;

}

.center5 {

background-color:#2c3e50;

padding: 10px 20px;

color:#fff;

display: inline-block;

margin: auto;

text-align: left;

}

二、 固定宽高元素水平垂直居中

6、absolute+ 负margin

水平垂直居中

.wrapper {

background-color: #eee;

height: 200px;

width: 100%;

position: relative;

}

.center6{

background-color: #2c3e50;

color: #fff;

width: 300px;

height: 50px;

line-height: 50px;

position: absolute;

top: 50%;

left: 50%;

margin-left: -150px;

margin-top: -25px;

}

设置绝对定位,left:50%; top: 50%;使当前元素的左上角处于父元素的中心位置, 再利用负margin使其中心位于父元素的中心。

7、absolute+ 上下左右等值

水平垂直居中

.wrapper {

background-color: #eee;

height: 200px;

width: 100%;

position: relative;

}

.center7 {

background-color: #2c3e50;

color: #fff;

width: 300px;

height: 50px;

line-height: 50px;

position: absolute;

top: 0;

left: 0;

right: 0;

bottom: 0;

margin: auto;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
响应式布局是指网页设计和开发中使用的一种技术,它能够根据用户的设备和屏幕尺寸自动调整和适应页面的布局和样式。在HTML第十一章中,涉及到响应式布局的内容包括flexbox布局和媒体查询。 在引用中提到了flex-direction属性,它用于指定flex容器中子元素的排列方向,可以选择水平方向(row、row-reverse)或垂直方向(column、column-reverse)。这个属性在实现响应式布局时非常有用,通过设置不同的排列方向,可以使布局在不同的屏幕尺寸下自动调整。 另外,在引用中提到了一些与响应式布局相关的CSS属性和媒体查询。align-content属性用于控制flex容器中多行或多列的对齐方式。媒体查询则可以根据设备的特性(如设备宽度、方向等)来应用不同的样式规则,从而实现页面在不同设备上的适应性。 此外,在引用中提供了一个示例的CSS代码片段,包含了一些用于响应式布局的样式规则。该代码中使用了flex布局和媒体查询,通过设置不同的容器宽度、对齐方式和字体大小等属性,实现了在不同屏幕尺寸下的布局调整。 综上所述,HTML第十一章中的响应式布局内容包括使用flexbox布局和媒体查询来实现页面在不同设备和屏幕尺寸下的自适应布局。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ACCP8.0Y2Web前端框架与移动应用开发第1章响应式布局](https://blog.csdn.net/weixin_30588675/article/details/96160947)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值