The provided web content is a comprehensive guide on CSS background properties, covering topics from adding background images and colors to blending, sizing, positioning, and using multiple backgrounds, with practical examples and interactive exercises for learning and practice.
Abstract
This web content serves as an in-depth tutorial for frontend developers, particularly focusing on the intricacies of CSS background properties. It is the twelfth chapter of a complete frontend developer course for beginners, offering insights into various background-related CSS properties such as background-image, background-blend-mode, background-size, background-repeat, background-origin, background-clip, background-position, and background-attachment. The lesson progresses from basic to advanced concepts, including the use of shorthand properties for efficiency and the layering of multiple background images. It concludes with a series of quizzes, interactive coding exercises, and flashcards to reinforce learning, along with a call to action for readers to join a remote frontend cohort program and a referral link for Medium membership to support a charitable cause.
Opinions
The author emphasizes the importance of understanding CSS background properties for frontend development.
The use of box-sizing: border-box; is recommended for better control over element dimensions including padding and borders.
The author provides a subjective opinion on the ease of using the CSS background shorthand property when dealing with multiple background images.
The tutorial is designed to be interactive, with the inclusion of Codepen examples, quizzes, and exercises to enhance the learning experience.
The author encourages readers to engage with the content by offering incentives in the form of SW Coins, which can be redeemed for discounts on Udemy courses.
The author promotes the Silicon Wat Campus initiative and suggests that joining Medium through their referral link supports educational efforts in Ukraine and Cambodia.
Chapter 12: CSS Background
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
The height and width will also include the padding and the border:
border-box:
width x height = content + padding + border
As you can see now, the scrollbarsdisappear because our main element is now exactly the size of the viewport.
We have learned that with background-clip, we can control how much the background-colorcovers.
main {
background-color: green;
}
To make it coveronly the contentarea,
background-color => content
we can use content-box:
main {
background-color: green;
background-clip: content-box;
}
To make it alsocover the paddingarea,
background-color => content + padding
we can use padding-box:
main {
background-color: green;
background-clip: padding-box;
}
To make it coverall the way to the border,
background-color => content + padding + border
we can use border-box (which is the default):
main {
background-color: green;
background-clip: border-box;
}
Similarly, we can control how the background-image is positioned using background-origin.
If we want it to start at the contentarea,
background-image => content
we can use content-box:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: content-box;
}
If we want it to start at the paddingarea,
background-image=> padding
we can use padding-box (which is the default):
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: padding-box;
}
Finally, if we want it to start at the borderarea,
background-image => border
we can use border-box:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
}
To summarize:
box-sizing => width x heightbackground-clip => background-colorbackground-origin => background-image
Position and Attachment
Relative to the background-origin, we can also set the background-position. By default, it is located at the top left:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: top left;
}
If we want, we can change it to:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: top right;
}
Or:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: bottom right;
}
Or:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: bottom left;
}
Or:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: center left;
}
Or:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: centercenter;
}
Etc!
We can even specify an exactdistance. For example, 100px from the left, and 50px from the top:
main {
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-origin: border-box;
background-position: 100px50px;
}
Now, let’s make the main element overflow by changing the width to 150vw and the height to 150vh:
If we want to write a lot less, CSS also gives us a simple shorthand property called background:
main {
background:
}
Using this shorthand, we can just write:
main {
background: green url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat fixed border-box border-box 100px 50px / auto 100px;
}
background-blend-mode is not yet supported.
background-origin needs to appearbeforebackground-clip:
main {
background: green url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat fixed border-box border-box 100px 50px / auto 100px;
}
And background-position needs to appearbeforebackground-sizeseparated by a /:
main {
background: green url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat fixed border-box border-box 100px 50px / auto 100px;
}
Other than this, the orderof the other properties does not matter.
Likewise, to position the top image at the center too, we simply write center and then a comma:
main {
background-color: green;
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png), url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-repeat: no-repeat, no-repeat;
background-position: center, center;
background-size: 75%;
background-blend-mode: luminosity;
}
Likewise again, to set the top imagebackground-size to 35%, we simply write 35% and then a comma:
main {
background-color: green;
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png), url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-repeat: no-repeat, no-repeat;
background-position: center, center;
background-size: 35%, 75%;
background-blend-mode: luminosity;
}
Finally, to set the top imagebackground-blend-mode to difference, we simply write difference and then a comma:
main {
background-color: green;
background-image: url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png), url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png);
background-repeat: no-repeat, no-repeat;
background-position: center, center;
background-size: 35%, 75%;
background-blend-mode: difference, luminosity;
}
In this way, as long as we separateeach layer with a comma, bearing in mind its exact order in the list, we can add as many background images as we like.
Note that there can only be one background-color.
Let’s compare and contrast this with the shorthand! To add the firstbackground-image on main, we write:
main {
background: green url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat center / 75%;
}
Again, background-blend-mode is not yet supported in the shorthand.
Similar to the longhand, to add a top layer, we simply write:
main {
background:
url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat center / 35%,
green url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png) no-repeat center / 75%;
}
Note that since there can only be one background-color, it can only be declared on the bottommost layer.
Which method is easier for you? Short or long?
What if there were not 2, but 10 layers?
As the number of layers increases, it may become increasinglymoredifficult to keep track of all the properties that belong to each layer. In such cases, the shorthand may be perfect! 😄
Lastly, we looked at how to layermultiplebackground images on the same element. As long as we separateeach layer with a comma, we can use the longhand or shorthand to add as many as we like!
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Now that we have mastered background-color and background-image, we’re perfectly ready to learn about CSS Gradients. In the next lesson, we will learn how to create linear gradients, radial gradients, and conic gradients. Moreover, we will learn how to repeat them and combine them to generate some really interesting shapes and patterns!
When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️