Putting text on an image css

CSS: Placing text over an inline image

CSS is a very powerful tool that works best when kept as simple as possible. Unfortunately there are a lot of WYSIWYG applications and other HTML editors that produce CSS that is anything but simple.

A common request is to be able to display HTML text over an image that’s already embedded on the page. There are a number of valid solutions using either tables or CSS.

Using a TABLE to overlay text on an image

The HTML solution has been possible since Netscape 3 and is fairly simple to implement, but not so flexible as more recent options.

Читайте также:  Python по марку лутцу

(text to appear over the image goes here)

The HTML approach is to set up a TABLE with a background image. Then a single TD is used to display the text. The width and height of the TABLE are set to match that of the background image. This is a valid approach according to HTML standards, but not so good if you look at the resulting code. Why should you have to use a TABLE to handle this kind of layout? Perhaps CSS has a cleaner solution.

Many years ago this was a novel approach, but these days it’s very much discouraged.

Using CSS to overlay text on an image

Indeed there is a simpler, more flexible solution. Instead of a TABLE we use a DIV and CSS positional attributes to place futher text and images with relation to that DIV.

As you can see, we are able to place any number of sub-elements within the surrounding div. In this case a div containing paragraphs with text (bottom left), and a paragraph by itself (top right).

The CSS approach is to use one DIV to define the area where the background image appears. This DIV has it’s position attribute set to relative in order that the items it contains can be properly placed.

This is done by setting the position attribute of contained items to absolute and placing them by setting one or more of the positional attributes: top, right, bottom and left.

This block has been positioned at the top, right

The position attribute may seem complicated at first, but it’s the key to implementing complicated layouts without excessive amounts of code. Remember that you need to have a container element with position: relative in order to place inner elements using position: absolute.

Without a container element all positioning will be in relation to the viewport (the corner of the screen). This in combination with position: fixed can create elements that stay in one corner of the screen and don’t move when the page scrolls, but only in browsers other than Explorer. On this site you might see a [top] link doing just that if you’re using a compatible browser.

Separating styles from content

Normally you will want to have your CSS in a separate file or at least it’s own style block rather than inline with the HTML. This can be easily achieved by assigning CSS classes to the various container elements:

The result is idential to the previous example, but now much easier to maintain as the HTML and CSS can be edited independently.

Showing text over an image on hover

Sometimes you only want text to appear when the mouse is over the image or the image has focus. One nice way of achieving this is to wrap the image in a link and use the title attribute to store the caption.

In the example below you can see this in action. We’ve set the outer link to position: relative and used its title attribute attr(title) to create some generated content and place it over the image.

The generated content is initially hidden opacity: 0 and then exposed using the hover/focus state and a CSS animation fader.

The slow fade may still not work in some browsers as they don’t support transition effects on generated content. It does fade in in Firefox 23.0.

The attr method in CSS is extremely useful as it allows you to embed data into HTML elements for use in generated content. In this case we’re using the title attribute, but you can also create your own custom attributes. In that case the custom is to name them data-* (e.g. data-hovertext).

Source code for this example:

You could also place HTML text over the images as in the earlier examples and have it show/hide the same way, but that’s not nearly as cool.

References

Источник

How to Create an Image with Transparent Text with CSS

In this snippet, learn about the methods used to overlay text on images. For an ideal combination, the image should be dark, and the text light.

How to create an image with transparent text

This is not so difficult as it seems. It is quite simple, and you can achieve your desired result by using only CSS.

For having a simple, transparent text on an image, you need to have your image or background image in a element, and the content block in another .

Then, use the CSS opacity property to set a transparent image or text. The opacity property is used to adjust the transparency of a text or photo. The value of opacity can be between 0.0 to 1.0, where the low value demonstrates high transparency and the high value shows low transparency. The amount of the opacity is counted as Opacity% = Opacity * 100.

Or instead of the opacity property, you can just set an RGBA color value. See an example in action.

Example of creating an image with transparent text:

html> html> head> title>Title of the document title> style> * < box-sizing: border-box; > .parent < position: relative; max-width: 800px; margin: 0 auto; > .parent img < vertical-align: middle; > .parent .text < position: absolute; bottom: 0; background: rgb(0, 0, 0);/* Fallback color */ background: rgba(0, 0, 0, 0.5);/* Black background with opacity */ color: #ffffff; width: 100%; padding: 20px; > style> head> body> h2>Transparent Text on an Image h2> div class="parent"> img src="https://www.w3docs.com/uploads/media/default/0001/03/faa2b10a44e1d88ddafbf7ab6002ce24659529d4.jpeg" alt="Notebook" style="width:100%;"> div class="text"> h2>Title h2> p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. p> div> div> body> html>

Result

Notebook

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Let’s see another example where the text is set on a background image which fills the entire screen.

Example of adding a text on the background image:

