Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create responsive text on top of an image?

Tags:

html

css

I'm really not sure how to pose this question any other way, but I'm trying to load text on top of an image - which appears to be a tricky task in itself, but I've got it going using this tutorial. Unfortunately, the tutorial is slightly out of date and I can't figure out a way to dynamically change both the font size and the span size for mobile and still maintain the text in the correct place on top of the image.

When the window is resized the text and the box doesn't resize properly (it overflows outside of the image).

I've tried percentage sizing as well as other techniques with little luck. The CSS I'm using to display the text over the image with a background can be seen below.

What's the best practice for overlaying text on an image and how would one go about making it responsive? This is what I'm trying to use for desktop browsers right now:

.herotext span {
  position:     absolute;
  top:          80%;
  font-family:  'museo_slab500';
  font-size:    150%;
  color:        #fff;
  padding-left: 20px;
  width:        40%;
  line-height:  35px;
  background:   rgb(0, 0, 0);
  background:   rgba(0, 0, 0, 0.7);
}

Does anyone have some advice on how to handle this properly these days? The article I mention above is from 2009 and I suspect it's not the best way to overlay text.

like image 202
Herp Avatar asked Jul 02 '13 01:07

Herp


People also ask

How do I add text overlay to an image?

On the Insert tab, in the Text group, click Text Box, drag to draw a text box anywhere near the picture, and then type your text. To change the font or style of the text, highlight the text, right-click it, and then select the text formatting you want on the shortcut menu.

How do I put text on top of an image in HTML?

CSS position property is used to set the position of text over an image. This can be done by enclosing the image and text in an HTML “div”. Then make the position of div “relative” and that of text “absolute”.

What is overlay text on image?

Image overlay is the technique of adding text or images over another base image. One of the simplest ways to add image or text overlay is using CSS properties and pseudo-elements.

What is an overlay text?

What is text overlay? Text overlay or video overlay refers to adding a text element over a video file so that users see text appear on their screen while watching a video. Text overlay is one of the most important components of social video, a growing trend in social media content.


1 Answers

Here are the changes I would make:

  • Position the span using bottom rather than top, so you always have a specific margin between the span and the bottom of the image.
  • Use a percentage-based line-height so that it will change proportionally to the font-size
  • Add some padding to the right of the span, so the text doesn't bump right up on the edge of the span

Updated CSS:

.herotext span {
    position:    absolute;
    bottom:      20px;
    font-family: 'museo_slab500';
    font-size:   150%;
    color:       #fff;
    padding:     0 20px;
    width:       40%;
    line-height: 150%;
    background:  rgb(0, 0, 0);
    background:  rgba(0, 0, 0, 0.7);
}
like image 73
Rob Kovacs Avatar answered Oct 13 '22 01:10

Rob Kovacs