当前位置: 首页 > Web前端 > CSS

css实现居中,自适应

时间:2023-03-30 18:13:58 CSS

单栏布局水平居中水平居中页面布局是最常见的布局形式,多出现在标题和内容区域的组织形式上。下面分别介绍四种实现水平居中的方法(注:以下示例中实现的是子元素的对齐操作,子元素的父容器为父元素)使用inline-block和text-align来实现achieve.parent{text-align:center;}.child{display:inline-block;}优点:兼容性好;缺点:需要同时设置子元素和父元素,使用margin:0auto;实现.child{display:table;margin:0auto;}优点:只需要自己设置不足:IE6、7需要调整结构,使用绝对定位实现.parent{position:relative;}/*或者实用margin-left负值也可以实现盒子宽度的一半,但是这种方式你必须知道盒子的宽度,但是兼容性好*/.child{position:absolute;left:50%;transform:translate(-50%);}不足:兼容性差,IE9以上可以用实用的flex布局实现/*第一种方法*/.parent{display:flex;justify-content:center;}/*第二种方法*/.parent{显示:flex;}。child{margin:0auto;}缺点:兼容性差,如果进行大面积布局,可能会影响效率要吃辣,有的人不喜欢吃芹菜,有的人不喜欢吃羊肉等等。CSS中的一些元素也是如此,有的只对牛奶感兴趣,有的只喜欢坚果果冻,讨厌牛奶。至于vertical-align,他是个挑食的。他只喜欢吃果冻。他是吃果冻长大的。没有果冻,他会发脾气,不理你。我称之为“果冻依赖元素”,也称为“inline-blockdependentelement”,也就是说只有一个元素属于inline或inline-block(table-cell也可以理解为inline-block级别)级别,其上的垂直对齐属性将起作用。我对css-vertical-align的一些理解和理解在使用vertical-align的时候,由于对齐的基线是用行高的基线标注的,所以需要设置line-height或者display:table-cell;/*一种方法*/.parent{display:table-cell;vertical-align:middle;height:20px;}/*第二种方法*/.parent{display:inline-block;vertical-align:middle;line-height:20px;}实用绝对定位.parent{position:relative;}.child{positon:absolute;top:50%;transform:translate(0,-50%);}实用flex实现.parent{display:flex;align-items:center;}水平和垂直都使用vertical-align,text-align,inline-block.parent{display:table-cell;vertical-align:middle;text-align:center;}.child{display:inline-block;}使用绝对定位实现.parent{position:relative;}.child{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}使用Flex实现.parent{display:flex;justify-content:center;align-items:center;}多列布局左列定宽,右列自适应,自适应的一边是内容布局使用float+margin.left{float:left;width:100px;}.right{margin-left;margin-left:100px;}注意:IE6将有一个3pxbug利用float+margin(fix)实现

