需求 :假设高度默认100px ,请写出三栏布局,其中左栏、右栏各为300px,中间自适应
将左右的div宽度设为300px,分别左右浮动,中间盒子不设宽度。注意:先写右边盒子,再写中间盒子,否则先渲染中间盒子,中间盒子会占满该行剩下的宽度,右边盒子只能换行显示,就会出现下面的情况
正常的渲染效果如下所示:
第二种:绝对定位(position:absolute)
设置父盒子position:relative(相对定位),
三个子盒子position:absolute,
左盒子left:0,
右盒子right:0
中间盒子left:300pxright:300px
第三种:flex布局
父盒子 display:flex
左右盒子设置宽度:300px
中间盒子flex:1(flex-grow:1 flex-shrink:1 flex-basis:0%)不考虑元素尺寸自由伸缩
第四种:grid布局
父盒子display: grid
grid-template-columns:300px auto 300px(分割成3列,宽度分别为300px auto 300px)
grid-template-rows:100px(占一行,行高100px)
html页面中表单怎么用div分列布局主要分两个步骤操作:
第一种情况,float浮动相同
让两个div盒子的float值相同,比如都设置float:left或float:right,宽度设置合适即可。这里我们通过案例实现一行两列DIV布局。
1、完整实例DIV+CSS代码:
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
<title>两个DIV并排</title>
<style>
.div-a{ float:leftwidth:49%border:1px solid #F00}
.div-b{ float:leftwidth:49%border:1px solid #000}
</style>
</head>
<body>
<div class="div-a">第一个DIV盒子</div>
<div class="div-b">第二个DIV盒子</div>
</body>
</html>
2、实例截图
一行两列DIV并排效果截图
需要注意是宽度,要想一行两列DIV布局,避免第三个DIV也并排,这里就要设置计算好宽度(这里设置百分比宽度为49%),三个DIV盒子宽度之和大于父级宽度,两个DIV宽度之和小于父级宽度,即可实现只有2个DIV并排。
第二种情况,float浮动值不同
一个设置为float:left一个设置为float:right.。
1、完整HTML源代码:
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
<title>两个DIV并排</title>
<style>
.div-c{ float:leftwidth:49%border:1px solid #F00}
.div-d{ float:rightwidth:49%border:1px solid #000}
</style>
</head>
<body>
<div class="div-c">第三个DIV盒子</div>
<div class="div-d">第四个DIV盒子</div>
</body>
</html>
2、两列并排DIV实例截图