很多站长朋友们都不太清楚html怎么将DIV排版,今天小编就来给大家整理html怎么将DIV排版,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 html 如何让几个div水平居中一行 2、 html怎么将两个div并排显示啊? 3、 html页面中表单怎么用div分列布局 4、 html 三个div如何排成左二右一 5、 html的div排列问题 6、 html中怎么样让div并排显示 html 如何让几个div水平居中一行需要准备的材料分别有:电脑、浏览器、html编辑器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html中的<body>标签中,输入html代码:<div style="float: left">123</div><div>456</div>。
3、浏览器运行index.html页面,此时多个div会水平居中在一行。
html怎么将两个div并排显示啊?在HTML中让两个div并排显示,通常情况下有三种实现方式,包括:
(1)设置为行内样式,display:inline-block
(2)设置float浮动
(3)设置position定位属性为absolute
以下为三种方式的具体实现代码:
1、设置每个div的展现属性为行内样式,示例代码为:
<div class="app">
<div style="display:inline-block;background:#f00;">div1</div>
<div style="display:inline-block;background:#0f0;margin-left:10px;">div2</div>
</div>
2、设置float浮动,示例代码为:
<div class="app">
<div style="float:left;background:#f00;">div1</div>
<div style="float:left;background:#0f0;margin-left:10px;">div2</div>
</div>
3、设置position定位属性为absolute, 示例代码为:
<div class="app">
<div style="position: absolute;width:100px;background:#f00;">div1</div>
<div style="position: absolute;left:100px;background:#0f0;margin-left:10px;">div2</div>
</div>
扩展资料:
css清除浮动方法
(1)添加新的元素 、应用 clear:both
.clear {
clear: both;
height: 0;
height: 0;
overflow: hidden;
}
(2)父级div定义 overflow: auto
.over-flow {
overflow: auto;
zoom: 1; //处理兼容性问题
}
(3)伪类 :after 方法 outer是父div的样式
.outer { zoom:1; } /*==for IE6/7 Maxthon2==*/
.outer :after {
clear:both;
content:'.';
display:block;
width: 0;
height: 0;
visibility:hidden;
}
参考资料来源:CSS官方文档:css-float
参考资料来源:CSS官方文档:css-Positioning
html页面中表单怎么用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:left;width:49%;border:1px solid #F00}
.div-b{ float:left;width: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:left;width:49%;border:1px solid #F00}
.div-d{ float:right;width: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如何排成左二右一1、浮动布局:
<style>
html,body{margin:0; padding:0}
.div1 {float:left; width:80%; height:600px; background-color:red}
.div2 {float:left; width:80%; background-color:green}
.div3 {float:right; width:20%; background-color:blue}
</style>
<div class="div1">宽80%,高600px</div>
<div class="div3">宽20%,高随内容</div>
<div class="div2">宽80%,高随内容</div>
<br style="clear:both"/>
2、绝对定位:
<style>
html,body{margin:0; padding:0}
.div1 {position:absolute; left:0; top:0; width:80%; height:600px; background-color:red}
.div2 {position:absolute; left:0; top:600px; width:80%; background-color:green}
.div3 {position:absolute; right:0; top:0; width:20%; background-color:blue}
</style>
<div class="div1">宽80%,高600px</div>
<div class="div2">宽80%,高随内容</div>
<div class="div3">宽20%,高随内容</div>
3、flex布局:
<style>
html,body{margin:0; padding:0}
.box {display:flex; align-items:flex-start}
.left {display:flex; flex-direction:column; flex:0 1 80%}
.div1 {flex:0 1 600px; background-color:red}
.div2 {flex:auto; background-color:green}
.right {flex:auto; background-color:blue}
</style>
<div class="box">
<div class="left">
<div class="div1">宽80%,高600px</div>
<div class="div2">宽80%,高随内容</div>
</div>
<div class="right">宽20%,高随内容</div>
</div>
html的div排列问题html中的div排版是通过css样式控制的。
DIV+CSS布局中主要CSS属性介绍:
Float:
Float属性是DIV+CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Float属性可以实现一行显示多个div的功能,最直接解释方法就是能实现表格布局的多列功能。
Margin:
Margin属性用于设置两个元素之间的距离。
Padding:
Padding属性用于设置一个元素的边框与其内容的距离。
Clear:
使用Float属性设置一行有多个DIV后(多列),最好在下一行开始之前使用Clear属性清楚一下浮动,否则上面的布局会影响到下面。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DIV+CSS布局教程</title>
<style type="text/css">
#Container{
width:1000px;
margin:0 auto;/*设置整个容器在浏览器中水平居中*/
background:#CF3;
}
#Header{
height:80px;
background:#093;
}
#logo{
padding-left:50px;
padding-top:20px;
padding-bottom:50px;
}
#Content{
height:600px;
/*此处对容器设置了高度,一般不建议对容器设置高度,一般使用overflow:auto;属性设置容器根据内容自适应高度,如果不指定高度或不设置自适应高度,容器将默认为1个字符高度,容器下方的布局元素(footer)设置margin-top:属性将无效*/
margin-top:20px;/*此处讲解margin的用法,设置content与上面header元素之间的距离*/
background:#0FF;
}
#Content-Left{
height:400px;
width:200px;
margin:20px;/*设置元素跟其他元素的距离为20像素*/
float:left;/*设置浮动,实现多列效果,div+Css布局中很重要的*/
background:#90C;
}
#Content-Main{
height:400px;
width:720px;
margin:20px;/*设置元素跟其他元素的距离为20像素*/
float:left;/*设置浮动,实现多列效果,div+Css布局中很重要的*/
background:#90C;
}
/*注:Content-Left和Content-Main元素是Content元素的子元素,两个元素使用了float:left;设置成两列,这个两个元素的宽度和这个两个元素设置的padding、margin的和一定不能大于父层Content元素的宽度,否则设置列将失败*/
#Footer{
height:40px;
background:#90C;
margin-top:20px;
}
.Clear{
clear:both;
}
</style>
</head>
<body>
<div id="Container">
<div id="Header">
<div id="logo">这里设置了padding属性介绍一下padding的用法,padding将设置文本与边框的距离。</div>
</div>
<div id="Content">
<div id="Content-Left">Content-Left</div>
<div id="Content-Main">Content-Main</div>
</div>
<div class="Clear"><!--如何你上面用到float,下面布局开始前最好清除一下。--></div>
<div id="Footer">Footer</div>
</div>
</body>
</html>
运行效果:
html中怎么样让div并排显示div属于块元素,通俗的讲,块元素会占一整行。如果让一行显示两个div有两种方法。
1、让两个想并排的div的转换成行内元素
div{display:inline;}
2、让两div设置固定宽度,然后让其浮动显示即可。
div{width:50%;float:left;}
注意也可将div宽度设置成像素宽度,但两个div的宽度加起来不能大于父级div的宽度,只能 小于或等于。
关于html怎么将DIV排版的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于html怎么将DIV排版 html如何设置div布局的详细内容...