02 CSS-Fundamentals-2
02 CSS-Fundamentals-2
Outline
1. CSS Syntax
2. CSS Selectors
3. Basic Text Styles
4. Margin, Border and Padding
2
CSS Syntax
3
CSS – Cascading Style Sheets
• CSS is used to control the presentation (look and
feel) and layout (positioning) of web page
elements
• Allows separating web page content from its
design and visual appearance
• Used in conjunction with HTML
• HTML is used for describing the content of a web
page, CSS is used for describing its presentation
• CSS a flexible, cross-platform, standards-based
styling language developed by the W 3 C
4
Style Sheets Syntax
• Stylesheets consist of rules that describe the
styling to be applied
o Each rule has selectors and declarations. A
declaration specifies a property and its value
5
Ways to incorporate CSS in an HTML document
• Inline – style included as the attribute of an HTML tag:
7
Inheritance
• Inheritance means that child elements will inherit
the properties applied to a parent element
9
Selectors: used to select elements to style on an HTML page
• Element Selectors - matches a HTML element directly
10
Specificity
• We have 3 selectors for the
h1 with different colors
o What will the color be?
11
Universal
• [*]: Selects all elements
o Used to override the browser default styles
*{
font-family: "Times New Roman";
color: blue;
}
12
An element can have more than one class
13
Combined Selectors
element, element div, p Selects all <div> elements and all <p>
elements
element element div p Selects all <p> descendant elements
inside <div> elements
element > element div > p Selects all <p> elements that are
direct child of <div> element
e.g.,
li a {text-decoration: none}
o This will match all <a> tags that are inside of <li>
https://www.w3schools.com/cssref/css_selectors.asp
14
Attribute Selectors
• Selects the elements whose attribute-value pair
matches the selector
[attribute ^= value] a[href ^= "https"] Selects every <a> element whose href
attribute value begins with "https"
[attribute $= value] a[href $= ".pdf"] Selects every <a> element whose href
attribute value ends with ".pdf"
[attribute *= value] a[href *= "qu"] Selects every <a> element whose href
attribute value contains "qu"
https://www.w3schools.com/cssref/css_selectors.asp
15
Pseudo-classes
• Pseudo-classes are used to define an element
state. E.g.,
•Style an element when a mouse is over it (:hover)
• Style visited and unvisited links differently (:visited , :link)
• Style an element when it gets focus (:focus)
e.g.,
a:hover { color: red; } -> Style link on mouse over
16
Structural Pseudo-classes
:first-child tr:first-child First row of an HTML table
https://www.w3schools.com/cssref/trysel.asp
17
has selector
• CSS :has() selector allows us to style an element
based on its descendants
• e.g., assign a green background to any div having
p as its descendent
18
Pseudo-elements
• A CSS pseudo-element is used to style specified
parts of an element. E.g.,
o Style the first letter or line of an element
21
Text-related CSS Properties
22
Text-related CSS Properties (2)
23
Font Sizing
Two ways to define font sizes in CSS
• Fixed Font Sizing: defines the size of fonts using
absolute units such as points (pt) or pixels (px)
• Relative Font Sizing: defines the size of fonts
using relative units such as em and rem units
o This allows fonts to scale appropriately to different
resolutions, browsers or platforms
o em is relative to the font-size of its direct or nearest
parent
o rem is relative to the font-size of the html (root)
element
24
Relative Font Sizing
body {font-size: .8em;
font-family: Verdana, Helvetica, Sans-Serif;}
h1 {font-size: 1.2em;}
div {font-size: .8em;}
li {font-size: .8em;}
p {font-size: .8em;}
25
Styles for Lists
• List properties are used to define the look and
feel of the list items
o Values for <ul>: circle, square,…
o Values for <ol>: upper-roman, lower-alpha
o Values for both: none
26
Practice …
• Use the W3Schools try-it-yourself editor to try
styling each of these properties
• Background
http://www.w3schools.com/css/css_background.asp
• Text
http://www.w3schools.com/css/css_text.asp
• Fonts
http://www.w3schools.com/css/css_font.asp
• Lists
https://www.w3schools.com/css/css_list.asp
27
Margin, Border and Padding
28
Box Model
29
Margin and Padding
• Margin and padding define the spacing around the
element
o Numerical value, e.g. 0.8rem
o Can be defined for each of the four sides separately: margin-
top, padding-left, … or using short rules:
• margin: 0.8rem;
o Sets all four sides to have margin of 0.8rem
.centered-rounded-box {
display: flex;
column-gap: 1rem;
align-items: center;
padding: 0.5rem;
border: 1px solid saddlebrown;
border-radius: 15px; Rounded Corner
box
width:80%;
margin: auto;
} Centered box
32
References
• CSS Tutorials http://www.w3schools.com/css/
• CSS Course https://web.dev/learn/css/
• Cheat sheet https://htmlcheatsheet.com/css/
• CSS developer guide
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS
• Selectors
http://code.tutsplus.com/tutorials/the-30-css-selectors-
you-must-memorize--net-16048
33