Professional Documents
Culture Documents
The good, the bad and the… ugly! Cascading Style Sheets (CSS)
3 4
<p>
<font face="Arial">Shashdot.</font> Describes the appearance, layout, and
News for <b>nerds!!</b> You will <i>never</i>, <u>EVER</u>
be presentation of information on a web
<font size="+4" color="red">BORED</font> here!
</p> HTML page
Slashdot. News for nerds!! You will never, EVER be BORED HTML describes the content of the page
here!
output Describes how information is to be
Tags such as b, i, u, and font are discouraged in displayed, not what is being displayed.
strict XHTML Can be embedded in HTML document or
Why is this bad? placed into separate .css file
Internet Programming I CSS Internet Programming I CSS
1
13-Jan-20
A CSS rule-set consists of a selector and a declaration Each declaration includes a CSS property name
block: and a value, separated by a colon.
A CSS declaration always ends with a semicolon,
and declaration blocks are surrounded by curly
braces.
The selector points to the HTML element you want to style. A CSS file consists of one or more rules
The declaration block contains one or more declarations
separated by semicolons.
2
13-Jan-20
A combinatory is something that explains the The descendant selector matches all elements that
relationship between the selectors. are descendants of a specified element.
A CSS selector can contain more than one simple The following example selects all <p> elements
selector. Between the simple selectors, we can include a inside <div> elements:
combinatory. Example
The child selector selects all elements that are the The adjacent sibling selector selects all elements that
immediate children of a specified element. are the adjacent siblings of a specified element.
The following example selects all <p> elements that Sibling elements must have the same parent element,
are immediate children of a <div> element: and "adjacent" means "immediately following".
Example The following example selects all <p> elements that
} div + p {
background-color: yellow;
Internet Programming I CSS
} Internet Programming I CSS
3
13-Jan-20
Example
element element div p Selects all <p> elements inside <div> elements 1
div ~ p { element>element div > p Selects all <p> elements where the parent is a <div> 2
background-color: yellow; element
element+element div + p Selects all <p> elements that are placed immediately 2
} after <div> elements
element1~element2 p ~ ul Selects every <ul> element that are preceded by a <p> 3
element
[attribute] [target] Selects all elements with a target attribute 2 Setting the style of an HTML element, can be done with
[attribute=value] [target=_blank] Selects all elements with target="_blank" 2 the style attribute.
[attribute~=value] [title~=flower] Selects all elements with a title attribute containing 2
the word "flower" The HTML style attribute has the following syntax:
[attribute|=value] [lang|=en] Selects all elements with a lang attribute value 2
starting with "en" style="property:value;"
[attribute^=value] a[href^="https"] Selects every <a> element whose href attribute 3
value begins with "https" Use the style attribute for styling HTML elements
[attribute$=value] a[href$=".pdf"] Selects every <a> element whose href attribute 3
value ends with ".pdf" Use background-color for background color
[attribute*=value] a[href*="w3scho Selects every <a> element whose href attribute 3 Use color for text colors
ols"] value contains the substring "w3schools"
:active a:active Selects the active link 1 Use font-family for text fonts
::after p::after Insert something after the content of each <p> 2 Use font-size for text sizes
element
::before p::before Insert something before the content of each <p> 2 Use text-align for text alignment
Internet Programming I CSS element Internet Programming I CSS
4
13-Jan-20
p { p { color: red; }
color: red; h2 { color: rgb(128, 0, 196); }
background-color: yellow; h4 { color: #FF8800; }
} CSS
CSS
This paragraph uses the first style above
This paragraph uses the style above output
This h2 uses the second style above.
property description
This h4 uses the third style above.
color color of the element's text output
color that will appear behind the
background-color color names: aqua, black, blue, fuchsia, gray, green, lime, maroon,
element
navy, olive, purple, red, silver, teal, white (white), yellow
RGB codes: red, green, and blue values from 0 (none) to 255 (full)
Internet Programming I CSS Internet Programming I CSS
hex codes: RGB values in base-16 from 00 (0, none) to FF (255, full)
p, h1, h2 {
color: green; property description
} font-family which font will be used
h2 {
background-color: yellow; font-size how large the letters will be drawn
} CSS font-style used to enable/disable italic style
font-weight used to enable/disable bold style
This paragraph uses the above style.
This h2 uses the above styles.
output Complete list of font properties (http://www.w3schools.com/css/css_reference.asp#font)
5
13-Jan-20
p { p {
font-family: Georgia; font-family: Garamond, "Times New Roman", serif;
} } CSS
h2 {
font-family: "Courier New"; This paragraph uses the above style.
} CSS output
This paragraph uses the first style above. We can specify multiple fonts from highest to lowest priority
Generic font names:
This h2 uses the second style above. serif, sans-serif, cursive, fantasy, monospace
output
If the first font is not found on the user's computer, the
Enclose multi-word font names in quotes next is tried
Placing a generic font name at the end of your font-family
value, ensures that every computer will use a valid font
Internet Programming I CSS Internet Programming I CSS
font-size font-size
23 24
p { p {
font-size: 24pt; font-size: 24pt;
} CSS } CSS
This paragraph uses the style above. This paragraph uses the style above.
output output
units: pixels (px) vs. point (pt) vs. m-size (em) pt specifies number of point, where a point is 1/72 of an inch
16px, 16pt, 1.16em onscreen
px specifies a number of pixels on the screen
vague font sizes: ,
xx-small ,
x-small , medium, large, x-large, xx-large,
small
em specifies number of m-widths, where 1 em is equal to the
smaller, larger font's current size
percentage font sizes, e.g.: 90%, 120%
Internet Programming I CSS Internet Programming I CSS
6
13-Jan-20
p {
font-weight: bold; property description
font-style: italic;
text-align alignment of text within its element
} CSS
text-decoration decorations such as underlining
This paragraph uses the style above. line-height,
output gaps between the various portions of
word-spacing,
the text
letter-spacing
Either of the above can be set to normal to turn them off (e.g. indents the first letter of each
headings) text-indent
paragraph
text-align text-decoration
27 28
7
13-Jan-20
background-image background-repeat
31 32
body { body {
background-image: url("images/draft.jpg"); background-image: url("images/draft.jpg");
} background-repeat: repeat-x;
CSS }
CSS
background image/color fills the element's content area can be repeat (default), repeat-x, repeat-y, or no-repeat
8
13-Jan-20
body {
font-size: 16px;
The CSS Box Model
} All HTML elements can be considered as boxes.
CSS
In CSS, the term "box model" is used when talking about design
and layout.
Applies a style to the entire body of your page The CSS box model is essentially a box that wraps around every
Saves you from manually applying a style to each element HTML element.
It consists of: margins, borders, padding, and the actual content.
The box model allows us to add a border around elements, and
to define space between elements.
The image below illustrates the box model:
9
13-Jan-20
10
13-Jan-20
Property Description
The display property specifies if/how an element is
outline Sets all the outline properties in one declaration
displayed.
outline-color Sets the color of an outline
Every HTML element has a default display value
depending on what type of element it is.
outline-offset Specifies the space between an outline and the edge or border of an
element The default display value for most elements
outline-style Sets the style of an outline is block or inline.
outline-width Sets the width of an outline Property Description
display Specifies how an element should be displayed
visibility Specifies whether or not an element should be visible
CSS Layout - width and max-width CSS Layout - The position Property
43 44
Example The position property specifies the type of positioning method used for an element (static,
relative, fixed or absolute).
div.ex1 {
width: 500px; Example position: absolute;
margin: auto; div.relative { top: 80px;
border: 3px solid #73AD21; position: relative; right: 0;
} width: 400px; width: 200px;
height: 200px; height: 100px;
div.ex2 { border: 3px solid #73AD21; border: 3px solid #73AD21;
max-width: 500px; } }
margin: auto;
border: 3px solid #73AD21; div.absolute {
}
Internet Programming I CSS Internet Programming I CSS
11
13-Jan-20
12
13-Jan-20
Create a dropdown box that appears when the user moves the mouse over an Create a dropdown menu that allows the user to
element.
Example z-index: 1; choose an option from a list:
<style> }
.dropdown { .dropdown:hover .dropdown-content {
position: relative; display: block;
display: inline-block; }
} </style>
.dropdown-content {
display: none; <div class="dropdown">
position: absolute; <span>Mouse over me</span>
background-color: #f9f9f9; <div class="dropdown-content">
min-width: 160px; <p>Hello World!</p>
box-shadow: 0px 8px 16px 0px </div>
rgba(0,0,0,0.2); </div>
padding: 12px 16px;
Internet Programming I CSS Internet Programming I CSS
Example
<style> position: absolute; display: block;
Three Ways to Insert CSS
/* Style The Dropdown Button background-color: #f9f9f9; }
51 52
*/ min-width: 160px;
.dropbtn { box-shadow: 0px 8px /* Change the background
background- 16px 0px rgba(0,0,0,0.2); color of the dropdown button There are three ways of inserting a style sheet:
color: #4CAF50; } when the dropdown content is
color: white; /* Links inside the dropdown shown */ External style sheet
padding: 16px; */ .dropdown:hover .dropbtn {
font-size: 16px;
border: none;
.dropdown-content a {
color: black;
background-
color: #3e8e41;
Internal style sheet
cursor: pointer; padding: 12px 16px; }
} text-decoration: none; </style> Inline style
/* The container <div> - display: block; <div class="dropdown">
needed to position the } <button class="dropbtn">Dr
dropdown content */ opdown</button>
.dropdown { /* Change color of dropdown <div class="dropdown-
position: relative; links on hover */ content">
display: inline-block; .dropdown-content <a href="#">Link 1</a>
} a:hover {background- <a href="#">Link 2</a>
color: #f1f1f1} <a href="#">Link 3</a>
/* Dropdown Content (Hidden /* Show the dropdown menu </div>
by Default) */ on hover */ </div>
.dropdown-content { I CSS
Internet Programming .dropdown:hover .dropdown- Internet Programming I CSS
display: none; content {
13
13-Jan-20
<head>
... <head>
<link href="filename" type="text/css" rel="stylesheet" /> <style type="text/css">
... p { font-family: sans-serif; color: red; }
</head> HTML h2 { background-color: yellow; }
</style>
</head>
<link href="style.css" type="text/css" rel="stylesheet" /> HTML
<link href="http://www.google.com/uds/css/gsearch.css"
rel="stylesheet" type="text/css" />
HTML
CSS code can be embedded within the head of an HTML page
A page can link to multiple style sheet files
In case of a conflict (two sheets define a style for the same HTML Bad style and should be avoided when possible (why?)
element), the latter sheet's properties will be used
<p style="font-family: sans-serif; color: red;"> Properties of an element cascade together in this
This is a paragraph</p> order:
HTML browser's default styles
This is a paragraph external style sheet files (in a <link> tag)
14
13-Jan-20
body { font-family: sans-serif; background-color: yellow; p, h1, h2 { color: blue; font-style: italic; }
} h2 { color: red; background-color: yellow; }
p { color: red; background-color: aqua; } CSS
a { text-decoration: underline; }
h2 { font-weight: bold; text-align: center; } This paragraph uses the first style above.
CSS
This heading uses both styles above.
This is a heading output
A styled paragraph. Previous slides are available on the website.
• A bulleted list output when two styles set conflicting values for the same property,
the latter style takes precedence
when multiple styles apply to an element, they are inherited
a more tightly matching rule can override a more general o not all properties are inherited (notice link's color above)
inherited rule
Internet Programming I CSS Internet Programming I CSS
<p>
<a href="http://jigsaw.w3.org/css-
validator/check/referer">
60 Question???
<img src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!" /></a>
</p> CSS Practice:
Lab Exercise # 1,2 and3
output
jigsaw.w3.org/css-validator/
checks your CSS to make sure it meets the official CSS
specifications
Internet Programming I CSS Internet Programming I CSS
15