Basic CSS
Basic CSS
Basic CSS
}
</style>
Página 3|9
However, an idis not reusable and should only be applied to one element. An idalso has a
higher specificity (importance) than a class so if both are applied to the same element and
have conflicting styles, the styles of the idwill be applied.
Here's an example of how you can take your element with the idattribute of cat-photo-
elementand give it the background color of green. In your styleelement:
#cat-photo-element {
background-color: green;
}
Note that inside your styleelement, you always reference classes by putting a .in front of
their names. You always reference ids by putting a #in front of their names.
Adjust the Padding of an Element
Now let's put our Cat Photo App away for a little while and learn more about styling HTML.
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.
An element's paddingcontrols the amount of space between the element's content and
its 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.
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.
Add a Negative Margin to an Element
An element's margincontrols the amount of space between an element's borderand
surrounding elements.
If you set an element's marginto a negative value, the element will grow larger.
Add Different Padding to Each Side of an Element
Sometimes you will want to customize an element so that it has different amounts
of paddingon each of its sides.
CSS allows you to control the paddingof all four individual sides of an element with
the padding-top, padding-right, padding-bottom, and padding-leftproperties.
Add Different Margins to Each Side of an Element
Sometimes you will want to customize an element so that it has a different marginon each of
its sides.
CSS allows you to control the marginof all four individual sides of an element with
the margin-top, margin-right, margin-bottom, and margin-leftproperties.
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:
padding: 10px 20px 10px 20px;
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.
Use Clockwise Notation to Specify the Margin of an Element
Página 4|9
Let's try this again, but with marginthis time.
Instead of specifying an element's margin-top, margin-right, margin-bottom, and margin-
leftproperties individually, you can specify them all in one line, like this:
margin: 10px 20px 10px 20px;
These four values work like a clock: top, right, bottom, left, and will produce the exact same
result as using the side-specific margin instructions.
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.
Let's bring out CatPhotoApp again to practice using CSS Selectors.
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;
}
Understand Absolute versus Relative Units
The last several challenges all set an element's margin or padding with pixels ( px). Pixels are
a type of length unit, which is what tells the browser how to size or space an item. In
addition to px, CSS has a number of different length unit options that you can use.
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.
Style the HTML Body Element
Now let's start fresh and talk about CSS inheritance.
Every HTML page has a bodyelement.
We can prove that the bodyelement exists here by giving it a background-colorof black.
We can do this by adding the following to our styleelement:
body {
background-color: black;
}
Inherit Styles from the Body Element
Now we've proven that every HTML page has a bodyelement, and that its bodyelement can
also be styled with CSS.
Remember, you can style your bodyelement just like any other HTML element, and all your
other elements will inherit your bodyelement's styles.
Prioritize One Style Over Another
Sometimes your HTML elements will receive multiple styles that conflict with one another.
For example, your h1element can't be both green and pink at the same time.
Página 5|9
Let's see what happens when we create a class that makes text pink, then apply it to an
element. Will our class override the bodyelement's color: green;CSS property?
Override Styles in Subsequent CSS
Our "pink-text" class overrode our bodyelement's CSS declaration!
We just proved that our classes will override the bodyelement's CSS. So the next logical
question is, what can we do to override our pink-textclass?
Create an additional CSS class called blue-textthat gives an element the color blue. Make
sure it's below your pink-textclass declaration.
Apply the blue-textclass to your h1element in addition to your pink-textclass, and let's see
which one wins.
Applying multiple class attributes to a HTML element is done with a space between them
like this:
class="class1 class2"
Note: It doesn't matter which order the classes are listed in the HTML element.
However, the order of the classdeclarations in the <style>section are what is important. The
second declaration will always take precedence over the first. Because .blue-textis declared
second, it overrides the attributes of .pink-text
Override Class Declarations by Styling ID Attributes
We just proved that browsers read CSS from top to bottom. That means that, in the event of
a conflict, the browser will use whichever CSS declaration came last.
But we're not done yet. There are other ways that you can override CSS. Do you remember
id attributes?
Let's override your pink-textand blue-textclasses, and make your h1element orange, by
giving the h1element an id and then styling that id.
Give your h1element the idattribute of orange-text. Remember, id styles look like this:
<h1 id="orange-text">
Leave the blue-textand pink-textclasses on your h1element.
Create a CSS declaration for your orange-textid in your styleelement. Here's an example of
what this looks like:
#brown-text {
color: brown;
}
Note: It doesn't matter whether you declare this CSS above or below pink-text class, since id
attribute will always take precedence.
Override Class Declarations with Inline Styles
So we've proven that id declarations override class declarations, regardless of where they
are declared in your styleelement CSS.
There are other ways that you can override CSS. Do you remember inline styles?
Use an inline styleto try to make our h1element white. Remember, in line styles look like
this:
<h1 style="color: green;">
Leave the blue-textand pink-textclasses on your h1element.
Override All Other Styles by using Important
Yay! We just proved that inline styles will override all the CSS declarations in
your styleelement.
Página 6|9
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
Let's go all the way back to our pink-textclass declaration. Remember that our pink-
textclass was overridden by subsequent class declarations, id declarations, and inline
styles.
Let's add the keyword !importantto your pink-text element's color declaration to make 100%
sure that your h1element will be pink.
An example of how to do this is:
color: red !important;
Use Hex Code for Specific Colors
Did you know there are other ways to represent colors in CSS? One of these ways is called
hexadecimal code, or hex codefor short.
We usually use decimals, or base 10 numbers, which use the symbols 0 to 9 for each
digit. Hexadecimals(or hex) are base 16 numbers. This means it uses sixteen distinct symbols.
Like decimals, the symbols 0-9 represent the values zero to nine. Then A,B,C,D,E,F
represent the values ten to fifteen. Altogether, 0 to F can represent a digit in hexadecimal,
giving us 16 total possible values. You can find more information about hexadecimal
numbers here.
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;
}
Use Hex Code to Mix Colors
To review, hex codes use 6 hexadecimal digits to represent colors, two each for red (R),
green (G), and blue (B) components.
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.
Color Hex Code
Dodger Blue #1E90FF
Green #00FF00
Orange #FFA500
Red #FF0000
Use Abbreviated Hex Code
Many people feel overwhelmed by the possibilities of more than 16 million colors. And it's
difficult to remember hex code. Fortunately, you can shorten it.
Página 7|9
For example, red's hex code #FF0000can be shortened to #F00. This shortened form gives one
digit for red, one digit for green, and one digit for blue.
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.
Color Short Hex Code
Cyan #0FF
Green #0F0
Red #F00
Fuchsia #F0F
Use RGB values to Color Elements
Another way you can represent colors in CSS is by using RGBvalues.
The RGB value for black looks like this:
rgb(0, 0, 0)
The RGB value for white looks like this:
rgb(255, 255, 255)
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)
Use RGB to Mix Colors
Just like with hex code, you can mix colors in RGB by using combinations of different
values.
Replace the hex codes in our styleelement with their correct RGB values.
Color RGB
Blue rgb(0, 0, 255)
Red rgb(255, 0, 0)
Orchid rgb(218, 112, 214)
Sienna rgb(160, 82, 45)
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.
Create a custom CSS Variable
To create a CSS Variable, you just need to give it a namewith two dashesin front of it and
assign it a valuelike this:
Página 8|9
--penguin-skin: gray;
This will create a variable named --penguin-skinand assign it the value of gray.
Now you can use that variable elsewhere in your CSS to change the value of other elements
to gray.
Página 9|9