visit
<element to be styled>.style.<style property> = "desired outcome";
That wasn't very well-explained. Let me give you a more concrete example to demonstrate; the next line of code will result in my
header
turning green:header.style.color = "green";
That looks better. In that example,
header
is the element being acted upon, style
is usually the keyword for accessing style properties, color
is the style property that is being accessed, and green
is the new desired color.With our basic pattern of styling elements in mind, let's segue into our first CSS essential: color.Let's start by taking the example from the previous section and applying it here: change the color of your
header
. Use my code above, but instead of green
you can select any basic color you like. Think color wheel/kindergarten colors, not the wackiest color you remember from the crayon box you used to have. For example:header.style.color = "orange";
Your display should now look something like this, depending on what color you chose:Nicely done. The
color
property can be applied to text, backgrounds, borders, and pretty much any element on your webpage that you can think of.Before we move on, go ahead and apply that same color you chose to your
text1
and text2
elements.Cool, now let's think about background color. White background is cool and all, but let's decide on something a little more exciting.
document.body.style.backgroundColor = "red";
Good! Although this might seem quite different from changing the color of our header, it still follows our same basic pattern:<element to be styled>.style.<style property> = "desired outcome";
For our background color styling,
document.body
is the element to be styled, the style
keyword is still used, backgroundColor
is the style attribute we are modifying, and red
is my desired outcome.One important distinction between CSS and JavaScript styling becomes relevant when modifying styling attributes that are more than one word (such as background color). In CSS, the syntax for such attributes is kebab-case (i.e.
background-color
). For JavaScript, the syntax is camelCase (i.e. backgroundColor
).Now then, with my choice of orange text and red background, my example looks like this:
document.body.style.backgroundColor = "black";
header.style.color = "cornflowerblue";
text1.style.color = "chartreuse";
text2.style.color = "lightsalmon";
document.body.style.backgroundColor = "dimgray";
header.style.color = "rgb(100, 149, 237)";
text1.style.color = "rgb(127, 255, 0)";
text2.style.color = "rgb(255, 160, 122)";
document.body.style.backgroundColor = "rgb(105, 105, 105)";
header.style.color = "#6495ed";
text1.style.color = "#7fff00";
text2.style.color = "#ffa07a";
document.body.style.backgroundColor = "#696969";
background-color
In the previous section, we learned that we can modify the background color of the page with
document.body.style.backgroundColor = "color_goes_here";
. There is another important tidbit to know about the background color property, which is that it can be applied to individual elements as well. For example:const header = document.createElement('h1');
header.textContent = 'Vanilla JS practice';
header.style.backgroundColor = "red";
document.body.appendChild(header);
const text1 = document.createElement('p');
text1.textContent = 'Go hang a salami, I\'m a lasagna hog.';
text1.style.backgroundColor = "yellow";
document.body.appendChild(text1);
const text2 = document.createElement('p');
text2.textContent = 'Read the previous sentence backwards.';
text2.style.backgroundColor = "green";
document.body.appendChild(text2);
background-image
document.body.style.backgroundImage = 'url("lasagna.jpg")';
document.body.style.backgroundImage = 'url("//imgur.com/TUQLNdi.jpg")';
background-repeat
The two main style properties that affect this are
background-repeat
and background-size
.background-repeat
has four options:1.
no-repeat
(image renders one time, default location is top left corner)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "no-repeat";
2.
repeat-x
(image repeats horizontally across the screen)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "repeat-x";
3.
repeat-y
(image repeats vertically across the screen)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "repeat-y";
4.
repeat
(default behavior; image repeats horizontally and vertically to fill the entire screen)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "repeat";
background-size
background-size
has two main options:1.
auto
(default behavior; image renders at its original size)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "no-repeat";
document.body.style.backgroundSize = "auto";
2.
cover
(a single image stretches to cover the entire screen)document.body.style.backgroundImage = "url('//biologydictionary.net/wp-content/uploads/2020/09/Walrus-1.jpg')";
document.body.style.backgroundRepeat = "no-repeat";
document.body.style.backgroundSize = "cover";
The main types of text/typographical emphasis include bold, italic, and underline.
To make text bold, set the
font-weight
property to the value "bold".text1.style.fontWeight = "bold";
To make text italic, set the
font-style
property to the value "italic".text2.style.fontStyle = "italic";
To make text underlined, set the
text-decoration
property to the value "underline".
In addition to underlined text, the
text-decoration
property can also overline and strikethrough text.header.textContent = 'Vanilla JS practice';
header.style.textDecoration = "underline";
text1.textContent = 'Go hang a salami, I\'m a lasagna hog.';
text1.style.fontWeight = "bold";
text1.style.textDecoration = "overline";
text2.textContent = 'Read the previous sentence backwards.';
text2.style.fontStyle = "italic";
text2.style.textDecoration = "line-through";
Borders have several properties, which can be set and modified individually. However, in my experience most developers use the shorthand
border
property, which combines the border-width
, border-style
, and border-color
properties. Instead of writing three lines of code...header.style.borderWidth = "5px";
header.style.borderStyle = "dotted";
header.style.borderColor = "salmon";
header.style.border = "5px dotted salmon";
When using the
border
property, the only parameter that is required is border-style
. border-width
and border-color
are optional, and default to 3px and black respectively.As with color, borders are a big topic with lots of different options for customization and specification. Rather than explain and show all the options in this tutorial, .Your task: Make a square on the screen, below your text.</span> This is very open-ended, and there are nearly limitless ways to do this.
Extra credit: If you complete this task, try to complete it one or two MORE times using different approaches.
Before you get started, you should do some independent Google research on CSS
height
and width
properties. I didn't cover those properties in this tutorial, but you may need them now.If you get stuck, scroll down for a few hints, and down to the next section for a couple sample answers. Good luck!Hint #1: The first step to completing this task is to create a new element, likely a
<div>
. From there, you can use a combination of the style properties you learned in this tutorial to make a square.Hint #2: Have you looked up the
height
and width
properties yet? You may need to set those properties on your new element to give it a defined area.Hint #3: If you can't get a square to appear on the screen, re-check the basic stuff:
- Have you used
document.body.appendChild(YOUR_SQUARE_HERE)
?- Have you given your square some sort of style feature that makes it visible? Double-check your syntax for every line.1. Using the
border
property:const newSquare = document.createElement('div');
newSquare.style.border = "solid";
newSquare.style.height = "200px";
newSquare.style.width = "200px";
document.body.appendChild(newSquare);
2. Using the
background-color
property:const newSquare = document.createElement('div');
newSquare.style.backgroundColor = "red";
newSquare.style.height = "200px";
newSquare.style.width = "200px";
document.body.appendChild(newSquare);
3. Using the
background-image
property:const newSquare = document.createElement('div');
newSquare.style.backgroundImage = "url('purple-square.png')"
newSquare.style.height = "200px";
newSquare.style.width = "200px";
document.body.appendChild(newSquare);
That was but a brief introduction to all that CSS has to offer, and how to implement it in JavaScript. I hope you learned a lot that you can apply in your own projects! Be sure to check out the third part of this tutorial, which will (I think) cover modals in JavaScript.
Previously published at