6

I have a requirement of displaying multiple images in cards and I want to write some text over them. These are random images uploaded by users, so can be of any color. Need the white text on top of them to not be transparent as shown in attached fiddle.

This is an example fiddle - http://jsfiddle.net/7dgpbLd8/1/

This was my solution to add some gray div over image. But, the text should be always white on a gray background. But it is also shadowed here. It would be great to know how to shadow the actual background so text is readable.

Sam
  • 4,302
  • 12
  • 40
  • 74
  • 1
    Please post some code, or an illustration of what you want to do, and what you've tried, or have at the moment. – Lee Mar 27 '15 at 16:14
  • Ok, give me a moment. – Sam Mar 27 '15 at 16:15
  • You won;t be able to do that with just CSS / HTML. You probably need some JavaScript or a server side language to define what colours are used in the image. – putvande Mar 27 '15 at 16:16
  • You can add opacity only to background-color: rgba(255,0,0,0.7); – vitdes Mar 27 '15 at 17:01

8 Answers8

8

Either follow Lee's advice (though I'd recommend adding some padding) or use text-shadow, like so.

div {
  width: 100px;
  height: 100px;
  margin: 20px;
  text-align: center;
  line-height: 100px;
  color: white;
  text-shadow: 0 1px black;
}

.dark {
  background: #333;
}

.light {
  background: #ccc;
}
<div class="dark">Some text</div>
<div class="light">Some text</div>

Or you can ever merge our two approaches.

div {
  width: 100px;
  height: 100px;
  margin: 20px;
  text-align: center;
  line-height: 100px;
}

.dark {
  background: #333;
}

.light {
  background: #ccc;
}

span {
  color: white;
  text-shadow: 0 1px black;
  background: #333;
  background: rgba(0, 0, 0, 0.18);
  padding: 4px 8px;
}
<div class="dark"><span>Some text</span></div>
<div class="light"><span>Some text</span></div>

The problem with your post is that you set the opacity. However, when you lower the opacity, not only does the background change, but also all its content. In other words, the text also has a lower opacity in your fiddle. In my fiddle, presented above, you do not have this problem because you use rgba. RGBA uses the default RGB color representation, but adds an alpha layer component to that (i.e.: opacity). This means that you can add a color that is (semi-)transparent.

It works in the same way as opacity, simply add the value you want for the color (let's say 0.8), and add it after the default rgb values. An example: RGB for white is 255,255,255 and for black 0,0,0. If you want those to have an opacity of 0.8, add 0.8 at the back: rgba(255,255,255,0.8) or rgba(0,0,0,0.8) respectively. By doing this, only the opacity of the background will change, and not that of the text. For an example, see the examples above.

Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
2

I would put the image(s) in a div with a dark background, then lower the opacity of the images themselves, darkening the images so you can read the text. This way you can also darken the image on hover for better readability.

http://jsfiddle.net/3w34k1ea/

    .img-wrapper{
    width: 100%;
    height: 100%;
    background: #000;    
}
     img {
      width: 100%
        height: 100%;
        opacity: .5;
}
     img:hover{
    opacity: .3;
}
     p {
    color: white;
    position: absolute;
    top: 15px;
    left: 15px;
    font-size: 20px;
}
TannerHolm
  • 1,983
  • 1
  • 11
  • 10
  • This is close to what I am trying to do. But how does this work in case of background images? – Sam Mar 27 '15 at 16:40
  • you will need a background to fade the image against, so as long as the div you have with the image as the background is in a larger div with a dark background, it should work just fine. http://jsfiddle.net/3w34k1ea/1/ jsfiddle updated to show opacity on background image of div. – TannerHolm Mar 27 '15 at 17:03
1

I would use text shadow in your position but insteed of one I would experiment with multiples shaodws till reaching the best solution. For example:

text-shadow: 2px 2px 5px rgba(0,0,0,0.8), 0px 0px 2px  rgba(0,0,0,1);

FIDDLE

Alvaro Menéndez
  • 8,766
  • 3
  • 37
  • 57
1

The easiest way and best result at the same time is simply using a semi-transparent overlay, e.g.: https://jsfiddle.net/zmpwunr7

<div class="box">

    <div class="overlay top">
        text
    </div>

    <img ... />

</div>

.box {
    position: relative;
}

.box .overlay {
    background-color: rgba(0, 0, 0, 0.50);
    color: rgb(255, 255, 255);
    position: absolute;
}
    .box .overlay.top {
        top: 0px;
    }
Alex
  • 7,743
  • 1
  • 18
  • 38
0

Put the text inside a <span> tag and give it a class, then in your CSS file:

span.your-class {
    background:rgba(255,255,255,0.5);
    padding:1em; // Adds a nice comfortable spacer between the text and the div edge
}

This will put the text inside a semi-transparent box ontop of the image. Experiment with your text colour, and the background colour until you're happy.

Here's an example: http://jsfiddle.net/9svp8qoh/

Lee
  • 4,187
  • 6
  • 25
  • 71
  • I'd stay away from the MS proprietary filters. Simply use a white background, and override it with an RGBA background for new browsers. Note that you are now using a black default background, and overwrite it with a white rgba one. Be consistent and use two similar ones, i.e. white and white or black and black. Leave out all the filters, as they will also changes the opacity of the text, not only the background. – Bram Vanroy Mar 27 '15 at 16:25
  • Huh? I never put that on there! – Lee Mar 27 '15 at 16:28
  • Please check Fiddle and comment I added. – Sam Mar 27 '15 at 16:49
  • Can you make a sketch of how you would like it to appear? – Lee Mar 27 '15 at 16:50
0

I had this scenario once. I compromised creating span with opacity 0.5 and giving dark background, and then placing the text. If I understood you question correctly this could be a solution for you.

You can add opacity only to background:

rgba(255,0,0,0.5)

Check this post

Community
  • 1
  • 1
vitdes
  • 124
  • 1
  • 12
0

There are some answers here that will help you make the text more readable. But they do not darken the background images which is what you asked for. You could achieve this by using css filters, e.g. the brightness filter:

img {
    filter: brightness(20%);
}

A value of 0 means a completely black image, a higher value will bring you a brighter result. Example: http://codepen.io/anon/pen/OPqRJK

Attention: only Firefox supports at the moment the unprefixed version, IE has no filter support. See http://caniuse.com/#feat=css-filters If you need to support these browser, have a look at the answer from BenSlight. It's basically the same solution.

For further reading: there's a nice article on css-tricks.com explaining all possibilities we have with css filters: https://css-tricks.com/almanac/properties/f/filter/

chaenu
  • 1,915
  • 16
  • 32
  • A couple of things: browser support [isn't good](http://caniuse.com/#feat=css-filters), and as the OP states you cannot know in advance when an image is too light or too dark. With your style, you will even make the light images even brighter. And that's not what you want. – Bram Vanroy Mar 27 '15 at 16:37
  • I updated your fiddle, Sam: https://jsfiddle.net/6c3c3ace/3/ (Child elements will inherit the filter effect, therefore you need the absolut positioning.) @BramVanroy That's why I added the link to caniuse.com. And in my opinon, the OP just asked for a way to darken images and not how to analyze images if they are bright or not. – chaenu Mar 27 '15 at 16:50
-1

you can use background property of css where in you can give color and image path eg :-

background:#FFFFFF url("image path");

This will add background color to image.

  • How is that going to help show white text on a white-ish image? This will not add a background color to an image. – putvande Mar 27 '15 at 16:16