.left{width:100px;float:left;}.right-fix{width:100%;margin-left:-100px;float:right;}.right{margin-left:100px;}使用float+overflow实现.left{width:100px;float:left;}.right{overflow:hidden;}overflow:hidden,触发bfc模式,floating不能影响,隔离其他元素,IE6不支持,设置左边margin-left为左右边距,右边使用overflow:hidden形成bfc模式if我们需要将两列设置为相同的高度,可以使用下面的方法将“背景”设置为相同的高度,与content.left不等高{width:100px;float:left;}.right{溢出:隐藏;}。parent{overflow:hidden;}.left,.right{padding-bottom:9999px;margin-bottom:-9999px;}用table实现.parent{display:table;table-layout:fixed;width:100%;}.left{width:100px;}.right,.left{display:table-cell;}实用的flex实现。parent{display:flex;}.left{width:100px;}.right{flex:1;}使用右侧容器的flex:1,将剩余的宽度平分,达到同样的效果align-items的默认值是stretch,所以两者的高度是相等的。右栏固定宽度,左栏自适应实用float+margin.parent{background:red;height:100px;margin:0auto;}.left{background:green;margin-right:-100px;width:100%;float:left;}.right{float:right;width:100px;background:blue;}usingtableimplementation.parent{display:table;table-layout:fixed;width:100%;}.left{display:table-cell;}.right{width:100px;display:table-cell;}实用的flex实现.parent{display:flex;}.left{flex:1;}.right{width:100px;}二columnsoffixedwidth,一列自适应html的基本结构是父容器parent,自容器left,center,right。其中left和center固定宽度,right自适应使用float+margin.left,.center{float:left:width:200px;}.right{margin-left:400px;}使用float+overflow实现.left,.center{float:left:width:200px;}.right{overflow:hidden;}用table实现.parent{display:table;table-layout:fixed;width:100%;}.left,.center,.right{display:table-cell;}.left,.center{width:200px;}使用flex实现.parent{display:flex;}.left,.center{width:100px;}.right{flex:1}两边固定宽度,用float+margin实现.left{width:100px;float:left;}.center{float:left;width:100%;margin-right:-200px;}。right{width:100px;float:right;}使用table实现.parent{width:100%;display:table;table-layout:fixed}.left,.center,.right{display:table-cell;}。left{width:100px;}.right{width:100px;}使用flex实现.parent{display:flex;}.left{width:100px;}.center{flex:1;}.right{width:100px;}一列不定宽,一列自适应使用float+overflow.left{float:left;}.right{overflow:hidden;}usingtable.parent{display:table;table-layout:fixed;width:100%;}。left{width:0.1%;}.left,.right{display:table-cell;}用flex实现.parent{display:flex;}.right{flex:1;}多列相等布局Multi-columnequal布局经常出现在内容中,大多是功能性的,同级内容的并排展示等。html结构如下1111实用的float实现.parent{margin-left:-20px}/*假设列间距为20px*/.column{float:left;width:25%;padding-left:20px;box-sizing:border-box;}使用table实现.parent-fix{margin-left:-20px;}.parent{display:table;table-layout:固定的;width:100%;}.column{display:table-cell;padding-left:20px;}用flex实现.parent{display:flex;}.column{flex:1;}.column+.column{margin-left:20px;}九宫格布局使用table实现</div>.parent{display:table;table-layout:fixed;width:100%;}.row{display:table-row;}.item{display:table-cell;width:33.3%;height:200px;}实现flex实现.parent{display:flex;flex-direction:column;}.row{height:100px;display:flex;}.item{width:100px;background:red;}全局布局利用绝对定位现实topleftrightbottomhtml,body,parent{height:100%;overflow:hidden;}.top{position:absolute:top:0;左:0;右:0;高度:100px;}.left{位置:绝对;顶部:100px;左:0;底部:50px;宽度:200px;}.right{位置:绝对;溢出:自动;左:200px;right:0;top:100px;bottom:50px;}.bottom{position:absolute;left:0;right:0;bottom:0;height:50px;}使用flex实现topleftright底部.parent{display:flex;flex-direction:column;}.top{height:100px;}.bottom{height:50px;}。middle{flex:1;display:flex;}.left{width:200px;}.right{flex:1;overflow:auto;}响应式布局元标签的实用设置布局宽度等于设备宽度,布局视口等于测量视口媒体查询HTML4和CSS2目前支持为不同的媒体类型设置专有样式表,例如,页面在屏幕上显示时使用无衬线字体,而在打印时使用衬线字体。屏幕和打印是两种已定义的媒体类型。媒体查询使样式表更有针对性,扩展了媒体类型的功能;媒体查询由媒体类型和一个或多个检测媒体特征的条件表达式组成。媒体查询中可用于检测的媒体特性包括宽度、高度和颜色(等)。使用媒体查询,你可以在不改变页面内容的情况下,为一些特定的输出设备自定义显示效果语法@mediascreenand(max-width:960px){....}