2013-04-25 23 views
10

コンテンツA、コンテンツB、およびコンテンツCの列を水平に配置します。マージン:私は、このコードは水平テーブルセルを中央に揃える方法

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

HTMLを追加しようとしている.columns-コンテナに0自動車を、それが動作しません。誰でも助けてくれますか?

/************************* 
 
* 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

答えて

20

あなたが.columns-containerの宣言にtext-align: centerを追加した場合、彼らは中央で揃える:

.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

+0

私は同じことを達成しようとしているので、これは私が一瞬混乱させましたが、上記のクラスの幅は冗長です。残念ながら、テーブルセルはできるだけ多くの水平方向の部屋を占めるように見えます。 – Dave

+1

これが機能するには 'display:inline-block;'に '.column'が必要であることを忘れないでください。 OPはそれをすでに持っていましたが、他の人たちはなぜ答えが単に 'text-align:center; – poshest

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>

関連する問題