DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
單欄固定寬度版型:用負值定位玩轉背景圖
編輯:CSS詳解     

這是一個單欄固定寬度的版型筆記,重點在於示範背景圖片的定位技巧——包含負值位移。整個結構非常精簡。

一、HTML 結構

body 之間只需要一個 div,id 取名為 layout:

<div id="layout">這裡寫入文章內容</div>

二、先寫 body 的樣式

body {
 margin: 0px;
 padding: 0px;
 background-attachment: fixed;
 background-image: url(images/tupian/a017.JPG);
 background-repeat: no-repeat;
 background-position: left bottom;
}

background-attachment: fixed 讓背景不隨捲動移動;background-position: left bottom 則把裝飾圖釘在視窗左下角,作為整頁的視覺基調。

三、再寫 layout 的樣式

#layout {
 height: 480px;
 width: 720px;
 margin-top: 20px;
 margin-right: 30px;
 margin-bottom: 20px;
 margin-left: 150px;
 padding: 40px;
 background-image: url(images/tupian/a017-2.jpg);
 background-position: right -100px;
 background-repeat: no-repeat;
 border: thick double #F79A94;
 font-family: "宋體";
 font-size: 9pt;
 color: #993300;
 line-height: 150%;
}

除了寬高與邊距之外,這裡做了兩件事。第一是設定四邊的外邊距與內距,讓內容有呼吸空間;第二是透過 background-position: right -100px 把右上角那張圖往上推出容器——負值讓圖片的一部分溢出裁切,營造出「探出邊界」的效果,同時又不影響文字排版。

四、想改成水平居中

只要把上面四個 margin 值換成一行即可:

#layout { margin: 0px auto; }

這裡有個容易忽略的細節:區塊的實際佔寬是 width 加上左右 padding,也就是 720 + 80 = 800px。用固定寬度做居中時,務必把 padding 與 border 一起算進去,否則在不同瀏覽器的盒模型解讀下,置中位置會出現偏移。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved