Sitemap
Faith Rosella Kyalo

Faith Rosella Kyalo

Press enter or click to view image in full size
Cascading Style Sheet

Learning Basic CSS

5 min readAug 26, 2025

CSS — Cascading Style Sheet. this is a language that we use to style an HTML document.

CSS is used to beautify and give an attractive look to a web page(s).

Press enter or click to view image in full size
a sample of a webpage containing CSS

there are 3 methods for adding CSS to HTML
1. Inline CSS — add styles directly in the html element by using the style attribute.

<p style="color: pink; text-align: center:" >this is basic CSS</p>

2. Internal CSS — using <style> tags within a html document. this is done in the head tag.

<style>
body {
background-color: blue;
}
p {
color: red;
font-family: arial;
}
</style>

3.External CSS — linking an external CSS file to the html document. you create a CSS file and then link it to the html file using;

<link rel="stylesheet" href="styles.css">

this is done in the head section.

CSS properties
1.colors- there are 3 ways to set colors;

h1 { color: red;
color: rgb(0,0,255);
color: #0000ff;}

2. background. the properties are;

background-color, image, repeat, position, attachment. but instead of specifying all the background properties, you can just use a shorthand property.

body {
background: blue url(https://medium.com/"bright.jpg") no-repeat right top;
}

3. border. the properties include;

border-style, width, color, radius, sides, left, right, top, bottom. to simplify all these or to shorten the code, we just use a shorthand property.

p {
border: 8px solid red;
}

if you would like your borders to be rounded, you use border-radius

p {
border-radius: 5px;
}

4. margin. The CSS margin properties are used to create space around elements, outside of any defined borders.

the properties include;

margin-top, right, bottom, left. and they can have the following values auto, inherit, length, %. still you can use a short hand property. this margin property has 4 values; top margin, right margin, bottom margin and left margin respectively.

margin: 20px 10px 15px 12px;

5. padding. padding properties are used to generate space around an element’s content, inside of any defined borders.

these properties include;

padding-top, right, bottom, left. and they can have the following values auto, inherit, length, %. still you can use a short hand property. this padding property has 4 values; top padding, right padding, bottom padding and left padding respectively.

padding: 20px 10px 15px 12px;

the properties of CSS are many to mention but a few. others include; font, text among others.

Specificity — the prioritization of styles.
Selectors — selects the html element you want to style.
- simple selectors
- combinator selectors
- pseudo-class selectors
- pseudo-elements selectors
Attribute selectors
id — highest priority
classes — 2 highest priority
generics — lowest priority

Get Faith Rosella Kyalo’s stories in your inbox

Join Medium for free to get updates from this writer.

In situations where we need to maybe give one color to a certain paragraph and another paragraph a different color, we create class names.
class names provides a way to single out certain elements and we only want to add styles to them. for example in html

<p class="paragraph-1"> we are learning basic CSS </p>

in CSS

.paragraph-1 { color: red;}

for classes we use a dot (.) at the beginning
for id we use #

#id-name{color: black}

Combinators. this is something that explains the relationship between the selectors. these include
-descendant combinators(space)
-child combinators(>)
-next sibling combinators(+)
-subsequent-sibling combinators(~)

Pseudo classes. this is used to define a special state of an element.
syntax of pseudo-classes

selector:pseudo-class {
property: value;
}

Pseudo elements are used to style specific parts of an element.
syntax

selector::pseudo-element {
property: value;
}
  1. first-line can only be applied to block-level elements.
    properties of first-line
    -font, color, background, word-spacing, letter-spacing, text-decoration, vertical-align, text-transformation, line-height, clear.

::first-line is used to add a special style to the first line of a text.

p::first-line {
color: #ff0000;
font-variant: small-caps;
}

2. first-letter, to add a specific style to the first letter of a text.

p::first-letter {
color: blue;
font-size: xx-large;
}

the ::before pseudo element
this is used to insert some content before the content of an element.

h1::before {
content: url(https://medium.com/smiley.gif);
}

the ::after pseudo element
this is used to insert some content after the content of an element.

h1::after{
content: url(https://medium.com/smiley.gif);
}

the ::marker pseudo element
this is selects the markers of list items.

::marker {
color: red;
}

the ::selection pseudo element
this matches the portion of element that is selected by a user. this is applied to color, background, cursor, outline

::selection{
color: blue;
background: pink;
}

CSS opacity/transparency — this specifies the transparency of an element. the value is from 0.0 to 1.0
transparent hover effect

img {
opacity: 0.5;
}
img::hover{
opacity: 1.0;
}
transparent box
div {
opacity 0.5;
}

text in a transparent box

<html>
<head>
<style>
div.background {
background: url(https://medium.com/klematis.jpg) repeat;
border: 2px solid black;
}

div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
}

div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
</head>
<body>

<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.</p>
</div>
</div>

</body>
</html>

the box model
this divides the elements on our page into four different layers
content- the content of the box, where text and images appear -width: 200px;
padding- clears an area around the content — padding: 16px;
border- a border that goes around the padding and content — border: 8px solid red;
margin- clears an area outside the border — margin: 10px;
box-sizing — box-sizing: border-box;
box-sizing has 2 properties, the content-box and border-box
content-box is a default and it includes the width and height properties while border and padding are not.
border-box has the width and height properties and includes content, padding and border.

Press enter or click to view image in full size

CSS math functions
these functions allow mathematical expressions to be used as property values. e.g. calc(), max() and min()
calc(.) performs a calculation.
max(.) uses the largest value, from a comma separated list of values as the value property.
min(.) uses the smallest value, from a comma separated list of values as the value property.

CSS PERFORMANCE AND OPTIMIZATION
this helps your website to run smoothly and load faster even on slower devices.
1. use simple selectors
2. avoid universal selector for styling(*)
3. avoid inline styles
4. combine and minify CSS
5. load CSS efficiently
6. use shorthand properties
7. cache you CSS

--

--