Background size on both top and bottom css năm 2024

The background-position property in CSS allows you to move a background image (or gradient) around within its container.

html {
  background-position: 100px 5px; 
}

It has three different types of values:

  • Length values (e.g. 100px 5px)
  • Percentages (e.g. 100% 5%)
  • Keywords (e.g. top right)

The default values are 0 0. This places your background image at the top-left of the container.

Length values are pretty simple: the first value is the horizontal position, second value is the vertical position. So 100px 5px will move the image 100px to the right and five pixels down. You can set length values in px,

.three-values {
  background-position: right 45px bottom;
}

0, or any of the other CSS length values.

Percentages work a little differently. Get your math hats out: moving a background image by X% means it will align the X% point in the image to the X% point in the container. For example,

.three-values {
  background-position: right 45px bottom;
}

1 means it will align the middle of the image with the middle of the container.

.three-values {
  background-position: right 45px bottom;
}

2 means it will align the last pixel of the image with the last pixel of the container, and so on.

Keywords are just shortcuts for percentages. It’s easier to remember and write top right than

.three-values {
  background-position: right 45px bottom;
}

4, and that’s why keywords are a thing. Here is a list of all five keywords and their equivalent values:

  • .three-values { background-position: right 45px bottom; }

    5: 0% vertically
  • .three-values { background-position: right 45px bottom; }

    6: 100% horizontally
  • .three-values { background-position: right 45px bottom; }

    7: 100% vertically
  • .three-values { background-position: right 45px bottom; }

    8: 0% horizontally
  • .three-values { background-position: right 45px bottom; }

    9: 50% horizontally if horizontal isn’t already defined. If it is then this is applied vertically.

It’s interesting to note that it doesn’t matter what order you use for the keywords:

.four-values {
  background-position: right 45px bottom 20px;
}

0 is the same as

.four-values {
  background-position: right 45px bottom 20px;
}

1. You can only do this if you’re exclusively using keywords, though.

.four-values {
  background-position: right 45px bottom 20px;
}

2 is not the same as

.four-values {
  background-position: right 45px bottom 20px;
}

3.

Demo

This demo shows examples of background-position set with length units, percentages, and keywords.

Declaring values

You can give background-position up to four values in modern browsers:

  • If you declare one value, that value is the horizontal offset. The browser sets the vertical offset to

    .three-values { background-position: right 45px bottom; }

    9.
  • When you declare two values, the first value is the horizontal offset and the second value is the vertical offset.

Things get a little trickier when you start using three or four values, but you also get more control over your background placement. A three- or four-value syntax alternates between keywords and length or percentage units. You can use any of the keyword values except

.three-values {
  background-position: right 45px bottom;
}

9 in a three- or four-value background-position declaration.

When you specify three values, the browser interpets the “missing” fourth value as 0. Here’s an example of a three-value background-position:

.three-values {
  background-position: right 45px bottom;
}

This positions the background image `background-position`0 from the right and `background-position`1 from the bottom of the container.

Here’s an example of a four-value background-position:

.four-values {
  background-position: right 45px bottom 20px;
}

This puts the background image `background-position`0 from the right and `background-position`4 from the bottom of the container.

Notice the order of the values in the examples above: keywords followed by length units. A three- or four-value background-position must follow that format, with a keyword preceding a length or percentage unit.

There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated with comma).

Show demo ❯


Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property background-size 4.0 1.0 -webkit- 9.0 4.0 3.6 -moz- 4.1 3.0 -webkit- 10.5 10.0 -o-



CSS Syntax

background-size: auto|length|cover|contain|initial|inherit;

Property Values

Value Description Demo auto Default value. The background image is displayed in its original size Demo ❯ 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". Read about length units Demo ❯ 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" Demo ❯ 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 Demo ❯ contain Resize the background image to make sure the image is fully visible Demo ❯ initial Sets this property to its default value. Read about initial inherit Inherits this property from its parent element. Read about inherit


More Examples

Example

Specify the size of a background image with percent:

example1 {

background: url(mountain.jpg); background-repeat: no-repeat; background-size: 100% 100%; }

example2 {

background: url(mountain.jpg); background-repeat: no-repeat; background-size: 75% 50%; }

Try it Yourself »

Example

Specify the size of a background image with "cover":

example1 {

background: url(mountain.jpg); background-repeat: no-repeat; background-size: cover; }

Try it Yourself »

Example

Specify the size of a background image with "contain":

example1 {

background: url(mountain.jpg); background-repeat: no-repeat; background-size: contain; }

Try it Yourself »

Example

Here we have two background images. We specify the size of the first background image with "contain", and the second background-image with "cover":

How to center background image vertically and horizontally in CSS?

Centering an Image Vertically.

Step 1: Define Position Absolute. Firstly, we change the positioning behavior of the image from static to absolute : div { height: 800px; position: relative; background: red; } img { width: 80%; position: absolute; } ... .

Step 2: Define Top & Left Properties. ... .

Step 3: Define the Transform Property..

Can you have multiple backgrounds in CSS?

You can apply multiple backgrounds to elements. These are layered atop one another with the first background you provide on top and the last background listed in the back. Only the last background can include a background color.

What does background

background-size:cover; means the background image will always fit the whole div , you won't be left with any empty spots in your div. background-size:100% 100%; won't leave any empty space too, but of course this will destroy the original image aspect ratio.

How do I make background image fit all screen sizes in CSS?

Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.