2012-09-12 73 views
3

我一直想用CSS定位替換我的網站中的表格,並一直試圖通過教程等自學。我已經有一些早期的成功,但它都崩潰了我試圖創建一個側邊欄。我希望問題有一些簡單的解決方案。元素的相對/絕對定位不會接近我想要做的。我的目標是創建一個帶有從上到下堆疊(浮動?)圖像的側欄,中間元素是無序列表的一部分。我有一次工作,但現在堆疊在一起。它必須是我設置浮動和絕對/相對定位的方式。在閱讀了一些文章之後,我嘗試添加一個div封裝器,將它們放在裏面,但我覺得自己變得更加困惑。是否有人可以推動我朝着正確的方向發展?下面是代碼:簡單的css定位(我認爲)

CSS

body 
{ 
    background: #b6b7bc; 
    font-size: .80em; 
    font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif; 
    margin: 50px; 
    padding: 0px; 
    color: #696969; 
    height: 160px; 
} 

a:link, a:visited 
{ 
    color: #034af3; 
} 

a:hover 
{ 
    color: #1d60ff; 
    text-decoration: none; 
} 

a:active 
{ 
    color: #034af3; 
} 

p 
{ 
    margin-bottom: 10px; 
    line-height: 1.6em; 
} 


/* HEADINGS ----------------------------------------------------------*/ 

h1, h2, h3, h4, h5, h6 
{ 
    font-size: 1.5em; 
    color: #666666; 
    font-variant: small-caps; 
    text-transform: none; 
    font-weight: 200; 
    margin-bottom: 0px; 
} 

h1 
{ 
    font-size: 1.6em; 
    padding-bottom: 0px; 
    margin-bottom: 0px; 
} 

h2 
{ 
    font-size: 1.5em; 
    font-weight: 600; 
} 

h3 
{ 
    font-size: 1.2em; 
} 

h4 
{ 
    font-size: 1.1em; 
} 

h5, h6 
{ 
    font-size: 1em; 
} 



/* PRIMARY LAYOUT ELEMENTS ---------------------------------------------------------*/ 

.page 
{ 
    width: 960px; 
    background-color: #fff; 
    margin: 20px auto 0px auto; 
    border: 1px solid #496077; 
} 

.header 
{ 
    position: relative; 
    margin: 0px; 
    padding: 0px; 
    background: #4b6c9e; 
    width: 100%; 
    top: 0px; 
    left: 0px; 
} 

.header h1 
{ 
    font-weight: 700; 
    margin: 0px; 
    padding: 0px 0px 0px 20px; 
    color: #f9f9f9; 
    border: none; 
    line-height: 2em; 
    font-size: 2em; 
} 

.main 
{ 
    padding: 0px 12px; 
    margin: 0px 4px 4px 4px; 
    min-height: 420px; 
    width: 500px; 
    float: left; 
} 

.leftCol 
{ 
    padding: 6px 0px; 
    margin: 12px 8px 8px 8px; 
    width: 200px; 
    min-height: 200px; 
} 

.footer 
{ 
    color: #4e5766; 
    padding: 8px 0px 0px 0px; 
    margin: 0px auto; 
    text-align: center; 
    line-height: normal; 
} 




/* MISC ----------------------------------------------------------*/ 

.clear 
{ 
    clear: both; 
    width: 936px; 
    height: 35px; 
} 

.title 
{ 
    display: block; 
    float: left; 
    text-align: justify; 
} 


.bold 
{ 
    font-weight: bold; 
} 


p.clear 
{ 
    clear: both; 
    height: 0; 
    margin: 0; 
    padding: 0; 
} 

#wrapper 
{ 
    position:relative; 
    height: 500px; 
    width: 900px; 
} 

#insidemain 
{ 
    position:absolute; 
    float: left;  
    width: 500px; 
    height 180px; 
} 


/* ---------------- Sidebar Items ---------------------*/ 


#sidebar /* Sidebar container */ 
{ 
    position:absolute; 
    border-top: 1px solid #99CC33; 
    border-left: 1px solid #99CC33; 
    height: 300px; 
    width: 180px; 
    margin-right: 5px; 
    padding: 5px 0 0 5px; 
} 

#sidebarHeader 
{ 
    position:absolute; 
    height: 37px; 
    width: 172px; 
    float: left; 
    background-image: url(../img/TopMenu.jpg); 
    background-repeat:no-repeat; 
} 

