Background Size Property

Last updated: 2026-09-13

background-size Background Size Property

background-size controls the dimensions of a background image and was introduced in CSS3. It supports auto (the image's intrinsic size), cover (cover the entire container), contain (show the whole image inside the container), as well as explicit length and percentage values. The property is very useful in responsive design.

Category
Initial Valueauto
InheritedNo
Applies Toall
Animation Typelength
CSS VersionCSS3
StatusStandard

📝 Syntax

background-size: auto | cover | contain | <length> <percentage>;

⚙️ Values

ValueTypeDescription
autoKeywordImage's intrinsic size
cover | containKeywordCover or contain the container
<length> | <percentage>TypeExplicit size values

▶ Example

Edit the code below and see the live result in the playground:

🌐 Browser Support

Browser ChromeChrome EdgeEdge FirefoxFirefox SafariSafari OperaOpera
background-size v4 v12 v4 v4.1 v10
Supported in all major browsers
Tip: cover keeps the aspect ratio and fills the container, possibly cropping some content; contain shows the whole image, possibly leaving empty space.
Important: When using size in the background shorthand, it must follow the position and be separated by a slash, e.g. center/cover.

❓ FAQ

QShould I use background-size: cover or contain?
AUse cover when the image must completely cover the container and cropping is acceptable — the long edge fits the container and the short edge is cropped. Use contain when you need to see the whole image and can accept empty space. cover is common for banners and card backgrounds; contain suits product shots, logos, and anything that must display in full. Both preserve the intrinsic aspect ratio, so the image never distorts.
QIs background-size animatable?
AYes. background-size is an interpolable property, so it animates smoothly in transitions and @keyframes, e.g. scaling from background-size: 100% to 110% on hover for a subtle zoom effect. Frequent changes can trigger repaints, though; for performance-sensitive zoom animations prefer transform: scale().
QHow do I make a background fill the area without stretching it?
AUse background-size: cover;. It scales the image by its intrinsic ratio until the shorter side fills the container and the longer side is cropped, so nothing distorts. If the image ratio differs greatly from the container, add background-position: center to control which part is kept. To avoid cropping entirely, use contain (accepting empty space) or match the container's ratio to the image's.
QHow do single and double values in background-size work?
ATwo values set width and height, e.g. background-size: 300px 200px; if height is auto it scales proportionally. One value, e.g. background-size: 300px, sets the width and the height scales automatically to keep the ratio. cover, contain, and auto are special keywords and cannot be mixed with length values.

📚 Related Tutorials

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