- Define CSS and describe its use
- How to add CSS rules to your app
- Diagram the structure of CSS
- Demonstrate how to apply rules to a specific tag
- List some common element properties that can be styled
- Describe ids and classes. Explain when should we use which
- Describe "The Cascade"
- Describe how to combine various selectors
- Describe Specificity
- Conclusion
- Fist to Five
- 1 - Iffy on CSS
- 5 - Rockstar
- CSS3 is pretty powerful now-a-days and can do cool stuff
- CSS3D + HTML5
- Describe 3 ways to add CSS to an app. Which is preferred? (David + Sophia)
- Describe the 3 components of a CSS rule. (Kiran + Michael)
- Describe
id
andclass
and use cases for each. (Kristina + Alan) - Explain specificity. (Alejandra + Daniel)
- Describe 3 ways to combine CSS selctors. (Matt + Audrey + Deb)
QUESTION: Can anyone define CSS for us?
HTML gives our site structure, but it doesn't do much in terms of how the site looks. This is where CSS comes in. CSS stands for:
- Cascading - We'll Define shortly
- Style - making things pretty!
- Sheets - it's just a sheet of text, not a program we write
QUESTION: What are 3 ways to add CSS to an app?
- A separate CSS file - This is the best practice. It allows multiple HTML pages to access a single file and share same set of CSS rules. You'll add a link to the CSS file in the
head
of your HTML page.
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
</body>
</html>
<style></style>
tags - You can add CSS rules to your HTML by addingstyle
tags to thehead
of your HTML page. The rules would only be accessible by that HTML page.
<html>
<head>
<style>
p {
color: red
}
</style>
</head>
<body>
</body>
</html>
- Inline styling - This is when you add CSS rules directly to the HTML element. It's the least preferred way to write CSS.
<p style="color: red">Hello World!</p>
What are 3 ways to add CSS rules to your page? Which is the best practice?
A CSS file is composed of many rules. Each rule governs the appearance of certain elements. The generalized form looks like:
selector {
property:value;
property:value;
property:value;
}
Everything inside the curly braces is called the "declaration block"
## Demonstrate how to apply rules to a specific tag
In Slack, can someone create the CSS rule to give all my `p` tags a red Georgia font and a yellow background?
If you wanted to style all the `<p>` tags of a page, you could style it by writing:
p {
color: red;
font-family: Georgia;
background: yellow;
}
What are the 3 components of a CSS rule?
Here are some properties that you can set for an element
####color
- names
- blue, red, yellow, white, grey, black, green, orange, purple
- http://www.crockford.com/wrrrld/color.html
- value
- hexidecimal number (0-F), six places
#0088FF
- https://color.adobe.com
- hexidecimal number (0-F), six places
- color
- url()
- measured in px (for now)
- System fonts
- Single word fonts
- Arial, Courier, Times, etc
- Multi word fonts must be placed in quotes
- "Times New Roman", "Arial Black", "Lucinda Console"
- Use http://www.cssfontstack.com/ to see what fonts are available on what operating systems
- Single word fonts
- Generic fonts
- serif, sans-serif, cursive, fantasy, monospace.
- can have several font families as a value
- starts with first and goes down the line until it finds one it has
- can have several font families as a value
- normal, bold
- left, right, center
- Create an html page with a
p
tag and some placeholder text
- Change its text color to a hex value that you choose from http://color.adobe.com
- Give it a background color
- Make the size of the text larger
- Change its font family to a sans-serif font
- Make the text bold
- Center the text
Sometimes just targeting an element is not enough. We can target other attributes of elements in our selectors.
<div id="left-column"></div>
#left-column {}
- each id on a page must be unique
- great for locations on a page that occur only once
- e.g. left column, contact form, etc
<div class="large-module"></div>
.large-module {}
- each class on a page does not have to be unique
- great for repeatable ideas
- module
- e.g. ad unit, comment block, etc
- function
- e.g. active, important,etc
- module
- Add two more
p
tags.
- Give one
p
tag an id - Give the other two
p
tags the same class - Use just the ids and classes to style the
p
tag with the id differently from the ones with a class
Some properties of elements are passed down to their children. In general:
- Properties dealing with text are inherited by their children
- Properties dealing with spacing are not inherited by their children
- https://www.w3.org/TR/CSS2/propidx.html
- Create a
p
tag within asection
tag
- Put some text in the
section
tag, but outside thep
tag - Put some text in the
p
tag - Style just the
section
tag to give its text some color - Note that the
p
tag has also been affected
Selectors can be more complex than just an element, id, or class.
- You can have a set of rules affect multiple sections by listing them
p, #left-column {}
- will style all
<p>
tags and whichever tag has and id of "left-column"
- will style all
- You can combine attributes (i.e. tag, class, id) to narrow down how many elements are effected
a.important {}
- styles all anchor tags that also have an "important" class
<a class="important"></a>
- styles all anchor tags that also have an "important" class
#left-column.visible {}
- styles whichever tag that has the id of "left-column", but only when it also has a class of "visible"
- styled:
<div id="left-column" class="visible"></div>
- not-styled:
<div id="left-column"></div>
- styled:
- styles whichever tag that has the id of "left-column", but only when it also has a class of "visible"
.profile.minimized {}
- styles all tags that have two classes (separated by a space): "profile" and "minimized"
<div class="profile minimized"></div>
div#left-column {}
- you could combine tags and ids, but there's only ever going to be one tag with an id of "left-column"
- it's not like you would have a group of tags with the same id, from which you want to select only those that are
div
tags - might as well just write
#left-column {}
and leave out the div
- You can filter out tags based on their ancestors
-
main p {}
- will style all
<p>
tags that are descendants of<main>
tags
<main> <p></p> </main>
<main> <section> <p></p> </section> </main>
- will style all
-
- Create a rule for all
p
tags that also have a class ofactive
- For this rule, make the text bold
- Add an
active
class to a few of your p tags - Create a rule for all
div
tags that also have a class ofactive
- For this rule, make the text very large
- Create a
div
tag with the class ofactive
When an element is being styled by two rules at the same time, the browser calculates which rule is more specific and applies that rule. To do this it looks at the selector. If both rules have the same specificity, the last rule is applied.
- Apply whichever rule has more ids in its selector
- In theory there should only be one or zero ids in each selector
#left-column #comments
- do not need to filter by ancestor
- can just write
#comments
- Apply whichever rule has more classes in its selector
- Apply whichever rule has more tags in its selector
Specificity is actually quite easy to calculate (these numbers are for example only):
selector | value |
---|---|
tag | 1 point |
class | 10 points |
id | 100 points |
inline | 1000 points |
When two selectors have the same net specificity score, then the last one applied wins. Note that stylesheets are read from top to bottom, and their styles are applied in order… therefore, styles defined lower within your stylesheet will win.
For example, an inline CSS style rule would have priority over a rule in a CSS stylesheet.
What is the net specificity of this selector `div#features a.slide h3 span`
This selector is worth **114 points**.
What type of CSS rule would take precidence?
- seperate stylesheet
- inline
- within
style
tags?
- Create a rule for the tag with an id of
blue
- Set the text color for this rule to blue
- On the next line of your css file, create a rule for all tags with classes of
red
- Set the text color for this rule to red
- In your HTML, create a
span
tag with both an id ofblue
and a class ofred
- Note that the color of the
span
tag should be blue
####Exercise #1 Add CSS to the Blog Mockup
####Exercise #2
REVIEW QUESTIONS
- Explain the use case for a
class
vs anid
- What is specificity?
RESOURCES
- Check out W3 Schools CSS documentation for extra stuff
- CSS Specificity