La background-size
propriété en CSS est l'une des propriétés d'arrière-plan les plus utiles - et les plus complexes. Il existe de nombreuses variantes et différentes syntaxes que vous pouvez utiliser pour cette propriété, qui ont toutes des cas d'utilisation différents. Voici un exemple de base:
html ( background: url(greatimage.jpg.webp); background-size: 300px 100px; )
C'est un exemple de la syntaxe à deux valeurs pour la taille de l'arrière-plan. Vous pouvez utiliser quatre syntaxes différentes avec cette propriété: la syntaxe du mot-clé, la syntaxe à une valeur, la syntaxe à deux valeurs et la syntaxe d'arrière-plan multiple.
Mots clés
En plus de la valeur par défaut ( auto
), vous pouvez utiliser deux mots clés avec background-size
: cover
etcontain
![](7414591/background-size_css-tricks_2.jpg.webp)
![](7414591/background-size_css-tricks_2.jpg.webp%22no%22+translate%3D%22no%22%3Ecover%3C/code%3E+tells+the+browser+to+make+sure+the+image+always+covers+the+entire+container,+even+if+it+has+to+stretch+the+image+or+cut+a+little+bit+off+one+of+the+edges.+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Econtain%3C/code%3E,+on+the+other+hand,+says+to+always+show+the+whole+image,+even+if+that+leaves+a+little+space+to+the+sides+or+bottom.%3C/p%3E%3Cp%3EThe+default+keyword+-+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Eauto%3C/code%3E+-+tells+the+browser+to+automatically+calculate+the+size+based+on+the+actual+size+of+the+image+and+the+aspect+ratio.%3C/p%3E%3Ch3%3EOne+Value%3C/h3%3E%3Cp%3EIf+you+only+provide+one+value+(e.g.+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Ebackground-size:+400px%3C/code%3E)+it+counts+for+the+width,+and+the+height+is+set+to+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Eauto%3C/code%3E.+You+can+use+any+CSS+size+units+you+like,+including+pixels,+percentages,+ems,+viewport+units,+etc.%3C/p%3E%3Ch3%3ETwo+Values%3C/h3%3E%3Cp%3EIf+you+provide+two+values,+the+first+sets+the+background+image%E2%80%99s+width+and+the+second+sets+the+height.+Like+the+single+value+syntax,+you+can+use+whatever+measurement+units+you+like.%3C/p%3E%3Ch3%3EMultiple+Images%3C/h3%3E%3Cp%3EYou+can+also+combine+any+of+the+above+methods+and+apply+them+to+multiple+images,+simply+by+adding+commas+between+each+syntax.+Example:%3C/p%3E%3Cpre+translate%3D%22no%22+rel%3D%22CSS%22%3E%3Ccode+translate%3D%22no%22+class%3D%22language-css%22%3Ehtml+(+background:+url(greatimage.jpg.webp),+url(wonderfulimage.jpg.webp);+background-size:+300px+100px,+cover;+/*+first+image+is+300x100,+second+image+covers+the+whole+area+*/+)%3C/code%3E%3C/pre%3E%3Cp%3EKeep+background+image+stacking+order+in+mind+when+using+multiple+images.%3C/p%3E%3Ch3%3EDemo%3C/h3%3E%3Cp%3EThis+demo+shows+examples+of+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Ecover%3C/code%3E,+%3Ccode+translate%3D%22no%22+translate%3D%22no%22%3Econtain%3C/code%3E,+and+multiple+background+images+with+a+mix+of+pixel+and+keyword+values.%3C/p%3E%3Cp+style%3D%22height:+268px;+display:+flex;+align-items:+center;+justify-content:+center;+border:+2px+solid+black;+margin:+1rem+0;+padding:+1rem;+overflow:+auto;%22+class%3D)
En rapport
- background-attachment
- clip de fond
- Couleur de l'arrière plan
- image de fond
- background-origin
- position de fond
- Répétition du fond
Plus de ressources
- Spécification CSS3
- MDN
Prise en charge du navigateur
Chrome | Safari | Firefox | Opéra | C'EST À DIRE | Android | iOS |
---|---|---|---|---|---|---|
3+ | 4.1+ | 3.6+ | 10+ | 9+ | 2.3+ | 4.0+ |