How to add a background image in CSS?
Use background-image with url(), and control it with the other background properties.
.hero {
background-image: url("images/banner.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 300px;
}
cover fills the whole box and may cut some part. Add a background-color as a backup. See CSS Background.
Learn more about the similar topics:
| Tutorials |
|---|
| CSS Colors |
| CSS Background |
| CSS Gradients |
| CSS Images |
| Exercises & Assignments |
|---|
| Write CSS to change the text colour and background of a paragraph |
| Interview Questions & Answers |
|---|
| How to add a background image in CSS? |
| What is the difference between opacity and rgba in CSS? |