网页布局

DIV+CSS布局中主要CSS属性介绍:

Float:

       Float属性是DIV+CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Float属性可以实现一行显示多个div的功能,最直接解释方法就是能实现表格布局的多列功能。

Position:

  Position定位当然也能实现不同的布局,比如我们在网页上所看见的很多广告都是由fixed这个定位属性来实现的,还有我们的导航,也能通过fixed固定在页面的顶部,并且不

会随着滚动条的滚动而滚动。还有absolute以及我们的relative属性,但是要注意参考点。

Display:

  display:inline-block;inline;block;我们也能通过该属性来灵活的进行我们的网页布局。

Margin:

       Margin属性用于设置两个元素之间的距离。

Padding:

       Padding属性用于设置一个元素的边框与其内容的距离。

Clear:

       使用Float属性设置一行有多个DIV后(多列),最好在下一行开始之前使用Clear属性清楚一下浮动,否则上面的布局会影响到下面。

实例讲解:下面使用实例如果做一个简单又基本的布局,效果如下图:

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#Container{
    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;
    200px;
    margin:20px;/*设置元素跟其他元素的距离为20像素*/
    float:left;/*设置浮动,实现多列效果,div+Css布局中很重要的*/
    background:#90C;
}
#Content-Main{
    height:400px;
    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>

注解:Container作为整个页面的容器,控制着整个页面在浏览器的位置,此处使用margin:0 auto;控制Container容器在浏览器中水平居中,一般固定宽度的布局都会用到这就代码。

上面的布局主要用的是float属性,但是float属性使用的时候一定得注意什么时候进行clear,用display的话不存在这个问题,但是使用了display的话会在两个盒子之间产生一个空格文本节点,所以需要我们将上一个的结束标签和下一个的开始标签放在同一行;例如;请看以下代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        body,div{
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <div style=" 100px;height: 100px;background: red;display: inline-block">firstDiv</div>
    <div style=" 100px;height: 100px;background: orange;display: inline-block">secondDiv</div>
</body>
</html>

  结果:

可见 确实是有个空格部分在他们中间;

我们再看一下代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        body,div{
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <div style=" 100px;height: 100px;background: red;display: inline-block">firstDiv</div><div style=" 100px;height: 100px;background: orange;display: inline-block">secondDiv
    </div>
</body>
</html>

再看我们的结果是不是达到要求了呢?

可见我们进行页面布局的方式是有很多的,也就需要我们做前端的灵活运用,孰能生巧,在完成一个页面之前,必做的一件事就是分析页面布局,然后将布局先完成,然后就进行完形填空吧~~~~

原文地址:https://www.cnblogs.com/Jayvenlee/p/3947084.html