Info2180 Lecture 3
Info2180 Lecture 3
Info2180 Lecture 3
CASCADING STYLESHEETS
(CSS)
CSS
<p>
<font face="Arial">Welcome to The Best Website.</
font>
You will <b>never</b>, <i>ever</i>, <u>EVER</u>
find a
<font size="+4" color="red">BETTER</font> website!
</p>
NOW WE USE CSS
CSS
▸ Internal/Embedded Stylesheet
EXTERNAL STYLESHEET
<link href="my-styles.css"
rel="stylesheet" type="text/css">
Place this between the <head> head> tags in your HTML document.
Recommended way as it’s easier to maintain and you don’t mix presentation
with content.
<
/
CSS
INTERNAL/EMBEDDED STYLESHEET
<style type="text/css">
/* Put CSS rules here */
</style>
INLINE STYLES
Not Recommended as its dif cult to maintain and mixes presentation with content.
fi
CSS
p {
color: red;
}
p,
h1,
li {
color: red;
}
CSS
p, li {
font-size: 16px;
line-height: 2;
letter-spacing: 1px;
}
CSS
TYPES OF SELECTORS
▸ Element Selector (e.g. p selects <p>)
▸ Pseudo Selector (e.g. a:hover, selects <a> but only when mouse
hovers over link)
CSS COMBINATORS
DESCENDANT SELECTOR
div p {
background-color: yellow;
}
fi
CSS
CHILD SELECTOR
▸ The child selector selects all elements that are the children
of a speci ed element.
div > p {
background-color: yellow;
}
fi
CSS
div + p {
background-color: yellow;
}
fi
CSS
div ~ p {
background-color: yellow;
}
fi
THE CASCADE
In CSS, all styles Cascade from the
top of the stylesheet to the
bottom. Therefore, styles can be
added or overwritten as the
stylesheet progresses.
p {
background: orange;
font-size: 24px;
}
p {
background: green;
}
p {
background: green;
background: orange;
}
There are, however, times where
the cascade doesn’t play so nicely.
Those times occur when different
types of selectors are used and
the speci city of those selectors
breaks the cascade.
fi
SPECIFICITY
SPECIFICITY
http://learn.shayhowe.com/html-css/getting-to-know-css/#specificity
SPECIFICITY
SPECIFICITY WEIGHT
#food {
background: green;
}
p {
background: orange;
}
#food (1-0-0) is more speci c than p (0-0-1).
fi
<div class="hotdog">
<p>...</p>
<p>...</p>
<p class="mustard">...</p>
</div>
.hotdog p {
background: brown;
}
.hotdog p.mustard {
background: yellow;
}
.hotdog p.mustard (0-2-1) is more
speci c than .hotdog p (0-1-1).
fi
COLOURS
COLOURS
.my-class {
background: maroon;
}
.some-other-class {
background: yellow;
}
HEXADECIMAL
.some-class {
background: #800000;
}
.another-class {
background: #fc6;
}
.task {
background: rgb(128, 0, 0);
}
.task {
background: rgba(128, 0, 0, .25);
}
HUE-SATURATION-LIGHTNESS (HSL)
.task {
background: hsl(0, 100%, 25%);
}
.count {
background: hsla(60, 100%, 50%, .25);
}
Adobe Color CC
https://color.adobe.com/
Coolors
https://coolors.co/app
UNITS OF
MEASUREMENT
CSS
UNITS
▸ Pixels
▸ Percentages
▸ Em
▸ REM
p {
font-size: 14px;
}
div {
width: 50%;
}
EXAMPLE WITH EM
.banner {
font-size: 14px;
width: 5em;
}
http://learn.shayhowe.com/html-css/opening-the-box-model/
BOX MODEL EXAMPLE
BOX MODEL EXAMPLE
div {
border: 6px solid #949599;
height: 100px;
margin: 20px;
padding: 20px;
width: 400px;
}
BOX MODEL EXAMPLE
div {
margin: 10px 20px 0 15px;
padding: 5px 10px 0 15px;
}
Top, Right, Bottom, Left
BOX MODEL
BORDERS
BORDER DECLARATION
div {
border: 6px solid #949599;
}
BORDER RADIUS
div {
border-radius: 5px;
}
div {
border-top-right-radius: 5px;
}
BOX SIZING
div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
What are those hyphens and
letters (-webkit-, -moz-)
before the property?
CSS
VENDOR PREFIXES
div {
border: 6px solid #949599;
height: 100px;
margin: 20px;
padding: 20px;
width: 400px;
box-sizing: border-box;
}
BOX MODEL EXAMPLE
This is because the border and padding measurements are now included in the width and
height of the content and are no longer added separately.
CSS CUSTOM
PROPERTIES (VARIABLES)
CSS
h1 {
color: var(--main-bg-color);
}
div {
background-color: var(--main-bg-color);
padding: var(--padding-small);
}
CSS
h1 {
color: var(--main-bg-color, black);
}
▸ Floats
▸ Relative Positioning
▸ Absolute Positioning
NORMAL FLOW
<header>...</header>
<section>...</section>
<aside>...</aside>
<footer>...</footer>
NORMAL FLOW
LAYOUTS AND POSITIONING
FLOATS
section {
float: left;
margin: 0 1.5%;
width: 63%;
}
aside {
float: right;
margin: 0 1.5%;
width: 30%;
}
FLOATS
FLOATS
CLEARING FLOATS
CLEARING FLOATS
▸ To prevent content from wrapping around oated elements, we
need to clear, or contain, those oats and return the page to its
normal ow.
▸ The left value will clear left oats, while the right value will
clear right oats. The both value, however, will clear both left
and right oats and is often the most ideal value.
fl
fl
fl
fl
fl
fl
FLOATS
CLEARING FLOATS
footer {
clear: both;
}
LAYOUTS AND POSITIONING
RELATIVE POSITIONING
div {
height: 100px;
width: 100px;
}
.offset {
left: 20px;
position: relative;
top: 20px;
}
LAYOUTS AND POSITIONING
ABSOLUTE POSITIONING
section {
position: relative;
}
.offset {
right: 20px;
position: absolute;
top: 20px;
}
LAYOUTS AND POSITIONING
FLEXBOX
▸ Flexible Box Layout (Flexbox) is a layout method designed for
one-dimensional layout. One-dimensional means that you wish
to lay out your content in a row, or as a column.
▸ Flex containers (parent) will then contain one or more Flex items
(children).
fi
fl
CSS
FLEXBOX
▸ By default the main axis is along the row and the cross axis
is runs along the column. However this can be changed.
CSS
FLEXBOX
▸ The direction Flex items are in can be de ned using the flex-direction
property. They value of this property can be row, row-reverse, column or
column-reverse.
▸ You can horizontally align these Flex items by using the justify-content
property. And this property takes the values flex-start, flex-end, center,
space-between or space-around.
▸ You can vertically align Flex items by using the align-items property. And
this property takes the values flex-start, flex-end, center, baseline or
stretch.
▸ You can also create gaps between ex items using the gap, column-gap or
row-gap properties.
fl
fi
CSS
FLEXBOX
<div class="flex-container">
</div>
EXAMPLE CSS
.flex-container {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
}
EXAMPLE OF EVENLY DISTRIBUTED FLEXBOX ITEMS
EXAMPLE HTML
<div class="flex-container">
<div id="box1" class="flex-item">Box 1</div>
<div id="box2" class="flex-item">Box 2</div>
<div id="box3" class="flex-item">Box 3</div>
</div>
EXAMPLE CSS
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
}
And there are many more interesting
layouts that can be created using a
combination of ex box properties
and their respective values.
fl
CSS
CSS GRIDS
fi
GRID GAPS
‣ A grid gap is basically a gutter or alley between tracks.
CSS
CSS GRIDS
▸ You de ne a Grid container (parent) by setting display: grid; on
an element.
▸ With these set, the direct children of the grid-container (parent) now
become grid items and the auto-placement algorithm lays them out,
one in each grid cell. Creating extra rows as needed.
fi
fi
CSS
CSS GRIDS
▸ grid-column: 1;
▸ grid-column: 1 / 3;
▸ grid-column: span 3;
CSS
▸ You can also mix absolute sized tracks with fraction units.
(e.g. 500px 1fr 2fr)
fl
CSS
▸ For large grids you can also use the repeat() notation to
repeat all or a section of the grid.
<div class="grid-container">
<div id="box1" class="grid-item">Box 1</div>
<div id="box2" class="grid-item">Box 2</div>
<div id="box3" class="grid-item">Box 3</div>
<div id="box4" class="grid-item">Box 4</div>
<div id="box5" class="grid-item">Box 5</div>
<div id="box6" class="grid-item">Box 6</div>
</div>
EXAMPLE CSS
.grid-container {
display: grid;
grid-template-columns: 150px 150px 150px;
grid-template-rows: 150px 150px;
grid-gap: 20px;
}
EXAMPLE OF A GRID WITH ITEMS THAT SPAN COLUMNS
EXAMPLE HTML
<div class="grid-container">
<div id="box1" class="grid-item">Box 1</div>
<div id="box2" class="grid-item">Box 2</div>
<div id="box3" class="grid-item">Box 3</div>
<div id="box4" class="grid-item">Box 4</div>
</div>
EXAMPLE CSS
.grid-container {
display: grid;
grid-gap: 20px;
}
EXAMPLE CSS
#box1 { #box3 {
grid-column: 1 / 3; grid-column: 1;
grid-row: 1; grid-row: 2;
} }
#box2 { #box4 {
grid-column: 3; grid-column: 2;
grid-row: 1 / 3; grid-row: 2;
} }
EXAMPLE CSS
.grid-container {
display: grid;
grid-gap: 20px;
}
And there are many more interesting
and complex layouts that can be
created using a combination of CSS
Grid properties and their respective
values.
HTML & CSS
▸ W3 Schools - https://www.w3schools.com