Reading notes for Code Fellows!
Imagine there is a box around every HTML element on a webpage. CSS controls the way each of those boxes is presented on the page. CSS associates style rules with HTML elements. Rules are structured as follows:
h1 {color: red;}
This rule changes to color of ‘heading 1’ text to red.
h1
and the declaration within the {} brackets is color: red;
CSS can be internal or external (sometimes even both) to the HTML document. However, the vastly preferred method is external.
<style>
tags, usually located in the <head>
of the HTML document. It is typically used in single page websites when it is used.<link>
element is added to the HTML document, usually in the <head>
, to tell the browser where to find the styling file for the webpage. Keeping the CSS in its own file keeps code cleaner and less cluttered. It also makes it easier to style multiple pages of a single site, because each separate HTML document can reference the same CSS file.Selector Type | Matched Target | Example |
---|---|---|
universal | all elements on page | * |
type | element names | h1, p, article |
class | elements with specified class attribute | .class |
ID | elements with specified id attribute | #id |
descendant | elements which are descendants of another element | li>a |
A hierarcy exists to govern application of conflicting CSS rules for the same targeted element.
!important
is added to the end of a declaration, it will always take precedenceSome CSS properties are inherited and others are uninherited. Inherited rules apply to the child elements of an element.
Examples of inherited properties are:
Examples of properties which are not inherited are:
Different operating systems and browsers (especially older or less-used ones) can cause your page styling to differ from what you planned. When this happens, it’s often called a “CSS Bug” or a “browser quirk”.
When selecting colors for an element, they can be specified in several ways. However, the three most common are:
Colors can be apply to different things.
The color
property sets the color of foreground items such as text.
The background-color
property sets the color of the background of a particular element.
It is important to select text and background colors that provide enough contrast to be legible.
Using color indicators like rgba
or hsla
, allows you to specify the opacity of a color. The “a” position is a value between 0.0 and 1.0 that represents a percentage of transparency. Some browsers may not support opacity so it is good practice to create an alternate backup CSS rule above the rule that those browsers can use.