gpt4 book ai didi

html - 如何居中水平表格单元格

转载 作者:技术小花猫 更新时间:2023-10-29 12:13:43 24 4
gpt4 key购买 nike

我希望内容 A、内容 B 和内容 C 列水平居中。我一直在尝试添加这段代码

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

HTML:margin: 0 auto 到 .columns-container 并且它不起作用。谁能帮忙?

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

最佳答案

如果您将 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 )。

关于html - 如何居中水平表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16226625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com