Coding Bootcamp: Introduction to CSS

Introduction to CSS

Antonis Kalipetis

@akalipetis - https://www.akalipetis.com

Agenda

What is CSS

Adding CSS to HTML elements

CSS can be added to HTML elements using the style="..." attribute

<p style="...">Styled paragraph content</p>

An old CSS example

<div style="background-color:yellow;">
    <h1>My <span style="color:red">favourite</span> cities</h1>
    <h2>Athens</h2>
    <h2>Barcelona</h2>
</div>

Rendered:

My favourite cities

Athens

Barcelona

CSS Styling

CSS styling is made by defining CSS declarations - property: value pairs.

Each element is rendered after applying the declarations that match it.

<div style="background-color:yellow;">
    <h1>My <span style="color:red">favourite</span> cities</h1>
    <h2>Athens</h2>
    <h2>Barcelona</h2>
</div>

Notes:

Are we really going to add style to each and every element?

Of course not!

How can I add stylesheets then?

CSS selectors

CSS can be applied to one or multiple elements using CSS selectors.

Simple CSS syntax

selector {
    property: value;
}

Notes:

The previous example, with proper CSS

<style>
    .bg-yellow {
        background: yellow;
    }
    .text-red {
        color: red;
    }
</style>
...
<div class="bg-yellow">
    <h1>My <span class="text-red">favourite</span> cities</h1>
    <h2>Athens</h2>
    <h2>Barcelona</h2>
</div>

Advanced CSS selectors

CSS has more advanced selectors, for example

Defining colours in CSS

In CSS, there are multiple wayts to define a colour

Notes:

CSS Backgrounds

Adding a background to an element

CSS display: ... property

The display property is the most important one for handling layout.

Hiding elements

Hiding elements

<p>First</p>
<p style="display: none;">Second</p>
<p>Third</p>
<p style="visibility: hidden">Fourth</p>
<p>Fifth</p>

Rendered:

First

Second

Third

Fourth

Fifth

Notes:

Positioning elements

Positioning can be made using the following ways

Notes:

References

Time to train

Time to train

Exercise 1

Add a green background to the whole body

<!DOCTYPE html>
<html>
<head>
    <style>
        
    </style>
</head>
<body>
</body>
</html>

Exercise 2

Create a page with a blue div, that stays in the bottom of the screen at all times

Exercise 3

Create an external CSS file, import it to an HTML page and make all paragraphs have red-coloured text

Exercise 4

Make all paragraphs have a blue yellow, except the last one.

Hint: Use a special selector

<!DOCTYPE html>
<html>
<head>
    <style>
        
    </style>
</head>
<body>
    <p>First</p>
    <p>Second</p>
    <p>Third</p>
    <p>Fourth</p>
    <p>Fifth</p>
</body>
</html>

Thanks!

@akalipetis - https://www.akalipetis.com


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.