MA UNIT - 1 CSS
MA UNIT - 1 CSS
• CSSs provide the means to control and change presentation of HTML documents
• The most important benefit of style sheets is their capability of imposing consistency on
the style of documents. Eg: they allow the author to specify that all occurrences of a
particular tag use the same presentation style. Style is specified for a tag by the values of its
property.
• Saves time
• Easy to change
• Inline style sheets - specified for a specific occurrence of a tag and apply only to that tag. This
is fine grain style, which defeats the purpose of style sheets - uniform style
• Document-level (Internal) style sheets - apply to the whole document in which they appear
When more than one style sheet applies to a specific tag in a document, the lowest level style
sheet has precedence. For eg: if an external style sheet specifies a value for a particular
property of a particular tag , that value is used until a different value is specified in either a
document style sheet or an inline style sheet
Likewise a document style sheet property value can be overridden by different property values
in an inline style sheet. Inline style sheets appear in the tag itself. Document-level style sheets
appear in the head of the document.
External style sheets are in separate files, they can be stored on any computer on the web. The
browser fetches just as it fetches documents.
Order:
• Browser default
• Inline Style sheets have precedence over document style sheets which have precedence over
external style sheet.
• Inline style sheet appears as the values of the style attribute of a tag.
General form:
property_2: value_2;
property_n: value_n;"
Example:
A style is applied to the entire HTML file. Use it when you need to modify all instances of
particular element (e.g., h1) in a web page.
Style sheet appears as a list of rules that are the content of a <style> tag within the header of a
document.
General form:
rule list
</style>
The <style> tag must include the type attribute, set to "text/css". Each style rule in a rule list has
two parts:
Example:
<style>
h1 {color:red;font-size:20;font-family:monospace;}
</style>
PROGRAM
<html>
<head>
<title>Getting Started</title>
<style type=“text/css”>
p{font-size:15;font-style:italic;background-color:grey;}
</style>
</head>
<body>
<h1>…..</h1>
<p>….</p>
</body>
</html>
An external style sheet is a text file containing the style definition (declaration). Use it when you
need to control the style for an entire web site. The <link> tag is used to specify external style
sheets. Within <link>, the rel attribute is used to specify the relationship of the linked to
document to the document in which the link appears. The href attribute of <link> is used to
specify the URL of the style sheet document as in the following document:
href = "http://www.wherever.org/termpaper.css">
</link>
• Save this file and the .css file in the same web server directory.
Selector Forms
The selector can have variety of forms:
2. Class selector
3. Generic selector
4. Id selector
5. Contexual Selectors
6. Universal selector
7. Pseudo classes
The selector is a tag name or a list of tag names, separated by commas. Consider the following
examples, in which the property is font-size and the property value is a number of points:
h2 { font-size: 20pt ;}
2. Class Selector
It is used to allow different occurrences of the same tag to use different style specifications. A
style class has a name, which is attached to a tag name.
p.warning{property/value list}
The class you want on a particular occurrence of a tag is specified with the class attribute of the
tag. For example,
A paragraph of text that is a warning to the reader ,which should be presented in an especially
noticeable style.
</p>
Classes:
HTML and XHTML require each id be unique– therefore an id value can only be used once in a
document. You can mark a group of elements with a common identifier using the class
attribute.
3. Generic Selectors
A generic class can be defined if you want a style to apply to more than one kind of tag. A
generic class must be named, and the name must begin with a period
Ex:
...
4. id Selectors
General form:
Example:
<h2 id = “section14”>Hello</h2>
5. Contextual selectors
Selectors can also specify that the style should apply only to elements in certain positions in the
document .This is done by listing the element hierarchy in the selector.
In the eg, selector applies its style to the content of emphasis elements that are descendants of
bold elements in the body of the document.
It is also called as descendant selectors. It will not apply to emphasis element not descendant of
bold face element.
6. Universal Selectors
The universal selector denoted by an asterisk(*). It applies its style to all elements in the
document.
Ex:
* {color : red}
7. Pseudo Classes
Pseudo classes are styles that apply when something happens, rather than because the target
element (tag) simply exists. Names begin with colons
The style of hover pseudo classes apply when the mouse cursor is over the element. The style
of focus pseudo classes apply when an element has focus (mouse cursor over the element and
click the left mouse button)
<head>
</style>
</head>
<body>
<p>
Your name:
</p>
</form>
</body>
</html>
• Fonts
• Lists
• Alignment of text
• Margins
• Colors
• Backgrounds
• Borders
• Length - numbers, maybe with decimal points followed by two character abbreviation of a
unit name.
• Units:
px - pixels
in - inches
cm - centimeters
mm - millimeters
pt - points
pc - picas (12 points)
em – value of the current font size in pixels
ex - height of the letter ‘x’
No space is allowed between the number and the unit specification. Ex: 1.5 in is illegal!
Eg: 10px, 24pt etc.
Percentage - just a number followed immediately by a percent sign. Eg: 70%
URL values - url(protocol://server/pathname)
Colors
Color name: eg: fuchsia
rgb(n1, n2, n3) Eg: rgb(255,0,255)
Numbers can be decimal or percentages
Hexadecimal form: hexadecimal numbers must be preceded with pound(#) sign.
Eg : #B0E0E6 stands for powder blue color.
6. Font Properties
1. Font-Families
The font-family property is used to specify a list of font name. The browser will use the first font
in the list that it supports. For example, the following could be specified.
If a font name has more than one word, it should be single-quoted. ‘Times New Roman‘
2. font-size
It sets the size of fonts. There are two categories of font-size values, absolute and relative.
In the case of absolute category the size value could be given as length value in points, picas or
pixels or keywords from the list xx-small, x-small, small, medium, large and x-large.
The relative size values are smaller and larger, which adjust the font size relative to the font
size of the parent element.
Eg: font-size:1.2em
This sets the font size to 1.2 times the font size of the parent element.1.2em and 120% are
same.
3. Font Variant
The default value of the font-variant property is normal, which specifies the usual character
font. This property can be set to small-caps to specify small capital letters.
4. font-style
5. font-weight
Could specify as a multiple of 100 (100 – 900) where 400 is same as normal.
6. Font Shorthands
For specifying more than one font properties. The values can be stated in a list as value of the
font property.
The order which browser follows is last must be font name, second last font size and then the
font style, font variant and font weight can be in any order but before the font size and names.
Ex:
<!DOCTYPE html>
<!—fonts.html
<html lang=”en”>
font-style: italic;
</style>
</head>
If a job is worth doing,it’s worth doing right</p> < p class = “minor”> Two wrong don’t make a
right,but they certainly can get you in a lot of trouble.
</p>
</body>
</html>
Output:
Revision of fonts.html that uses an external style sheet rather than document level
stylesheets:
fonts2.html
<!DOCTYPE html>
<html lang=”en”>
<head><title>Font Properties</title>
</head>
<body> <p class = “major”> If a job is worth doing,it’s worth doing right</p> < p class =
“minor”> Two wrong don’t make a right,but they certainly can get you in a lot of trouble.
</p>
</body>
</html>
/*styles.css */
font-style: italic;
Output:
It specifies some special features of text. Possible values of text-decoration property are line-
through, overline, underline, none
Example:
<!DOCTYPE html>
<!—fonts2.html
<html lang=”en”>
<head><title>Text Decoration</title>
p.cap {text-decoration:overline}
</style>
</head>
<body>
</p>
</p>
</p>
</body>
</html>
Output:
8. Text Spacing
The letter-spacing property controls the amount of space between letters in words. This
spacing is called tracking. The possible values of letter-spacing are normal or any length
property values—for example, 3px.
Positive values increase the letter spacing, negative values decrease it.
For eg: letterspacing: 1px spreads the letters of words. letter-spacing: -1px squeezes the letters
of the words together. The value normal resets letter-spacing back to that of parent element.
The space between words in a text can be controlled with the word-spacing property, whose
values are normal or any length value. Positive values increase the letter spacing, negative
values decrease it.
The space between lines of text can be controlled with the line-height property. This spacing is
called leading. The value of line-height can be a number or a length value.
List properties
Property Name: list-style-type can applied to both ordered and unordered list.
Unordered lists:
Bullet can be a disc (default), a square, or a circle. Set it on either the <ul> or <li> tag.
Ex:
ul {list-style-type:square}
</style> ……………………………
<ul>
</ul>
Ex:
<ul>
</ul>
Output:
Ordered lists:
On ordered lists list-style-type property can be used to change the sequence values.
upper-alpha Uc letters A, B, C, D
lower-alpha Lc letters a, b, c, d
Ex:
ol {list-style-type: upper-roman;}
ol ol {list-style-type: upper-alpha;}
ol ol ol {list-style-type: decimal;}
</style>
</head>
<body>
<ol>
<ol>
<ol>
</ol>
</li>
<ol>
</ol>
</li>
</ol>
</li>
</ol>
</body>
</html>
Output:
Alignment of Text
The text-indent property allows indentation (first line of a paragraph can be intended). This
property takes either a length or a % value.
Ex:
</style>
…………
Now is the time for all good Web programmers to begin using cascading style sheets for all
presentation details in their documents. No more deprecated tags and attributes, just nice,
precise style sheets. </p>
Output:
Now is the time for all good Web programmers to begin using cascading style sheets for
all presentation details in their documents. No more deprecated tags and attributes, just nice,
precise style sheets.
The text-align property has the possible values, left (the default), center, right, or justify
The float property is used to specify that text should flow around another element often an
image or table. 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.
Or we can specify float style using inline style sheet for image.
Output:
Colors
Color is a problem for the Web for two reasons:
Color Properties :
Ex:
</style>
<table>
<tr>
</tr>
</table>
Output:
Borders
• Borders – every element has a border-style property, that Controls whether the element has
a border and if so, the style of the border.
• The style of one particular element can be set with border-top-style, borderbottom-style,
border-left-style, and border-right-style
• border-width – thin, medium (default), thick, or a length value in pixels. border -width can be
specified for any of the four borders (e.g., border-top-width…)
• border-color – any color. Border color can be specified for any of the four borders (e.g.,
border-top-color)
The following document, borders.html, illustrates borders, using a table and a short paragraph
as examples:
Output:
Margin is the space between the border of an element and its neighbor element. Padding is the
space between the content of an element and its borders. The margin properties are named
MA – UNIT 1 - CSS Page 22
SMS COLLEGE, BRAHMAVAR
margin, which applies to all four sides of an element: margin-left, margin-right, margin-top and
margin-bottom. The padding properties are named padding, which applies to all four sides:
padding-left, padding-right, padding-top and padding-bottom.
Output:
Background Images
The background-image property is used to place an image in the background of an element.
The background image is replicated as necessary to fill the area of the element. This replication
is called tiling. The tiling can be controlled with the background-repeat property. Possible
values: repeat (default), no-repeat, repeat-x, or repeat-y. The no-repeat value specifies that just
one copy of the image is to be displayed. The repeat-x value means that the image is to be
repeated horizontally; repeat-y means that the image is to be repeated vertically. In addition,
the position of a non repeated background image can be specified with the background-
position property. Possible values: top, center, bottom, left, or right.
<p>
</p>
</style>
<p>
</p>
Output:
The <span> tag is similar to other HTML tags, they can be nested and they have id and class
attributes.
It is common for documents to have sections, each consisting of some number of paragraphs
that have their own presentation styles. Used to create document sections (or divisions) for
which style can be specified.
Ex: A section of five paragraphs for which you want some particular style.
Example:
<html>
<head>
<style>
margin: 30px;
padding-bottom: 20px;
text-align: justify;
width: 140px;
color: red; }
</style>
</head>
<body>
<div>This is some text in a div element. This is some text in a div element. This is some
text in a div element. This is some text in a div element. This is some text in a div
element. This is some text in a div element.</div>
</p>
</body>
</html>
Output: