L03 CSS
L03 CSS
HTML originally did not contain tags for formatting a Web page
Tags (e.g., <font>) and color attributes were eventually added to enable
formatting, but…
1. this made HTML documents contain both elements that described
content as well as style / format the content
2. this made Web development long and expensive
Example
Cascading Style Sheets (CSS)
Describes the presentation of a document written in markup
languages
Saves a lot of work: can be used to control the layout of multiple web
pages all at once
Example
https://www.w3schools.com/css/demo_default.htm
CSS rule (a.k.a, ruleset)
Selector: identifies element(s) to be
styled
Declaration: specifies which of the
element’s properties to style
Property: defines the style or
behaviour of an element
Internal CSS: defined inside the <style> element, inside the head
section
● Typically used when a single HTML page has a unique style
adapted from:
https://www.w3schools.com/css/css_howto.asp
Internal CSS
adapted from:
https://www.w3schools.com/css/css_howto.asp
Inline CSS
adapted from:
https://www.w3schools.com/css/css_howto.asp
CSS selectors
Selects the HTML element(s) you want to style
Five types
● Simple: select elements based on name, id, class
● Combinator: select elements based on a specific relationship
between them
● Pseudo-class: select elements based on a certain state
● Pseudo-elements: select and style a part of an element
● Attribute: select elements based on an attribute or attribute value
Simple selector
Element selector
● Syntax: tagname
● Example:
Simple selector
ID selector
● Syntax: #idname
● Example:
Simple selector
Class selector
● Syntax: .classname
● Example:
Simple selector
Universal selector
● Syntax: *
● Example:
More than 1 rule can apply (example 1)
adapted from:
https://www.w3schools.com/css/css_howto.asp
Combining CSS selectors
AND condition
● Syntax: join multiple selectors without space in between
● Example:
Selector Example
element.class p.red_text
element#id p#paragraph1
.class1.class2 .red_text.centered_text
.class#id .centered_text#paragraph1
How do you select an element that is both p and a?
● Not possible!
● An element an only be of one type
CSS properties
Font properties
color
● Selects text color
font-family
● Selects one or more fonts, in that order (in case former one is
unavailable)
● Web safe fonts:
https://www.w3schools.com/cssref/css_websafe_fonts.php
font-style
● Either normal or italic
font-weight
● normal or bold are most used options, others are possible
Font properties (continued)
text-decoration
● none, underline, overline, or line-through
text-align
● left, right, center, or justify (all lines of text are same width)
● You likely want to use the hyphens property with justify
font-size
● Set size of text
https://www.w3schools.com/css/css_boxmodel.asp
Spacing properties
Many ways to specify the margin, padding, and border-width
Specify all edges
border-width: 1px 2px 3px 5px; (top-right-bottom-left)
margin: 0; (all edges)
padding: 1rem 2px; (top & bottom, left & right)
Trick
Margin can be negative to pull other elements closer
Border properties
border-style: none, solid, or dotted
border-color
border-radius
● Adds rounded corners to elements
● Trick: creates a circle when the width and height of an element are
equal and the radius is set to 50%
Position property
Specifies the type of positioning method used for an element
Five different positioning options:
● static (default)
● relative
● absolute
● fixed
● sticky
https://www.w3schools.com/css/css_positioning.asp
Relative positioning
Relative to its static position (where it would have been)
Absolute positioning
Relative to the nearest “positioned” ancestor
Fixed positioning
Stays in the same place on the screen, relative to the viewport, even
when the user scrolls
Transform property
Allows for translating (moving), rotating, scaling, and skewing of an
element
Examples:
div {
transform: translate(50px, 100px);
}
div {
transform: rotate(20deg);
}
div {
transform: scale(2, 3);
}
https://www.w3schools.com/css/css3_2dtransforms.asp
Display property
Specifies how to render an element and/or its children
Some display behaviours affects how child elements are placed
https://medium.com/@kathanpatel0000/display-property-8bc97b965800
Element size properties
width, height
● Defines the exact width/height of an element
max-width, max-height
● Defines the maximum width/height an element is allowed to have
● Good for sizing images
box-sizing
● content-box (default): Does not take border and padding into account
when calculating size of element
● border-box: Use this to save a lot of headaches
* { box-sizing : border-box; }
Summary
You have learned about CSS selectors and common CSS properties
Learn more on your own: http://www.w3schools.com/css/
Experiment, search online, and/or read reference manuals
Reproduction and/or sharing of course materials is prohibited. Course materials include lecture slides, course notes,
assignments, data and documents provided by the instructors. Course materials created by the instructors of this course
are their intellectual properties. They may not be shared, posted, rehosted, sold, or otherwise distributed and/or
modified without expressed permission from the authors. All such reproduction or dissemination is an infringement of
copyright and is prohibited. All rights are reserved by the instructors.