div+css感悟

來源:互聯網
上載者:User

標籤:charset   今天   htm   image   感悟   包含   span   設定   head   

div+css感覺很簡單,可是真正做起來一些小細節把握不好,這個網頁的布局也是完成不了的。今天學習了一些技巧方法現在分享下:

即一個原則,網頁由一個個的大盒子組成,一個個的大盒子裡面裝著一個個的小盒子。先衡量好大盒子的尺寸,才好確定小盒子的大小。

大盒子包含小盒子,大的盒子必須又小的盒子組成,一個模組不能只有小盒子,要由大盒子來封裝。所以先設定大盒子的寬度,高度可以設定,也可以不設定,再設定小盒子的具體資訊。

1.現在的網頁布局都是採用盒子模型,即網頁由一個個div來構成的,div包含著很多div。所以我們通常來設計的話就是先將網頁的整體布局設計好,然後再向裡面填充內容。

比如整個頁面我們需要裝在一個大的div容器中:

這個容器中包含了頁面的:topdiv容器,maindiv容器,bottomdiv容器。

這其中就涉及到技巧了:topdiv容器中假設一般包含了logo的位置,導航條的位置。這兩個就是在topdiv容器中的兩個小的div部分。

           maindiv容器假設包含了main的左邊設定,右邊設定,中間設定。這三個也是maindiv的容器的三個小的div部分。

整個網頁就是由著三個大的容器部分組成。這裡的技巧就是:我們需要對每個大的容器(即topdiv/maindiv/bottomdiv)進行寬度的設定。然後對每個小的div(logo,導航條div)進行寬度和高度以及其他的設定,否則布局會出現問題

浮動是針對的div塊,所以這裡設定float是對大的div裡面的小的div的浮動設定。

2、第二個技巧就是網頁的置中問題,這裡為了保證瀏覽器的相容性,通常採用的方法是這樣子的;

在每個大div(比如topdiv)外面再設定一個div(top的上級div即toptopdiv)。對這個toptopdiv進行設定:將其寬度設定為100%,即整個網頁。對topdiv進行設定:margin:auto。這樣子就可以置中了。這樣子雖然比較繁瑣,但是相容性好些。

比如:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sohu.html</title>
<link rel="stylesheet" type="text/css" href="sohu.css">
</head>
<body>
<div id="divcontainer">
<div id="topp">
<div id="top">
<div class="logo"></div>
<div class="top_left">
<div class="nav"></div>
<div class="bar"></div>
</div>
</div>
</div>
<div id="mainmain">
<div id="main">
<div class="main_left"></div>
<div class="main_mid1">
<div class="mid1_top"></div>
<div class="mid1_mid"></div>
<div class="mid1_bottom"></div>
</div>
<div class="main_mid2">
<div class="mid2_left"></div>
<div class="mid2_right"></div>
</div>

<div class="main_right">
<img src="">
</div>

</div>
</div>

</div>


</body>
</html>

css模式:

body{

margin:auto;
}
#topp{
width: 100%;
height: 100px;
}
#top{
width: 1000px;
margin: auto;
height: 100px;
}

.logo{
background-color:red;
float: left;
width: 200px;
height:100px;

}
.nav{
float: right;
width: 790px;
height: 30px;
background-color: green;
}
.bar{
float: right;
width: 790px;
height:70px;
background-color: blue
}
#mainmain{
width: 100%;
}
#main{
width: 1000px;
margin:auto;

}
.main_left{
float: left;
width:200px;
height:400px;
background-color: pink;
margin-top:20px;
}
.main_mid1{
float: left;
width: 450px;

}
.mid1_top{
float: left;
width:450px;
height:100px;
background-color: green;
margin-left: 10px;
margin-top: 20px;
}
.mid1_mid{
float: left;
width:450px;
height:200px;
background-color:pink;
margin-left: 10px;

}
.mid1_bottom{
float: left;
width:450px;
height:100px;
background-color:blue;
margin-left: 10px;
}
.main_mid2{
float: left;
width: 150px;
margin-left: 20px;

}
.mid2_left{
width: 100px;
height: 400px;
background-color:red;
margin-top: 20px;
float: left;
}
.mid2_right{
width: 50px;
height: 400px;
background-color:blue;
margin-top: 20px;
float: left;

}
.main_right{
float: left;
width: 160px;
height: 400px;
margin-top: 20px;
margin-left: 10px;
background-color: green;
}

 

div+css感悟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.