Class 4
Help yourself to some pizza in the kitchen!
Got a minute?
Take our special topics workshops survey!
http://tinyurl.com/gdispecialtopics
:first-of-type
to :first-child
:first-of-type
to :first-child
Now if you view index.html in your browser, it should look jacked up ☺ — let's fix that!
Selector | Definition | Example |
---|---|---|
:first-of-type |
The :first-of-type selector styles the first sibling of its type (e.g. specific tag and class) that is a child of its parent element |
Example » |
:first-child |
The :first-child selector styles the first child of its parent element, regardless of type |
Example » |
Use the row class on each row of content:
<section class="row">
</section>
Within the row, use the column class and the width class (e.g. span_4_of_12) on each column of content:
<section class="row">
<div class="column span_4_of_12">
I'm the first of three columns!
</div>
<div class="column span_4_of_12">
I'm the second of three columns!
</div>
<div class="column span_4_of_12">
I'm the third of three columns!
</div>
</section>
When an element is floated, subsequent elements wrap around it
The clear
property prevents floated elements from appearing on a specific side (left, right, or both)
See the Pen Clearing - Before by Liz Shaw (@anythingcodes) on CodePen.
Each row:
Each column:
✗ The Problem?
Floating takes columns out of normal flow, preventing columns from contributing to the height of their container!
✔ The Solution
The row clears floats, which properly:
How? With an :after
pseudoelement, which injects content at the end of an element
We use :after
to inject an empty cleared, block-level pseudoelement at the end of each row
.row:after {
content: '';
clear: both;
display: block;
visibility: hidden;
height: 0;
}
Tip: If you don't have the content
property, :after
won't work. visibility
and height
are optional, but address problems in older browsers.
Since :after
acts as the last item in our row (after the columns), it clears the float at that point and sets the row height properly
Memorize this ☺ — it comes up all the time!
Use the grid system with HTML5 elements to make our Stripes page
<main>
<article>
</article>
</main>
selector:nth-child(odd) {
background: red;
}
The :nth-child() selector allows you to pick specific child elements to style
You can use a keyword (e.g. even or odd), number (e.g. 2), or formula (e.g. 3n + 3)
See the Pen The nth-child Selector by Liz Shaw (@anythingcodes) on CodePen.
:nth-child(even)
and :nth-child(odd)
selectors to alternate each blog post's background color:nth-child(even)
and :nth-child(odd)
selectors to alternate the float
value for each image (left or right) like so:
Homework solutions will be emailed on Sunday
Let's get semantic! Use the grid system with HTML5 elements to make our Africa and Diet pages.
<main>
<aside>
<nav>
</nav>
</aside>
<article>
</article>
</main>
As a counterpart to :after
, :before
injects content at the beginning of an element:
p:before {
content: 'Read this: ';
}
See the Pen The :before Pseudoelement by Liz Shaw (@anythingcodes) on CodePen.
p:before {
content: url('your_image_path.png');
}
See the Pen The :before Pseudoelement with Images by Liz Shaw (@anythingcodes) on CodePen.
Pseudoelements (:after
and :before
) are awesome
Learning to use the :before
and :after
pseudoelements in CSS »
Try to style your sidebar like the design!
:before
to place this arrow image before each item
Homework solutions will be emailed on Sunday
We commonly see sidebars that take up the entire height of the main content area:
This is possible with float
s, but hardly fun!
Browser support — effectively all browsers support the float property (IE6+, Firefox 2+, Chrome 1+, etc.)
But there are new CSS3 alternatives
to make our lives easier!
Hurray!
Flexbox: Flexible Box Model, the latest and greatest CSS3 offering
It was created largely because of common responsive development problems — float
is great, but it requires too much extra attention!
The central idea is that flexbox keeps your layout fluid by doing a lot of the math for you!
Example: Use flexbox on a parent container. Its children will automatically have the same height — set child dimensions only if you need to!
Set display:flex;
on a parent container.
selector {
display: flex;
}
Now all children will have the same height, unless that height was set elsewhere
Let's solve our sidebar height problem using flexbox!
You can do a lot more with flexbox, including:
Articles & Examples:
'Using Flexbox Today' by Chris Wright »
'Solved by Flexbox' by Philip Walton »
Flexbox is so new that you will need to use vendor prefixes until the simple non-prefixed display:flex;
is supported in a browser:
selector {
display: -webkit-box; /* iOS 6-, Safari 3.1-6 */
display: -ms-flexbox; /* IE 10 */
display: -webkit-flex; /* Safari 6.1+. iOS 7.1+ */
display: flex; /* MODERN VERSIONS: Firefox, Chrome, Opera */
}
Order matters! The non-prefixed version should always go last.
Don't memorize this! Use shouldiprefix.com
... but really, don't memorize this.
Use shouldiprefix.com.
Old browsers (for example, IE 9 & below) were made before flexbox existed; these browsers don't know what flexbox is
You can use a polyfill, which is code that replicates the functions of a modern HTML5 or CSS3 feature for browsers that don't support that feature natively
Flexie is a flexbox polyfill
A variety of polyfills exist for other modern CSS3 properties and HTML5 elements
Use caniuse.com to check if a certain HTML5 or CSS3 feature is supported. If not, google for a polyfill!
Press the down arrow or spacebar to view
We can use CSS3 to make a nifty mobile menu!
We know:
transition
vw
) unitmax-width
and max-height
We need to know:
<input>
s, <label>
s, and the :checked
selector<input>
The <input>
element specifies an input field, usually in a form
Use the type="checkbox" attribute to make a checkbox:
<input type="checkbox" />
Why a checkbox?
Because we can use the :checked
selector as an animation trigger state!
If it's checked, we'll show the mobile menu
<input>
's <label>
<label>Toggle the checkbox!</label>
The <label>
element is used for labeling form elements, and is the clickable text for toggling a checkbox
To link a label with a checkbox, use the for="" attribute. The for value should be the same as the checkbox's id value, for example:
<input type="checkbox" id="toggler" />
<label for="toggler">Toggle the checkbox!</label>
See the Pen Associating a label with a checkbox by Liz Shaw (@anythingcodes) on CodePen.
In the HTML, we'll place our checkbox before the menu's <ul>
Then, in the CSS, we'll show the <ul>
if the checkbox is checked — we can do this with a sibling selector!
.our-checkbox ~ .our-unordered-list {
max-height: 0;
}
The above targets an element with class our-unordered-list if it is preceded by a sibling element with class our-checkbox
See the Pen :checked and the sibling selector by Liz Shaw (@anythingcodes) on CodePen.
Now that the label is clickable, we don't need to see that ugly checkbox — hide it with display:none;
We're going to use max-height:0;
and overflow:hidden;
on the before state of our navigation menu's <ul>
When the box is checked, we'll animate max-height
to become 100vh
.
See the Pen :checked and the sibling selector by Liz Shaw (@anythingcodes) on CodePen.
Let's make a mobile menu!
Press the down arrow or spacebar to view
Masonry layouts have become popular lately, largely thanks to sites with infinite scrolling such as Pinterest
Now we can make them much more easily with CSS3!
.masonry {
column-count: 3;
column-gap: 3;
}
column-count
and column-gap
also need vendor prefixes — check shouldiprefix.com!
Each item in a masonry layout is usually called a brick and has some additional styling:
.brick {
display: inline-block;
width: 100%;
margin: 0 0 1em 0;
}
.brick img {
width: 100%;
}
See the Pen CSS Masonry Layout by Liz Shaw (@anythingcodes) on CodePen.
Feel free to copy this to your diet.html page!
Press the down arrow or spacebar to view
transition
to the before statetransition
accepts two values,Before Selector | After Selector |
---|---|
|
|
To animate multiple properties, separate each effect with a comma
selector {
transition: width 2s, background 5s;
}
See the Pen Animating Multiple Properties - Before by Liz Shaw (@anythingcodes) on CodePen.
Method 2: All Properties
Use all
to seamlessly transition all properties from before to after states at the same duration
selector {
transition: all 3s;
}
See the Pen Transitions Using 'all' - Before by Liz Shaw (@anythingcodes) on CodePen.
Sounds great, right? However, all
has performance drawbacks, so use it sparingly! (Example »)
The box-shadow
CSS3 property is often used to give a 3D effect when an element is hovered over
selector {
box-shadow: horizontalShadow verticalShadow blurDistance color;
box-shadow: 10px 10px 5px grey;
}
See the Pen CSS3 Generator: box-shadow by Liz Shaw (@anythingcodes) on CodePen.
Don't worry about memorizing this — use a generator instead!
opacity
changes the opacity of the whole element
selector {
opacity: 0.5;
}
The value can be a number between 0 (translucent) and 1.0 (opaque)
See the Pen Opacity vs. RBGA by Liz Shaw (@anythingcodes) on CodePen.
opacity
is commonly used in hover states to reveal transparent content
See the Pen Opacity and Hover - Before by Liz Shaw (@anythingcodes) on CodePen.
You may have noticed that one of these things is not like the other. That's because one is a hover state!
Let's animate multiple properties (for example, box-shadow
and opacity
) for the hover states in the About section.
Solution:
See the Pen Box-Shadow and Opacity for hover states by Liz Shaw (@anythingcodes) on CodePen.
Useful Frontend Skills:
Useful Sites to Join or Follow:
Also, GDI is a super nationwide network of techie ladies!
Press the down arrow or spacebar to view
GitHub is free and a great intro to version control
A version control system records changes to a file or set of files over time so that you can keep track of changes and roll back to previous revisions
Almost every web agency uses a version control system
http://anything.codes is hosted for free on GitHub! See?
There are two paid services involved:
You can buy hosting from one company and a domain name from another, but it's usually less of a hassle to buy both from the same company
I use 1&1's Starter package, since it includes hosting and one domain name
When it comes to hosting, don't be fooled by the extra bells and whistles — if you aren't sure what something is, you probably don't need it
Why are there two separate services?
First, your files need to be stored on a web host, which is a computer (a.k.a. server)
When you register a domain, it doesn't 'point' to the server where your files are located
This is why your web host may give you a Domain Name System (or DNS) address, which is the IP address of the computer (a.k.a. server) where your files are located
The Domain Name System (DNS) is a huge list that links your domain name (e.g. anything.codes) to where your files are located (using the IP address of the server where your files are located)
Freelance Sites:
You are all awesome! Please keep in touch!