3

I'm trying to get a better grasp on HTML/CSS and very often I see things like background-size: 100px

what does it mean when there is only one parameter given for something that requires a length and width? Does it default to only defining the length and scaling the width according to the image? Or does it default to the width?

1
  • 1
    One-value syntax: the value defines the width of the image, the height is implicitly set to 'auto' MDN Commented May 22, 2014 at 19:04

1 Answer 1

3

If only one size is defined it's handled as width and the height automatically set to auto.

From the Mozilla Developer Network:

/* Keywords syntax */
background-size: cover
background-size: contain

/* One-value syntax: the value defines the width of the image, the height is implicitly set to 'auto' */
background-size: 50%
background-size: 3em
background-size: 12px
background-size: auto

/* Two-value syntax: the first value defines the width of the image, the second its height */
background-size: 50% auto
background-size: 3em 25%
background-size: auto 6px
background-size: auto auto

/* Values for the multiple backgrounds, defined by background-image, may be listed separated by commas */
background-size: auto, auto     /* Do not confuse this with background-size: auto auto */
background-size: 50%, 25%, 25%
background-size: 6px, auto, contain

background-size: inherit
Sign up to request clarification or add additional context in comments.

5 Comments

so say i have background-size: 50% and that targets the width, what does auto target? Does it leave the height as what the original image's height is? or does it scale it down by 50% as well
It holds the aspect ratio of the background image. Note that background-size: 50%; doesn't resize the background image by 50%, but instead sets it to 50% of the width of the container. The height is then calculated as needed to hold the aspect ratio.
so an image that's 100x100 would not be resized to 50x50 with the background-size property right? it would crop it to 50x50?
The percentage isn't of the image size but the container's size. If your container is 200px x 200px and you set background-size: 50%, the background image will be resized (not cropped) to 100 x auto (auto holding the aspect ratio), regardless of what the original image dimensions were.
alright - thank you very much for clarifying! I appreciate all the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.