CSS From 0 To Hero
CSS From 0 To Hero
By Halim Boussada
● CSS Selectors.
● CSS Combinators
● Live coding.
CSS Basics
What is CSS
What is CSS ?
● Used to describe the presentation of HTML document.
CSS Selectors
● A CSS selector used to "find" (or select) the HTML elements you want to style.
● Types of selectors:
• HTML Tag
• ID (#)
• Class (.)
Question: When should I use tag selector with CSS? ID Selector? Class Selector?
CSS Basics - Selectors
By Tag
S page.html S style.css
By ID
S page.html S style.css
By Class
S page.html S style.css
<h1 class=”title-2”>
My Title
</h1>
1. Inline styles.
2. Internal stylesheet.
3. External stylesheet.
CSS Basics
• Inline style
● The Style attribute in a specific tag used to create inline style
● The style attribute can contain any CSS property between double quotation
<p style=”background-color:red;”>Test</p>
CSS Basics
<style type="text/css">
p{ background-color:red; }
</style>
CSS Basics
● The <link> tag is most often used to link to external style sheets or to add a favicon to your website.
● A CSS selector can contain more than one simple selector. Between the
simple selectors, we can include a combinator.
+ Adjacent ~ General
Sibling + Sibling ~
div + p { div ~ p {
} }
<div>
h2 + p {
<h2>Not applied</h2>
color: red;
<p>CSS applied</p>
}
<h2>Not applied</h2>
<h3>Not applied</h3>
<p>Not applied</p>
<h2>Not applied</h2>
<p>CSS applied</p>
</div>
CSS Basics - CSS Combinators
General Sibling:
● Elements share the same parent.
<div>
<h2>Not applied</h2>
h2 ~ p { <p>CSS applied</p>
color: red;
} <h2>Not applied</h2>
<h3>Not applied</h3>
<p>CSS applied</p>
</div>
CSS Basics - CSS Combinators
Child
<div>
<div>Not applied</div>
<p>CSS applied</p>
div > p {
<div>Not applied</div>
color: red; <article>
} <p>Not applied</p>
</article>
<p>CSS applied</p>
</div>
CSS Basics - CSS Combinators
Descendant
● Second element is a descendant of the first element.
<div>
<div>Not applied</div>
div p { <p>CSS applied</p>
color: red; <div>Not applied</div>
} <article>
<p>CSS applied</p>
</article>
<p>CSS applied</p>
</div>
CSS Basics
CSS Specificity
● If there are two or more CSS rules that point to the same element, the selector with the highest
specificity value will "win", and its style declaration will be applied to that HTML element.
● There are four categories which define the specificity level of a selector:
○ Inline styles -
■ Example: <h1 style="color: pink;">
○ IDs -
■ Example: #navbar
○ Classes, pseudo-classes, attribute selectors -
■ Example: .test, :hover, [href]
○ Elements and pseudo-elements -
■ Example: h1, :before
CSS Basics
CSS Specificity
<html>
<head>
<style>
#demo {color: blue;}
.test {color: green;}
p {color: red;}
</style>
</head>
<body>
</body>
</html>
CSS Basics
CSS Specificity
● When selectors have an equal specificity value, the latest rule is the one that counts.
<html>
<head>
<style>
h1 {background-color: yellow;}
h1 {background-color: red;}
</style>
</head>
<body>
CSS Specificity
● Inline style
● Internal style
● External CSS
○ These CSS files will inherit properties written in them once we include them in our application.
○ We will be able to apply internal CSS that overrides the external CSS properties thing will and
○ the same happen in the case of inline CSS. Inline CSS overrides the external and internal CSS.
The Box Model
Developer Tool
Learning Objectives:
● Of course, the technology to place web components on a page is not new. Since the
beginning of the Internet, web designers have used different ways to place images, text,
and other content where they wanted it to be.
● However, these were either not suitable for responsive design (tables),
● were never intended for as a layout tool in the first place (float),
● didn’t allow you to define equal heights for elements (inline-block), or had other issues.
CSS - FlexBox
How to use Flexbox (Concepts and Terminology)
● To start using the Flexbox model, you need to first define a flex container.
● The flex container becomes flexible by setting the display property to flex.
flex-direction:
Flexbox is (aside from optional wrapping) a single-direction layout
concept. Think of flex items as primarily laying out either in horizontal
rows or vertical columns.
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
flex-wrap:
● By default, flex items will all try to fit onto one line. You can
change that and allow the items to wrap as needed with this
property.
flex-flow:
● This is a shorthand for the flex-direction and flex-wrap properties, which together
define the flex container’s main and cross axes.
The default value is row nowrap.
CSS - FlexBox
justify-content:
● It helps distribute extra free space leftover when either all the
flex items on a line are inflexible, or are flexible but have
reached their maximum size. It also exerts some control over
the alignment of items when they overflow the line.
CSS - FlexBox
align-items:
● This defines the default behavior for how flex items are laid
out along the cross axis on the current line.
.container {
align-items: stretch | flex-start | flex-end | center |
baseline | first baseline | last baseline | start | end |
self-start | self-end + ... safe | unsafe;
}
CSS - FlexBox
align-content:
.container {
align-content: flex-start | flex-end | center | space-
between | space-around | space-evenly | stretch | start |
end | baseline | first baseline | last baseline + ... safe
| unsafe;
}
CSS - FlexBox
align-content:
CSS - FlexBox
○ order
○ flex-grow
○ flex-shrink
○ flex-basis
○ flex
○ align-self
CSS - FlexBox
Order:
● By default, flex items are laid out in the source
order.
● However, the order property controls the order
in which they appear in the flex container.
● Items with the same order revert to source
order.
CSS - FlexBox
flex-grow:
● This defines the ability for a flex item to grow if
necessary. It accepts a unitless value that serves
as a proportion. It dictates what amount of the
available space inside the flex container the item
should take up.
flex-grow:
CSS - FlexBox
flex-shrink:
flex
● This is the shorthand for flex-grow, flex-shrink and flex-basis combined.
● The second and third parameters (flex-shrink and flex-basis) are optional.
● The default is 0 1 auto, but if you set it with a single number value, like flex:
5;, that changes the flex-basis to 0%, so it’s like setting flex-grow: 5; flex-
shrink: 1; flex-basis: 0%;.
CSS - FlexBox
align-self:
Learning Objectives:
● Grid vs flexbox
● Grid Container
● Grid Line
● Grid Item
● Grid Cell
● Grid Track
● Grid Area
CSS - Grid
Grid Container:
○ It’s the direct parent of all the grid items. In this example div
with class container is the grid container.
Grid container
CSS - Grid
Grid Item:
● The children (i.e. direct descendants) of the grid container.
○ Here the item elements are grid items, but sub-item isn’t.
Grid items
CSS - Grid
Grid Line:
● The dividing lines that make up the structure of the grid.
line3
○ The lines between columns are called column lines.
Grid Cell:
● The space between two adjacent row and two adjacent column grid lines.
■ Here’s the grid cell between row grid lines 1 and 2, and column grid lines 2 and 3.
CSS - Grid
Grid Track:
● The space between two adjacent grid lines.
■ Here’s the grid track between the second and third-row grid lines.
CSS - Grid
Grid Area
● The total space surrounded by four grid lines. A grid area may be composed of any number of grid
cells.
Here’s the grid area between row grid lines 1 and 3, and column grid lines 1 and 3.
CSS - Grid
Properties for the Grid container
● grid-column-start ● grid-area
● grid-column-end ● justify-self
● grid-row-start ● align-self
● grid-row-end ● place-self
● grid-column
● grid-row
CSS - Grid
Further exploration:
● CSS-Grid
● Grid By Example
Learning Objectives:
● This property allows you to rotate, scale, move, skew, etc…, elements.
CSS Transform
○ The translate() method moves an element from its current position to a new one.
○ With this code, the adjusted rectangle is moved 40 pixels to the right, and 100 pixels down
from the current position.
● Rotate:
transform: rotate(40deg);
CSS Transform
● Scale:
transform: skewX(30deg);
transform: skewY(30deg);
CSS Transform
● Skrew
○ If you want to skew both the x and y-axis, this
can be done in one place.
The skew() method skews an element along the
x and y-axis using the specified angles.
● Matrix
○ This is where things get interesting, but also more
efficient in the right situation. If you are doing a lot of
transforms and don’t want to write them out all
individually, these 2D transforms can be combined
with the matrix() method.
matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())
Instead of having property changes take effect immediately, you can cause the changes in a property to
take place over a period of time.
● CSS transitions allows you to change property values smoothly, over a given duration.
CSS Transitions
CSS transitions properties:
● transition
● transition-delay
● transition-duration
● transition-property
● transition-timing-function
CSS Transitions
How to Use CSS Transitions?
Note:
If the duration part is not specified, the transition will have no effect, because the default value is 0.
CSS Transitions
● transition-property : sets the CSS properties to which a transition effect should be applied.It could be a single property or
multiple properties
● transition-duration: The transition-duration CSS property sets the length of time a transition animation should
take to complete. By default, the value is 0s, meaning that no animation will occur.
● transition-delay: Defines how long to wait between the time a property is changed and the transition actually begins.
CSS Transitions
CSS transitions properties:
● transition-timing-function:
○ specifies the speed curve of the transition effect.
● transition : A shorthand property for setting the four transition properties into a single property
● CSS Animation:
● They allow you to create multi-step transitions through the @keyframes syntax or without it (limited).
● To use an animation you created, you can specify the animation in animation or animation-property.
CSS Animation
CSS Animation:
● @keyframes
● animation-name
● animation-duration
● animation-delay
● animation-iteration-count
● animation-direction
● animation-timing-function
● animation-fill-mode
● animation