Adding image and text to html

Images and text overlays in HTML/CSS

Here is a very simple tutorial to design image overlays in HTML and CSS only.

Indeed, who has never wanted to quickly position a text over an image? Then change it on mouse hover or even display an alternate image?

The code provided in this article may apply to any website. Simply integrate the HTML code on a page and the CSS code into your stylesheet.

You can find different ways to do it on the internet, but I wanted to synthesize everything I found to extract a minimal and above all clean solution.

What is an image text overlay?

For me, an overlay is just a layer put on top of another. In HTML, it means that it is out of the flow, because it has to be positionned over another element, which is not possible with the default behaviour.

Note: I’m not talking here about text over a background image, which is standard in HTML, but text over an image element (the infamous img tag).

Simple text overlay with no hover

Alt text

Overlay with background and text change on hover

Texte alternatif

Overlay with text and image change on hover

Texte alternatif

Texte alternatif hover

HTML code: original content and overlay

Content structure

The overlay-image class container encapsulates all the necessary structure:

  • original image,
  • original overlay text block,
  • image/text overlay block displayed on mouse hover.

On a page written without a page builder (HTML page or simple WordPress theme), for instance like this article, it is essential to define it in order to properly include original content and overlay within the page.

With a builder, it can be replaced by an existing container block (for example it could be the Divi “code” module, to which we simply add the overlay-image class).

The code below also includes encapsulating link tags to make images clickable, but you can remove them if not needed.

The HTML code

CSS code: text overlay and text/image change on hover

The code below is to be included in your styles definition:

  • style.css file included in your HTML file if you work directly in HTML,
  • style.css file of your child theme if you work with WordPress,
  • or your Divi page options if the mechanism is only used at a specific location.

How to display the original or simple text overlay?

The overlay-image class allows you to set the global container when not using a page builder. In this article, I replaced the “width: 100%;” by “width: 80% margin: auto;” to add margins.

Important note: The image-overlay container MUST be “positioned” itself (here relative) in order to position its own elements.

Original image and text overlay are defined by .overlay-image .image and .overlay-image .text specifiers:

  • the image fills all available space,
  • the text is centered on the image using an absolute “positioning” on the container block and a X-Y translation (a CSS classic).

How to display the new overlay on hover?

The hover overlay is materialized by .overlay-image .hover, and “positioned” in absolute to occupy the entire surface of the container including original image and text (100% width and height).

When the mouse hovers, its opacity changes from 0 to 1 with a smooth transition to make it appear over the original div.

To overlay a text block only (without image), you must first hide the original text because the new image is not there to do so.

The CSS code

I tried to factorize the code to make it simple, readable, compact and easily usable. To do so, I divided it into three parts that you can pick according to your needs.

The first part is common to all three kinds of previous overlays and this is the only one needed for a simple text overlay:

/********* Simple or original overlay *******/ /* Main container */ .overlay-image < position: relative; width: 100%; >/* Original image */ .overlay-image .image < display: block; width: 100%; height: auto; >/* Original text overlay */ .overlay-image .text

This part is needed by both overlays on hover:

/********* Overlay on hover *******/ /* New overlay on hover */ .overlay-image .hover < position: absolute; top: 0; height: 100%; width: 100%; opacity: 0; transition: .5s ease; >/* New overlay appearance on hover */ .overlay-image:hover .hover

This last part in only needed for background + text overlay on hover:

/********* Background and text only overlay on hover *******/ .overlay-image .normal < transition: .5s ease; >.overlay-image:hover .normal < opacity: 0; >.overlay-image .hover

Conclusion

As you can see, overlays are not technically very difficult… It’s a shame that the mechanism is not part of all WordPress themes on the marketplace!

46 commentaires sur cet article

Thank you SO MUCH for providing this tutorial. I have been searching for hours and all of the other tutorials I found made it way too complicated. Yours is straightforward and very helpful.

You’re welcome, glad it could help!

Hello, Can anybody help me to change font-color to black on mouseover? Thank you!

Hi Lidiya, You can try this: .overlay-image .hover .text

Thank you very much for this really simple and super useful code! I used it, with a lot of ccs modifications, in many of my Joomla sites

Awesome! I’m glad it was so usefull 🙂

Thank you so much for this tutorial. How to put background color on image and on hover to image background color should be removed Thanks!!

Hi, You can add a background color with transparency on the “normal” overlay-image, with opacity 1, and set opacity to 0 on the “hover” overlay-image.

Hi! Thank you so much for this tutorial. 🙂 I was just wondering – would this be possible to accomplish using only HTML? Thanks!

Hi Andy, Do you mean without CSS? If so, the answer is no. But you can inline the CSS in HTML tags if you don’t want external CSS (using style=”…” instead of classes). Hope it can help!

How can I use that with Owl Carousele plugin? I need 4 divs, with inside foto+text in each slide.

Hi Den, Honestly I don’t use Owl Caroussel so I can’t tell you precisely. But if you can insert HTML in slides, you can try and use this code with the CSS in your style.css.

