html布局,下面怎么使两个div在同一行显示?

html-css012

html布局,下面怎么使两个div在同一行显示?,第1张

两种常用方法:

<p class="p_left">左左左左左左左</p><p class="p_right">右右右右右右右</p>。

一、

.p_left{float:left}。

.p_right{float:left}。

二、(中间如果有空格 他会留空格的 上面的就不会,你要块状也可以换成display:inline-block)。

.p_left{display:inline;}。

.p_right{display:inline}。

在界面设计的时候,经常需要将两个div在同一行显示。

如以下要将“第一个div”和“第二个div”显示在同一行:

<div id="id1">  /*外层div*/

<div id="id2" style="width:100pxheight:20px">第一个div</div>

<div id="id3" style="width:100pxheight:20px">第二个div</div>

</div>

只需要对id2和id3增加css样式即可,如下所示:

<div id="id1">  /*外层div*/

<div id="id2" style="width:100pxheight:20pxfloat:left">第一个div</div>

<div id="id3" style="width:100pxheight:20pxfloat:left">第二个div</div>

</div>

这样就可以了。

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实例截图

田字格布局,要求大小相同的四个正方形。而html里div如果不加控制的话是独占一行的,现在要做的是把四个大小相同的方块,排列成“田”字。

第一步、新建html文档并搭建框架

新建一个TXT文档,重命名为“田子格布局.html”,然后用记事本打开,输入表头信息,已经html整体框架搭建。包括head与body。

第二步、DIV布局

分别复制4个不同的div作为4部分,并且分别命名为不同id;显示内容为块1、块2、块3、块4。

【提示】div在html里是单独占一列的(如果不控制),现在4个div布局完成。

【代码如下】

</head>

<body>

<div id="prat1">块1</div>

<div id="prat2">块2</div>

<div id="prat3">块3</div>

<div id="prat4">块4</div>

</body>

</html>

第三步、CSS控制4个DIV显示

输入style然后开始对4个div进行控制,分别对四个块进行大小和颜色的设定,处理之后在浏览器中打开显示如下图所示。

【提示】由于是田子格,所以四个div大小应该相同,为了可以区分颜色分别采用不同的颜色。

【代码如下】

<style>

#prat1{

width: 200px

height: 200px

background: blue/*边长200像素的蓝色方块*/

}

#prat2{

width: 200px

height: 200px

background: red/*边长200像素的蓝色方块*/

}

#prat3{

width: 200px

height: 200px

background: yellow/*边长200像素的蓝色方块*/

}

#prat4{

width: 200px

height: 200px

background: green/*边长200像素的蓝色方块*/

}

</style>

第四步、使用浮动

在CSS里控制输入float:left;四个div全部输入一样内容,这时候看到的是四个并排的div,而没有达到想要的效果,如下图所示。

【代码如下】

<style>

#prat1{

width: 200px

height: 200px

background: blue

float: left

}

#prat2{

width: 200px

height: 200px

background: red

float: left

}

#prat3{

width: 200px

height: 200px

background: yellow

float: left

}

#prat4{

width: 200px

height: 200px

background: green

float: left

}

</style>

第五步、清除浮动

在第三块上使用清除浮动clear:left;其余的代码保持不变,然后保存代码,刷新打开的页面,就会看到一个田字格了,如下图所示。

【代码如下】

#prat3{

width: 200px

height: 200px

background: yellow

float: left

clear: left

【注意】只清除第三块的就可以了。

【完整的代码】

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>田字格布局</title>

<style>

#prat1{

width: 200px

height: 200px

background: blue

float: left

}

#prat2{

width: 200px

height: 200px

background: red

float: left

}

#prat3{

width: 200px

height: 200px

background: yellow

float: left

clear: left

}

#prat4{

width: 200px

height: 200px

background: green

float: left

}

</style>

</head>

<body>

<div id="prat1">块1</div>

<div id="prat2">块2</div>

<div id="prat3">块3</div>

<div id="prat4">块4</div>

</body>

</html>