Darken image overlay and add text over it in CSS

泄露秘密 提交于 2019-12-20 02:33:18

问题


How would I darken (add a semi-transparent overlay) and add text to this image (but centred horizontally and vertically) as below:

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 here: http://jsfiddle.net/6jf0nxd5/


回答1:


To center the text horizontaly and verticaly, you will need to wrap it in a container with text-align:center;. Then you can use a pseudo element to center it verticaly. For the overlay, you can give the text container a rgba() background color that can have transparency :

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>



回答2:


html

<div id='back'><div id='mask'><div id='text'>fsfsfsssf</div></div></div>

css

body{
    margin:0px;
}
#back{
    width:100%;
    height:500px;
    background: url("http://luxurylaunches.com/wp-content/uploads/2014/05/uber-london.jpg") no-repeat;
    background-size:contain;
}
#mask{
    position:relative;  
    width:100%; 
    height:500px; 
    background:rgba(255,255,255,0.5); 
}
#text{
    position:absolute;
    top:230px;
    left:48%;
}

Fiddle: http://jsfiddle.net/6jf0nxd5/20/




回答3:


Try this

HTML :

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

CSS :

#top {
    width: 100%;
    height: auto;
    opacity:.3;
    position:absolute;
    z-index:0;
}
body {
    margin: 0px;
}
.sometext {
    position:absolute;
    z-index:1;
}



回答4:


http://jsfiddle.net/6jf0nxd5/21/

HTML

<div class='imgWrap'>
    <span>This is some very long text that might or might now flow on top of the image</span>
   <img src="http://luxurylaunches.com/wp-content/uploads/2014/05/uber-london.jpg" id="top" data-appear-animation=fadeIn">
</div>

CSS

.imgWrap{
    display:inline-block;
    background:#000;
    position:relative;
}

.imgWrap > img{ display:block; opacity:.5; }
.imgWrap > span{ position:absolute; display:table; text-align:center; z-index:1; height:100%; left:0; right:0; padding:20px; color:#FFF; font-size:2em; }
.imgWrap > span::after{ content:attr(data-title); display:table-cell; vertical-align: middle; }


来源:https://stackoverflow.com/questions/27148825/darken-image-overlay-and-add-text-over-it-in-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!