CSS
CSS
CSS Basics
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document
written in HTML.
Desired Appearance
CSS Ruleset
p{
color: red;
p - selector
color - property
red - value
* You must use colon (:) to separate the property from its value or values in eac declaration.
* You must use a semicolon (;) to separate each declaration from the next one.
p{
color: red;
width: 500ppx;
p, li, h1 {
color: red;
}
* Element selector
* ID selector
* Class Selector
* Attribute selector
* Pseudo-class selector.
Selectors allow us to grab a particular element within our html page for us to style them or define their
properties.
Element Selector
p{
color: red;
Id Selector
#my-id {
margin: 5px;
Class Selector
my-class {
margin: 5px;
NOTE: id selectors are only applied to a single element (i.e it's a unique identifier) but a class selector
can be applied to different elements in the same class (i.e elements that have similar properties).
Attribute Selector
img[src] {
height: 100;
In the attribute selector above, you want to select all images with a source (src) attribute to them and
then apply that height (having provided the the link to where all those images are located)
Pseudo Selector
a:hover {
color: blue;
What the pseudo selector does is to grab all link elements from the html page and when you hover (i.e.
when that action happens) you apply the color "blue" to the elements.
* Inline Styles
* Internal Style
* External Styles
<head>
<style>
h1 {
color: blue;
margin-left: 20px;
}
</style>
</head>
<head>
</head>
External styles are styles that are in a different location, so you want to link to them, hence you need to
provide a link to where they are located.
* The best practice for styling an html document is the external stylesheet.
* When you specify more than one CSS type to an element, the style for that item follows the cascading
rule.
Note that when you define different styles for the same element, say different colours for same
element, the last one defined will take effect on the element.
Note that we said in applying our external styles, we create another folder, say styles, then create a file
index.css where we are going to put our external styles. Now, for them to be applied, we have to go
back to our html file and link it. An example of linking external styles is shown below:
Note that "./" represents our root folder. "rel" regards represents "relationship" and thus must always
be provided after stating the address of the external styles.
Note also that sometimes, we might want to apply different styles to different elements. We can use
inline Styles to do this, but where they are many and we just want to do everything in our external
styles, we use the id selector. Here, we name the elements in our html file using id=" " (and we put the
id name we want within the quotes), and then going to our file containing our external styles (index.css),
we, grab the id from our html page using #footer-p {}, where "footer-p" is the id name we provided in
our html page and then we now provide the styles we want for that particular element inside the curly
brackets. Hence, an id selector can be used to uniquely style a particular element.
Remember just as we said, we can use the class selector to group and style all the elements we want to
style as a block. Here, in our html page, we name the class, then in our index.css (external styles) page,
we denote the class with a dot, say .title (where title is the class name) and we apply all the styles we
want. Note that we can now apply these styles for the class selector for other elements by giving them
the same class name in our html page.
Padding is the space within an element. Margin is the space around an element.
In our external styles file (index.css), we can decide to grab entire html and style it, using:
html, body {
We will define all the styles we want inside the curly brackets.
If we want to grab element inside another element, say to grab an "a" element inside a "nav" element,
we use:
nav a{
In our index.css, we can style a class selector, say we have a class called "main", as shown below:
.main {
If say we have two class selectors, say "main" and "nav" that we want to nest their styling together, we
use:
.nav, .main {
CSS VARIABLES
CSS variables or custom properties reduce repeatability and also allows you to write smarter and more
usable kind of roll sets within your CSS or style sheets.
element {
--main-text-color: brown;
}
/* Use in a selector */
selector {
color: car(--main-text-color)
Global Scope
:root {
--main-bg-color: brown;
Note that the global scope will apply to the entire html page.
Note that in order to make changes to your website, go to your defined custom properties and change it
there and all changes will be applied to all the elements that will be used in custom properties value.
CSS layout is defining how different content or different elements and their content are arranged in an
HTML page and how we could actually achieve that with CSS.
* Grid
* Flexbox
Block is where each element is on its own line, then inline is where we have blocks or content next to
each other.
We create a grid container by declaring display: grid or display: inline-grid on an element.
/* Grid display */
.grid-container {
display: grid;
.grid-container {
display: inline-grid;
Grid Styles
<section class="grid-container">
<div>One</div>
<div>Two</div>
<div>Three</div>
</section>
/* Grid display */
. grid-container {
display: grid;
Grid Tracks
These are basically the space between different kinds of grid lines.
Grid Columns
Let's add the grid-template-columns property, then defining the size of the column tracks.
/* Grid display */
. grid-container {
display: grid;
Grid Rows
Let's add the grid-template-rows, then defining the size of the column tracks.
/* Grid display */
. grid-container {
display: grid;
Note that with grid-template-rows you are defining the height of the grid elements, while grid-template-
columns define the width.
The fr Unit
The fr (fraction) unit references a fraction of the available space in the grid container.
/* Grid display */
. grid-container {
display: grid;
You can also mix fixed and flexible sizes as shown below:
/* Grid display */
. grid-container {
display: grid;
Note that what the styling "1fr 1fr 1fr" means is that the columns (or rows) divides the available space
equally among themselves.
The Repeat ()
Large grids with many tracks can use the repeat() notation.
. grid-container {
display: grid;
. grid-container {
display: grid;
The repeat () notation just improves the way we write. The repeat () notation can also be used to
achieve a lot of styling patterns as shown below.
. grid-container {
display: grid;
We use grid-auto-rows to ensure that tracks created in the implicit grid are of a given size.
/* Using grid-auto-rows */
. grid-container {
display: grid;
grid-auto-rows: 200px;
Grid minmax()
. grid-container {
display: grid;
Grid Lines
We have row lines, which indicates where a row begins and ends, and also column lines which indicates
where a column begins and ends.
Grid Cells
These are the specific spaces that have been defined within a grid.
Grid Areas
Grid Gutters
Grid gutters or alleys or spaces between grid cells can be created using the column-gap and row-gap
properties, or the shorthand, gap.
. grid-container {
display: grid;
column-gap: 10px;
row-gap: 15px;
/* Flex display */
.flex-containter {
display: flex;
.flex-containter {
display: inline-flex;
Note that flex is blocked, while inline-flex shows that you can have another inline element next to it.
Flex Direction
Flex provides a property called flex-direction which specifies the direction the flexbox children are laid
out in.
/* Flex direction */
.flex-containter {
display: flex;
flex-direction: column;
We can also lay out flex items in a reverse direction using row-reverse and column-reverse values.
/* Flex direction */
.flex-containter {
display: flex;
flex-direction: row-reverse;
Flex Wrap
We use flex-wrap to prevent flex children from breaking out of their container.
/* Flex wrap */
.flex-containter {
display: flex;
flex-wrap: wrap;
. flex-containter {
display: flex;
Flex wrap makes sure that every flex element or every flex item fits within the flexport container as
much as possible without breaking out of it.
Flex Sizing
We sue the flex the flex property to control what propertion of space flex items wake up compared to
other flex items.
/* Flexbox sizing */
.flex-item {
flex: 1;
.flex-item {
flex: 1 200px;
Flexbox Alignment
You can also use the flexbox features the align flex items along the main or cross axis.
/* Flexbox Alignment */
. flex-containter {
display: flex;
align-items: centre;
justify-content: space-around;
You can override the align-items behaviour for individual flex-items by applying the align-self property to
them.
/* Flexbox align-self */
.flex-item: first-child {
align-self: flex-end;
Flexbox Ordering
We use the order property to change the layout order of flex items without affecting the source order.
/* Flexbox Ordering */
.flex-item: first-child {
order: 1;
* To conditionally apply styles based on features like device types or screen sizes.
CSS ANIMATION
Animation makes it possible to animate transition from one CSS style configuration to another.
* They are easy to use for simple animations; you can create them without even having to know
JavaScript.
* The animations run well, even under moderate system load. Simple animations can often perform
poorly in JavaScript.
The animation property is specified as one or more single animations, separated by commas.
CSS @keyframes
* At-rules are CSS statements that instruct CSS how to behave. They begin with an at sign (@).
* The @keyframes CSS at-rule controls the intermediate steps in a CSS animation sequence.
We can't animate anything if we don't have the @keyframes specified.
@keyframes slidein {
from {
transform: translateX(0%);
to {
translateX(100%);
Examples:
p{
animation-duration: 3s;
animation-name: slidein;
@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
to {
margin-left: 0%;
width: 100%;
}
* Making it repeat
p{
animation-duration: 3s;
animation-name: slidein;
animation-iteration-count: infinite;
@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
to {
margin-left: 0%;
width: 100%;
p{
animation-duration: 3s;
animation-name: slidein;
@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
}
75% {
font-size: 300%;
margin-left: 25%;
width: 150%;
to {
margin-left: 0%;
width: 100%;
NOTES
* JavaScript is used to add interactivity to the websites and web applications or HTML pages created.
* HTML is the markup language used to define the contents displayed on the pages that make up the
websites and web applications.
* CSS is the styling tool or the styling technology that is used to dictate or define the appearance of
these content (html content) into the web applications or websites.