2013-03-17 121 views
0

我試圖將一個div滾動。我希望它死在網頁上,但它不是用我在下面提供的代碼來完成的。請幫忙。我遇到了一個滾動問題的中心問題

CSS

#content 
{ 
text-align: center; 
} 

.scroll 
{ 
background-color:#fff; 
color:#000; 
width:500px; 
height:400px; 
overflow:scroll; 
} 

HTML

<div id ="content"> 
<div class="scroll"> Stuff </div> 
</div> 

回答

1

一個div是塊級元素,也不會聽text-algin。您將需要在.scroll元素上使用margin: 0 auto,或者使div成爲內聯塊元素。儘管支持塊級元素是內嵌塊級元素並不完全支持,所以您將不得不使用跨度來獲得完整的支持。然而,更好的選擇是如果你的div有一個設置的寬度,在你想要居中的元素上使用左右邊距auto

+0

「塊級元素的支持是inline-block的樂vel元素不完全支持「除了IE6之外的任何瀏覽器都有這樣的問題嗎? – 2013-03-17 04:02:58

+0

非常感謝你 – user2094719 2013-03-17 04:03:30

+0

不,我以爲IE7有它。我真的不確定。如果我需要一個內嵌塊元素,我通常不會使用div。我不知道爲什麼你會在一個跨度上使用div。 – Leeish 2013-03-17 04:05:03

0

text-align隻影響文字。要將<div>定位在中心,請使用
margin-left:auto;margin-right:auto

+0

文本對齊還會影響內聯/內聯塊元素,而不僅僅是文本。 – Leeish 2013-03-17 03:59:20

+0

非常感謝你 – user2094719 2013-03-17 04:03:54

0

您可以將display:inline;margin:auto添加到您的<div>

0

試試這個

HTML

<div id ="content"> 
<div class="scroll"> Stuff </div> 
</div> 

CSS

#content 
{ 
    text-align: center; 
    margin-left:auto; 
    margin-right:auto; 
    width:300px; 
    height:200px; 
    overflow: scroll; 
} 

.scroll 
{ 
    background-color:#fff; 
    color:#000; 
    width:500px; 
    height:400px; 
} 

現場小提琴here