Week Three - CSS
Week Three - CSS
WEEK THREE
(CSS)
CASCADING STYLE SHEETS - HISTORY
• Origin of CSS:
• the creation of CSS is largely credited to Norwegian web pioneer named Håkon
Wium Lie
• In 1994 Lie sought to create a universal standardized style sheet for the World
Wide Web.
• Initially tested on the Arena web browser.
• Lie went on to co-create CSS1, CSS2 and RFC 2318 versions with Tim Berners-Lee
and Robert Cailliau.
• Has become an established web standard greatly influencing the look and
accessibility of the world wide web.
declaration block
selector propertyvalue
• CSS for a heading (h1 the selector) that is green, floats to the
right and is 36 pixels in size.
(color : green, float : right, font-size : 36 px the property : value)
CASCADING STYLE SHEETS - PROPERTIES
• A list of the most commonly used properties (* indicates the
properties has sub-properties that are not listed):
Property Type Property
Fonts font
font-family
font-size
font-style
font-weight
@font-face
Text lettering-spacing
line-height
text-align
text-decoration*
text-indent
CASCADING STYLE SHEETS - PROPERTIES
Property Type Property
Color and Background background
background-color
background-image
background-position
background-repeat
background-shadow
color
opacity
Borders border*
border-color
border-width
border-style
border-top, border-left, border-right,…*
border-image*
border-radius*
CASCADING STYLE SHEETS PROPERTIES
Property Type Property
Spacing padding
padding-bottom, padding-left, padding-
right,…
margin
margin-bottom, margin-left, margin-right,
…
Sizing height
max-height
max-width
min-height
min-height
width
Layout bottom, right, left, top
clear
display
CASCADING STYLE SHEETS - PROPERTIES
• For example:
<head>
<meta charset="utf-8">
<title>Share Your Food Recipes</title>
<link rel="stylesheet" href="styles.css" />
</head>
CASCADING STYLE SHEETS – TYPES OF CSS
• The universal element selector (*) is used to select all HTML elements
for the indicated styling.
•universal
For example:
selector
*{
color: blue; /* the color all the elements is set to blue */
font-weight: bold /* the font-weight of all the elements is set to bold */
}
CASCADING STYLE SHEETS – ELEMENT
SELECTOR - GROUP SELECTOR
• Group selectors – allows for the grouping of elements with identical rules
into a single rule.
• Separate the different element names by commas.
• Reduces the size and complexity of you CSS files.
• For example:
Eleme
nts
p, div{
margin: 10;
padding: 20;
}/* both the p and div elements now have a margin of 10 and
padding of 20*/
• NOTE: elements p and div now share the same styling rules for margin and
padding.
CASCADING STYLE SHEETS – CLASS
SELECTOR
.
CASCADING STYLE SHEETS –CONFLICT HANDLING -
INHERITANCE
• Inheritance – some CSS properties by default inherit
values based upon the current element’s parent, but not
every CSS property is inherited by default by child
elements.
• Colors, fonts, lists, and text properties are inheritable.
• Layout, sizing, border, background, and spacing
properties are not.
• Inheritance greatly reduces the time and effort required
to create a webpage/website.
• Inheritance helps to make code less error prone and
less difficult to maintain.
CASCADING STYLE SHEETS – CONFLICT
HANDLING - INHERITANCE
A visual example of inheritance via the <body> element:
CASCADING STYLE SHEETS – CONFLICT HANDLING -
INHERITANCE
• The :inherit property can be used to allow elements to
inherit non-inheritable properties. Using the previous
example, we can illustrate how to do so:
CASCADING STYLE SHEETS – CONFLICT
HANDLING - INHERITANCE
• The figure in the previous slide illustrates visually how the border property
of the <body> and <div> elements is inherited by the <p> element.
• The <body> element properties font-weight (bold), color (blue) and text-
align (center) are inherited by both the <div> and <p> elements.
• While the border and width properties are not inherited by either the <div>
or <p> elements since we know they are not inheritable properties.
• In the code above we define a border property for the <div> element of
limegreen and we can see a border of limegreen is created around each
<div> element defined in our HTML code.
• Note: I gave <div> elements a background color of lightgoldenrodyellow to
make them easily verifiable. The <p> element has a border property whose
value is inherit and therefore it will inherit the border of its parent. As you
can see certain <p> elements’ parent is the <body> element and thus the
border is black (i.e., Homemade Delights) and others are the <div> element
and their border is limegreen (i.e., By Leila 2022-05-07). Note: I gave <p>
elements a background color of plum to make them easily verifiable.
CASCADING STYLE SHEETS – CONFLICT
HANDLING - SPECIFICITY
• Using the rules, we just discussed and looking at the code above
we can see that the <body> element has color and font-weight
defined thus making them inheritable to its child elements.
• The <div> and <p> elements have the same properties set and
thus will override the <body> element properties as their
selectors are more specific.
• Class selectors take precedence over element selectors, and id
selectors take precedence over class selectors.
CASCADING STYLE SHEETS – CONFLICT
HANDLING - LOCATION
• Location – is used when inheritance and specificity cannot be used to
determine style precedence.
• This principle indicates that if CSS rules have the same specificity, then
the latest are given more weight.
• For example, an inline rule will override one defined in an external
author style sheet or an embedded/document style sheet.
• An embedded/document style sheet will override an equally specific rule
defined in an external author style sheet if it appears after the external
sheet’s <link> element.
• If there are two or more links to external author style sheets the last
link’s styles will override the previous ones.
• When the same style property is defined multiple times within a single
declaration block, the last one will take precedence.
CASCADING STYLE SHEETS – CONFLICT
HANDLING - LOCATION
• An example:
CASCADING STYLE SHEETS – THE BOX
MODEL
• In CSS all HTML elements exist with in an element box known as the Box
Model.
• To become proficient in CSS, you need to become familiar with the Box Model.
• An illustration of the Box Model:
By default, the box for a block element is as wide as the block that contains it
and as tall as it needs to be based upon its content. The height and width
properties allow you to explicitly specify the size of the content area. The
setting of borders, margins and paddings can also be used for the block
element.
CASCADING STYLE SHEETS – THE BOX MODEL
• Let’s exam how to calculate the overall height and width of a box. To do so I am going
to add a few more terms to Box Model figure..
• How to calculate the height of a box: top margin + top border + top padding + height + bottom padding +
bottom border + bottom margin
• How to calculate the width of a box: left margin + left border + left padding + width + right padding + right
border + right margin
• To set the height and width properties of a box element you can use a variety of units (pixels, points, ems
or percents). If you use pixels, then the sizes are fixed and therefore when you change the size of the
CASCADING STYLE SHEETS – THE BOX MODEL –
CONTENT AREA ELEMENT
• Let’s look at each element in this model.
• Content area – is where your text and images appear. Its properties are used
to determine the size of an element and spacing between elements on a
page. The two properties that will be used the most are the height and
width properties. By default, these properties are set to auto meaning it is
automatically adjusted to so that it is as wide as the element that contains it
and tall as the content
Property it contains. The following are the most common
Description
content are properties:
width width of the content area for the block element.
height height of the content area for the block element.
min-width minimum width of the content area for the block
element regardless of its content.
max-width maximum width of the content area for the block
element regardless of its content.
min-height minimum height of the content area for the block
element regardless of its content.
max-height maximum height of the content area for the block
element regardless of its content.
CASCADING STYLE SHEETS – THE BOX MODEL –
BACKGROUND ELEMENT
background- specifies whether the background image scrolls with the document
(default). Possible values are fixed or scrolled.
attachment
background-color sets the background color of an element.
background- specifies the background image (generally a jpeg, gif or png) for the
element. Consists of an absolute or relative URL.
image
background- specifies where on the element the background image will be placed.
Possible values are bottom, center, left and right. Can also
position supply a pixel or percentage numeric position value.
• Can have borders around one, two, three or four sides of an element.
border-style specifies the line type of the border. Possible values are solid,
dotted, dashed, double, groove, ridge, inset and outset.
The default value is none. If a single value is assigned, it will
set the style for all 4 sides. If two values are assigned, the first
style is set to the top and bottom sides and the second will be
set to the left & right sides. If three values are assigned, the
first style is set to the top, the second is set to the left and
right, the third is set to the bottom. If four-style values are
assigned, the styles are set to the top, right, bottom, and left,
which follows the clockwise order.
border-color specifies the color of the border in a color unit. The default
color is the color of the element.
Property Description
margin-top specifies the top margin.
margin-right specifies the top right margin.
margin- specifies the bottom margin.
bottom
margin-left specifies the left margin.
margin specifies the margin for a box. This is the shorthand property.
CASCADING STYLE SHEETS – THE BOX
MODEL – MARGINS AND PADDING
• If the overlapping margins did not collapse, then the margin
between #1 would be 150px (75px for the bottom margin of the
first <div> and 75px for the top margin of the second <div>) and
using the same logic the margin between #2 would be 80px. As
we can see that is not the case.
CASCADING STYLE SHEETS – THE BOX MODEL
• Border properties allow a developer to set the properties for one or more
sides of the element box in a single property or to set them individually
using separate properties.
• For example, setting four border sides to a different color:
CASCADING STYLE SHEETS – THE BOX
MODEL
CASCADING STYLE SHEETS – THE BOX
MODEL
CASCADING STYLE SHEETS – THE BOX MODEL
Property Description
padding-top specifies the top padding.
padding-right specifies the right padding.
padding- specifies the bottom padding.
bottom
padding-left specifies the left padding.
padding specifies the padding for a box. This is the
shorthand property.
CASCADING STYLE SHEETS – BOX
DIMENSIONS
Note: that font names with multiple words need to be enclosed in quotes (Ex: "Times New Roman") .
The final choice should be from a generic font family. The font-family property supports five different
generic families (serif, sans-serif, monospace, cursive and fantasy) and browsers support a typeface
from each family.
NOTE: Keep in mind that the sans-serif fonts are the easiest fonts to read in a browser.
CASCADING STYLE SHEETS – GENERIC
FONTS
• The simplest way is to specify a color by its name such as red, blue or green.
• For example: color: magenta;
• A second way is to specify a color using a RGB (red, green, blue) value. This can be done in several
ways:
• By indicating the percentage of red, green and blue that make up the color
• For example: rgb(100%, 40%, 20%); which indicates the color will consist of 100% red, 40%
green and 20% blue.
• Or instead of using percentages you can use values from 0 – 255 where 0 would be
equivalent to 0% and 255 equivalent to 100%. For example: rgb(255, 102, 51);
• The final way is to use hexadecimal (hex) values to specify a color. Hexadecimal values allow you
to choose from over 16 million colors. In hex the value of “000000” is equivalent to black and the
value “FFFFFF” is equivalent to white. The simple conversions of percentages to hex are 0% is 00,
20% is 33, 40% is 66, 60% is 99, 80% is CC and 100% is FF. When using hex values, you need to
always place a leading hash tag (#) before the value.
• For example: color: #FF0000; is equivalent to red. Under the Week 0 Module in the Useful
Websites posting, you can find a site that gives you information about colors and also
conversions between the RGB and hexadecimal values.
Note: the color property determines the color of the foreground color (the text). As we learned
previously the background color is set by the background-color property.
CASCADING STYLE SHEETS – COLOR
PROPERTY
An Example:
CASCADING STYLE SHEETS – ADVANCED
COLOR TECHNIQUES
•CSS provides three more ways to code color specification.
• HSL (Hue, Saturation and Lightness) values which allow you to specify the
number of hue degrees for the color. The saturation and lightness allow for
the enhancement of the hues. The value can be a number from 1 through
359 that represents the hue. The saturation can be a number from 0% to
100% with 100% being the full hue. The lightness can be a number 0 % to
100% with 0% being black, 50% being normal and 100% being white.
• HSLA is equivalent to HSL, but it adds a fourth value for opacity that can
have a value from 0 to 1.
CASCADING STYLE SHEETS – ADVANCED
COLOR TECHNIQUES