2016-02-27 92 views
0

以下是我的網頁的以下html和CSS代碼。我似乎無法集中標題div中的標題div。我很困惑這個問題是由於定位還是顯示。謝謝。爲什麼margin:auto標籤在「title」div上不起作用?

#header { 
 
    position: absolute; 
 
    position: block; 
 
    width: 100%; 
 
    height: 120px; 
 
    background-color: #3e8dff; 
 
    text-decoration: underline; 
 
    top: 0px; 
 
    left: 0px; 
 
} 
 
#image { 
 
    height: 120px; 
 
    width: 150px; 
 
    float: left; 
 
} 
 
#image a img { 
 
    position: absolute; 
 
    height: 120px; 
 
    width: 150px; 
 
} 
 
#navBar { 
 
    position: absolute; 
 
    width: 1500px; 
 
    height: 60px; 
 
    background-color: #96CDCD; 
 
    top: 120px; 
 
    left: -15px; 
 
} 
 
#navBar ul li { 
 
    position: relative; 
 
    list-style-type: none; 
 
    float: left; 
 
    display: inline-block; 
 
    border: 2px solid black; 
 
    padding: 5px; 
 
    margin-right: 50px; 
 
    margin-left: 50px; 
 
    font-family: Arial; 
 
    font-size: 1.5em; 
 
    bottom: 5px; 
 
    background-color: blue; 
 
} 
 
#top_left { 
 
    float: left; 
 
    position: absolute; 
 
    top: 190px; 
 
    left: 0px; 
 
    width: 48%; 
 
    height: 500px; 
 
    border-radius: 5px; 
 
    background-color: #00FA9A; 
 
} 
 
#top_right { 
 
    float: right; 
 
    position: absolute; 
 
    top: 190px; 
 
    width: 50%; 
 
    right: 0px; 
 
    height: 500px; 
 
    border-radius: 5px; 
 
    background-color: #40E0D0; 
 
    opacity: .7; 
 
} 
 
#bot_left { 
 
    position: absolute; 
 
    clear: both; 
 
    height: 500px; 
 
    width: 48%; 
 
    border-radius: 5px; 
 
    background-color: yellow; 
 
    top: 700px; 
 
    left: 0px; 
 
} 
 
#bot_right { 
 
    position: absolute; 
 
    float: right; 
 
    height: 500px; 
 
    width: 50%; 
 
    border-radius: 5px; 
 
    background-color: purple; 
 
    top: 700px; 
 
    right: 0px; 
 
} 
 
.simplePar { 
 
    display: inline-block; 
 
    font-size: 26px; 
 
    font-family: Tahoma; 
 
} 
 
#title { 
 
    position: absolute; 
 
    display: inline-block; 
 
    margin: auto; 
 
    height: 90px; 
 
    width: 500px; 
 
    background-color: purple; 
 
}
<!Doctype html> 
 
<html> 
 

 
<head> 
 
    <title>Page 1</title> 
 
    <link rel="stylesheet" type="text/css" href="style.css"> 
 
</head> 
 

 
<body> 
 

 
    <div id="header"> 
 

 
    <div id="image"> 
 
     <a href="http://google.com/"> 
 
     <img src="C:\Users\Raiyan\Desktop\files for website\google-logo.jpg"> 
 
     </a> 
 
    </div> 
 
    <div id="title"> 
 
     pasa 
 
    </div> 
 
    </div> 
 

 
    <div id="navBar"> 
 
    <ul> 
 
     <li>Home</li> 
 
     <li>Latest News</li> 
 
     <li>Buy Tickets</li> 
 
     <li>Blog</li> 
 
     <li>More Info</li> 
 
     <li>Contact us</li> 
 
    </ul> 
 
    </div> 
 

 

 
    <div id="top_left"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 

 
    </div> 
 
    <div id="top_right"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 
    <div id="bot_left"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 
    <div id="bot_right"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 

 
    <div id="footer"></div> 
 

 

 

 
</body> 
 

 
</html>

+0

您正在代碼中混合多個定位系統(浮點數,絕對值,內置...)。我可以簡化你的標記嗎? – Vi100

+0

是的。我仍然在學習,並且非常困惑於何時何地使用定位,顯示和浮動。請給我一些建議,以便我可以清楚地理解它。 @ Vi100 – raiyan106

+0

您的頭文件中有太多html元素,因此最終會出現過多和複雜的css規則以適合您的佈局。一般來說,儘量使用更少的元素來完成你想要的任務,儘量做到「語義」。舉例來看下面的答案。 – Vi100

回答

-1

div#title的位置被設置爲絕對的。 margin: 0 auto不適用於絕對定位元素,因爲它們脫離了上下文。

0

對於如此簡單的結構,您有太多的div和樣式。當然,這可以用各種各樣的方式來完成,在這裏你有其中之一:

