Introduction To Basic CSS
Introduction To Basic CSS
Cascading Style Sheets (CSS) tell the browser how to display the text and other
content that you write in HTML.
Note that CSS is case-sensitive so be careful with your capitalization. CSS has
been adopted by all major browsers and allows you to control:
color
fonts
positioning
spacing
sizing
decorations
transitions
There are three main ways to apply CSS styling. You can apply inline styles
directly to HTML elements with the styleattribute. Alternatively, you can place
CSS rules within styletags in an HTML document. Finally, you can write CSS
rules in an external style sheet, then reference that file in the HTML document.
Even though the first two options have their use cases, most developers prefer
external style sheets because they keep the styles separate from the HTML
elements. This improves the readability and reusability of your code. The idea
behind CSS is that you can use a selector to target an HTML element in the
DOM (Document Object Model) and then apply a variety of attributes to that
element to change the way it is displayed on the page.
In this section, you'll see how adding CSS styles to the elements of your
CatPhotoApp can change it from simple text to something more.
Basic CSS: Import a Google Font
In addition to specifying common fonts that are found on most operating
systems, we can also specify non-standard, custom web fonts for use on our
website. There are various sources for web fonts on the internet but, for this
example we will focus on the Google Fonts library.
Google Fonts is a free library of web fonts that you can use in your CSS by
referencing the font's URL.
So, let's go ahead and import and apply a Google font (note that if Google is
blocked in your country, you will need to skip this challenge).
To import a Google Font, you can copy the font(s) URL from the Google Fonts
library and then paste it in your HTML. For this challenge, we'll import
the Lobsterfont. To do this, copy the following code snippet and paste it into the
top of your code editor(before the opening styleelement):
Create a font-familyCSS rule that uses the Lobsterfont, and ensure that it will be
applied to your h2element.
Basic CSS: Import a Google Font
In addition to specifying common fonts that are found on most operating
systems, we can also specify non-standard, custom web fonts for use on our
website. There are various sources for web fonts on the internet but, for this
example we will focus on the Google Fonts library.
Google Fonts is a free library of web fonts that you can use in your CSS by
referencing the font's URL.
So, let's go ahead and import and apply a Google font (note that if Google is
blocked in your country, you will need to skip this challenge).
To import a Google Font, you can copy the font(s) URL from the Google Fonts
library and then paste it in your HTML. For this challenge, we'll import
the Lobsterfont. To do this, copy the following code snippet and paste it into the
top of your code editor(before the opening styleelement):
Create a font-familyCSS rule that uses the Lobsterfont, and ensure that it will be
applied to your h2element.
Basic CSS: Specify How Fonts Should Degrade
There are several default fonts that are available in all browsers. These generic
font families include monospace, serifand sans-serif
When one font isn't available, you can tell the browser to "degrade" to another
font.
Generic font family names are not case-sensitive. Also, they do not need quotes
because they are CSS keywords.
Note
If you have the Lobster font installed on your computer, you won't see the
degradation because your browser is able to find the font.
Basic CSS: Add Borders Around Your Elements
CSS borders have properties like style, colorand width
<style>
.thin-red-border {
border-color: red;
border-width: 5px;
border-style: solid;
}
</style>
You can specify a border-radiuswith pixels. Give your cat photo a border-
radiusof 10px.
Note: this challenge allows for multiple possible solutions. For example, you
may add border-radiusto either the .thick-green-borderclass or the .smaller-
imageclass.
<style>
.blue-text {
color: blue;
}
</style>
You can see that we've created a CSS class called blue-textwithin the <style>tag.
<h2 class="blue-text">CatPhotoApp</h2>
Note that in your CSS styleelement, class names start with a period. In your
HTML elements' class attribute, the class name does not include the period.
There are several benefits to using idattributes: You can use an idto style a
single element and later you'll learn that you can use them to select and modify
specific elements with JavaScript.
<h2 id="cat-photo-app">
You may have already noticed this, but all HTML elements are essentially little
rectangles.
Three important properties control the space that surrounds each HTML
element: padding, margin, and border.
Here, we can see that the blue box and the red box are nested within the yellow
box. Note that the red box has more paddingthan the blue box.
When you increase the blue box's padding, it will increase the distance(padding)
between the text and the border around it.
Basic CSS: Adjust the Margin of an Element
An element's margincontrols the amount of space between an
element's borderand surrounding elements.
Here, we can see that the blue box and the red box are nested within the yellow
box. Note that the red box has a bigger marginthan the blue box, making it
appear smaller.
When you increase the blue box's margin, it will increase the distance between its
border and surrounding elements.
Change the marginof the blue box to match that of the red box.
Basic CSS: Use Clockwise Notation to Specify the
Padding of an Element
Instead of specifying an element's padding-top, padding-right, padding-bottom,
and padding-leftproperties individually, you can specify them all in one line,
like this:
These four values work like a clock: top, right, bottom, left, and will produce the
exact same result as using the side-specific padding instructions.
Basic CSS: Use Attribute Selectors to Style Elements
You have been giving idor classattributes to elements that you wish to
specifically style. These are known as ID and class selectors. There are other CSS
Selectors you can use to select custom groups of elements to style.
For this challenge, you will use the [attr=value]attribute selector to style the
checkboxes in CatPhotoApp. This selector matches and styles elements with a
specific attribute value. For example, the below code changes the margins of all
elements with the attribute typeand a corresponding value of radio:
[type='radio'] {
margin: 20px 0px 20px 0px;
}
The two main types of length units are absolute and relative. Absolute units tie
to physical units of length. For example, inand mmrefer to inches and
millimeters, respectively. Absolute length units approximate the actual
measurement on a screen, but there are some differences depending on a
screen's resolution.
Relative units, such as emor rem, are relative to another length value. For
example, emis based on the size of an element's font. If you use it to set the font-
sizeproperty itself, it's relative to the parent's font-size.
Note
There are several relative unit options that are tied to the size of the viewport.
They are covered in the Responsive Web Design Principles section.
Remember, you can style your bodyelement just like any other HTML element,
and all your other elements will inherit your bodyelement's styles.
Then, let's give all elements on your page the color of greenby adding color:
green;to your bodyelement's style declaration.
There are other ways that you can override CSS. Do you remember inline styles?
But wait. There's one last way to override CSS. This is the most powerful method
of all. But before we do it, let's talk about why you would ever want to override
CSS.
In many situations, you will use CSS libraries. These may accidentally override
your own CSS. So when you absolutely need to be sure that an element has
specific CSS, you can use !important
In CSS, we can use 6 hexadecimal digits to represent colors, two each for the red
(R), green (G), and blue (B) components. For example, #000000is black and is
also the lowest possible value. You can find more information about the RGB
color system here.
body {
color: #000000;
}
From these three pure colors (red, green, and blue), we can vary the amounts of
each to create over 16 million other colors!
For example, orange is pure red, mixed with some green, and no blue. In hex
code, this translates to being #FFA500.
The digit 0is the lowest number in hex code, and represents a complete absence
of color.
The digit Fis the highest number in hex code, and represents the maximum
possible brightness.
Replace the color words in our styleelement with their correct hex codes.
Green #00FF00
Orange #FFA500
Red #FF0000
<style>
.red-text {
color: #FF0000;
}
.green-text {
color: #00FF00;
}
.dodger-blue-text {
color: #1E90FF;
}
.orange-text {
color: #FFA500;
}
</style>
This reduces the total number of possible colors to around 4,000. But browsers
will interpret #FF0000and #F00as exactly the same color.
Go ahead, try using the abbreviated hex codes to color the correct elements.
<style>
.red-text {
color: #F00;
}
.fuchsia-text {
color: #F0F;
}
.cyan-text {
color: #0FF;
}
.green-text {
color: #0F0;
}
</style>
rgb(0, 0, 0)
Instead of using six hexadecimal digits like you do with hex code, with RGByou
specify the brightness of each color with a number between 0 and 255.
If you do the math, the two digits for one color equal 16 times 16, which gives us
256 total values. So RGB, which starts counting from zero, has the exact same
number of possible values as hex code.
Here's an example of how you'd change the body background to orange using its
RGB code.
body {
background-color: rgb(255, 165, 0);
}
Let's replace the hex code in our bodyelement's background color with the RGB
value for black: rgb(0, 0, 0)
<style>
body {
background-color: rgb(0,0,0);
}
</style>
Basic CSS: Use CSS Variables to change several
elements at once
CSS Variables are a powerful way to change many CSS style properties at once
by changing only one value.
Follow the instructions below to see how changing just three values can change
the styling of many elements.
--penguin-skin: gray;
Now you can use that variable elsewhere in your CSS to change the value of
other elements to gray.
style>
.penguin {
background: var(--penguin-skin);
This will change the background of whatever element you are targeting to gray
because that is the value of the --penguin-skinvariable.
Note that styles will not be applied unless the variable names are an exact
match.
background: var(--penguin-skin);
This will change the background of whatever element you are targeting to gray
because that is the value of the --penguin-skinvariable.
Note that styles will not be applied unless the variable names are an exact
match.
Note: This fallback is not used to increase browser compatibilty, and it will not
work on IE browsers. Rather, it is used so that the browser has a color to display
if it cannot find your variable.
By creating your variables in :root, they will be available throughout the whole
web page.