2014-10-11 49 views
0

我無法讓columnContainer在sectionTwo中居中。代碼如下。無論我做什麼,它似乎都保持一致。保證金:汽車不起作用,其他問題沒有幫助解決它

HTML:

<section class="sectionTwo"> 
     <div class="columnContainer"> 
      <div class="columnExplanation"> 
       <img src="Images/imagehere.png" style="width:150px;height:auto;margin-left:30%;margin-bottom:10px;"/> 
       <p>text here.</p> 
      </div> 
     </div> 
</section> 

CSS:

section.sectionTwo{ 

padding-bottom:20px; 
width:100%; 
height:340px; 
position:relative; 
background-color:#262626; 
border-top: 8px solid #3C3C3C; 
} 

div.columnContainer{ 
width:100%; 
position:relative; 
display:block; 
margin-left:auto; 
margin-right:auto; 
margin-top:20px; 
} 

感謝您的幫助!

+0

哪個元素必須居中? – 2014-10-11 12:45:31

+1

試試這個 - http://jsfiddle.net/2hh286ys/ – Anonymous 2014-10-11 12:46:31

回答

0

如果您嘗試居中'ColumnContainer',它已居中,因爲它們都設置爲100%寬度;

它改變這個,看看它實際工作:

section.sectionTwo{ 

padding-bottom:20px; 
width:100%; 
height:340px; 
position:relative; 
background-color:#262626; 
border-top: 8px solid #3C3C3C; 
} 

div.columnContainer{ 
background-color: red; 
width:100%; 
position:relative; 
display:block; 
margin-left:auto; 
margin-right:auto; 
margin-top:20px; 
text-align: center; 
} 
+0

當我使columnContainer小於100%時有什麼奇怪的地方在於,它不是居中,而是縮進到正確的30或40像素......還有,背景-紅色;沒有工作。不知道爲什麼在這一點上大聲笑 – 2014-10-11 13:00:51

+0

你想達到什麼?我會爲你嘲笑它。 – stackerz 2014-10-11 13:02:41

0

我不認爲,如果你想中心,它應該在圖像上使用保證金,在剩下的30%。的確,作爲一個內聯元素,你可以使用文本對齊:中心對你的父母DIV這樣的伎倆:

HTML:

<section class="sectionTwo"> 
     <div class="columnContainer"> 
      <div class="columnExplanation"> 
       <img src="Images/imagehere.png" style="width:150px;height:auto;margin-bottom:10px;"/> 
       <p>text here.</p> 
      </div> 
     </div> 
</section> 

CSS

section.sectionTwo{ 

padding-bottom:20px; 
width:100%; 
height:340px; 
position:relative; 
background-color:#262626; 
border-top: 8px solid #3C3C3C; 
} 

div.columnContainer{ 
width:100%; 
position:relative; 
display:block; 
margin-top:20px; 
    text-align: center; 
} 
+0

對不起,我打錯了。我正在嘗試將列容器居中。雖然我在對圖像進行定位時也遇到了麻煩,並且將30%用作臨時修復。我會嘗試你的文本對齊修復。謝謝! – 2014-10-11 12:56:24

0

這個人會幫你:

section {position:relative;} 
section #your_id { 
    position:absolute; 
    top:50%; 
    margin-top:-100px; */half of your height*/ 
    right:50%; 
    margin-right:-100px; */half of your width*/ 
}