HTML:margin: 0 auto to .columns-container und es funktioniert nicht. Könnte jemand helfen?
/*************************
* 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;
}
Nebenbemerkung: seit .columns-container als Tabellenzelle angezeigt wird, können keine Ränder darauf angewendet werden, weshalb dieser Trick hier nicht funktioniert.
– Animation ♦
26. April 2013 um 0:05 Uhr
David Thomas
Wenn Sie hinzufügen text-align: center zu den Erklärungen für .columns-container dann richten sie sich zentral aus:
/*************************
* 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;
}
Dies erfordert jedoch, dass Sie die zurücksetzen .column Elemente zu text-align: left (vorausgesetzt du wollen sie linksbündig, offensichtlich (JS Fiddle-Demo).
Dies hat mich einen Moment lang verwirrt, da ich versuche, dasselbe zu erreichen, aber die Breite in der obigen Klasse ist redundant, diese Zelle hat tatsächlich 100% Breite. Tabellenzellen scheinen leider so viel horizontalen Raum wie möglich einzunehmen.
– David
13. Dezember 2013 um 2:16 Uhr
Vergessen Sie nicht, dass Sie Folgendes benötigen, damit dies funktioniert .column sein display: inline-block;. Das OP hatte das bereits, aber andere fragen sich vielleicht, warum die Antwort für sie nicht nur mit funktioniert text-align: center;
– am schicksten
10. Dezember 2015 um 8:26 Uhr
David
Manchmal haben Sie andere Dinge als Text in einer Tabellenzelle, die Sie gerne horizontal zentriert haben möchten. Um dies zu tun, richten Sie zuerst etwas CSS ein …
Dann deklariere a div mit class="centered" in jeder Tabellenzelle, die Sie zentrieren möchten.
<td>
<div class="centered">
Anything: text, controls, etc... will be horizontally centered.
</div>
</td>
Hallo, willkommen bei Stack Overflow. Da dies eine alte Frage mit mehreren vorhandenen Antworten ist, darunter eine sehr beliebte akzeptierte, bearbeiten Sie bitte Ihre Antwort, um zu erklären, warum Ihre Lösung den bereits vorhandenen vorzuziehen ist. Vielen Dank.
– MandyShaw
28. März 2019 um 23:52 Uhr
Kurzer Ausschnitt für zukünftige Besucher – wie man horizontale Tabellenzellen zentriert (+ vertikal)
Nebenbemerkung: seit
.columns-container
als Tabellenzelle angezeigt wird, können keine Ränder darauf angewendet werden, weshalb dieser Trick hier nicht funktioniert.– Animation
♦
26. April 2013 um 0:05 Uhr