R18 WT Unit-2 CSS NOTES
R18 WT Unit-2 CSS NOTES
Rules
At the very core of CSS are rules. The following is an example of a simple CSS rule:
h1 {color:green;}
Rules consist of two parts - the selector and the declaration. In the example above, the H1
tag is the selector (the object being modified), and the color is the declaration (what part
– color – and how – to green – the selector is being changed).
{color:green;}
The declaration has two parts - the property and the value. In the example, above,
property is the color of the H1 tag that is being modified. The value is set to green, which
specifies how the property is being modified.
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 1
<!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>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
h1 {
color: #FF0000;
}
-->
</style>
</head>
<body>
<h1>How are you doing today?
</h1>
</body>
</html>
Grouping Selectors
As you add more and more styles to your pages, you might find yourself making the
same stylistic change to multiple XHTML elements. In these cases, you might consider
grouping your CSS selects. This can shorten the amount of code and make for a quicker
download.
This rule specifies that all the text within h1, h2, h3 tags will display in the Verdana font
with a blue color.
Class Selectors
In the preceding examples, you learned how to create styles based on XHTML elements
using selectors. Selectors are related to XHTML elements. If you wanted to apply a style
to something that wasn’t a tag (let’s say there was a certain sentence in your document
that you wanted to be bold, but it wasn’t anything other than text from a structural
definition), this kind of situation is where Class selectors are of value. Another situation
is which a Class selector comes in handy is when you want to style the appearance of
XHTML tags differently. Earlier you learned how to redefine the appearance of an
XHTML element (the h1 tag). Suppose you don’t want to format every h1 tag the same
way? This is a perfect place to consider creating a Class selector. Consider the following
code:
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 2
<!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>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
.mytext {
color: #FF6600;
}
-->
</style>
</head>
<body>
<p class="mytext">This is how you use class selectors.
</p>
<p> </p>
</body>
</html>
Class selectors are written in this syntax: always beginning with a dot (.) and a
unique name.
The Class selector is applied to the XHTML element using the class attribute and
the name you assigned to the class. In this example, we gave the class the name of
“mytext”. Notice that the dot (.) is not included in the class attribute.
By creating a Class selector, you could now apply this formatting anywhere in your
document independent of the XHTML element. It will apply this format only where it
encounters this class, not across every instance of an XHTML element like previously
shown.
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 3
</head>
<body>
<p>This is how you use <span class="mytext"> class</span> selectors. </p>
<p> </p>
</body>
</html>
The Class selector syntax is displayed on this line with the font-family and the
font-weight properties applied.
The <span> tag is used to designate the area of text that is to be formatted using
the Class selector. Notice that there is a closing </span> tag to end the formatting.
<body>
<div class="mytext">
<p>Greenwaves, Inc </p>
<p>Company Information</p>
</div>
</body>
</html>
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 4
A <div> element is used to create a range, or invisible box if you will, around the two
<p> elements. The body Class is attached to the <div> element, which causes both
paragraphs to be formatted with the body Class. So, instead of applying the body Class to
both paragraph elements, it was applied once to the <div> element. This results in less
code, which is a good thing.
Inline elements: As described, inline-elements act like boxes as well, except they are used
within block-level elements. This is the difference between the two. For example, if you
have a <div> element that is the formatting a paragraph of text with a style sheet and you
want to format a single word within that <div> element, you would use the inline element
<span>.
Pseudo-Classes
So far, you have learned about selectors, classes, and Ids. Things are about to get
interesting with pseudo-classes. These are class selector that let the designer apply styles
to element that don’t exist within the document. It lets you apply styles to elements that
you know will exist, you just don’t know when. For example, users are likely to move
their cursor over a link at some point. The anchor tag is probably the most common
pseudo-class. Here is an example:
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 5
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
a:link {
color: #FF6600;
}
a:visited {
color: #FFCC33;
}
a:hover {
color: #0066FF;
}
a:active {
color: #99CC33;
}
-->
</style>
</head>
<body>
<a href="http://www.cnn.com">link to cnn
</a>
</body>
</html>
The pseudo class is automatically applied to every anchor tag in the document. You do
not have to add any additional code in the <body> portion of the document.
The following chart identifies the pseudo-classes available in the CSS2 recommendation.
Note that pseudo-class selectors and links must be in a specific order to work properly.
Internal
Here is an example of “internal” style sheet. Notice that the <style> element is used
inside the <head> portion of the page. All of the formatting information is contained
within the HTML page. These types of style sheets will only affect the appearance of this
single page. They can be effective for creating design efficiency within the single page.
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 6
Example:
<!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>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
h1 {
color: #FF0000;
}
-->
</style>
</head>
<body>
<h1>How are we doing today? </h1>
</body>
</html>
External
Here’s an example of an “external” style sheet. Notice the <link> element is using the
<href> attribute to point to the “external.css” file. The external.css file is the document
that contains all the formatting information for this XHTML page. Because the
formatting information exists independent of the XHTML page, it can easily be applied
to several XHTML pages and updated more efficiently.
The file this document references is called external.css. You can give your style sheet any
name you want; just be sure to save it in text only mode and upload it to the same
directory. Make sure to give it a .css extension. You don’t have to put an external style
sheet inside a comment tag because no style information is stored within the XHTML
code.
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 7
Your external.css document could look like this:
h1 {color: red; font-family: sans-serif}
p {background: black; color:yellow; font-family: verdana;}
Inline
An “inline” style sheet only applies to the parts of an HTML document that are specified
and will override any style settings being applied by an external or internal style sheet.
In the above example, Inline style will override both external and internal style. Internal
will override external.
ID versus Class
At this point, you might be wondering about the difference between a Class and ID
selector. Functionally, it doesn’t really matter because they both accomplish the same
thing. However, there are a few differences.
Ids start with a # symbol instead of a dot (.). Also, it is considered an error to use the
same ID selector more than once. In fact, Ids should be used only once within a given
document, and they should always have a unique name different from other Ids in the
document. This makes them great for absolute positioning, where they would only want
to use them once. Classes are great for formatting text because they can be used multiple
times on a web page. Because of this, you will find yourself probably using classes most
of the time.
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 8
CASCADING STYLE SHEETS (CSS)
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 9
font-size:xx-small or 16px
x-small 18px
small 24px
medium
large 9. Changing the Text Case
x-large
xx-large text-transform:capitalize
uppercase
Example: lowercase
1. P{font-size:medium}
2. H1{font-size:24px} Example:
H1{text-align:center;
5. Setting the Text Color text-transform:uppercase}
border-style:dotted
7. Adding Indents dashed
solid
text-indent:length (18px, 24px.....) double
R18 WT – UNIT2 – CSS NOTES By P.Kishor, HOD & Assoc. Prof., SCIT,KNR Page 10