<div id="header"> 
    <a href="http://google.com/"><img id="image" src="https://upload.wikimedia.org/wikipedia/commons/a/a2/Example_logo.jpg"></a> 
    <span>pasa</span> 
</div> 

#header { 
    text-align:center; 
    line-height: 120px; 
    font-size:24px; 
} 

#image 
{ 
    height: 120px; 
    width: 150px; 
    position:absolute; 
    left:0; 
} 

如果你想要的是集中在剩餘空間標題後的圖像(而不是在頁面本身居中)那麼只需刪除position:absolute style並將float:left代替。

您應該簡化標記,然後您的樣式規則將更容易理解(對於您和瀏覽器...)。您也有類似的錯誤:

position: block; 

這不存在。

0

集#title內聯和然後給它中心的文本對齊:

#header { 
 
    position: absolute; 
 
    position: block; 
 
    width: 100%; 
 
    height: 120px; 
 
    background-color: #3e8dff; 
 
    text-decoration: underline; 
 
    top: 0px; 
 
    left: 0px; 
 
} 
 
#image { 
 
    height: 120px; 
 
    width: 150px; 
 
    float: left; 
 
} 
 
#image a img { 
 
    position: absolute; 
 
    height: 120px; 
 
    width: 150px; 
 
} 
 
#navBar { 
 
    position: absolute; 
 
    width: 1500px; 
 
    height: 60px; 
 
    background-color: #96CDCD; 
 
    top: 120px; 
 
    left: -15px; 
 
} 
 
#navBar ul li { 
 
    position: relative; 
 
    list-style-type: none; 
 
    float: left; 
 
    display: inline-block; 
 
    border: 2px solid black; 
 
    padding: 5px; 
 
    margin-right: 50px; 
 
    margin-left: 50px; 
 
    font-family: Arial; 
 
    font-size: 1.5em; 
 
    bottom: 5px; 
 
    background-color: blue; 
 
} 
 
#top_left { 
 
    float: left; 
 
    position: absolute; 
 
    top: 190px; 
 
    left: 0px; 
 
    width: 48%; 
 
    height: 500px; 
 
    border-radius: 5px; 
 
    background-color: #00FA9A; 
 
} 
 
#top_right { 
 
    float: right; 
 
    position: absolute; 
 
    top: 190px; 
 
    width: 50%; 
 
    right: 0px; 
 
    height: 500px; 
 
    border-radius: 5px; 
 
    background-color: #40E0D0; 
 
    opacity: .7; 
 
} 
 
#bot_left { 
 
    position: absolute; 
 
    clear: both; 
 
    height: 500px; 
 
    width: 48%; 
 
    border-radius: 5px; 
 
    background-color: yellow; 
 
    top: 700px; 
 
    left: 0px; 
 
} 
 
#bot_right { 
 
    position: absolute; 
 
    float: right; 
 
    height: 500px; 
 
    width: 50%; 
 
    border-radius: 5px; 
 
    background-color: purple; 
 
    top: 700px; 
 
    right: 0px; 
 
} 
 
.simplePar { 
 
    display: inline-block; 
 
    font-size: 26px; 
 
    font-family: Tahoma; 
 
} 
 
#title { 
 
    position: absolute; 
 
    display: inline; 
 
    text-align:center; 
 
    margin: auto; 
 
    height: 90px; 
 
    width: 500px; 
 
    background-color: purple; 
 
}
<!Doctype html> 
 
<html> 
 

 
<head> 
 
    <title>Page 1</title> 
 
    <link rel="stylesheet" type="text/css" href="style.css"> 
 
</head> 
 

 
<body> 
 

 
    <div id="header"> 
 

 
    <div id="image"> 
 
     <a href="http://google.com/"> 
 
     <img src="C:\Users\Raiyan\Desktop\files for website\google-logo.jpg"> 
 
     </a> 
 
    </div> 
 
    <div id="title"> 
 
     pasa 
 
    </div> 
 
    </div> 
 

 
    <div id="navBar"> 
 
    <ul> 
 
     <li>Home</li> 
 
     <li>Latest News</li> 
 
     <li>Buy Tickets</li> 
 
     <li>Blog</li> 
 
     <li>More Info</li> 
 
     <li>Contact us</li> 
 
    </ul> 
 
    </div> 
 

 

 
    <div id="top_left"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 

 
    </div> 
 
    <div id="top_right"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 
    <div id="bot_left"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 
    <div id="bot_right"> 
 
    <p class="simplePar">All of these are under the <span>divs</span> 
 
    </p> 
 
    </div> 
 

 
    <div id="footer"></div> 
 

 

 

 
</body> 
 

 
</html>

-1

變化爲#title {顯示:塊;位置:相對;} 您正在取代由innerhitteng塊的上,ans位置絕對不會像@Paul消息之前工作!

相關問題