CSS3 box-flex 属性

[TOC]

流布局存在的问题

  1. 之前要实现横列的web布局,通常就是float或者display:inline-block; 但是都不能做到真正的流体布局。至少width要自己去算百分比。
  2. flexible box 就可以实现真正意义上的流体布局。只要给出相应属性,浏览器会帮我们做额外的计算。

提供的关于盒模型的几个属性:

box-orient           子元素排列 vertical or horizontal
box-flex             兄弟元素之间比例,仅作一个系数
box-align            box 排列
box-direction        box 方向
box-flex-group       以组为单位的流体系数
box-lines
box-ordinal-group    以组为单位的子元素排列方向
box-pack以下是关于flexible box的几个实例

几种布局方法

三列自适应布局,且有固定margin:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
.wrap { display: -webkit-box; -webkit-box-orient: horizontal; }
.child { min-height: 200px; border: 2px solid #666; -webkit-box-flex: 1; margin: 10px; font-size: 100px; font-weight: bold; font-family: Georgia; -webkit-box-align: center; }
</style>
<div class="wrap">
    <div class="child">1</div>
    <div class="child">2</div>
    <div class="child">3</div>
</div>
</body>
</html>
image

当一列定宽,其余两列分配不同比例亦可(三列布局,一列定宽,其余两列按1:2的比例自适应):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
.wrap { display: -webkit-box; -webkit-box-orient: horizontal; }
.child { min-height: 200px; border: 2px solid #666; margin: 10px; font-size: 40px; font-weight: bold; font-family: Georgia; -webkit-box-align: center; }
.w200 { width: 200px }
.flex1 { -webkit-box-flex: 1 }
.flex2 { -webkit-box-flex: 2 }
</style>
<div class="wrap">
    <div class="w200 child">200px</div>
    <div class="flex1 child">比例1</div>
    <div class="flex2 child">比例2</div>
</div>
</body>
</html>
image

下面是一个常见的web page 的基本布局:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
header, footer, section { border: 10px solid #333; font-family: Georgia; font-size: 40px; text-align: center; margin: 10px; }
#doc { width: 80%; min-width: 600px; height: 100%; display: -webkit-box; -webkit-box-orient: vertical; margin: 0 auto; }
header, footer { min-height: 100px; -webkit-box-flex: 1; }
#content { min-height: 400px; display: -webkit-box; -webkit-box-orient: horizontal; }
.w200 { width: 200px }
.flex1 { -webkit-box-flex: 1 }
.flex2 { -webkit-box-flex: 2 }
.flex3 { -webkit-box-flex: 3 }
</style>
<div id="doc">
    <header>Header</header>
    <div id="content">
        <section class="w200">定宽200</section>
        <section class="flex1">比例3</section>
        <section class="flex2">比例1</section>
    </div>
    <footer>Footer</footer>
</div>
</body>
</html>

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 6,835评论 0 11
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,681评论 1 92
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 5,798评论 0 2
  • 前言 温馨提示:本文较长,图片较多,本来是想写一篇 CSS 布局方式的,但是奈何 CSS 布局方式种类太多并且实现...
    sunshine小小倩阅读 8,340评论 0 59
  • 空中看葫芦岛城市还真不小
    葫芦岛有事你说话阅读 5,443评论 0 0