所以,我有一个图像,我想在它的中心显示一些文字.
码:
.img {
position: relative;
}
.img h3 {
position: absolute;
width: 100%
top: 85px;
text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
好的,所以我设法做到了.但事情就是这样:当我调整浏览器大小并且图像变小时,文本将从图像中移出.
所以我的问题是,我是否必须@media对所有不同的维度使用规则?我怎么知道我的CSS中使用哪些尺寸?
或者我可以做些什么,所以我的文本元素总是留在图像中?
先感谢您.
您可以使用许多不同的选项,每个选项都有其优点和缺点,并且在浏览器支持方面有所不同:
Flexbox是您拥有的最简单的选项,不使用表格或不必将您的元素从文档流中取出,但它不像其他可行选项那样受到广泛支持.我相信它很快就会到来.
码:
/* --- CSS --- */
.background {
height: 10em;
display: flex;
align-items: center;
justify-content: center;
background-size: cover;
background-position: center;
}
.background > h4 {
color: #000;
font-size: 2em;
font-family: sans-serif;
}Run Code Online (Sandbox Code Playgroud)
<!--- HTML --->
<div class = "background" style = "background-image: url(https://images.freecreatives.com/wp-content/uploads/2015/05/HD-Vintage-Photography-Wallpaper.jpg);">
<h4>Hello, world!</h4>
</div>Run Code Online (Sandbox Code Playgroud)
使用此选项时,您必须确保:
(查看说明#2)
码:
/* --- CSS --- */
.background {
height: 10em;
text-align: center;
background-size: cover;
background-position: center;
}
.background > h4 {
color: #000;
font-size: 2em;
margin: 0 auto;
font-family: sans-serif;
line-height: 5em; /* container height / 2 */
}Run Code Online (Sandbox Code Playgroud)
<!--- HTML --->
<div class = "background" style = "background-image: url(https://images.freecreatives.com/wp-content/uploads/2015/05/HD-Vintage-Photography-Wallpaper.jpg);">
<h4>Hello, world!</h4>
</div>Run Code Online (Sandbox Code Playgroud)
这可能是最常用的方法,因为它得到了广泛的支持,但它的缺点是使元素(标题)脱离正常流程.
码:
/* --- CSS --- */
.background {
height: 10em;
position: relative;
background-size: cover;
background-position: center;
}
.background > h4 {
top: 50%;
left: 50%;
margin: 0;
color: #000;
font-size: 2em;
position: absolute;
font-family: sans-serif;
transform: translate(-50%, -50%);
}Run Code Online (Sandbox Code Playgroud)
<!--- HTML --->
<div class = "background" style = "background-image: url(https://images.freecreatives.com/wp-content/uploads/2015/05/HD-Vintage-Photography-Wallpaper.jpg);">
<h4>Hello, world!</h4>
</div>Run Code Online (Sandbox Code Playgroud)
好吧,我可能会被私刑,如果有人发现这一点,但你可以使用display: table容器和display: table-cell标题来利用表的对齐.
您现在可以将标题集中在一起:
text-align: center和vertical-align: middle码:
/* --- CSS --- */
.background {
width: 100%;
height: 10em;
display: table;
background-size: cover;
background-position: center;
}
.background > h4 {
color: #000;
font-size: 2em;
text-align: center;
display: table-cell;
vertical-align: middle;
font-family: sans-serif;
}Run Code Online (Sandbox Code Playgroud)
<!--- HTML --->
<div class = "background" style = "background-image: url(https://images.freecreatives.com/wp-content/uploads/2015/05/HD-Vintage-Photography-Wallpaper.jpg);">
<h4>Hello, world!</h4>
</div>Run Code Online (Sandbox Code Playgroud)
div对图像使用空,因此height必须始终明确定义.line-height到中心的元素垂直地要求line-height是等于到父的高度.在这种情况下,50%家长的高度时,由于该font-size称号的是2x在font-size母公司及其也表示ems.@media你提到的查询,它不应该用于简单的东西,如居中文本,而是用于根据屏幕大小显示/隐藏元素等.px % em font-size@media