将文本定位在图像上(html,css)

1 html css centering

所以,我有一个图像,我想在它的中心显示一些文字.

码:

.img {
    position: relative;
}

.img h3 {
    position: absolute;
    width: 100%
    top: 85px;
    text-align: center;
}
Run Code Online (Sandbox Code Playgroud)

好的,所以我设法做到了.但事情就是这样:当我调整浏览器大小并且图像变小时,文本将从图像中移出.

所以我的问题是,我是否必须@media对所有不同的维度使用规则?我怎么知道我的CSS中使用哪些尺寸?

或者我可以做些什么,所以我的文本元素总是留在图像中?

先感谢您.

Ang*_*tis 6

您可以使用许多不同的选项,每个选项都有其优点和缺点,并且在浏览器支持方面有所不同:

1. Flexbox的: (支持)

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号线高度: (支持)

使用此选项时,您必须确保:

  • 标题的行高度相等的容器的高度,
  • 标题是一个单行.

(查看说明#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)


3.位置是:绝对和变换: (支持)

这可能是最常用的方法,因为它得到了广泛的支持,但它的缺点是使元素(标题)脱离正常流程.

码:

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


4.表 (支持)

好吧,我可能会被私刑,如果有人发现这一点,但你可以使用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)


笔记:

  1. 由于我们div对图像使用空,因此height必须始终明确定义.
  2. 使用line-height到中心的元素垂直地要求line-height等于到父的高度.在这种情况下,50%家长的高度时,由于该font-size称号的是2xfont-size母公司及其也表示ems.
  3. 关于@media你提到的查询,它不应该用于简单的东西,如居中文本,而是用于根据屏幕大小显示/隐藏元素等.
  4. 如果您关心网站的可移植性到较小的屏幕,我的建议是避免使用(像素),而是使用(百分比)将根据屏幕或(ems)通过使用查询手动更新容器来更新.px % em font-size@media