Add image and text 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.
Читайте также:  Javascript remove this div

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

Читайте также:  Проверить содержит ли строка подстроку php

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

Источник

Place Text Next to Image in HTML

Place Text Next to Image in HTML

  1. Use the float CSS Property to Place the Text Next to an Image in HTML
  2. Use display: inline-block and vertical-align: top to Place the Text Next to an Image in HTML

The article explains the ways to place text beside images by using HTML and CSS.

Use the float CSS Property to Place the Text Next to an Image in HTML

We can use the float CSS property to define how an element can float. An element can float to the right or the left. Some other options are none which means the element will not float and, inherit which, will exhibit its parent’s behavior. We use the float property to specify the positioning and formatting of an element. We can also use the property to place a text next to an image.

We can easily achieve the following style by wrapping both image and text content with a div. The HTML should be structured as below.

div>  div>  img src="url" />  div>  div>  Text content goes here  div>  div> 

Now that the HTML is structured, we can add our CSS as inline, internal, or external. For this example, we will be implementing the styles using inline CSS. First, set the float property to left for the div wrapping the image. Use the URL https://loremflickr.com/320/240 as the image source. Next, write any text of your choice and wrap it with another div .

div>  div style="float: left">  img src="https://loremflickr.com/320/240" />  div>  div>  Text content goes here  div>  div> 

Here, the property float: left is given to the wrapper of the image. The float: left property will place the image at the left, and another wrapper wrapping the text content will be placed just beside the image. In this way, we can use the CSS float property to place a text next to an image.

Читайте также:  The example of application of style attribute in HTML

Use display: inline-block and vertical-align: top to Place the Text Next to an Image in HTML

We can use the display and vertical-align properties to place a text next to an image in HTML. The display defines how an element displays in HTML. We can set an element’s display property as inline , inline-block , block , etc. When we assign display to inline-block , it will make the element an inline element, but we still can set height and width properties to it. Thus, we will be able to place the text beside an image. The vertical-align property defines the vertical alignment of an element. When we use the value top , the element will be aligned to the top of the tallest element on the line.

Here, we can again structure our code as below.

div>  img src="" alt="img"/>  div> div>  p>  Text Here,  p>  div> 

For example, set the display property to inline-block and the vertical-align property to top for the image wrapper div . As for the wrapper, div of the text set the display property to inline-block .

div style="display:inline-block;vertical-align:top;">  img src="https://loremflickr.com/320/240" alt="img"/>  div> div style="display:inline-block;">  p>  Here goes the text content.  p>  div> 

Here, the property display: inline-block sets the property of the wrapper that is wrapping image an inline-block property. The inline-block option does not add a line break beside the element. Therefore, the elements will align next to one another. We again wrap the text wrapper with the display: inline-block property as well. Similar to the previous wrapper, it will place the text content right beside the image.

Ashok is an avid learner and senior software engineer with a keen interest in cyber security. He loves articulating his experience with words to wider audience.

Related Article — HTML Text

Related Article — HTML Image

Copyright © 2023. All right reserved

Источник

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