Hello everybody,
Michael here, and today’s lesson will cover basic principles of using backgrounds in CSS.
Just as I did for my previous CSS lessons, I’ll use the sample form I created in HTML for this lesson. Here’s the code for the form:
<!DOCTYPE html>
<html lang="es-US" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="Form.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Press+Start+2P">
<title></title>
</head>
<body>
<h1>Flight finder:</h1>
<form action="Submitted.html" method="POST">
<label for="datepicker1">Pick the date you want to depart for your vacation</label><br>
<input type="date" id="datepicker1" name="datepicker1" min="2021-03-25" max="2022-03-25"><br>
<br>
<label for="datepicker2">Pick the date you want to return from your vacation</label><br>
<input type="date" id="datepicker2" name="datepicker2" min="2021-03-25" max="2022-03-25"><br>
<br>
<label for="time1">What time would you like to depart? (flights shown within 90 minutes of selected time)</label><br>
<input type="time" id="time1" name="time1"><br>
<br>
<label for="time2">What time would you like to return? (flights shown within 90 minutes of selected time)</label><br>
<input type="time" id="time2" name="time2"><br>
<br>
<label for="layover">How many layovers do you want?</label><br>
<input type="number" id="layover" name="layover" min="0" max="3"><br>
<br>
<input type="submit" value="Submit">
</form>
<div class="container">
<p>Thank you for booking your next trip with XYZ Airlines!!</p>
<p>Can't wait to see you on your travels!!</p>
</div>
</body>
</html>
And here’s the CSS styling code we’ll use (I’ll keep the styling I applied at the end of CSS Lesson 2: Fun with Fonts):
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
Here’s what the webpage looks like with the current styling:

Now, how would we add some background styling to the webpage. Take a look at the highlighted segment of code below:
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
body{
background-color: #87CEEB
}
To set a background color for the webpage, use the body selector and inside the selector, call the background-color property and set the value of this property to a certain color, which can take one of these three forms:
- a conventional color name (e.g. red, yellow, green)
- a color HEX code (e.g. #87CEEB)
- a color RGB code (e.g. rgb(123, 10, 88))
In this example, I specified the backround color with a hex code-#87CEEB. In case you’re wondering, this hex code produces a sky-blue background (heck, I thought it was appropriate given that this a form for an imaginary airline). Here’s what the webpage looks like with the background styling applied:

- If you want to apply a background color to the entire webpage, always use the
bodyselector! - When specifying a HEX code color, don’t wrap it in quotation marks.
So, the background looks great, but all the input elements in the form could use some more styling as well. How should we approach this? Take a look at the CSS code below and pay attention to the highlighted section:
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
body{
background-color: #87CEEB
}
input{
background-color: #00FFFF
}
Here’s what the webpage looks like with the additional styling:

Yes, you can give background stylings to elements other than the main webpage as I did here with the input elements. To style the input elements, I created a CSS styling call with input as the selector and background-color: #00FFFF as the styling call that will change the background color of the input elements.
- #00FFFF refers to cyan by the way. I thought it would be an appropratie color given that this is a form for an (imaginary) airline.
Alright, the webpage looks great so far! However, what if you wanted to use a picture for the background rather than a color? How would you go about doing this? Take a look at the highlighted section of the code below:
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
body{
background-image: url('stock photo.jpg')
}
input{
background-color: #00FFFF
}

So, how did I get the result for my webpage that you see above? Well, I first obtained a stock photo from a stock photo website (https://shop.stockphotosecrets.com/index.cfm?/home_EN&CFID=351309901&CFTOKEN=65225479 for those curious). I then saved the stock photo to the same directory where my form HTML and CSS code is located.
To add the stock photo to the website, use CSS’s background-image property and set the value of this property to url(image name.image extension); in this example, the value of the background-image property was url('stock photo.jpg'), since I had saved this stock photo of a plane onto my computer as stock photo.jpg (creative, I know).
- If you want to succesfully connect your chosen background image to your HTML webpage, wrap the name of your image (as it’s saved on your computer) inside a
url()function. Also, wrap the name of your image in quotes (whether single quotes or double quotes) as I did in the above example.
Once I set the form’s background-image property, the webpage’s background image changes to the stock photo of a plane I saved onto my computer.
Looks pretty good, right? Well, there’s one thing we can fix-if you’re thinking of the fact that the stock photo is repeated several times throughout the webpage (both vertically and horizontally), you’d be right. Yes, there’s a simple fix to the repeating background image issue, and all it takes is a single line of code. Check out the highlighed section of the code below:
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
body{
background-image: url('stock photo.jpg');
background-repeat: no-repeat;
}
input{
background-color: #00FFFF
}

To ensure the background image doesn’t repeat, I added the background-repeat property to the body styling call in the CSS file and set this property’s value to no-repeat to tell my CSS code to only display the background image once.
- If I only wanted to repeat the background image horizontally, I could set the value of the
background-repeatproperty torepeat-x. Likewise, if I only wanted to repeat the background image vertically, I could set the value of thebackground-repeatproperty torepeat-y.
Alright, the webpage is looking better, but we’ve still got another issue-the background image only covers the top-left corner of the webpage when it should ideally cover the whole webpage. How would we fix this issue? Take a look at the highlighted section of the CSS code below:
h1{
color: green;
font-family: "Comic Sans MS";
font-size: 40px;
text-align: center
}
.container{
color: red;
font-family: "Press Start 2P";
font-size: 30px;
text-align: center
}
body{
background-image: url('stock photo.jpg');
background-repeat: no-repeat;
background-size: cover;
}
input{
background-color: #00FFFF
}

Just as I did with the background-repeat property, I managed to fix the background image display issue with a single line of code-in this case, background-size: cover. What this single line of code does is utilize CSS’s background-size property to change the size of the background image to cover, which will stretch the background image to cover the whole webpage. Pretty neat what you can do with a single line of code is CSS, amirite?
- If you set the size of a background image to
coverkeep in mind that the image will likely either stretch or be slightly cut off.
So, the webpage is looking a lot nicer! However, before I go, let me leave you with these web background design tips:
- When picking a background (whether its a color or an image), pick something that doesn’t clash with the webpage’s text too much. If you like your choice of background but find that it clashes with the text too much, change the color scheme of the text.
- Now that I think of it, the last two lines of text on this webpage somewhat clash with the background image. But then again, this is for a programming lesson, not a production-ready website.
- Also, if you’re creating a webpage for a business (not for a programming lesson), PLEASE PLEASE PLEASE don’t use stock photos. It just looks unprofessional and fakey.
Thanks for reading,
Michael







