Lec 3-CSS
Lec 3-CSS
1
Content
Basic CSS
Advanced CSS
Basic CSS
3
Content vs. Presentation
• Most HTML tags define content type, independent of presentation.
• exceptions? (e.g. <b> …… </b> for bold text and <i> ….. </i> for italicized text)
• The trend has been towards an increasing separation of the content of webpages from the
presentation of them.
• Style sheets allow us to maintain this separation, which allows for easier maintenance of
webpages, and for a consistent look across a collection of webpages.
Content vs. Presentation (cont.)
• Style sheets can be used to specify how tables should be rendered, how lists
should be presented, what colors should be used on the webpage, what fonts
should be used and how big/small they are, etc.
• HTML style sheets are known as Cascading Style Sheets, since can be defined
at three different levels
1. inline style sheets apply to the content of a single HTML element
2. document style sheets apply to the whole BODY of a document
3. external style sheets can be linked and applied to numerous documents, might also specify how things should be presented on
screen or in print lower-level style sheets can override higher-level style sheets
• User-defined style sheets can also be used to override the specifications of the
webpage designer. These might be used, say, to make text larger (e.g. for
visually-impaired users).
5
Inline Style Sheets •Using the style attribute, you can specify
<html> presentation style for a single HTML element
<!–- CS443 page17.html 17.10.14 -->
font-size:larger;" text-decoration:none
<head>
<title> Inline Style Sheets </title>
</head>
<body>
<table style="font-family:Arial,sans-serif">
•style sheets can be
<caption style="color:red;
font-style:italic; applied to tables for
text-decoration:underline">
Student data. </caption>
<tr style="background-color:red"> interesting effects
<th> name </th> <th> age </th>
</tr>
<tr>
<td> Chris Smith </td> <td> 19 </td>
</tr>
<tr>
<td> Pat Jones </td> <td> 20 </td>
</tr>
<tr>
<td> Doogie Howser </td> <td> 9 </td>
</tr>
</table>
</body>
</html>
view page
Document Style Sheets
• Inline style sheets apply to individual elements in the page.
• using inline style directives can lead to inconsistencies, as similar elements
are formatted differently
• e.g., we might like for all <h1> elements to be centered
view page
Document Style Sheets (cont.)
<html>
<!–- CS443 page21.html 17.10.14 --> •document style sheets are
<head>
<title> Inline Style Sheets </title>
especially useful in formatting
<style type="text/css">
table {font-family:Arial,sans-serif}
tables
caption {color:red;
font-style:italic;
text-decoration:underline}
th {background-color:red}
</style>
•effectively separates content
</head>
from presentation
<body>
<table>
<caption> Student data. </caption>
• what if you wanted to right-
<tr><th> name </th>
<tr><td> Chris Smith </td>
<th> age</th></tr>
<td> 19 </td></tr>
justify the column of numbers?
<tr><td> Pat Jones </td> <td> 20 </td></tr>
<tr><td> Doogie Howser </td> <td> 9 </td></tr>
• what if you changed your
</table>
</body>
mind?
</html>
view page
Pseudo-Elements
<html>
<!–- CS443 page23.html 17.10.14 -->
•pseudo-elements are used to address sub-parts of
elements
<head>
<title>Title for Page</title>
<style type="text/css"> • can specify appearance of link in various states
a {color : red; • :visited :active :hover
text-decoration : none;
font-size : larger}
a:visited {color : black} • can specify format of first line in page or paragraph
a:active {color : orange} • :first-line
a:hover {color : blue}
p:first-letter {font-size : large;
color : white; • can specify format of first letter in page or paragraph
background-color : darkblue} • :first-letter
</style>
</head>
view page
External Style Sheets
• modularity is key to the development and reuse of software
• design/implement/test useful routines and classes
• package and make available for reuse
• saves in development cost & time
• central libraries make it possible to make a single change and
propagate the changes
• external style sheets place the style definitions in separate files
• multiple pages can link to the same style sheet, consistent look across a site
• possible to make a single change and propagate automatically
• represents the ultimate in content/representation separation
Modularity & Style Sheets
<html> /* myStyle.css CS443 02.09.05 */
<!–- CS443 page26.html 17.10.14 -->
h1 {color : blue; text-align : center}
<head> p.indented {text-indent:0.2in}
<title>Title for Page</title>
<link rel="stylesheet"
type="text/css"
href="myStyle.css"
•Ideally, the developer(s) of a Web
title="myStyle“ /> site would place all formatting
</head>
options in an external style sheet.
<body>
<h1>Centered Title</h1>
<p class="indented">This paragraph will •All Web pages link to that same
have the first line indented, but subsequent
lines will be flush.</p> style sheet for a uniform look.
<p>This paragraph will not be indented.
</p>
• simplifies Web pages since only need to
<h1>The End</h1> specify structure/content tags
</body>
</html> • Note: no <style> tags are used in the
view page external style sheet
<div> and <span> Tags
• Problem: font properties apply to whole elements, which are often too large
• Solution: a new tag to define an element in the content of a larger element - <span>
• The default meaning of <span> is to leave the content as it is (i.e. unchanged)
⮚ use color & fonts sparingly and be careful how elements fit together
e.g, no purple text on a pink background, no weird fonts
e.g. I find bright white text on a black background difficult to read
Consider the needs of visually impaired users of your website!!
⮚ don’t be annoying
e.g., lots of pop-up windows, excessive advertising, silly music
⮚ break a large document into several smaller ones or provide a menu for navigation
⮚ stick to standard features and test as many browsers as possible (and versions of the same browser)
17
Rounded Corners
• With the CSS border-radius property, you can give any element “rounded corners”.
➢ Rounded corners for an element with a specified background color:
#rcorners1 {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
18
Rounded Corners
• With the CSS border-radius property, you can give any element “rounded corners”.
➢ Rounded corners for an element with a border:
#rcorners2 {
border-radius: 25px;
border: 2px solid #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
19
Rounded Corners
• With the CSS border-radius property, you can give any element “rounded corners”.
➢ Rounded corners for an element with a background image:
#rcorners3 {
border-radius: 25px;
background: url(paper.gif);
background-position: left top;
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
}
20
Rounded Corners
• With the CSS border-radius property, you can give any element “rounded corners”.
➢ Rounded corners for an element with a background image:
#rcorners3 {
border-radius: 25px;
background: url(paper.gif);
background-position: left top;
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
}
21
Rounded Corners
• border-radius with multiple values
22
Shadows
• With CSS you can add shadow to text and to elements.
❖ Box Shadows: applies shadow to elements.
• The first value is the horizontal offset — how far the shadow is nudged to the right (or left if it’s negative)
• The second value is the vertical offset — how far the shadow is nudged downwards (or upwards if it’s negative)
• The third value is the blur radius — the higher the value the less sharp the shadow. (“0” being absolutely sharp).
This is optional — omitting it is equivalent of setting “0”.
• The fourth value is the spread distance — the higher the value, the larger the shadow (“0” being the inherited
size of the box). This is also optional — omitting it is equivalent of setting “0”.
• The fifth value is a color. That’s optional, too.
23
Shadows
• With CSS you can add shadow to text and to elements.
❖ Box Shadows: applies shadow to elements.
24
Shadows
❖ Text Shadows: applies shadow to text.
25
Universal, Child, and Adjacent Selectors
• Universal selectors: set global styles for a page, or as a descendant of a selector to set
styles of everything within something.
* {
margin: 0;
Example: set the margin and padding on everything in
padding: 0; a page to zero and everything within an element with
} the ID “contact” to be displayed as a block
#contact * {
display: block;
}
26
Universal, Child, and Adjacent Selectors
• Child selectors: A greater-than symbol (“>”) can be used to specify something that is a
child of something else, that is, something immediately nested within something.
27
Universal, Child, and Adjacent Selectors
• Adjacent selectors: A plus sign (“+”) is used to target an adjacent sibling of an element,
essentially, something immediately following something.
<h1>Clouded leopards</h1>
<p>Clouded leopards are cats that
belong to the genus Neofelis.</p>
<p>There are two extant species:
Neofelis nebulosa and Neofelis
diardi.</p>
h1 + p { font-weight: bold }
Only the first paragraph, that following the heading, will be made bold.
28
Advanced Colors
• We already know that colors can be defined by name, RGB, or hex values
• CSS 3 also allows you to paint away with HSL — hue, saturation, and lightness
• An HSL color value is specified with: hsl(hue, saturation, lightness).
➢ Hue is a degree on the color wheel (from 0 to 360):
• 0 (or 360) is red
• 120 is green
• 240 is blue
➢ Saturation is a percentage value: 100% is the full color.
➢ Lightness is also a percentage; 0% is dark (black) and 100% is white.
29
Advanced Colors
• HSLA
30
CSS Transitions
• Transitions allow you to easily animate parts of your design without the need for the
likes of JavaScript
• CSS transitions allows you to change property values smoothly, over a given duration.
31
Backgrounds: Multiples, Size, and Origin
• Multiples background: CSS3 allows you to apply multiple background images to a
single box by simply putting image locations in a comma-separated list
32
Backgrounds: Multiples, Size, and Origin
• Background size: The background-size property allows you to stretch or compress a
background image.
• auto, which maintains the background image’s original size and width/height ratio.
• lengths, a width and a height
• percentages, a width and a height
• A combination of lengths, percentages, and auto
• contain, which maintains the background image’s original ratio and makes it as large as
possible whilst fitting entirely within the box’s background area.
• cover, which maintains the background image’s original ratio and makes it large enough to
fill the entire background area, which may result in cropping of either the height or width.
33
Backgrounds: Multiples, Size, and Origin
• Background origin: specifies where the background image is positioned.
• The property takes three different values:
• border-box - the background image starts from the upper left corner of the border
• padding-box - (default) the background image starts from the upper left corner of the padding
edge
• content-box - the background image starts from the upper left corner of the content
34
Transformations
• CSS transforms allow you to move, rotate, scale, and skew elements.
• The translate() method moves an element from its current position (according to the
parameters given for the X-axis and the Y-axis).
• The rotate() method rotates an element clockwise or counter-clockwise according to a given
degree.
• The scale() method increases or decreases the size of an element (according to the
parameters given for the width and height).
• The skew() method skews an element along the X and Y-axis by the given angles.
• The matrix() method combines all the 2D transform methods into one.
35
36