2013-04-25 44 views
10

我想要的內容A,內容B和內容C柱被水平居中。我有這樣的代碼一直試圖如何中心水平臺小區

http://jsfiddle.net/hsX5q/24/

HTML添加:保證金:0汽車到.columns容器,它不工作。誰能幫忙?

/************************* 
 
* Sticky footer hack 
 
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/ 
 
************************/ 
 

 
/* Stretching all container's parents to full height */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
/* Setting the container to be a table with maximum width and height */ 
 

 
#container { 
 
    display: table; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
/* All sections (container's children) should be table rows with minimal height */ 
 

 
.section { 
 
    display: table-row; 
 
    height: 1px; 
 
} 
 
/* The last-but-one section should be stretched to automatic height */ 
 

 
.section.expand { 
 
    height: auto; 
 
} 
 
/************************* 
 
* Full height columns 
 
************************/ 
 

 
/* We need one extra container, setting it to full width */ 
 

 
.columns-container { 
 
    display: table-cell; 
 
    height: 100%; 
 
    width: 300px; 
 
    margin: 0 auto; 
 
} 
 
/* Creating columns */ 
 

 
.column { 
 
    /* The float:left won't work for Chrome for some reason, so inline-block */ 
 
    display: inline-block; 
 
    /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */ 
 
    vertical-align: top; 
 
    height: 100%; 
 
    width: 100px; 
 
} 
 
/**************************************************************** 
 
* Just some coloring so that we're able to see height of columns 
 
****************************************************************/ 
 

 
header { 
 
    background-color: yellow; 
 
} 
 
#a { 
 
    background-color: pink; 
 
} 
 
#b { 
 
    background-color: lightgreen; 
 
} 
 
#c { 
 
    background-color: lightblue; 
 
} 
 
footer { 
 
    background-color: purple; 
 
}
<div id="container"> 
 
    <header class="section"> 
 
    foo 
 
    </header> 
 

 
    <div class="section expand"> 
 
    <div class="columns-container"> 
 
     <div class="column" id="a"> 
 
     <p>Contents A</p> 
 
     </div> 
 
     <div class="column" id="b"> 
 
     <p>Contents B</p> 
 
     </div> 
 
     <div class="column" id="c"> 
 
     <p>Contents C</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <footer class="section"> 
 
    bar 
 
    </footer> 
 
</div>

+1

側面說明:由於'.columns-container'顯示爲表格單元格,因此無法對其應用邊距,這就是爲什麼該技巧不適用於此處。 – animuson 2013-04-26 00:05:39

回答

20

如果添加text-align: center的聲明爲.columns-container然後他們一致集中:

.columns-container { 
    display: table-cell; 
    height: 100%; 
    width:600px; 
    text-align: center; 
} 

/************************* 
 
* Sticky footer hack 
 
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/ 
 
************************/ 
 

 
/* Stretching all container's parents to full height */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
/* Setting the container to be a table with maximum width and height */ 
 

 
#container { 
 
    display: table; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
/* All sections (container's children) should be table rows with minimal height */ 
 

 
.section { 
 
    display: table-row; 
 
    height: 1px; 
 
} 
 
/* The last-but-one section should be stretched to automatic height */ 
 

 
.section.expand { 
 
    height: auto; 
 
} 
 
/************************* 
 
* Full height columns 
 
************************/ 
 

 
/* We need one extra container, setting it to full width */ 
 

 
.columns-container { 
 
display: table-cell; 
 
height: 100%; 
 
width:600px; 
 
text-align: center; 
 
} 
 
/* Creating columns */ 
 

 
.column { 
 
    /* The float:left won't work for Chrome for some reason, so inline-block */ 
 
    display: inline-block; 
 
    /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */ 
 
    vertical-align: top; 
 
    height: 100%; 
 
    width: 100px; 
 
} 
 
/**************************************************************** 
 
* Just some coloring so that we're able to see height of columns 
 
****************************************************************/ 
 

 
header { 
 
    background-color: yellow; 
 
} 
 
#a { 
 
    background-color: pink; 
 
} 
 
#b { 
 
    background-color: lightgreen; 
 
} 
 
#c { 
 
    background-color: lightblue; 
 
} 
 
footer { 
 
    background-color: purple; 
 
}
<div id="container"> 
 
    <header class="section"> 
 
    foo 
 
    </header> 
 

 
    <div class="section expand"> 
 
    <div class="columns-container"> 
 
     <div class="column" id="a"> 
 
     <p>Contents A</p> 
 
     </div> 
 
     <div class="column" id="b"> 
 
     <p>Contents B</p> 
 
     </div> 
 
     <div class="column" id="c"> 
 
     <p>Contents C</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <footer class="section"> 
 
    bar 
 
    </footer> 
 
</div>

這不,不過,需要你的.column元素重置爲text-align: left(假設你希望他們左對齊,顯然(JS Fiddle demo)。

+0

正常工作,謝謝:) – Daniel 2013-04-26 07:37:17

+0

這讓我困惑了一會兒,因爲我想要達到同樣的事情,但在上面的類寬度是多餘的,該小區實際上是100%的寬度。不幸的是,表格單元似乎佔用儘可能多的水平空間。 – Dave 2013-12-13 02:16:14

+1

不要忘了這個工作,你需要'.column'是'顯示:inline-block的;'。 OP已經有了,但其他人可能想知道爲什麼只用'text-align:center'來解答他們的問題。 – poshest 2015-12-10 08:26:50

1

只要添加這個類在你的CSS

.column p{ 
    text-align:center; 
} 
1

一小段爲未來的訪客 - 如何中心水平表單元格(+垂直)

html, body { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.tab { 
 
    display: table; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.cell { 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
    text-align: center; /* the key */ 
 
    background-color: #EEEEEE; 
 
} 
 

 
.content { 
 
    display: inline-block; /* important !! */ 
 
    width: 100px; 
 
    background-color: #00FF00; 
 
}
<div class="tab"> 
 
    <div class="cell"> 
 
    <div class="content" id="a"> 
 
     <p>Content</p> 
 
    </div> 
 
    </div> 
 
</div>