#sidebarItems ul 
{ 
    position:absolute; 
    height: 27px; 
    width: 172px; 
    float:left; 
    background-image: url(../img/MenuItems.jpg); 
    background-repeat:no-repeat; 
    /*left: 6px; 
    top: 45px;*/ 
    background-position: 0px -27px; 
} 

#sidebarFooter 
{ 
    position:absolute; 
    height: 46px; 
    width: 172px; 
    float:left; 
    background-image: url(../img/BottomMenu.jpg); 
    background-repeat:no-repeat; 
} 

而且HTML

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
    <head> 
     <title></title> 
     <link href="Styles/Simple.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body> 
     <div class="page"> 
      <div class="header">header 
       <div class="title"> 
        <h1> 
         Test Page 
        </h1> 
       </div> 
      </div> 
      <p class = "clear">clear</p> 
      <div id="wrapper"> 
       <div id="sidebar"> 
        <div id="sidebarHeader"> 
        </div> 
        <div id="sidebarItems"> 
         <ul> 
          <li>test item</li> 
         </ul> 
        </div> 
        <div id="sidebarFooter"> 
        </div> 
       </div> 
      </div> 
      <div id="insidemain"> 
       main 
      </div> 
     </div> 
     <div class="clear">clear</div> 
     <div class="footer"> 
      <a href="http://www.google.com/"> 
       Blah blah test to see how far this will go across the page blah blha lorem ipsum and various other stuff that is meaningless etc 
      </a> 
     </div> 
    </body> 
</html> 
+0

浮動側邊欄左邊 –

+1

難道你把你的代碼上[的jsfiddle(http://jsfiddle.net/)? – Wex

+0

這看起來很酷!我會盡力解決這個問題。謝謝 – Dana

回答

1

你真的不應該浮動你的div或列表。這些是默認的塊元素,並且將垂直堆疊。

另外,正如Scrimothy提到的,您不需要絕對定位的元素,因爲這會將元素從頁面流中移出。換句話說,它們不再佔用頁面中的「真實」空間,而是在您定位它們的任何座標處渲染。

同樣,浮動也不佔用空間,除了其他浮動元素。這就是爲什麼一些UI開發人員幾乎可以浮動頁面上的所有元素,並使用頁腳或頁面中的關鍵點「清除」它們。我個人不建議以這種方式來定位,而是要以他自己的方式。

看看這個快速教程,幫助您進行一些關鍵的概念定位:HERE

+0

這個快速教程很有幫助。謝謝。這些概念開始沉入更多。我意識到的另一件事是,Dreamweaver中的預覽窗格很方便,但顯然不像Firefox這樣的典型瀏覽器。所以現在它回到了沙箱的一些簡單的佈局!再次感謝! – Dana

2

通常(非敏感部位尤其是),你必須在你的.wrapper DIV整個內容(標題,內容,側欄,頁腳等)。然後設置您的.wrapper的寬度。您的.sidebar將有一組width,它可以是float: left;float: right;,具體取決於您想要的一面。設置您的.content div的width這將小於或等於您的.wrapper width - 您的.sidebar width。然後在下面加上你的.clearfix,這樣.footer就落在了下面。在大多數情況下(至少對於大頁面塊),您可以避免position:absolute;,這有助於使事情更容易落實到位。

+0

有道理,謝謝。我也會嘗試按照下面的建議讓他們上課。我讀了一些關於課程的文章與divs並且會更加混亂。這是令人沮喪的時刻! – Dana

+0

是的,@理查德也給出了很好的建議。嘗試爲樣式化鉤子使用類,除非您想要所有'div'元素的默認樣式。 – Scrimothy

1

不要在定位既floatposition:absolute相同的元素。這沒有多大意義。任何地方你都有float,你應該擺脫position:absolute

接下來,擺脫那些愚蠢的class="clear"元素。相反,目標.footerclear:both.pageoverflow-y:hidden;

+0

我在Lynda.com做了一個教程,當頁面上的內容最初看起來不錯時,我想我已經明白了。他在「清理浮法」這個明確要素上表現出色。清楚:因爲您擁有下一個元素的風格,所以兩者似乎都會更有意義。 – Dana

+0

問題不在於你使用'clear:left'還是'clear:right' - 爲'clear'屬性添加一個元素的想法是愚蠢的。只要理解CSS選擇器並理解CSS屬性,就不必訴諸於這種不尋常的黑客行爲。 –