Flexbox - 垂直居中文本
Flexbox - center text vertically
我有一个方形的 flexbox,完全被一个超链接覆盖。
现在我想将 flexbox 的内容垂直居中。
但是,使用带有 属性 display: table
的 div
元素和带有 属性 display: table-cell; vertical-align: middle
的嵌套 div
不起作用, 因为我失去了正方形。
如果我使用 div
s 而不是 ul
和 li
,我将失去可以在任何地方点击的 属性。
我希望"Text"在红框的水平和垂直中心对齐:
body {
margin: 0px;
width: 100%;
}
main {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.flex-container {
width: 100%;
}
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
-webkit-padding-start: 0px;
-webkit-margin-before: 0px;
-webkit-margin-after: 0px;
}
li {
display: flex;
flex-direction: column;
width: 50%;
}
.red {
background-color: red;
}
.white {
background-color: white;
}
.tile:before {
content: '';
float: left;
padding-top: 100%;
}
.tile {
text-align: center;
}
<main>
<div class="flex-container">
<ul>
<li class="red">
<a href="/">
<div class="tile">
Text
</div>
</a>
</li>
</ul>
</div>
</main>
在您的 .tile 声明中,您需要如下 flexbox 代码:
justify-content: center;
display: flex;
align-items: center;
你的 LI 声明应该只是 display:block 因为它没有使用 flexbox 属性。
main {
height: 200px;
width: 200px;
}
a {
display: flex;
align-items: center;
justify-content: center;
background-color: red;
height: 100%;
}
<main>
<a href="/">
<div class="tile">Text</div>
</a>
</main>
如下更改您的CSS:
.tile:before {
content: '';
float: left;
}
.tile {
text-align: center;
padding-top: 50%;
padding-bottom: 50%;
}
您不会丢失您的 square-shape 并且填充将始终使文本居中。
你可以在这里查看:
我删除了 .tile
的 :before
部分,并将其移动到 a
标签(现在 class-ed click-container
。因为它没有感觉它应该在的位置。您希望 a
元素本身是方形的,而不是它的内容。
现在我们有了一个方形锚点,您可以使用任何垂直对齐技巧来使内容居中,在这个例子中我使用了绝对+翻译技巧:
body {
margin: 0px;
width: 100%;
}
main {
display:flex;
flex-wrap:wrap;
}
.flex-container {
width: 100%;
}
ul {
display: flex;
flex-wrap:wrap;
list-style: none;
-webkit-padding-start:0px;
-webkit-margin-before: 0px;
-webkit-margin-after: 0px;
}
li {
display: flex;
flex-direction: column;
width: 50%;
}
.red {
background-color: red;
}
.white {
background-color: white;
}
/* edited code */
.tile {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.click-container {
display: block;
text-align: center;
position: relative;
}
.click-container:before{
float: left;
padding-top: 100%;
content: "";
}
<body>
<main>
<div class="flex-container">
<ul>
<li class="red">
<a class="click-container" href="/">
<div class="tile">
Text
</div>
</a>
</li>
</ul>
</div>
</main>
</body>
我有一个方形的 flexbox,完全被一个超链接覆盖。
现在我想将 flexbox 的内容垂直居中。
但是,使用带有 属性 display: table
的 div
元素和带有 属性 display: table-cell; vertical-align: middle
的嵌套 div
不起作用, 因为我失去了正方形。
如果我使用 div
s 而不是 ul
和 li
,我将失去可以在任何地方点击的 属性。
我希望"Text"在红框的水平和垂直中心对齐:
body {
margin: 0px;
width: 100%;
}
main {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.flex-container {
width: 100%;
}
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
-webkit-padding-start: 0px;
-webkit-margin-before: 0px;
-webkit-margin-after: 0px;
}
li {
display: flex;
flex-direction: column;
width: 50%;
}
.red {
background-color: red;
}
.white {
background-color: white;
}
.tile:before {
content: '';
float: left;
padding-top: 100%;
}
.tile {
text-align: center;
}
<main>
<div class="flex-container">
<ul>
<li class="red">
<a href="/">
<div class="tile">
Text
</div>
</a>
</li>
</ul>
</div>
</main>
在您的 .tile 声明中,您需要如下 flexbox 代码:
justify-content: center;
display: flex;
align-items: center;
你的 LI 声明应该只是 display:block 因为它没有使用 flexbox 属性。
main {
height: 200px;
width: 200px;
}
a {
display: flex;
align-items: center;
justify-content: center;
background-color: red;
height: 100%;
}
<main>
<a href="/">
<div class="tile">Text</div>
</a>
</main>
如下更改您的CSS:
.tile:before {
content: '';
float: left;
}
.tile {
text-align: center;
padding-top: 50%;
padding-bottom: 50%;
}
您不会丢失您的 square-shape 并且填充将始终使文本居中。
你可以在这里查看:
我删除了 .tile
的 :before
部分,并将其移动到 a
标签(现在 class-ed click-container
。因为它没有感觉它应该在的位置。您希望 a
元素本身是方形的,而不是它的内容。
现在我们有了一个方形锚点,您可以使用任何垂直对齐技巧来使内容居中,在这个例子中我使用了绝对+翻译技巧:
body {
margin: 0px;
width: 100%;
}
main {
display:flex;
flex-wrap:wrap;
}
.flex-container {
width: 100%;
}
ul {
display: flex;
flex-wrap:wrap;
list-style: none;
-webkit-padding-start:0px;
-webkit-margin-before: 0px;
-webkit-margin-after: 0px;
}
li {
display: flex;
flex-direction: column;
width: 50%;
}
.red {
background-color: red;
}
.white {
background-color: white;
}
/* edited code */
.tile {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.click-container {
display: block;
text-align: center;
position: relative;
}
.click-container:before{
float: left;
padding-top: 100%;
content: "";
}
<body>
<main>
<div class="flex-container">
<ul>
<li class="red">
<a class="click-container" href="/">
<div class="tile">
Text
</div>
</a>
</li>
</ul>
</div>
</main>
</body>