Background-size - CSS-Tricks
Có thể bạn quan tâm
The background-size property in CSS is one of the most useful — and most complex — of the background properties. There are many variations and different syntaxes you can use for this property, all of which have different use cases. Here’s a basic example:
html { background: url(greatimage.jpg); background-size: 300px 100px; }That’s an example of the two-value syntax for background size. There are four different syntaxes you can use with this property: the keyword syntax, the one-value syntax, the two-value syntax, and the multiple background syntax.
Keywords
In addition to the default value (auto), there are two keywords you can use with background-size: cover and contain
cover 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. contain, on the other hand, says to always show the whole image, even if that leaves a little space to the sides or bottom.
The default keyword — auto — tells the browser to automatically calculate the size based on the actual size of the image and the aspect ratio.
One value
If you only provide one value (e.g. background-size: 400px) it counts for the width, and the height is set to auto. You can use any CSS size units you like, including pixels, percentages, ems, viewport units, etc.
Two values
If you provide two values, the first sets the background image’s width and the second sets the height. Like the single value syntax, you can use whatever measurement units you like.
Multiple images
You can also combine any of the above methods and apply them to multiple images, simply by adding commas between each syntax. Example:
html { background: url(greatimage.jpg), url(wonderfulimage.jpg); background-size: 300px 100px, cover; /* first image is 300x100, second image covers the whole area */ }Keep background image stacking order in mind when using multiple images.
Demo
This demo shows examples of cover, contain, and multiple background images with a mix of pixel and keyword values.
Browser support
IE | Edge | Chrome | Firefox | Safari | Opera |
---|---|---|---|---|---|
9+ | All | All | 3.6+ | All | All |
iOS Safari | Chrome Android | Firefox Android | Android Browser | Opera Mobile |
---|---|---|---|---|
All | All | All | 90+ | All |
More information
Article on Feb 19, 2016A Quick Overview of `object-fit` and `object-position`
Robin Rendle Article on Sep 30, 2022Perfect Full Page Background Image
Chris Coyier Article on Apr 10, 2017Optimizing for Large-Scale Displays
Jon Yablonski Article on Jun 10, 2020Creative Background Patterns Using Gradients, CSS Shapes, and Even Emojis
Preethi Article on Aug 7, 2020More Control Over CSS Borders With background-image
Chris CoyierRelated properties
Almanac on May 20, 2021background
.element { background: url(texture.svg) top center / 200px 200px no-repeat fixed #f8a100; } Sara Cope Almanac on Jun 6, 2017background-attachment
.hero { background-attachment: fixed; } Chris Coyier Almanac on May 20, 2021background-blend-mode
.element { background-blend-mode: screen; } Robin Rendle Almanac on May 20, 2021background-clip
.element { background-clip: padding-box; } Chris Coyier Almanac on Sep 28, 2022background-image
.element { background: url(texture.svg); } Chris Coyier Almanac on May 20, 2021background-origin
.element { background-origin: border-box; } Chris Coyier Almanac on Jun 2, 2021background-position
.element { background-position: 100px 5px; } Chris Coyier Almanac on May 21, 2024background-repeat
.element { background-repeat: repeat-x; } Geoff GrahamTừ khóa » Html Background Image Size Stretch
-
HTML Stretch Background Image
-
CSS Background-size Property - W3Schools
-
How Do I Stretch A Background Image To Cover The Entire HTML ...
-
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
-
How To Stretch And Scale An Image In The Background With CSS
-
Background-size - CSS: Cascading Style Sheets - MDN Web Docs
-
Resizing Background Images With Background-size - CSS
-
How To Stretch And Scale Background Image Using CSS?
-
Stretch Background Image
-
Stretch Background Image To Fit Screen Html Code Example
-
Perfect Full Page Background Image - CSS-Tricks
-
How To Stretch Background Image In Html Without Css?
-
HTML How To Make Background Image Fit Screen - YouTube