CSS Part 1
CSS Part 1
1 INTRODUCTION TO CSS
What is Cascading Style Sheet (CSS)?
A CSS stylesheet comprises set of rules that are interpreted by the web browser and then applied to the
corresponding elements such as paragraphs, headings, etc. in a web document.
The CSS rule consist of statement that defines the style of element in a web page.
- CSSs provide the means to control and change presentation of HTML documents
- CSS is not technically HTML, but can be embedded in HTML documents
- A style sheet is a syntactic mechanism for specifying style information
- Style sheets allow you to impose a standard style on a whole document, or even a whole collection of documents
- Style is specified for a tag by the values of its properties
History:
syntax:
/* … */
3.2 LEVELS OF STYLE SHEETS
- Inline style sheets appear in the opening tag of html.
- Document-level / Embedded style sheets appear in the head section of the HTML document.
- External style sheets are in separate files, potentially on any server on the Internet
Note:
- When more than one style sheet applies to a specific tag in a document, the lowest level style sheet has precedence
- In a sense, the browser searches for a style property spec, starting with inline, until it finds one (or there isn’t one)
- A <link> tag is used to specify that the browser is to fetch and use an external style sheet file
href = "http://www.wherever.org/termpaper.css">
</link>
Note:
Inline style sheet should be avoided in two cases:
1. If we want to apply the same style declaration to different elements every time.
2. Inline style sheet mixes the content with the presentation. So, if you want to
avoid this mixing up, don't use Inline style sheet.
3.3 STYLE SPECIFICATION FORMATS
1. INLINE CSS
Style sheet appears as the value of the style <html>
attribute
<head>
- General Syntax:
<title> Inline CSS example</title>
<tag style = "property_1: value_1; <meta http-equiv="content-style-type"
property_2: value_2; content="text/css">
… </head>
property_n: value_n“> <body style="background:orange">
…..</tag> <h1 style="color:White; font-
family:arial; font-size:14pt; text-
transform:uppercase; text-align:left;">
This is an example of inline css </h1>
3.3 STYLE SPECIFICATION FORMATS (CONTINUED)
2. EMBEDDED CSS
- The <style> tag must include <!DOCTYPE html>
<html>
the type attribute, <head>
<title> Embedded CSS</title>
set to "text/css" <style type="text/CSS">
body {
background-color:#ccffff;
}
- General form/Syntax: h1 { color: purple; font-family: arial; font-size: 30
px; text-transform: uppercase; text-align: left;}
<style type = "text/css"> </style>
</head>
<body>
rule list <h1> This is an example of embedded CSS</h1>
<h1> B E </h1>
</style> </body>
</html> Output
3.3 STYLE SPECIFICATION FORMATS (CONTINUED)
2. EMBEDDED CSS EXAMPLE
<html>
<style type="text/css">
<!--
h4 {
font: 17pt Arial,Helvetica";
font-weight: bold;
color: maroon
}
Advantages:
1. It is a “true separation” of style and content.
2. It is easier to reuse CSS code in any separate file.
3. It is easier to edit the css rule.
3. Demonstration of external style sheet. (you will have two or more documents
body { background:#ccffff;}
h2,p {
color: green;
font-family:arial;
text-align:center;
}
p i{
color: orange;
border-style: solid;
font-weight: lighter;
}
.ex{color:purple}
Save the following program as external.html and link above CSS file in it.
<html>
<head><title>Extenal style sheet</title>
<link red= "stylesheet" type= "text/CSS" href="external.css">
</head>
<body>
<h2> It is an example of External style sheet</h2>
<p class="ex"> This is a "true separation" of style and content</p>
<p><i> External CSS </i> </p>
</body>
</html>
IMPORTANT TAGS
• <STYLE> tag
Defines document level styles
• <LINK> tag
References external style sheets. Allows for alternates.
• STYLE attribute
Defines inline styles for a specific block of HTML code
• @import Directive
Loads an external style sheet. Not supported in some older browsers.
• Rules
Defines which styles to apply to which elements
• Selectors
Specifies the element or type of element that style affects
• Declarations
Specifies CSS properties and values
CSS: RULE STRUCTURE
2. Class Selectors
- Used to allow different occurrences of the same tag to use different style
specifications
- For example,
- The class you want on a particular occurrence of a tag is specified with the class
attribute of the tag
- For example,
- General form:
#specific-id {property-value list}
- Example:
#section14 {...}
3.4 Selector Forms (continued)
5. Contextual Selectors
- Descendant Selectors
- Child Selectors
p > h1 > em – applies to em when it is the child of an h1 element that is the child of
a p element
p:empty
for no children
PSEUDO-ELEMENTS AND PSEUDO-CLASSES
There are two types of keywords that you can combine with selectors: Pseudo-elements and Pseudo-classes
1. Pseudo-classes Example:
/* unvisited link */
A pseudo-class is used to define a special a:link {
state of an element. color: #FF0000;
It can be used to: }
Style an element when a user mouses over it /* visited link */
Style visited and unvisited links differently a:visited {
color: #00FF00;
Style an element when it gets focus }
Syntax:
/* mouse over link */
selector:pseudo-class { a:hover {
property: value; color: #FF00FF;
} }
/* selected link */
a:active {
color: #0000FF;
}
PSEUDO-ELEMENTS AND PSEUDO-CLASSES-CONT.
There are two types of keywords that you can combine with selectors: Pseudo-elements and Pseudo-classes
2. Pseudo-elements
A CSS pseudo-element is used to style specified parts of an element.
- Pseudo classes are styles that apply when something happens, rather than because the
target element simply exists
- hover classes apply when the mouse cursor is over the element
7. Universal Selector
* {color: red;}
- Applies to all elements in the document
3.5 Property Value Forms
- There are 60 different properties in 7 categories:
- Fonts
- Lists
- Alignment of text
- Margins
- Colors
- Backgrounds
- Borders
- URL values
- url(protocol://server/pathname)
- Colors
- Color name
- rgb(n1, n2, n3)
- Numbers can be decimal or percentages
- Hex form: #XXXXXX
- Generic fonts: serif, sans-serif, cursive, fantasy, and monospace (defined in CSS)
- Browser has a specific font for each
- If a font name has more than one word, it should be single-quoted
3.6 Font Properties (continued)
- font-size
- Possible values: a length number or a name,
such as smaller, xx-large, etc.
- Points or picas do not display the same
- Percentages and em are the best
- Font variants
- Default is normal, but can be set to small-caps
- font-style
- italic, oblique (useless), normal
- font-weight - degrees of boldness
- bolder, lighter, bold, normal
- Could specify as a multiple of 100 (100 – 900)
- font (shorthand)
- For specifying a list of font properties
font: bolder 14pt Arial Helvetica
- Order must be: style, weight, size, name(s)
3.6 Font Properties (continued)
- The text-decoration property
- line-through, overline, underline, none
- Text Spacing
- word-spacing property – the amount of space between words possible values – like
those of letter-spacing
- Unordered lists
- Bullet can be a disc (default), a square, or a circle
- Example:
- The text-align property has the possible values, left (the default), center, right, or
justify
- Sometimes we want text to display around another element - the float property
- The float property has the possible values, left, right, and none (the default)
- If we have an element we want on the right, with text flowing on its left, we use the
default text-align value (left) for the text and
the right value for float on the element we want on the right
3.8 Alignment of Text (continued)
<img src = "c210.jpg" style = "float: right" />
1. There is a set of 17 colors that are guaranteed to be displayable by all graphical browsers on
all color monitors
- 216 colors
- Use hex color values of 00, 33, 66, 99, CC, and FF
Color Specification
To apply a font to a specific section of text, create a class, and use the span tag with that
class:
.neatstuff {font-family: Comic Sans MS;}
- Sources of conflict:
4. Property values can come from style sheets written by the document author, the browser
user, and the browser defaults
- Resolution mechanisms: