BIM222 InternetProgramming Week6
BIM222 InternetProgramming Week6
Outline
§ CSS Simple Selectors
§ What is CSS?
§ Element
§ CSS Syntax § Class
§ CSS How to § Id
§ Inline Style § CSS Combinator Selectors
§ Internal Style Sheet
§ CSS Colors
§ External Style Sheet
§ CSS Backgrounds
§ “Cascading” Part of CSS
§ Style Text
§ CSS Text
§ CSS Fonts
1
4/6/21
What is CSS?
§ CSS stands for Cascading Style Sheets
§ CSS is a language that describes the style of an HTML document
§ CSS describes how HTML elements are to be displayed on screen,
paper, or in other media
§ CSS saves a lot of work
§ It can control the layout of multiple web pages all at once
§ With an external stylesheet file (saved in external .css file), you can change the
look of an entire website by changing just one file!
Ex: https://www.w3schools.com/css/css_intro.asp
§ Why use CSS?
§ CSS is used to define styles for your web pages, including the design, layout and
variations in display for different devices and screen sizes
2
4/6/21
CSS Syntax
§ A CSS rule-set consists of a selector and
a declaration block: Selector Property Value
§ The selector points to the HTML element
you want to style
§ The declaration block contains one or more
declarations separated by semicolons
§ Each declaration includes a CSS property
name and a value, separated by a colon
§ Ex: color is the property and blue is the
property value Declaration
§ A CSS declaration always ends with a
semicolon, and declaration blocks are
surrounded by curly braces
§ CSS defined generic rules that can apply
to multiple elements
3
4/6/21
Zero or More
Declarations
are allowed
Stylesheet
4
4/6/21
week6-sampleCodes/inlineStyle.html
Inline Styles
§ To use inline styles, add the style attribute to the relevant element
§ The style attribute can contain any CSS property
5
4/6/21
§ Styles are applied to all elements in all files that links to the style sheet
6
4/6/21
External Style Sheet § The external “.css” file should not contain any HTML tags
§ Here is how the "mystyle.css" file looks like:
week6-sampleCodes/externalStyle.html
week6-sampleCodes/css/mystyle.css
7
4/6/21
§ Then, assume that an internal style sheet also has the following style for the
<h2> element:
week6-sampleCodes/external_than_internal.html
8
4/6/21
week6-sampleCodes/internal_than_external.html
Rule Precedence
§ What if one selector is defined in two external files?
§ The rules from the most recent file have precedence
§ What if one selector has more than one rule in the same file?
§ The most recent rule has precedence
week6-sampleCodes/twoExternalSheets.html
week6-sampleCodes/multiple.html
9
4/6/21
CSS Selectors
§ CSS selectors are used to "find" (or select) HTML elements you want
to style
§ We can divide CSS selectors into 5 categories:
1. Simple Selectors (select elements based on element name, id, class)
§ The element Selector
§ The class Selector
§ The id Selector
2. Combinator Selectors (select elements based on a specific relationship
between them)
3. Pseudo-class Selectors (select elements based on a certain state) (next week!!)
4. Pseudo-elements Selectors (select and style a part of an element) (next week!!)
(next week!!)
5. Attribute Selectors (select elements based on an attribute or attribute
value)
10
4/6/21
Element Name
Blue Text
Class Name
11
4/6/21
Unaffected
Blue Text
text
week6-sampleCodes/classSelector.html
12
4/6/21
week6-sampleCodes/classSelector2.html
week6-sampleCodes/classSelector3.html
13
4/6/21
Unaffected
The id Selector text
14
4/6/21
week6-sampleCodes/idSelector.html
Grouping Selectors
§ If you have elements with the same style
definitions, like this à
15
4/6/21
Separate selectors
with commas (,) Unaffected
text
Blue Text
week6-sampleCodes/simple-selectors-before.html
week6-sampleCodes/simple-selectors-after.html
16
4/6/21
Combining Selectors
§ A CSS selector can contain more than one simple selector
§ Between the simple selectors, we can include a combinator
§ A combinator is something that explains the relationship between the
selectors
§ There are five different combinators in CSS:
1. Element with class Selector à (selector.class)
2. Child (direct) Selector (>) à (selector > selector)
3. Descendant Selector (space) à (selector selector)
4. Adjacent Sibling Selector (+) à (selector + selector)
5. General Sibling Selector (~) à (selector ~ selector)
p.big {
font-size: 20px;
}
17
4/6/21
...
p.big {
<p class=“big”> ... </p>
font-size: 20px;
<div class=“big”> ... </div>
}
<p> ... </p>
...
week6-sampleCodes/advanceSelectors/elementWithClassSelector.html
week6-sampleCodes/advanceSelectors/element-with-class-before.html Unaffected text
week6-sampleCodes/advanceSelectors/element-with-class-after.html
18
4/6/21
week6-sampleCodes/advanceSelectors/childSelector.html
week6-sampleCodes/advanceSelectors/child-selector-before.html Unaffected text
week6-sampleCodes/advanceSelectors/child-selector-after.html
19
4/6/21
20
4/6/21
Example:
Adjacent Sibling
Selector (+)
week6-sampleCodes/advanceSelectors/adjacentSiblingSelector.html
21
4/6/21
Example:
General Sibling
Selector (~)
week6-sampleCodes/advanceSelectors/generalSiblingSelector.html
22
4/6/21
Example: Question1
Given the following HTML code:
Which of the following CSS rules turns the text of the first <p> tag blue,
but NOT the second <p> tag?
Example: Question2
Which of the following CSS rules turns the text of the first <p> tag blue,
but NOT the second <p> tag?
23
4/6/21
CSS Colors
Colors in CSS are most often specified by:
1) Predefined Color Names – like ”blue, red, yellow, etc.”
CSS Colors
Colors in CSS are most often specified by:
2) A RGB Value – like “rgb(255,0,0)” displayed as red
§ A color can be specified as an RGB value, using this formula: rgb(red, green, blue)
§ Each parameter (red, green, blue) defines the intensity of the color between 0 and 255
§ To display the color black, all color parameters must be set to 0, like this: rgb(0, 0, 0)
§ To display the color white, all color parameters must be set to 255, like this: rgb(255, 255, 255)
24
4/6/21
CSS Colors
Colors in CSS are most often specified by:
3) A Hexadecimal Value – like “#000FF, #FF0000, #FFFF00”
§ A color can be specified using a hexadecimal value
§ #RRGGBB, where RR (red), GG (green), BB (blue) are hexadecimal values between 00 and FF
(same as decimal 0-255)
CSS Colors
Colors in CSS are most often specified by:
§ In addition, CSS3 also introduces:
4) A RGBA Value
§ RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity
§ An RGBA color value is specified with: rgba(red, green, blue, alpha)
§ The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all)
5) A HSL Value
§ A color can be specified using hue, saturation and lightness (HSL) in the form:
hsl(hue, saturation, lightness)
§ Hue is a degree on the color wheel from 0 to 360, where 0 is red, 120 is green, and 240 is blue
§ Saturation is a percentage value, 0% means a shade of gray, and 100% is the full color
§ Lightness is also a percentage, 0% is black, 50% is neither light or dark, 100% is white
6) A HSLA Value
§ HSLA color values are an extension of HSL color values with an alpha channel - which specifies the opacity
§ An HSLA color value is specified with: hsla(hue, saturation, lightness, alpha)
§ The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all)
25
4/6/21
CSS Colors
We can set the color of, what?
§ Text Color
§ We can set the color of text
<h1 style="color:Tomato;">Hello World</h1>
§ Background Color
§ We can set the background color for HTML elements
<h1 style="background-color:DodgerBlue;">Hello World</h1>
§ Border Color
§ We can set the color of borders
<h1 style="border:2px solid Tomato;">Hello World</h1>
https://www.w3schools.com/css/css_colors.asp
CSS Backgrounds
§ CSS background properties are used to define the background effects for
elements
§ CSS background properties are
§ background-color à specifies the background color of an element
§ The background color of a page is set like this:
26
4/6/21
CSS Backgrounds
§ CSS background properties are
§ background-repeat à Some images should be repeated only horizontally or vertically
§ no-repeat, repeat-x (to repeat an image horizontally), repeat-y (to repeat an image vertically)
§ background-attachment à To specify that the background image should scroll or be fixed
§ fixed, scroll (will not scroll with the rest of the page)
Ex: https://www.w3schools.com/css/tryit.asp?filename=trycss_background-image_attachment
§ background-position à sets the starting position of a background image
§ By default a background-image is placed at the top-left corner of an element, and repeat both vertically
and horizontally
§ left top, left center, left bottom, right top, right center, right bottom, center top, center center, center
bottom week6-sampleCodes/background/background-before.html
§ Background – Shorthand property week6-sampleCodes/background/background-after.html
§ Specify all the background properties in one single property, namely background property
27
4/6/21
week6-sampleCodes/stylingText/styling-text-before.html
week6-sampleCodes/stylingText/styling-text-after.html
28
4/6/21
Styling Text:
CSS Fonts
§ Font Weight
§ The font-weight property specifies the weight of a font (normal, bold)
§ Font Variant
§ The font-variant property specifies whether or not a text should be displayed
in a small-caps font
§ In a small-caps font, all lowercase letters are converted to uppercase letters
However, the converted uppercase letters appears in a smaller font size than
the original uppercase letters in the text
29
4/6/21
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px
(16px=1em)
30
4/6/21
Styling Text: In this example the text size is same in both. However, with
the em size, it is possible to adjust the text size in all browsers
CSS Fonts week6-sampleCodes/font-size/absoluteSize.html
week6-sampleCodes/font-size/relativeSize.html
Unfortunately, there is still a problem with older versions of IE with the em sizeà The text becomes
larger than it should when made larger, and smaller than it should when made smaller!
week6-sampleCodes/font-size/combination.html
week6-sampleCodes/font-size/font-size-before.html
week6-sampleCodes/font-size/font-size-after.html
31