html> html> head> title>Title of the document title> style> body < font-family: 'Source Sans Pro', sans-serif; > header < background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(https://w3docs.com/uploads/media/default/0001/01/e4ecd8f48ea6eac1f966ccb81cd388241d54c948.jpeg); background-size: cover; height: 100vh; > .container < position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; text-align: center; > h1 < text-transform: uppercase; margin: 0; font-size: 3rem; white-space: nowrap; > style> head> body> header> div class="container"> h1>Cakes and Fruits h1> div> header> body> html>

How to position a text over an image

To position the text over an image and define its place on the image, you need to apply the CSS position property set to its «absolute» value for the text blocks and the «relative» value for the parent element. Then, use the CSS top, bottom left, and right properties to define the position of an element in combination with the position property. And for centering the text block, apply the transform property like this: transform: translate(-50%, -50%) .

Example of positioning the text over an image:

html> html> head> title>Title of the document title> style> .container < position: relative; text-align: center; color: #000000; font-size: 30px; > .bottom-left < position: absolute; bottom: 8px; left: 16px; > .top-left < position: absolute; top: 8px; left: 16px; > .top-right < position: absolute; top: 8px; right: 16px; > .bottom-right < position: absolute; bottom: 8px; right: 16px; > .centered < position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); > style> head> body> h2>Positionet Text over an Image h2> div class="container"> img src="https://www.w3docs.com/uploads/media/default/0001/03/faa2b10a44e1d88ddafbf7ab6002ce24659529d4.jpeg" alt="Snow" style="width:100%;"> div class="bottom-left">Bottom Left div> div class="top-left">Top Left div> div class="top-right">Top Right div> div class="bottom-right">Bottom Right div> div class="centered">Center div> div> body> html>

How to add text blocks over an image

Here, let’s see how to create text in a box over images. In a reliable and straightforward way, a transparent text block can be added to an image. Whip up on some white text a slightly transparent black rectangle. If the overlay is sufficiently opaque, you can have almost any image below, and the text will still be completely readable.

As we have already learned how to make and position text blocks over an image above, now we need to use the CSS background property to add a background style and color to our text block.

Example of adding a dark text block over an image:

html> html> head> title>Image with text block title> style> .parent < position: relative; > .text-block < position: absolute; bottom: 50px; left: 20px; background-color: #000; opacity: .6; color: white; padding-left: 20px; padding-right: 20px; > .parent img < width: 100%; > style> head> body> h2>Text Block on an Image h2> div class="parent"> img src="https://www.w3docs.com/uploads/media/default/0001/03/faa2b10a44e1d88ddafbf7ab6002ce24659529d4.jpeg" alt="Books"/> div class="text-block"> h3>Books h3> p>Do you like reading books? p> div> div> body> html>

Here, see another example with dark and light text blocks.

Example of adding dark and light text blocks over an image:

html> html> head> title>Image with text block title> style> .block < margin: 10px; width: 230px; height: 150px; float: left; background: url(https://w3docs.com/uploads/media/default/0001/03/b847dafa71d0dc9d81db3ddda418e12934a5aa88.png); background-size: cover; position: relative; > h2 < position: absolute; bottom: 10px; left: 10px; background: rgba(0, 0, 0, 0.75); padding: 4px 8px; color: white; margin: 0; font: 14px Sans-Serif; > .light h2 < background: rgba(255, 255, 255, 0.75); color: black; > style> head> body> div class="block"> h2>Flying Birds h2> div> div class="block light"> h2>Flying Birds h2> div> body> html>

How to add blur effect to the text block

The blurring part of the underlying image is an amazingly great way to make an overlaid text easy to read.

For having a blur effect, you need to set the filter property with the «blur» value ( filter: blur ) for your text block.

Example of adding a blur effect (4px) to the text block:

html> html> head> title>Blur Effect on Text Block title> style> * < box-sizing: border-box; > .text-block < background: url(https://w3docs.com/uploads/media/default/0001/03/0a71a511b3381286ced8e2fc7f331296cd96649f.png); background-attachment: fixed; width: 300px; height: 200px; position: relative; overflow: hidden; margin: 20px; > .text-block > header < position: absolute; bottom: 0; left: 0; width: 100%; padding: 20px 10px; background: inherit; background-attachment: fixed; overflow: hidden; > .text-block > header::before < content: ""; position: absolute; top: -20px; left: 0; width: 200%; height: 200%; background: inherit; background-attachment: fixed; -webkit-filter: blur(4px); filter: blur(4px); > .text-block > header::after < content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.25) > .text-block > header > h2 < margin: 0; color: white; position: relative; z-index: 1; > style> head> body> h1>Blur Effect on Text Block h1> div class="text-block"> header> h2> Blur Effect h2> header> div> body> html>

You can add the blur effect more to make your text easier to read.

Example of adding a blur effect (9px) to the text block:

html> html> head> title>Blur Effect on Text Block title> style> * < box-sizing: border-box; > .text-block < background: url(https://w3docs.com/uploads/media/default/0001/03/0a71a511b3381286ced8e2fc7f331296cd96649f.png); background-attachment: fixed; width: 300px; height: 200px; position: relative; overflow: hidden; margin: 20px; > .text-block > header < position: absolute; bottom: 0; left: 0; width: 100%; padding: 20px 10px; background: inherit; background-attachment: fixed; > .text-block > header::before < content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: inherit; background-attachment: fixed; -webkit-filter: blur(9px); filter: blur(9px); transform: scale(2) translateY(20px); > .text-block > header > h2 < margin: 0; color: white; position: relative; z-index: 1; > style> head> body> h1>Blur Effect on Text Block h1> div class="text-block"> header> h2> Blur Effect h2> header> div> body> html>

Источник

Оцените статью