Url upload 1 resources l_159000220_bn3.jpg no-repeat center top
You can use the ` `6 value by itself, but if you try to use just `404 not found`1 by itself, the CSS rule will be invalid and none of the background properties will work at all. Show
What a user sees on a website will impact how good their user experience is. It will also affect how easily they can use the whole site in general. Adding images to the background of certain parts of a website is often more visually appealing and interesting than just changing the background-color. Modern browsers support a variety of image file types like
3,
4,
5, and
6. This article explains how to add images to your HTML code and how to then fine-tune them to look better. Background Image SyntaxThe first step is to make sure you create an assets directory (folder) to hold the images you want to use in your project. For example we can create an
7 folder in the project we are working on and add an image called
8 that we want to use. The
9 CSS property allows you to then place the image behind any HTML element you wish. This could either be the whole page (by using the
0 selector in CSS which targets the
1 element in our HTML), or just a standalone specific part of the page such as a
2 element like in the example below. To add a
9 to a section tag in your
4 file, write the following code:
Let's discuss what's going on here in detail:
How to Stop Background RepeatWhen you apply a background image to an element, by default it will repeat itself. If the image is smaller than the tag of which it's the background, it will repeat in order to fill in the tag. How do we stop this from happening? The
2 property takes in 4 values and we are able to change the direction in which the image repeats, or stop the image from repeating itself all together.
This is the default value if we don't give the
2 property a value. In this case the image is repeated both horizontally and vertically so in both x-direction and y-direction respectively until it fills the space.
The
4 value stops the image from repeating itself from all directions. The image is only shown once.
This value repeats the image only horizontally on the page. The image is repeated across the page, in the
5. The
5 in math is from the left to the right.
This value repeats the image only vertically on the page. The image is repeated down the page ,in the
7. The
7 in math is from top to bottom. How to Set Background PositionAfter adding a background image and stopping it from repeating, we are able to further control how it looks within the background of the tag by improving its position. We'll use the
9 property to do this. The selector takes in two values. The first one is for the horizontal position, or x-direction (how far across the tag). The second one is for the vertical position, or y-direction (how far down the tag). The values can be units, like a pair of pixels:
This will move the image 20px across and 30px down the containing tag. Instead of pixels we can use a set of keywords like right, left, top, down, or center to place the image at the right, left, top, down, or center of the tag.
This places the image at the right hand side of the center of the tag. If we wanted to center it both horizontally and vertically, we would do the following:
To position an image with finer detail, it is worth mentioning that we can use percentages.
This positions the image 20% across the tag and 40% down the tag. So far we have seen values used in combination, but we can also just specify one value like
0 or
1 or
2, which applies it to both directions. How to Resize a Background ImageTo control the size of the background image we can use the background-size property. Again, like the previous properties mentioned, it takes in two values. One for the horizontal (x) size and one for the vertical (y) size. We can use pixels, like so:
If we do not know the exact width of the container we are storing the image in, there is a set of specific values we can give the property.
The rule
6 in this case will crop of parts of the image When we change it to
7 we see that the image shrinks to fit the section tag. How to Use the Background Attachment PropertyWith the
8 property we can control where the background image is attached, meaning if the image is fixed or not to the browser. The default value is
9, where the background image stays with its tag and follows the natural flow of the page by scrolling up and down as we scroll up and down. The second value the property can have is
See the Pen by Dionysia Lemonaki (@deniselemonaki) on CodePen. Background GradientsA different use case for the
9 property is for telling the browser to create a gradient. The
9 in this case does not have a URL, but a linear-gradient instead. The simplest way to do this is to specify the angle. This controls the direction of the gradient and how to colors will blend. Lastly add two colors that you want blended together in a gradient for the tag's background. A gradient that goes from top to bottom and from black to white is:
0 The most common degrees used for gradients are:
The above degrees each correspond with
7,
8,
9 and
0, respectively.
1 See the Pen by Dionysia Lemonaki (@deniselemonaki) on CodePen. Instead of keyword colors we can use hex colors to be more particular and have a wider range of options:
2 See the Pen by Dionysia Lemonaki (@deniselemonaki) on CodePen. We can also include more than two colors in a gradient, creating different affects and color schemes. ConclusionThis marks the end of our introduction to the basic syntax of the
9 property. From here the possibilities are endless and leave room for a lot of creative expression. These effects help the user have a pleasant experience when visiting your website. I hope this was helpful, and thank you for reading. Have fun with your designs and happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What does background nobackground-repeat repeat-x: The image is repeated horizontally only. repeat-y: The image is repeated vertically only. no-repeat: The image is not repeated: only one copy of the image is drawn. How do I fix the background image in CSS without repeating?If you want the background image not to repeat at all, use background-repeat: no-repeat; . Which is the default value for backgroundProperty Values. How do you repeat an image in CSS?CSS Background Image Repeat. body { background-image: url("gradient_bg.png"); } ... . body { background-image: url("gradient_bg.png"); background-repeat: repeat-x; ... . Show the background image only once: body { background-image: url("img_tree.png"); ... . Position the background image in the top-right corner: body {. |