最近要准备移动端项目,大半年没好好写过CSS了,今天恶补了一下CSS的一些布局,下面做一些分享。

水平居中布局

1.margin + 定宽

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.child {
width: 100px;
margin: 0 auto;
}
</style>

这种常见的写法就不再赘述了。

2. table + margin

<div class="parent">
<span class="child">Demo</span>
</div> <style>
.child {
display: table;
margin: 0 auto;
}
</style>

display: table 在这里的作用有两个:一、将span元素转为块元素性质,二、设置了span宽度为内容宽。

* 无需设置父元素样式 (支持 IE 8 及其以上版本),IE 8以下版本需要调整页面结构至 table。

3.inline-block + text-align

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.child {
display: inline-block;
}
.parent {
text-align: center;
}
</style>
display: inline-block 将div块元素转为行内块元素性质(具备了一些行内元素的特性),固给父元素设置text-align: center 能起到居中效果。

* 兼容性佳(甚至可以兼容 IE 6 和 IE 7)

4. absolute + margin-left

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
width: 100px;
margin-left: -50px; /* width/2 */
}
</style>

* 宽度固定

* 相比于使用transform ,有兼容性更好

5. absolute + transform

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
</style>

transform 为 CSS3 属性,有兼容性问题

注:绝对定位脱离文档流,不会对后续元素的布局造成影响

6. flex + justify-content

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
display: flex;
justify-content: center;
}
</style>

* 只需设置父节点属性,无需设置子元素

flex布局有兼容性问题

垂直居中

1.table-cell + vertical-align

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
display: table-cell;
vertical-align: middle;
}
</style>

* 兼容性好(IE 8以下版本需要调整页面结构至 table)

2.absolute + transform

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>

transform 为 CSS3 属性,有兼容性问题

* 同水平居中,这也可以用margin-top实现,原理水平居中

3.flex + align-items

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
display: flex;
align-items: center;
}
</style>

* 有兼容问题

水平垂直居中

1. absolute + transform

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>

2. inline-block + text-align + table-cell + vertical-align

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child {
display: inline-block;
}
</style>

* 兼容性好

3. flex + justify-content + align-items

<div class="parent">
<div class="child">Demo</div>
</div> <style>
.parent {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /*垂直居中*/
}
</style>

* 只需设置父节点属性,无需设置子元素

* 有兼容性问题

一列定宽,一列自适应

1.float + margin

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div> <style>
.left {
float: left;
width: 100px;
}
.right {
margin-left: 100px
/*间距可再加入 margin-left */
}
</style>

* IE 6 中会有3像素的 BUG,解决方法可以在 .left 加入 margin-left:-3px 当然也有解决这个小bug的方案如下:

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right-fix">
<div class="right">
<p>right</p>
</div>
</div>
</div> <style>
.left {
float: left;
width: 100px;
}
.right-fix {
float: right;
width: 100%;
margin-left: -100px;
}
.right {
margin-left: 100px
/*间距可再加入 margin-left */
}
</style>

* 此方法不会存在 IE 6 中3像素的 BUG,但 .left 不可选择, 需要设置 .left {position: relative} 来提高层级。 注意此方法增加了不必要的 HTML 文本结构。

2.float + overflow

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div> <style>
.left {
float: left;
width: 100px;
}
.right {
overflow: hidden;
}
</style>

* 设置 overflow: hidden 会触发 BFC 模式(Block Formatting Context)块级格式上下文。BFC是什么?用通俗的来讲就是,随便你在BFC 里面干啥,外面都不会受到影响 。此方法样式简单但不支持 IE 6。

3 .table

  <div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div> <style>
.parent {
display: table;
width: 100%;
table-layout: fixed;
}
.left {
display: table-cell;
width: 100px;
}
.right {
display: table-cell;
/*宽度为剩余宽度*/
}
</style>

table 的显示特性为每列的单元格宽度和一定等与表格宽度。 table-layout: fixed 可加速渲染,也是设定布局优先。table-cell 中不可以设置 margin 但是可以通过 padding 来设置间距。

4. flex

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
</div>
</div> <style>
.parent {
display: flex;
}
.left {
width: 100px;
margin-left: 20px;
}
.right {
flex: 1;
}
</style>

* 低版本浏览器兼容问题

* 性能问题,只适合小范围布局

等分布局

1. float

<div class="parent">
<div class="column">
<p>1</p>
</div>
<div class="column">
<p>2</p>
</div>
<div class="column">
<p>3</p>
</div>
<div class="column">
<p>4</p>
</div>
</div>
<style>
.parent {
margin-left: -20px;
}
.column {
float: left;
width: 25%;
padding-left: 20px;
box-sizing: border-box;
}
</style>

* 此方法可以完美兼容 IE8 以上版本。

2. flex

<div class="parent">
<div class="column">
<p>1</p>
</div>
<div class="column">
<p>2</p>
</div>
<div class="column">
<p>3</p>
</div>
<div class="column">
<p>4</p>
</div>
</div> <style>
.parent {
display: flex;
}
.column {
flex: 1;
}
.column+.column { /* 相邻兄弟选择器 */
margin-left: 20px;
}
</style>

* 强大简单,有兼容问题。

3. table

<div class='parent-fix'>
<div class="parent">
<div class="column">
<p>1</p>
</div>
<div class="column">
<p>2</p>
</div>
<div class="column">
<p>3</p>
</div>
<div class="column">
<p>4</p>
</div>
</div>
</div> <style>
.parent-fix {
margin-left: -20px;
}
.parent {
display: table;
width: 100%;
/*可以布局优先,也可以单元格宽度平分在没有设置的情况下*/
table-layout: fixed;
}
.column {
display: table-cell;
padding-left: 20px;
}
</style>

