0% found this document useful (0 votes)
8 views

Cascading Style Sheets (CSS)

CSS is used to describe the presentation of HTML documents including sizes, spacing, fonts, colors and layout. CSS separates presentation from content and improves accessibility and flexibility. CSS rules consist of selectors, declarations and properties that are applied using different selector types and CSS can be linked to HTML using inline, embedded and external styles.

Uploaded by

xilice9481
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Cascading Style Sheets (CSS)

CSS is used to describe the presentation of HTML documents including sizes, spacing, fonts, colors and layout. CSS separates presentation from content and improves accessibility and flexibility. CSS rules consist of selectors, declarations and properties that are applied using different selector types and CSS can be linked to HTML using inline, embedded and external styles.

Uploaded by

xilice9481
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Cascading Style Sheets (CSS)

CSS Introduction
• Cascading Style Sheets (CSS)
– Used to describe the presentation of documents
– Define sizes, spacing, fonts, colors, layout, etc.
– Improve content accessibility
– Improve flexibility
• Designed to separate presentation from content
• Due to CSS, all HTML presentation tags and
attributes are deprecated, e.g. font, center,
etc.
Style Sheets Syntax
• Style-sheets consist of rules, selectors, declarations,
properties and values

• Selectors are separated by commas


• Declarations are separated by semicolons
• Properties and values are separated by colons

h1,h2,h3 { color: green; font-weight: bold; }


Selectors
• Selectors determine which element the rule applies
to:
– All elements of specific type (tag)
– Those that match a specific attribute (id, class)
– Elements may be matched depending on how they
are nested in the document tree (HTML)
• Examples:

.header a { color: green }

#menu>li { padding-top: 8px }


• Three primary kinds of selectors:
– By tag (type selector):
h1 { font-family: verdana,sans-serif; }

– By element id:
#element_id { color: #ff0000; }
– By element class name (only for HTML):
.myClass {border: 1px solid red}

• Selectors can be combined with commas:


h1, .link, #top-link {font-weight: bold}

This will match <h1> tags, elements with class link, and
element with id top-link
• Pseudo-classes define state
– :hover, :visited, :active , :lang
• Pseudo-elements define element "parts" or
are used to generate content
– :first-line , :before, :after

a:hover { color: red; }


p:first-line { text-transform: uppercase; }
.title:before { content: "»"; }
.title:after { content: "«"; }
Match relative to element placement:
p a {text-decoration: underline}

This will match all <a> tags that are inside of <p>
* – universal selector (avoid or use with care!):
p * {color: black}

This will match all descendants of <p> element


+ selector – used to match “next sibling”:
img + .link {float:right}
This will match all siblings with class name link that
appear immediately after <img> tag
> selector – matches direct child nodes:
p > .error {font-size: 8px}
This will match all elements with class error, direct
children of <p> tag
[ ] – matches tag attributes by regular expression:
img[alt~=logo] {border: none}
This will match all <img> tags with alt attribute
containing the word logo
.class1.class2 (no space) - matches elements with both
(all) classes applied at the same time
Values in the CSS Rules
• Colors are set in RGB format (decimal or hex):
– Example: #a0a6aa = rgb(160, 166, 170)
– Predefined color aliases exist: black, blue, etc.
• Numeric values are specified in:
– Pixels, ems, e.g. 12px , 1.4em
– Points, inches, centimeters, millimeters
• E.g. 10pt , 1in, 1cm, 1mm
– Percentages, e.g. 50%
• Percentage of what?...
– Zero can be used with no unit: border: 0;
Default Browser Styles
• Browsers have default CSS styles
– Used when there is no CSS information or any
other style information in the document
• Caution: default styles differ in browsers
– E.g. margins, paddings and font sizes differ most
often and usually developers reset them
* { margin: 0; padding: 0; }

body, h1, p, ul, li { margin: 0; padding: 0; }


