Name two CSS properties that we learned that help define the layout of an HTML document. →
display
position
Every element on a webpage is essentially treated as a box.
The display property determines how that box is rendered. What are some possible values of the display
property? →
inline
inline-block
block
none
We'll take a peek at some other interesting ones later. But first. →
An element that is display:block
:
div
, p
, h1
, etc.An element that is display:inline
width
or height
that can be specifiedIf an element is display:none
:
If an element is display:inline-block
:
Two consecutive inline elements will display ______________ each other. →
adjacent to
Two consecutive block-level elements will display ______________ each other. →
stacked on
Two consecutive inline-block elements will display ______________ each other. →
adjacent to
There are many other possible values for the display
property. Let's take a look at a weird one: →
Table related values:
table
table-cell
table-row
So… table
, table-cell
and table-row
are all display properties that apply to (as their names imply) tables, cells and rows.
However, you can make any element behave like a table; just add the appropriate display (but whyyyyy?) →
Maybe there's an element that describes your content more accurately than td
?
Oh yeah… what's the difference between visibility:hidden
vs display:none
. →
display:none
takes the element out of the document completely
visibility:hidden
keeps the element in the document, but does not show it
Now that we know some elements have a width and height, while some don't… we should probably figure out what width and height actually mean.
Is the width and height all there is to figuring out the dimensions of an element? What else has to be taken into account? →
border
margin
padding
How are they all related? Let's see on the next few slides. →
box-sizing is the CSS property that determines the box-model used to calculate the width
and height
of an element.
Two values that we'll look at are:
content-box
(the default)border-box
content-box is the default value for box-sizing. This box-model:
width
and height
are calculated by the content onlypadding
, border
, or margin
If the value of the border-box
property is set to border-box,
width
and height
include padding
and border
margin
box-sizing: border-box
is a bit new:
-moz-
for firefox and -webkit-
for Chrome and Safari
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
The position property specifies what rules to use for positioning an element in the document:
What are some possible values? (we know two of them at least) →
static
relative
absolute
fixed
sticky
The default positioning of an element is static
, or not positioned.:
top
, bottom
, left
, right
, and z-index
(depth) properties do not apply to an element that is not positionedAn element with position:relative
can be positioned relative to where it would normally be in the document:
top
, bottom
, left
, right
, or z-index
properties, the element will be moved accordinglyFor relatively positioned elements:
By default, these are all set to auto
. They can be set to a length or percentage of the containing element.
There's also a property called z-index. What does z-index do? →
An element with position:fixed
:
An element with position:absolute
:
Let's talk through what's involved in creating this layout. →
How did we make the header stay in place, cover every element and stretch across? →
header {
position: fixed;
top: 0;
left: 0;
height: 50px;
background-color: white;
width: 100%;
border:3px solid #090;
z-index: 1;
}
How do you think the nav is positioned so that it's on the top right of the header? →
nav {
position: absolute;
right: 0px;
top: 0px;
}
It's relative to its ancestor, the header… so we can use absolute.
Finally… how is the sidebar right aligned using the position property? →
.sidebar {
position: absolute;
right: 0px;
width: 150px;
border: 3px solid #800;
}
What will happen if we make position absolute? →
It'll go under the header and above the div with class="hero".
You might have noticed that there are multiple units of measurement that we could use for properties such as width
or margin
.
What are the possible units? →
There are many! (Here are a few that we'll go over).
em
rem
px
An em is a dynamic sizing unit relative to font-size:
em
is equal to the size of the font that applies to the parent of the element2em
is twice the size of the parent's font-sizerem represents the font-size of the root (<html>
element).
2rem
is twice the size of the root element's font-sizeBoth rem
and em
are relative measurements.
Why might using relative measurements be useful? →
Let's try upping the font size … when we have them initially set to pixels. →
It may be confusing determining which one to use. It depends on what you're trying to accomplish.
Some articles…
What's a selector again? →
A selector is the part of a CSS rule that determines what element or elements the style declarations apply to:
In the following code, p
is the selector:
p {
margin: 10px;
}
This one may be a bit difficult to answer… but what are the possible values for selectors? →
Some that you might be familiar with are:
class
nameid
class
and id
are attributes that you can place in your elements. What's the difference between the two again? →
class
This has a selector, .highlight
that gives an element a yellow background, and a couple of ids that specify borders.
You can use both a tag name and class or id as your selector for increased specificity:
In the previous example, how could be modify the markup so that div
s that are highlighted have a green background, while paragraphs that are highlighted stay with a yellow background? →
You're not limited to class
and id
for attributes:
attribute
or attribute_name='value'
[type='button']
- selects only elements with a type attribute that's equal to "button"[data-pinned]
- selects elements that just have an attribute called data-pinned (no associated value is necessary)input[type='button']
- you can also prefix to specify tag with attribute!A couple of examples using attributes other than class or id:
In some of the previous examples, you have noticed that there were several selectors on one line, separated by commas.
Selector grouping allows multiple selectors to be defined for a single rule:
Relationships between elements can also be expressed with selectors! (from the CSS selectors article on MDN):
A E
… Any E element that is a descendant of an A element (that is: a child, or a child of a child, etc.)A > E
… Any E element that is a (direct) child of an A elementE:first-child
… Any E element that is the first child of its parentB + E
… Any E element that is the next sibling of a B element (that is: the next child of the same parent)This demonstrates the selection of elements based on their relationships:
pseudo-classes are keywords that you add to a selector to specify the state of the element.
For example, what are some states that you think an anchor element (a link) could have? →
There are pseudo-classes for each of these states! →
:hover
- mouse hovers over (any element, not just links):link
- non visited link:visited
- visited linkBefore and after actually lets you insert content before or after an element!
::before
::after
Some values that the content can be are
Two examples, one with text… and the other with an image.
:nth-child(…) matches an element that is the nth child of its parent. Children are numbered starting from 1. The argument for nth-child can be:
nth-child(5)
- selects the 5th childnth-child(even)
- selects every even numbered childnth-child(2n + 3)
- selects every 2nd child starting from the 3rd
Try out nth-child using the nth-child tester from css-tricks…
Or, check out the fiddle:
Stylesheets can get unwieldy pretty quickly. Some ways to keep things readable an organized are:
/\* \*/
) when appropriateSome others in:
As we mentioned in the previous class, you can use an element's style property to modify its actual style properties.
An element's style property has properties as well… and these are the names of CSS properties.
var myelement = document.getElementById('myelement');
myelement.style.color = '#a20';
If the style property is hyphenated, use camel case.
// background-color becomes backgroundColor
myelement.style.backgroundColor = '#797';
You can also use the classList property of an element to add or remove classes. It has methods of the same name.
For the following element:
<p class="foo bar">first one</p>
You can use this code to manipulate its classes.
myelement.classList.add('baz');
myelement.classList.remove('foo');
What color will the background of the list item be, and how did you come to that conclusion? →
The CSS 3specs provides an algorithm for determining specificity - which selector has precedence over another:
Whew… um, so how did that actually work? Let's check out some examples:
Try to avoid access style… and instead toggle classes