如何在绝对元素内的文本前垂直对齐?
How to vertically align before text inside absolute element?
我正在尝试垂直对齐用 :before
CSS 编写的中间文本。我想不通。
似乎是重复的,但我已经尝试了我找到的所有内容。也许我写错了,但我认为这个案例还没有涉及:绝对元素+文本之前。
这是我目前的情况:
.element {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
vertical-align: middle;
}
<div class="element"></div>
display flex 或 table 很容易做到:
html,
body,
.element {
height: 100%;
width: 100%;
margin:0;
border-collapse:collapse;
box-sizing:border-box;
}
.element {
position: absolute;
display: table;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
display: table-cell;
vertical-align: middle;
}
<div class="element"></div>
弹性
.element {
position: absolute;
display: flex;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
align-items:center;
justify-content:center;
}
.element:before {
content: "Some text to align vertically";
}
<div class="element"></div>
或
.element {
position: absolute;
display: flex;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
margin:auto;
}
<div class="element"></div>
我正在尝试垂直对齐用 :before
CSS 编写的中间文本。我想不通。
似乎是重复的,但我已经尝试了我找到的所有内容。也许我写错了,但我认为这个案例还没有涉及:绝对元素+文本之前。
这是我目前的情况:
.element {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
vertical-align: middle;
}
<div class="element"></div>
display flex 或 table 很容易做到:
html,
body,
.element {
height: 100%;
width: 100%;
margin:0;
border-collapse:collapse;
box-sizing:border-box;
}
.element {
position: absolute;
display: table;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
display: table-cell;
vertical-align: middle;
}
<div class="element"></div>
弹性
.element {
position: absolute;
display: flex;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
align-items:center;
justify-content:center;
}
.element:before {
content: "Some text to align vertically";
}
<div class="element"></div>
或
.element {
position: absolute;
display: flex;
top: 0;
right: 0;
left: 0;
bottom: 0;
border: 3px solid black;
border-radius: 5px;
text-align: center;
height: inherit;
background-color: rgba(0, 0, 0, 0.2);
}
.element:before {
content: "Some text to align vertically";
margin:auto;
}
<div class="element"></div>