CSS常见布局解决方案

最近要准备移动端项目,大半年没好好写过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>

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

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

时间: 2024-07-31 02:58:54

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

CSS常见布局问题整理

实现div的水平居中和垂直居中 多元素水平居中 实现栅格化布局 1. 实现div的水平居中和垂直居中 实现效果: 这大概是最经典的一个题目了,所以放在第一个. 方法有好多, 一一列来 主要思路其实就是 使用position,相对父元素做绝对定位(设置百分比[由父元素宽高调节子元素大小]/设置margin和相对位置(确定宽高)) 使用flex属性 使用tranfrom做相对位移的调节 1) 只适用: 宽高已定 设置position: absolute(父元素记得设置: relative), 然后t

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

04.页面架构 第1章--CSS Reset 第2章--布局解决方案 居中布局 多列布局 全屏布局 第3章--响应式 浏览网页使用的设备屏幕大小迥异,如何保证页面在不同设备上都正常显示呢? --  解决方法:响应式 优点:无需为不同设备编写对应的页面 --> 降低开发成本.维护成本 缺点:页面中某些部分在某些设备上会被隐藏(资源加载了,但是只是用CSS隐藏了) 为什么在手机端的页面会很小呢? 所有的移动端的页面都引入了viewport视窗 刚开始所有的元素都是加载到一个比较大的viewport视

&lt;转载&gt;div+css布局教程之div+css常见布局结构定义

在使用div+css布局时,首先应该根据网页内容进行结构设计,仔细分析和规划你的页面结构,你可能得到类似这样的几块: 页面层容器.页面头部.标志和站点名称.站点导航(主菜单).主页面内容.子菜单.搜索框.页脚(版权和有关法律声明). 通常采用DIV元素来将这些结构定义出来,类似这样: <div id="Container"></div> 页面层容器 <div id="header"></div> 页面头部 <di

css常见布局方式

布局是CSS中一个重要部分,下面总结了CSS布局中的常用技巧,包括常用的水平居中.垂直居中方法,以及单列布局.多列布局的多种实现方式(包括传统的盒模型布局和比较新的flex布局实现). 一.居中方式 水平居中 水平居中对于子元素为行内元素还是块状元素,宽度一定还是宽度未定,采取的布局方案是不同. 行内元素:对父元素设置text-align:center;定宽块状元素: 设置左右margin值为auto;不定宽块状元素: 设置子元素为display:inline,然后在父元素上设置text-ali

HTML CSS常见布局

首先将页面划分为大的结构性区域,比如容器.页眉.内容区和页脚: 然后是内容区域本市,建立网格结构,分析页面结构 最后再各内容区中设计结构,确定页面布局. 1. 水平居中 使用display:inline 和 text-align /*.parent { text-align: center; } .child { display: inline-block; }*/ 使用margin:0 auto 设定 .child { width: 300px; margin: 0 auto; } 使用tab

css常见布局

一列布局 1.一列布局 不是自适应 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>一列布局</title> <style> *{margin:0;padding:0;} #header{ height:50px; background:blue; } #main{ width:960px; height:800px; /* 在实际

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

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css常见布局</title> <style> *{ padding: 0;margin: 0; } .header,.nav,.main,.footer{ background-color: silver; border: 1px solid

Div+CSS网页布局中CSS无效的十个常见原因

学习Div+CSS网页布局的知识,可是兄弟连validation有时难以操作,但用它你可以查看由版面设计引起的差错,验证程序抛出大量差错和警告,说明你的XHTML尚未完善,可能无法在不同浏览器上保持一致功能,下面十个细微的失效问题难住了大批程序员,本文就告诉你如何解决,在本文开始前介绍一些使用兄弟连PHP培训的基础div+css验证程序时需要注意的问题. 1.不要担心验证程序的警告:如果验证程序说发现12处错误以及83处警告,不要理它,继续进行下一步. 2.一次更正一个错误:按顺序进展工作,从上

纯CSS实现移动端常见布局——高度和宽度挂钩的秘密

纯CSS实现移动端常见布局--高度和宽度挂钩的秘密 不踩坑不回头.之前我在一个项目中大量使用css3的calc计算属性.写代码的时候真心不要太爽啊-可是在项目上线之后,才让我崩溃了,原因非常easy,在低于安卓4.4的版本号的手机上,自带的浏览器是不支持这个属性的. 好吧,这还不时最坑爹的,在国产的猎豹浏览器以及其它一些浏览器里面,有可能也不支持.总而言之,这个坑踩大了.只是没关系,大部分的常见布局问题,我都能解决掉.可是,以下这个-.我真心有点费解.只是,没关系,通过我的研究,终于还是非常快用