CSS Background Size report
Background image, people who have written CSS must know that this property plays a vital role. The CSS background-size is to set the background image size. The picture can keep its original size, stretch to a new dimension, or zoom to the size of the available space of the element while maintaining its original proportions.
The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and height. By doing so, you can scale the image upward or downward as desired.
The background is a CSS shorthand property that defines all background properties at once, including color, image, origin, size, repeat, etc. If you are learning to write CSS, background size is one of the attributes you must master. The following is a simple guide for CSS background size.
CSS background-size Property
It defines the size of the background image. 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:
- The keyword syntax: "auto", "cover" and "contain".
The default keyword "auto" tells the browser to automatically calculate the size based on the actual size of the image and the aspect ratio. "Cover" tells the browser to make sure the image always covers the entire container, even if it has to stretch the picture or cut a little bit off one of the edges. "Contain" means that show the whole image, even if that leaves a little space to the sides or bottom.
- The one-value syntax: It sets the width of the image, and height becomes "auto." You can use any CSS size units you like, including pixels, percentages, ems, viewport units, etc.
- The two-value syntax: The first value is the width, the second value is height.
- The multiple background syntax: You can combine any of the above methods and apply them to multiple background pictures, simply by adding commas between each syntax.
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Browser | Internet Explorer | Firefox | Safari | Opera | |
---|---|---|---|---|---|
Background-size | 4.0 1.0 -webkit- | 9.0 | 4.0 3.6 -moz- | 4.1 3.0 -webkit- | 10.5 |
CSS Formal Syntax
Learning how to read CSS syntax is essential for editing website pages. The following are the descriptions of the CSS syntax you should know.
Property Values | Descriptions |
---|---|
auto | Default value. The background image is displayed in its original size |
length | Sets the width and height of the background image. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto" |
percentage | Sets the width and height of the background image in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto" |
cover | Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges |
contain | Resize the background image to make sure the image is fully visible |
initial | Sets this property to its default value |
inherit | Inherits this property from its parent element |
I hope the above information is helpful for you to further understand CSS. You can find more information worth taking the time to read in related articles.