Linking HTML and CSS
• HTML (content) and CSS (presentation) can be linked in
three ways:
– Inline: the CSS rules in the style attribute
• No selectors are needed
– Embedded: in the <head> in a <style> tag
– External: CSS rules in separate file (best)
• Usually a file with .css extension
• Linked via <link rel="stylesheet" href=…> tag or
@import directive in embedded CSS block
• Using external files is highly recommended:
– Simplifies the HTML document
– Improves page load speed as the CSS file is cached
Inline Styles: Example
inline-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/
DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html>
Output:
inline-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/
DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html>
Embedded Styles
• Embedded in the HTML in the <style> tag:
<style type="text/css">
– The <style> tag is placed in the <head> section of
the document
– type attribute specifies the MIME type
• MIME describes the format of the content
• Other MIME types include text/html,
image/gif, text/javascript …
• Used for document-specific styles
Embedded Styles: Example
embedded-stylesheets.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Style Sheets</title>
<style type="text/css">
em {background-color:#8000FF; color:white}
h1 {font-family:Arial, sans-serif}
p {font-size:18pt}
.blue {color:blue}
</style>
<head>

<body>
<h1 class="blue">A Heading</h1>
<p>Here is some text. Here is some text. Here
is some text. Here is some text. Here is some
text.</p>
<h1>Another Heading</h1>
<p class="blue">Here is some more text.
Here is some more text.</p>
<p class="blue">Here is some <em>more</em>
text. Here is some more text.</p>
</body>
</html>
Output:

<body>
<h1 class="blue">A Heading</h1>
<p>Here is some text. Here is some text. Here
is some text. Here is some text. Here is some
text.</p>
<h1>Another Heading</h1>
<p class="blue">Here is some more text.
Here is some more text.</p>
<p class="blue">Here is some <em>more</em>
text. Here is some more text.</p>
</body>
</html>
External CSS Styles
• External linking
– Separate pages can all use a shared style sheet
– Only modify a single file to change the styles across
your entire Web site
• link tag (with a rel attribute)
– Specifies a relationship between current document
and another document
<link rel="stylesheet" type="text/css"
href="styles.css">
– link elements should be in the <head>
@import
– Another way to link external CSS files
– Example:
<style type="text/css">
@import url("styles.css");
/* same as */
@import "styles.css";
</style>
– Ancient browsers do not recognize @import
– Use @import in an external CSS file to workaround
the IE 32 CSS file limit
External Styles: Example
styles.css
/* CSS Document */

a { text-decoration: none }

a:hover { text-decoration: underline;


color: red;
background-color: #CCFFCC }

li em { color: red;
font-weight: bold }

ul { margin-left: 2cm }

ul ul { text-decoration: underline;
margin-left: .5cm }
External Styles: Example
external-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Importing style sheets</title>
<link type="text/css" rel="stylesheet"
href="styles.css" />
</head>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<li>Milk</li>


<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
Output:

<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
Text-related CSS Properties
• color – specifies the color of the text
• font-size – size of font: xx-small, x-small, small, medium,
large, x-large, xx-large, smaller, larger or numeric value
• font-family – comma separated font names
– Example: verdana, sans-serif, etc.
– The browser loads the first one that is available
– There should always be at least one generic font
• font-weight can be normal, bold, bolder, lighter or a
number in range [100 … 900]
• font-style – styles the font
– Values: normal, italic, oblique
• text-decoration – decorates the text
– Values: none, underline, line-trough, overline, blink
• text-align – defines the alignment of text or other
content
– Values: left, right, center, justify
Shorthand Font Property
font
– Shorthand rule for setting multiple font properties at
the same time
font:italic normal bold 12px/16px verdana
is equal to writing this:
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: verdana;
Backgrounds
• background-image
– URL of image to be used as background, e.g.:
background-image:url("back.gif");
• background-color
– Using color and image and the same time
• background-repeat
– repeat-x, repeat-y, repeat, no-repeat
• background-attachment
– fixed / scroll
• background-position: specifies vertical and horizontal
position of the background image
– Vertical position: top, center, bottom
– Horizontal position: left, center, right
– Both can be specified in percentage or other
numerical values
Examples:
background-position: top left;

background-position: -5px 50%;


Background Shorthand Property
background: shorthand rule for setting background
properties at the same time:
background: #FFF0C0 url("back.gif") no-repeat
fixed top;
is equal to writing:
background-color: #FFF0C0;
background-image: url("back.gif");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top;
– Some browsers will not apply BOTH color and image for
background if using shorthand rule
Background-image or <img>?

• Background images allow you to save many image tags


from the HTML
– Leads to less code
– More content-oriented approach
• All images that are not part of the page content (and are
used only for "beautification") should be moved to the
CSS
Borders
• border-width: thin, medium, thick or
numerical value (e.g. 10px)
• border-color: color alias or RGB value
• border-style: none, hidden, dotted, dashed,
solid, double, groove, ridge, inset, outset
• Each property can be defined separately for
left, top, bottom and right
– border-top-style, border-left-color, …
Border Shorthand Property
• border: shorthand rule for setting border properties
at once:
border: 1px solid red
is equal to writing:
border-width:1px;
border-color:red;
border-style:solid;
• Specify different borders for the sides via shorthand
rules: border-top, border-left, border-
right, border-bottom
• When to avoid border:0
Width and Height

• width – defines numerical value for the width of


element, e.g. 200px
• height – defines numerical value for the height of
element, e.g. 100px
– By default the height of an element is defined by its
content
– Inline elements do not apply height, unless you
change their display style.
Margin and Padding

• margin and padding define the spacing around the


element
– Numerical value, e.g. 10px or -5px
– Can be defined for each of the four sides separately -
margin-top, padding-left, …
– margin is the spacing outside of the border
– padding is the spacing between the border and the
content
Margin and Padding: Short Rules
• margin: 5px;
– Sets all four sides to have margin of 5 px;
• margin: 10px 20px;
– top and bottom to 10px, left and right to 20px;
• margin: 5px 3px 8px;
– top 5px, left/right 3px, bottom 8px
• margin: 1px 3px 5px 7px;
– top, right, bottom, left (clockwise from top)
• Same for padding
The Box Model
Benefits of using CSS
• More powerful formatting than using presentation
tags
• Your pages load faster, because browsers cache
the .css files
• Increased accessibility, because rules can be
defined according given media
• Pages are easier to maintain and update

You might also like