2016-07-10 41 views
0

如果將以下代碼放在全屏中,則只要放大到足夠大,元素只會集中在頁面上。我怎樣才能讓它以中心爲中心?爲什麼這隻在我放大時居中? (使用引導程序)

.col-centered { 
 
    float: none; 
 
    text-align: center; 
 
} 
 
.col-centered table { 
 
    margin: 0 auto; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
 
<div class='container-fluid'> 
 
    <div class='quiz-list'> 
 
    <div class='row'> 
 
     <div class='col-md-4 col-centered'> 
 

 
     This text should be centered and the table underneath should be centered as well 
 

 
     <table class="table"> 
 
      <thead> 
 
      <tr> 
 
       <th>#</th> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>Username</th> 
 
      </tr> 
 
      </thead> 
 
      <tbody> 
 
      <tr> 
 
       <th scope="row">1</th> 
 
       <td>Mark</td> 
 
       <td>Otto</td> 
 
       <td>@mdo</td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">2</th> 
 
       <td>Jacob</td> 
 
       <td>Thornton</td> 
 
       <td>@fat</td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">3</th> 
 
       <td>Larry</td> 
 
       <td>the Bird</td> 
 
       <td>@twitter</td> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     </div> 
 

 
    </div> 
 
    </div> 
 

 
</div>

回答

0

因爲引導。

.col-md-4上的width的默認值爲33.3333%。所以,由於div沒有浮動或邊距或任何東西,它只是簡單地與屏幕左側對齊,其中的內容居中居中,但這並不明顯。

一個解決方案是刪除col-md-4類。

.col-centered { 
 
    float: none; 
 
    text-align: center; 
 
} 
 
.col-centered table { 
 
    margin: 0 auto; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
 
<div class='container-fluid'> 
 
    <div class='quiz-list'> 
 
    <div class='row'> 
 
     <div class='col-centered'> <!-- this --> 
 

 
     This text should be centered and the table underneath should be centered as well 
 

 
     <table class="table"> 
 
      <thead> 
 
      <tr> 
 
       <th>#</th> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>Username</th> 
 
      </tr> 
 
      </thead> 
 
      <tbody> 
 
      <tr> 
 
       <th scope="row">1</th> 
 
       <td>Mark</td> 
 
       <td>Otto</td> 
 
       <td>@mdo</td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">2</th> 
 
       <td>Jacob</td> 
 
       <td>Thornton</td> 
 
       <td>@fat</td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">3</th> 
 
       <td>Larry</td> 
 
       <td>the Bird</td> 
 
       <td>@twitter</td> 
 
      </tr> 
 
      </tbody> 
 
     </table> 
 
     </div> 
 

 
    </div> 
 
    </div> 
 

 
</div>

我嘗試過其他解決方案,以及,如設置寬度或明確的利潤,但似乎並沒有工作。

編輯:
哦,我明白爲什麼其他解決方案不起作用:因爲在這段代碼中,Bootstrap css被加載到頁面中的樣式塊之後。
在正常情況下,加載Bootstrap樣式表後,將樣式放入樣式表中可以很好地覆蓋它。所以你可以使用width:automargin:0 auto,無論你喜歡什麼。

+0

感謝您的回答 - 我不是100%肯定我明白你到底說了什麼。我想要'col-md-4',因爲我想要特定的寬度,是否沒有辦法保持這個寬度並讓它居中? – nachime

+0

在stacksnippet中,Bootstrap樣式表位於您自己定義的樣式之後。因此,所有默認的Bootstrap樣式都將覆蓋您放置的樣式:您的'float:none'會再次變爲'float:left'等 –

+0

因此,您應該怎麼做(以及您可能在真實網站上做了什麼)在鏈接到Bootstrap之後鏈接到自己的樣式表,因此您的樣式將優先。然後你可以選擇任何你想要的方法,就像我在編輯中說的那樣。 –

0

在css文件,更新

.col-centered { 
    float: none; 
    text-align: center; 
} 

.col-centered { 
    float: none !important; 
    text-align: center; 
    margin: 0 auto; 
} 
+0

這是爲什麼修復它? – nachime

+0

因爲使用'!important'的樣式無需重寫樣式,即使其他樣式稍後會出現。在大多數情況下,你應該儘量避免這種情況。 –

相關問題