首页 > 试题广场 >

三栏布局的实现方式,尽可能多写,浮动布局时,三个div的生成

[问答题]

三栏布局的实现方式,尽可能多写,浮动布局时,三个div的生成顺序有没有影响

三栏布局一般指的是页面中一共有三栏,左右两栏宽度固定,中间自适应的布局。

大致写了五种方式实现,前三种无法达到重要内容优先显示的效果,而后面两个可以:

1、float
原理:浮动元素脱离文档流
HTML 结构
<div class="outer">
    <div class="sider left"></div>
    <div class="center"></div>
    <div class="sider right"></div>
</div>
CSS 样式
.sider {
    width: 300px;
    height: 400px;
    background-color: lightskyblue;
}
 
.left {
    float: left;
}
 
.right {
    float: right;
}
 
.center {
    /* 方案一:
    margin: 0 200px 0 300px;
    */
    /* 方案二: */
    float: left;
    width: calc(100% - 600px);
 
    height: 400px;
    background-color: pink;
}

2、position
思想基本和float相同,就是设置的值不同,在float中设置的是float-left和float-right,而这其中设置的是left和right
原理:绝对定位元素脱离文档流
HTML 结构同上
CSS 样式
.outer {
    position: relative;
}
 
.sider {
    position: absolute;
    top: 0;
    height: 500px;
    width: 300px;
    background-color: lightskyblue;
}
 
.left {
    left: 0;
}
 
.right {
    right: 0;
}
 
.center {
    /* 方案一:
    margin: 0 200px 0 300px;
    */
    /* 方案二:
    position: absolute;
    left: 300px;
    width: cala(100% - 600px);
    */
    /* 方案三: */
    position: absolute;
    left: 300px;
    right: 300px;
 
    height: 500px;
    background-color: pink;
}

3、flex 布局
原理:利用了容器项目 order 属性
CSS 样式
.outer {
    display: flex;
    height: 500px;
}
 
.sider {
    width: 200px;
    height: 500px;
    background-color: lightskyblue;
}
 
.center {
    order: 2; /* 默认值为0,order越小位置越靠前,越靠上,此时就不用考虑覆盖的问题了*/
    flex-grow: 1;
    /* flex-grow : 默认值为0,为0时有剩余空间也不放大,子元素该属性均为1时,剩余空间被所有为1的子元素均分,有一个子元素该属性为2时,该元素将分得或者努力分得其他为1的子元素所分得空间2倍大小的空间 */
    /* flex-shrink : 默认值为1,当子元素中存在某个元素该属性为0时,若空间不足,则为1的缩小,为0的不变,因此可以猜测,为2的弱小的应该是更多,应该是缩小了为1缩小的空间的两倍 */
 
    background-color: pink;
}
 
.left {
    order: 1;
}
 
.right {
    order: 3;
}


4、table 布局(很少用)
CSS 样式
.outer {
    display: table;
}
 
.sider {
    width: 200px;
    height: 500px;
    background-color: lightskyblue;
}
 
.center {
    display: table-cell;
    width: 100%;
    height: 500px;
    background-color: pink;
}

5、grid 布局(存在兼容性问题)
CSS 样式
.outer {
    display: grid;
    width: 100%;
    grid-template-rows: 500px;
    grid-template-columns: 200px auto 200px;  
}
 
.sider {
    background-color: lightskyblue;
}
 
.center {
    background-color: pink;
}


6、圣杯布局
圣杯布局可以实现中间内容优先显示
HTML 结构
<div class="wrapper">
 
    <div class="center"></div>
    <div class="left"></div>
    <div class="right"></div>
 
</div>
CSS 样式
.wrapper {
    height: 500px;
    padding: 0 200px 0 300px; /* wrapper的container两侧空出来left和right的宽度 */
}
 
.center, .left, .right {
    position: relative;
    float: left;
    height: 500px;
}
 
.center {
    width: 100%; /* 宽度=wrapper的container的宽度,两侧已空出left和right */
    background-color: tomato;
}
 
.left {
    width: 300px;
    margin-left: -100%; /* 左侧紧邻container左侧 */
    left: -300px; /* 移至左侧紧邻wrapper最左侧,右侧紧邻container左侧 */
    background-color: lightgreen;
}
 
.right {
    width: 200px;
    margin-left: -200px; /* 右侧紧邻container右侧 */
    right: -200px; /* 移至右侧紧邻wrapper最右侧 */
    background-color: lightskyblue;
}

7、双飞翼布局:
双飞翼布局其实和圣杯布局类似,都是为了实现三栏布局,且中间内容部分优先展示
HTML 结构
<div class="wrapper">
    
    <div class="center-wrapper">
        <div class="center">1111111</div>
    </div>
 
    <div class="left"></div>
    <div class="right"></div>
 
</div>
CSS 样式
.wrapper {
    height: 500px;
}
 
.center-wrapper,
.left,
.right {
    float: left;
    height: 500px;
}
 
.center-wrapper {
    width: 100%;
    background-color: tomato;
}
 
.left {
    width: 300px;
    margin-left: -100%;
    background-color: lightgreen;
}
 
.right {
    width: 200px;
    margin-left: -200px;
    background-color: lightskyblue;
}
 
.center {
    margin-left: 300px;
    margin-right: 200px;
    height: 500px;
}

具体内容可以看博客:【三栏布局的七种实现方式】
编辑于 2021-10-19 18:25:43 回复(0)