CSS Background-size Property - GeeksforGeeks
Có thể bạn quan tâm
The background-size property specifies the size of background images. It accepts values in pixels, percentages, or keywords such as cover (to ensure the image covers the entire element) and contain (to fit the image within the element), enabling flexible and responsive designs.
Syntax
background-size: auto | length | cover | contain | initial | inherit ;The background-size property can be specified using the following syntaxes:
1. Keyword Values:
- auto: Sets the background image to its original size.
- cover: Ensures the image covers the entire element without distortion.
- contain: Fits the image within the element, maintaining its aspect ratio.
2, Single-Value Syntax:
- Set the width (and optionally the height) of the background image using specific length units (e.g., pixels, points, ems).
3. Double-Value Syntax:
- Specify both the width and height of the background image.
- Use percentages, pixels, or auto for each value.
Default Value: Its default value is auto.
Property Values
All the properties are described well with the example below.
Value | Description |
---|---|
auto | Displays the background image in its original size. |
length | Sets the background image’s width and height. If one value is provided, height defaults to “auto”. |
percentage | Sets the background image’s width and height as a percentage of the parent element. If one value is provided, height defaults to “auto”. |
cover | Scales the background image to cover the entire container, even if part of the image is cut off. |
contain | Resizes the background image to ensure it’s fully visible within the container. |
initial | Sets the background image to its default value. |
inherit | Inherits the background image property from the parent element. |
Let’s discuss commonly used CSS background-size property values in detail below:
1. Using auto
It is used to set the background-size property to its default value. It is used to display the background-image to its original size.
Syntax:
background-size: auto;This example illustrates the use of the background-size property whose value is set to auto.
HTML <!DOCTYPE html> <html> <head> <title> background-size Property </title> <style> body{ background-image:url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); background-size:auto; background-repeat:no-repeat; } </style> </head> <!DOCTYPE html> <html> <head> <title> background-size Property </title> <style> body{ background-image:url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); background-size:auto; background-repeat:no-repeat; } </style> </head> <body> <h2>background-size: auto;</h2> </body> </html> <body> <h2>background-size: auto;</h2> </body> </html>Output:
2. Setting a Specific Length
It is used to set the width and height of the background-image. The first value indicates the width, and the second value indicates the height of the background image in terms of px, pt, em, etc. If any value is not given then it is set to auto.
Syntax:
background-size: length;Example: This example illustrates the use of the background-size property whose value is set to a specific length value.
HTML <!DOCTYPE html> <html> <head> <title> background-size Property </title> <style> body{ background-image:url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); background-size:400px450px; background-repeat:no-repeat; } </style> </head> <body> <h2>background-size: length;</h2> </body> </html>Output:
3. Setting a Specific percentage
It is used to set the width and height in terms of percentage as related to the parent element. The first value indicates the width, and the second value indicates the height of the background image. If any value is not given it is set to auto.
Syntax:
background-size: percentage;Example: This example illustrates the use of the background-size property whose value is specified in percentage.
HTML <!DOCTYPE html> <html> <head> <title> background-size Property </title> <style> body{ background-image:url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); background-size:50%; background-repeat:no-repeat; } </style> </head> <body> <h2>background-size: percentage (%);</h2> </body> </html>Output:
4. Setting Up background size to cover
It is used to resize the background image to cover a whole container element.
Syntax:
background-size: cover;Example: This example illustrates the use of the background-size property whose value is set to cover.
HTML <!DOCTYPE html> <html> <head> <title> background-size Property </title> <style> body{ background-image:url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); background-size:cover; background-repeat:no-repeat; } </style> </head> <body> <h2>background-size: cover;</h2> </body> </html>Output:
- initial: It is used to set an element’s CSS property to its default value.
- inherit: It is used to inherit a property to an element from its parent element property value.
Supported Browsers:
The browsers supported by the background-size property are listed below:
- Google Chrome
- Edge
- Firefox
- Opera
- Safari
Note: Most modern browsers, including Chrome, Firefox, Edge, and Safari, fully support the background-size property. However, older versions of Internet Explorer (before version 9) and Opera Mini do not support this property
CSS background-size Property – FAQs
What is the background-size property in CSS?
The background-size property in CSS specifies the size of a background image. It can be used to stretch, scale, or resize an image to fit within its container.
How do I increase background-size in CSS?
To increase background size in CSS, use the background-size property like this: background-size: cover; or specify dimensions such as background-size: 100% 100%;.
Which CSS property specifies the size of the background image?
The CSS property that specifies the size of the background image is background-size.
Can I set custom dimensions for a background image using background-size?
Yes, you can specify custom dimensions using pixel, percentage, or other units. For example: background-size: 200px 100px; sets the image to 200px wide and 100px tall.
Why is my background image blurry when using background-size?
Blurriness occurs if the image is scaled up too much, reducing its resolution. To avoid this, use high-resolution images and consider using background-size: cover; or contain for better scaling results.
M
ManasChhabra2 Follow Improve Previous Article CSS background-repeat Property Next Article CSS border PropertyTừ khóa » Html Background Image Original Size
-
CSS Background-size Property - W3Schools
-
Resizing Background Images With Background-size - CSS
-
Background-size - CSS: Cascading Style Sheets - MDN Web Docs
-
Background-size - CSS-Tricks
-
Background-size - CSS Reference
-
CSS Background Image To Fit Width, Height Should Auto-scale In ...
-
HTML Stretch Background Image
-
Example: Background Image Size - HTML Dog
-
How To Resize An Image In HTML?
-
Background-size · WebPlatform Docs
-
Background Image Original Size Code Example - Code Grepper
-
CSS Background-size Property - Dofactory
-
Truly Perfect Full-Size Background Images On The Web - Zeiss
-
Background Size - Tailwind CSS