Hướng dẫn background-image trong html w3schools
A background image can be specified for almost any HTML element. Show Background Image on a HTML elementTo add a background image on an HTML element, use the HTML ExampleAdd a background image on a HTML element: Try it Yourself » You can also specify the background image in the ExampleSpecify the background image in the Try it Yourself » Background Image on a PageIf you want the
entire page to have a background image, you must specify the background image on the ExampleAdd a background image for the entire page: Try it Yourself » Background RepeatIf the background image is smaller than the element, the image will repeat itself, horizontally and vertically, until it reaches the end of the element: ExampleTry it Yourself » To avoid the background image from repeating itself, set the ExampleTry it Yourself » Background CoverIf you want the background image to cover the entire element, you can set the Also, to make sure the entire element is always covered, set
the This way, the background image will cover the entire element, with no stretching (the image will keep its original proportions): ExampleTry it Yourself » Background StretchIf you want the background image to stretch to fit the entire element, you can set the Try resizing the browser window, and you will see that the image will stretch, but always cover the entire element. ExampleTry it Yourself » Learn More CSSFrom the examples above you have learned that background images can be styled by using the CSS background properties. To learn more about CSS background properties, study our CSS Background Tutorial. ExampleSet a background-image for the element: body { Try it Yourself » ExampleSet two background images for the element: body { Try it Yourself » More "Try it Yourself" examples below. Definition and UsageThe By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally. Tip: The background of an element is the total size of the element, including padding and border (but not the margin). Tip: Always set a background-color to be used if the image is unavailable. Show demo ❯
Browser SupportThe numbers in the table specify the first browser version that fully supports the property.
Note: IE8 and earlier do not support multiple background images. CSS Syntaxbackground-image: url|none|initial|inherit; Property ValuesMore ExamplesExampleSets two background images for the element. Let the first image appear only once (with no-repeat), and let the second image be repeated: body { Try it Yourself » ExampleUse different background properties to create a "hero" image: .hero-image { Try it Yourself » ExampleSets a linear-gradient (two colors) as a background image for a element: #grad1 { Try it Yourself » ExampleSets a linear-gradient (three colors) as a background image for a element: #grad1 { Try it Yourself » ExampleThe repeating-linear-gradient() function is used to repeat linear gradients: #grad1 { Try it Yourself » ExampleSets a radial-gradient (two colors) as a background image for a element: #grad1 { Try it Yourself » ExampleSets a radial-gradient (three colors) as a background image for a element:
#grad1 { Try it Yourself » ExampleThe repeating-radial-gradient() function is used to repeat radial gradients: #grad1 { Try it Yourself » Related PagesCSS tutorial: CSS Background, CSS Backgrounds (advanced), CSS Gradients HTML DOM reference: backgroundImage property |