html - 使图像覆盖变暗并在 CSS 中在其上添加文本

标签 html css overlay transparency

我将如何变暗(添加半透明覆盖)并向该图像添加文本(但水平和垂直居中),如下所示:

enter image description here

HTML

<img src="http://luxurylaunches.com/wp-content/uploads/2014/05/uber-london.jpg" id="top" data-appear-animation="fadeIn">

CSS

#top {
width: 100%;
height: auto;
}
body {
margin: 0px;
}

在这里 fiddle :http://jsfiddle.net/6jf0nxd5/

最佳答案

要使文本水平和垂直居中,您需要使用 text-align:center; 将其包裹在容器中。然后你可以使用伪元素将其垂直居中。对于叠加层,您可以为文本容器提供一个可以具有透明度的 rgba() 背景色:

DEMO

body {
    margin: 0px;
}
.wrap{
    position:relative;
}
.wrap img{
    width:100%;
    height:auto;
    display:block;
}
.text{
    position:absolute;
    top:0; left:0;
    width:100%; height:100%;
    background:rgba(255,255,255,.5);
    text-align:center;
}
.text:after{
    content:'';
    width:1px; height:100%;
    vertical-align:middle;
    display:inline-block;
}
.text span{
    display:inline-block;
    vertical-align:middle;
}
<div class="wrap">
    <img src="http://luxurylaunches.com/wp-content/uploads/2014/05/uber-london.jpg" id="top" data-appear-animation="fadeIn" />
    <div class="text"><span>Text over the image
        <br/>Second line</span></div>
</div>

关于html - 使图像覆盖变暗并在 CSS 中在其上添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27148825/

相关文章:

php - 基本页脚定位问题

iphone - 不可缩放的 webview,适用于 iPhone 和 iPhone4 分辨率

javascript - 如何覆盖/撤消 Twitter Bootstrap 的 CSS 类属性

javascript - 如何仅在单击时而不是之前加载图片

jquery - SB Admin 2 侧边栏菜单在加载时扩展

html - 难以理解 bootstrap scroll spy

html - 透明 PNG 图像不考虑背景颜色,并且不透明

python - 用scrapy爬表,网站有不正常的html代码。

python - 无法使用 Gtk.Overlay 覆盖 Gstreamer 视频

android - 过度绘制布局允许通过 LinearLayout 进行触摸