All about CSS3 for Absolute Beginners: Part 3

All about CSS3 for Absolute Beginners: Part 3





Hi everyone, I hope you're making great progress in this web development journey. Best of luck to all the curious future web developers and let's continue the ride of learning CSS.

Background Color

Earlier, we learned how to change the color of  text such as a paragraph or a heading. Talking about containers like divs, we may also need to give a background color to a whole container. To test that, let's create a nice box and then fill it with a color of our choice.

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div class="box"></div>

</body>

</html>


CSS

.box {

height: 200px;
width: 200px;
background-color: green;

}

We made container a div in the body tag and gave it a class "box" to target it in CSS. Then we gave it a height and width of 200 pixels each to make it a box shape.

CSS Background Properties and the Box Model

In HTML, we can add an image on our webpage but it gives us less control. With the background-image property, it's going to be more adjustable and customizable. Moreover, the background property also accepts other values such as position, repeat and size, making it convenient to reach a desired effect for your webpage. Let's go through each value one by one. 

background-image

To use this property, go ahead and create a nice box whose background is going to be your image. To make that, create a div and give it some width and height.

After that, you can give the path to your image exactly like how you would give it in the image tag's source (src) attribute. Give this path in the URL function. It would look like this:

.box {

background-image: url("images/headphones.jpg");

}

background-position

Your image may not fit nicely after using the background-image property. To fix that, we can use background-position. The value can be chosen from left, right, top, bottom, center or by giving x and y coordinates.

.box {

background-image: url("images/headphones.jpg");
background-position: center;

}

Try experimenting with different values to see the behavior.

background-size

This controls the size of the image. Sometimes, it may not fit into containers, so we will need this property. Mostly used values are cover and contain. Cover forces the image to fit inside the container, usually cropping it's edges. Contain would make it fit inside a container and ensure its fully visible without cropping anything. 

.box {

background-image: url("images/headphones.jpg");
background-position: center;
background-size: cover;

}

background-repeat

Sometimes the image may be smaller than the container we're trying to fit it in. So that would cause the image to repeat itself to fill the leftover space. To control that, we have background-repeat. The most commonly used values for this property are no-repeat, repeat-x, repeat-y, space, round, and more. Try experimenting and analyzing the behavior. 

.box {

background-image: url("images/headphones.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;

}


Those were some of the popular background properties. However, there are still more that are not that widely used but if you're curious, then check them out on MDN web docs.

https://developer.mozilla.org/en-US/docs/Web/CSS/background

Before we wrap up the background, notice that the code has become a bit repetitive. To make it short, we can use a shorthand property. Here is how it would work:

.box {

background: url("images/headphones.jpg") center / cover no-repeat;

}




Box Model in CSS 

The box model is a fundamental part of CSS and something that you will work with a lot in your CSS projects. To understand, consider the picture below:




This is a normal box that we have been building using the div tag. When we analyze it, it has some margin around it, then the border starts in which there is some padding and finally the content. Let's understand margin first:


Margin property in CSS

To analyze what margin does, create two boxes of the same dimensions and give them each a class called box. See the output, then add margin like this:

.box {

height: 200px;
width: 200px;
background-color: lightblue;
margin: 15px;

}

Now observe that there is a space between both the boxes. This is the margin between them. You can increase or decrease the quantity to make observations.

You will notice that margin is applied on all sides of the box. Sometimes we might want to give margin only on the top, bottom, left, right or maybe one or two of these. Then that's easy:

.box {

height: 200px;
width: 200px;
background-color: lightblue;
margin-top: 15px;
margin-bottom: 5px;
margin-left: 12px;
margin-right: 20px;

}

To avoid repetition, we can use shorthand properties, similar to how we did with background properties. It will work clockwise such as first will be top, then right, then bottom and finally left.

.box {

height: 200px;
width: 200px;
background-color: lightblue;
margin: 15px 10px 3px 7px;

}

Border Property in CSS

To add a nice border around an element, use the border property in CSS. Here is how the code would look like:

.box {

height: 200px;
width: 200px;
background-color: lightblue;
margin: 15px;
border: 3px solid black

}

Now "3px" determines the thickness of the border and the solid is one of the designs CSS gives by default. They include solid, dashed and dotted. Border property is really simple so try changing the values and you will get the idea.

Wrapping up

The box model is not completed yet so we will continue that in the next blog. Just be patient and keep learning so that you see the power of CSS.

If you have any questions, leave a comment or reach out via the contact page, and I'll be happy to help.