Excellent tutorial. Thanks for share your knowledge!

Thank you for your message 🙂

Thanks for this useful content. It’s always a pleasure to read your great posts filled with tips really!

You’re welcome, and thx for your message!

Thank you SO MUCH! I have been looking for several days for something that lays out the whole idea so simply and clearly! This is such a helpful tutorial.

You’re welcome, glad I could help.

Diego Alvarez February 6, 2019

Источник

For example, the HTML heading elements ( through ) are block-level elements that automatically place their content onto a new line and push any content that follows onto a new line. Each of these six heading elements represent a different heading size.

Let’s study how this works in practice. At the bottom of your index.html file, try pasting in the highlighted code snippet:

strong>My strong textstrong> br> em>My emphasized textem> h1>Heading 1h1> h2>Heading 2h2> h3>Heading 3h3> h4>Heading 4h4> h5>Heading 5h5> h6>Heading 6h6> 

Save your file and reload it in the browser. You should receive something like this:

HTML Headings

Let’s now inspect the block-level heading elements to study how they differ from the inline-level text elements above. Open up the Firefox Web Developer Inspector and hover over each of the elements to inspect their occupied space as indicated by the blue highlighting. You should be able to confirm that the occupied horizontal space of each of the inline-level elements is determined by its text content, while the occupied horizontal space of each of the block-level elements stretches across the entire web page:

Block-level elements will always push inline-level elements down to the next line, even if you write those HTML elements on the same line of the HTML document. To confirm this for yourself, try writing a block-level element and an inline-level element on the same line of your index.html file. Erase the existing content from your file and add the following code snippet:

strong>My strong textstrong>h1>My headingh1>em>My emphasized texta> 

Can you guess how this HTML code will render in the browser? Save your file and reload it in the browser. You should receive something like this:

Inline and block level elements

Notice that the heading element has started on a new line and pushed the subsequent text element to a new line even though the elements were written on the same line in the HTML document.

You should now have an understanding of how inline-level and block-level elements are positioned and how they affect the position of nearby elements. Remembering their distinct behaviors can be useful when arranging HTML elements in the future.

We’ll continue learning about new inline and block elements in the tutorials ahead.

// Tutorial //

How To Nest HTML Elements

This tutorial will teach you how to nest HTML elements in order to apply multiple HTML tags to a single piece of content.

HTML elements can be nested, meaning that one element can be placed inside another element. Nesting allows you to apply multiple HTML tags to a single piece of content. For example, try pasting the following code snippet inside your index.html file:

strong>My bold text and em>my bold and emphasized textem>strong> 

Save your file and reload it in the browser. (For instructions on creating an index.html file, please see our tutorial here or for loading the file in your browser, see our tutorial here.) You should receive something like this:

My bold text and my bold and emphasized text

Nesting Best Practices

Note that it is recommended to always close nested tags in the reverse order that they were opened.
For example, in the example below, the tag closes first as it was the last tag to open. The tag closes last as it was the first to open.

This sentence contains HTML elements that are strong>em>nested according to best practicesem>strong>. 

As a counter example, the following HTML code contains tags that are not nested according to best practices, as the tag closes before the tag:

This sentence contains HTML elements that are strong>em>not nested according to best practicesstrong>em>. 

While not technically necessary for rendering your HTML in the browser, nesting your tags in the proper order can help improve the readability of your HTML code for you or other developers.

// Tutorial //

How To Use HTML Attributes

HTML attributes can be used to change the color, size, and other features of HTML elements. For example, you can use an attribute to change the color or size of a font for a text element or the width and height for an image element. In this tutorial, we’ll learn how to use HTML attributes to set values for the size and color properties of HTML text elements.

An HTML attribute is placed in the opening tag like this:

element attribute="property:value;"> 

One common attribute is style , which allows you to add style properties to an HTML element. While it’s more common to use a separate stylesheet to determine the style of an HTML document, we will use the style attribute in our HTML document for this tutorial.

Using the Style Attribute

We can change multiple properties of an element with the style attribute. Clear your “index.html” file and paste the code below into your browser. (If you have not been following the tutorial series, you can review instructions for setting up an index.html file in our tutorial Setting Up Your HTML Project.

h1 style="font-size:40px;color:green;">This text is 40 pixels and green.h1> 

Before we load the file in our browser, let’s review each of the parts of this HTML element:

  • h1 is the name of the tag. It refers to the largest-sized Heading element.
  • style is the attribute. This attribute can take a variety of different properties.
  • font-size is the first property we’re setting for the style attribute.
  • 40px; is the value for the property font-size , which sets the text content of the element to 40 pixels.
  • color is the second property we’re setting for the style attribute.
  • green is the value for the property color , which sets the text content color to green

Note that the properties and values are contained by quotation marks, and that each property:value pair ends with a semicolon ; .

Save the file and reload it in your browser. (For instructions on loading the file in your browser, see our tutorial here.) You should receive something like this:

This text is 40 pixels and green.

Источник

Читайте также:  Python интерпретатор для pycharm
Оцените статью