Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (2024)

Dans ce didacticiel, nous allons passer en revue la technique la plus simple pour étirer complètement une image d'arrière-plan afin de couvrir l'intégralité de la fenêtre d'affichage du navigateur. Nous utiliserons le CSStaille d'arrière-planpropriété pour y arriver; pas besoin de JavaScript.Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (1) Voir la démo Télécharger la source depuis GitHub

Exemples d'images d'arrière-plan complètes responsives

Avoir une grande photo qui couvre tout l'arrière-plan d'une page Web est actuellement très populaire.

Voici quelques sites Web qui ont des images d'arrière-plan complètes réactives :Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (2) Collectif Voile Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (3) Télépathie numérique Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (4) Marianne RestaurantSi vous souhaitez obtenir un look similaire dans votre prochain projet de conception Web, vous êtes au bon endroit.

Concepts de base pour créer des images d'arrière-plan réactives avec CSS

C'est notre plan de match.

Utiliser la propriété background-size pour couvrir toute la fenêtre

Le CSStaille d'arrière-planpropriété peut avoir la valeur decouverture. Lecouvertureindique au navigateur de redimensionner automatiquement et proportionnellement la largeur et la hauteur de l'image d'arrière-plan afin qu'elles soient toujours égales ou supérieures à la largeur/hauteur de la fenêtre d'affichage.

Utiliser une requête multimédia pour diffuser une image d'arrière-plan plus petite pour les appareils mobiles

Pour améliorer la vitesse de chargement de la page sur les petit* écrans, nous utiliserons une requête multimédia pour servir une version réduite du fichier d'image d'arrière-plan.Ceci est facultatif.

La technique fonctionnera sans cela.

Mais pourquoi est-il judicieux de diffuser une image d'arrière-plan plus petite pour les appareils mobiles ? L'image que j'ai utilisée dans la démo est d'environ 5500x3600px. Cette dimension nous couvrira sur la plupart des écrans d'ordinateur à écran large actuellement vendus sur le marché, mais au prix d'un fichier de 1,7 Mo.

Cette énorme charge utile juste pour une photo d'arrière-plan n'est jamais une bonne chose dans n'importe quelle situation, mais c'est exceptionnellement mauvais sur les connexions Internet mobiles.

Et aussi, la dimension de l'image est excessive sur les appareils à petit écran (plus à ce sujet plus loin dans ce tutoriel).

Passons en revue le processus.

HTML

C'est tout ce dont vous avez besoin pour le balisage :

  ...Votre contenu va ici... 
Comment créer une image d'arrière-plan complète réactive à l'aide de CSS [Tutoriel] (2024)

FAQs

How do I fix text on an image in CSS? ›

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”. The absolute elements are positioned relative to their parent (div).

How to fit image to screen HTML CSS? ›

The CSS object-fit property is used to specify how an <img> or <video> should be resized to fit its container. This property tells the content to fill the container in a variety of ways; such as "preserve that aspect ratio" or "stretch up and take up as much space as possible".

How do you pass an image in CSS? ›

CSS has a variety of different properties that can reference an image file, displaying that file on a web page normally as part of an element's background. This is done using the CSS image syntax, which is url().

How to set image in CSS HTML? ›

CSS styles choose image sources using the background image property.
  1. Open your website's stylesheet with your HTML editor or a text editor.
  2. Paste the following code into the sheet to create a new style: styledimg { ...
  3. Replace "path" with the image's URL within the site.

How do I space an image between text in CSS? ›

There are several easy ways to add space between images using CSS. However, the simplest approach is to use the margin property. The margin property lets us create a space around the HTML elements. The margin property is actually a shorthand of the margin-top, margin-right, margin-bottom, and margin-left properties.

How do I hover an image with text in CSS? ›

Use the <figcaption> for the text of the overlay. Set display: flex , flex-direction: column and justify-content: center to center the text into the image. Use the :hover pseudo-selector to update the opacity and transform of all the elements and display the overlay.

How do I auto fit an image in HTML? ›

The max-height property sets the maximum height of an element, and the max-width property sets the maximum width of an element. To resize an image proportionally, set either the height or width to "100%", but not both. If you set both to "100%", the image will be stretched.

How to set all images same size in HTML CSS? ›

Responsive equal height images with CSS
  1. Put all of your images inside a container div.
  2. Set display: flex; on the container div.
  3. Wrap each image in a div.
  4. Set the flex property of each image's wrapper div to the image's aspect ratio (its width divided by its height)

How do I align an image size in CSS? ›

Here's how:
  1. In your HTML file, locate the image you want to center.
  2. Set the width of the image to a fixed length value, i.e <img width="400">.
  3. Wrap the image in a div element.
  4. In your CSS code, find your div selector.
  5. Inside the curly brackets, set the display property of the div to flex.
Jun 14, 2023

How do I make background image fit all screen sizes in CSS? ›

It is possible to set the CSS background-size property to cover. Using this value, the browser will automatically and proportionally scale the background image's width and height so that they are either equal to or greater than the view port's width and height.

How do I make a background image responsive in CSS? ›

Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.

How do I make my background fit my screen CSS? ›

The magic happens with the background-size property: background-size: cover; cover tells the browser to make sure the image always covers the entire container, in this case html . The browser will cover the container even if it has to stretch the image or cut a little bit off the edges.

How do I fix text position in CSS? ›

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.

How do I fix text align in CSS? ›

To center text in CSS, use the text-align property and define it with the value 'center.' You can use this technique inside block elements, such as divs.

How do you adjust text in CSS? ›

Setting the text size with pixels gives you full control over the text size:
  1. h1 { font-size: 40px; } h2 { font-size: 30px; } p { font-size: 14px; ...
  2. h1 { font-size: 2.5em; /* 40px/16=2.5em */ } h2 { font-size: 1.875em; /* 30px/16=1.875em */ } p { ...
  3. body { font-size: 100%; } h1 { font-size: 2.5em; } h2 {

How do I put text on left and image on right in CSS? ›

The float property is used for positioning and formatting content e.g. let an image float left to the text in a container. The float property can have one of the following values: left - The element floats to the left of its container. right - The element floats to the right of its container.

References

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5962

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.