等高布局

1.table

table 的特性为每列等宽,每行等高可以用于解决此需求

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div> <style>
.parent {
display: table;
width: 100%;
table-layout: fixed;
}
.left {
display: table-cell;
width: 100px;
}
.right {
display: table-cell
/*宽度为剩余宽度*/
}
</style>

2.flex

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div> <style>
.parent {
display: flex;
}
.left {
width: 100px;
margin-left: 20px;
}
.right {
flex: 1;
}
</style>

* 注意这里实际上使用了 align-items: stretch,flex 默认的 align-items 的值为 stretch。

3. float

<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div> <style>
.parent {
overflow: hidden;
}
.left,
.right {
padding-bottom: 9999px;
margin-bottom: -9999px;
}
.left {
float: left;
width: 100px;
margin-right: 20px;
}
.right {
overflow: hidden;
}
</style>

* 此方法为伪等高(只有背景显示高度相等),左右真实的高度其实不相等。 兼容性较好

结语: 一样的布局可能有很多种方法实现,以上只作为参考!

CSS常见布局解决方案的更多相关文章

  1. css CSS常见布局解决方案

    CSS常见布局解决方案说起css布局,那么一定得聊聊盒模型,清除浮动,position,display什么的,但本篇本不是讲这些基础知识的,而是给出各种布局的解决方案.水平居中布局首先我们来看看水平居 ...

  2. 前端进阶系列(二):css常见布局解决方案

    水平居中布局 margin+定宽 <div class="parent"> <div class="child">Demo</di ...

  3. css常见布局方式

    CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...

  4. CSS常见布局问题整理

    实现div的水平居中和垂直居中 多元素水平居中 实现栅格化布局 1. 实现div的水平居中和垂直居中 实现效果: 这大概是最经典的一个题目了,所以放在第一个. 方法有好多, 一一列来 主要思路其实就是 ...

  5. 前端开发工程师 - 04.页面架构 - CSS Reset & 布局解决方案 & 响应式 & 页面优化 &规范与模块化

    04.页面架构 第1章--CSS Reset 第2章--布局解决方案 居中布局 课堂交流区 水平列表的底部对齐 如图所示,一个水平排列的列表,每项高度都未知,但要求底部对齐,有哪些方法可以解决呢? & ...

  6. <转载>div+css布局教程之div+css常见布局结构定义

    在使用div+css布局时,首先应该根据网页内容进行结构设计,仔细分析和规划你的页面结构,你可能得到类似这样的几块: 页面层容器.页面头部.标志和站点名称.站点导航(主菜单).主页面内容.子菜单.搜索 ...

  7. CSS常见布局

    一.单列布局 1. 水平居中 1.1 使用inline-block和text-align .parent{text-align:center;} .child{display:inline-block ...

  8. css常见布局问题

    1.如何实现一个盒子在页面中上下左右居中 方法一:(盒子宽高固定时) .box{ width:400px; height:200px; background:#000; position:absolu ...

  9. 学习微信小程序之css16常见布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. win10 UWP button

    button有很多和wpf一样,可以看<深入浅出WPF> 我们可以在button的click写上 <Button Content="确定" Click=" ...

  2. 整理一批 国内外优秀设计团队 & 设计相关网站

    设计做不好,因为看得少!这里精心整理了一批国内外优秀设计团队的官网,以及同设计相关的网站.每个网站,我都浏览了一下,确保里面真的是有干货,并且保持一定的频率正常更新. [腾讯] 腾讯社交用户体验设计 ...

  3. Kotlin——最详细的控制语句使用

    在前面 的章节中讲解了Kotlin语言中的数据类型.变量与常量的定义.不了解请参见前面的内容: Kotlin从无到有系列之数据类型介绍. Kotlin从无到有系列之变量.常量.注释的使用. 下面详细为 ...

  4. 我的第一个python web开发框架(11)——工具函数包说明(二)

    db_helper.py是数据库操作包,主要有两个函数,分别是read()数据库读操作函数和write()数据库写操作函数.这个包的代码是从小戴同学分享的博文改造过来的. #!/usr/bin/env ...

  5. ASP.NET没有魔法——ASP.NET MVC 直连路由(特性路由)

    之前对Controller创建的分析中,知道了Controller的创建是有两个步骤组成,分别是Controller的类型查找以及根据类型创建Controller实例. 在查询Controller的类 ...

  6. asp.net 第三方UI控件 Telerik KendoUI 之 TreeVIew 的用法记录

    一.前台显示 备注:一次性取出所有节点 function loadTreeData() { $.ajax({ type: 'POST', url: '@(Html.UrlHref("Scri ...

  7. 【译】10分钟学会Pandas

    十分钟学会Pandas 这是关于Pandas的简短介绍主要面向新用户.你可以参考Cookbook了解更复杂的使用方法 习惯上,我们这样导入: In [1]: import pandas as pd I ...

  8. 最新数据库排行,Oracle略显疲惫

    9月份TOPDB Top Database Index排行榜出炉,TOPDB Top Database Index是根据数据库在谷歌上的搜索频率分析得出的,数据库被搜索的频率越大,表示数 9月份的TO ...

  9. C#与SQl数据的对应关系(tinyint、smallint、int、bigint)

    SQL                                                           C# bigint(sql大小:8byte)                 ...

  10. 使用sklearn进行数据挖掘-房价预测(6)—模型调优

    通过上一节的探索,我们会得到几个相对比较满意的模型,本节我们就对模型进行调优 网格搜索 列举出参数组合,直到找到比较满意的参数组合,这是一种调优方法,当然如果手动选择并一一进行实验这是一个十分繁琐的工 ...