CSS Notes
CSS Notes
Unit -2
Getting Started With CSS
CSS Advanced
Unit -2
Getting Started With CSS
Learning objectives:
❖ What a style sheet is and how it actually styles a web page
❖ The importance of CSS.
❖ How to create a style sheet and link an html document to the style sheet.
❖ The basic building blocks of any style sheet: rules, selectors, properties and
values.
❖ How External Style Sheets are stored in CSS files
❖ Three ways of inserting a style sheet
❖ Different selectors using CSS
❖ Define background properties, Background colour & images handling
❖ Font family, Style and Size using CSS
❖ Designing Tables using CSS
3.1 Introduction of CSS
CSS stands for Cascading Style Sheets. It is a simple design language intended to simplify the
process of making web pages presentable. CSS handles the look and feel part of a web page. Using
CSS, you can control the color of the text, the style of fonts, the spacing between paragraphs, how
columns are sized and laid out, what background images or colors are used, as well as a variety
of other effects.
CSS works with HTML and other Markup Languages (such as XHTML and XML) to
control the way the content is presented. Cascading Style Sheets is a means to separate the
appearance of a webpage from the content of a webpage.
3.1.1 Definition
Cascading Style Sheets (CSS) is a simple mechanism used to format the layout of Web
Pages and adding style (e.g., fonts, colors, spacing...) to web documents that previously could
only be defined in a page's HTML. CSS describes how HTML elements are to be displayed
on screen, paper, or in other media. It can control the layout of multiple web pages all at once.
3.1.2 Advantages
The cascade part of CSS means that more than one style sheet can be attached to a
document, and all of them can influence the presentation. For example, a designer can have a
global style sheet for the whole site, but a local one for say, controlling the link color and
background of a specific page. Or, a user can use own style sheet if s/he has problems seeing
the page, or if s/he just prefers a certain look.
3.2 CSS Syntax
Selector {property:
Selector
{
property1: some value;
property2: some value;
}
The declaration contains the property and value for the selector. The property is the attribute you
wish to change and each property can take a value. The property and value are separated by a
colon and surrounded by curly braces:
body { background-color: black}
If the value of a property is more than one word, put quotes around that value: body { font-family:
"sans serif"; } If you wish to specify more than one property, you must use a semi-colon to separate
each property. This rule defines a paragraph that will have blue text that is centered.
p { text-align: center; color: blue }
You can group selectors. Separate each selector with a comma. The example below groups
headers 1, 2, and 3 and makes them all yellow. h1, h2, h3 { color: yellow}
3.3 CSS
Selectors
You can define selectors in various simple ways based on your comfort. Let me put these
selectors one by one. Three types of CSS Selectors
1. The Element selectors
2. The ID Selectors
3. The Class Selectors
3.3.1 The Element selectors
A CSS declaration always ends with a semicolon, and declaration groups are surrounded
by curly brackets: example -
p {color:red;text-align:center;}
To make the CSS more readable, you can put one declaration on each line, like this:
#welcome
{
color:red;
text-align:center;
}
3.3.3 The Class selectors
The class selector is used to specify a style for a group of elements. Unlike the id selector,
the class selector is most often used on several elements. This allows you to set a
particular style for many HTML elements with the same class. The class selector uses the
HTML class attribute, and is defined with a ".". In the example below, all HTML elements
with class="center" will be center-aligned:
Imagine within the body element of our html page, we have the following header element
<h2 class=”center”>Summary</h2>
We can then create a CSS rule with the class selector:
.center {text-align:center;}
You can also specify that only specific HTML elements should be affected by a class. In
the example below, all p elements with class="center" will be center-aligned: example
p.center {text-align:center;}
Some of the other selectors are used in CSS, they are :
3.3.3.1 Universal selector
An asterisk (*) is the universal selector for CSS. It matches a single element of any type.
Omitting the asterisk with simple selectors has the same effect. For instance, *.warning
and. warning are considered equal. Rather than selecting elements of a specific type, the
universal selector quite simply matches the name of any element type: Example-
*
{
Color:#000000;
}
This rule renders the content of every element in our document in black.
3.3.3.2 Attribute Selector
You can also apply styles to HTML elements with particular attributes. The style rule
below will match all the input elements having a type attribute with a value of text:
input[type="text"]
{
color: #000000;
}
The advantage to this method is that the <input type="submit" /> element is unaffected,
and the color applied only to the desired text fields.There are following rules applied to
attribute selector.
➢ p[lang] - Selects all paragraph elements with a lang attribute.
➢ p[lang="fr"] - Selects all paragraph elements whose lang attribute
has a value of exactly "fr".
➢ p[lang~="fr"] - Selects all paragraph elements whose lang attribute
contains the word "fr".
➢ p[lang|="en"] - Selects all paragraph elements whose lang attribute
contains values that are exactly "en", or begin with "en-".
An external style sheet can be written in any text editor. The file should not contain any html tags.
Your style sheet should be saved with a .css extension. An example of a style sheet file is shown
below:
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
Notes : Do not leave spaces between the property value and the units! "margin- left:20 px"
(instead of "margin- left:20px") will work in IE, but not in Firefox or Opera.
body {background-color:#b0c4de;}
<p style="background-color:yellow;">
This text has a yellow background color. </p>
This color value is specified using the rgb( ) property. This property takes three values,
one each for red, green, and blue. The value can be an integer between 0 and 255 or a percentage.
NOTE: All the browsers does not support rgb() property of color, so it is recommended not to use
it.
3.7 Text Management using CSS
CSS is a language that describes the style of an HTML document. You can set the following
text properties of an element:
The following example demonstrates how to set the space between characters. Possible
values are normal or a number specifying space.
The following example demonstrates how to set the space between words. Possible
values are normal or a number specifying space.
<p style="text-indent:1cm;">
This text will have first line indented by 1cm and this line will
remain at its actual position this is done by CSS text-indent
property.
</p>
The following example demonstrates how to align a text. Possible values are left, right,
center, justify.
The following example demonstrates how to decorate a text. Possible values are none,
underline, overline, line-through, blink.
The following example demonstrates how to set the cases for a text.
Possible values are none, capitalize, uppercase, lowercase.
:
3.8 Font Management using CSS
A font is the combination of typeface and other qualities, such as size, pitch, and spacing. For
example, Times Roman is a typeface that defines the shape of each character. Within Times
Roman, however, there are many fonts to choose from -- different sizes, italic, bold, and so
on.You can set the following font properties of an element:
Following is the example, which demonstrates how to set the font family of an element.
Possible value could be any font family name.
<p style="font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the default serif font
depending on which font you have at your system. </p>
The following example demonstrates how to set the font style of an element. Possible
values are normal, italic and oblique.
The following example demonstrates how to set the font stretch of an element. This
property relies on the user's computer to have an expanded or condensed version
of the font being used.
Possible values could be normal, wider, narrower, ultra-condensed, extra-
condensed, condensed, semi-condensed, semi-expanded, expanded, extra-
expanded, ultra-expanded.
<p style="font-stretch:ultra-expanded;">
If this doesn't appear to work, it is likely that your computer
doesn't have a condensed or expanded version of the font being
used. </p>
Example:-
3.9 Managing hyperlinks using CSS
An element in an electronic document that links to another place in the same document or to an
entirely different document. A hyperlink, or simply a link, is a reference to data that the reader
can directly follow either by clicking, tapping, or hovering. A hyperlink points to a whole
document or to a specific element within a document. Hypertext is text with hyperlinks. The text
that is linked is called anchor text. You can set the following properties of a hyperlink:
Usually, all these properties are kept in the header part of the HTML document. Remember
a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective. Also,
a:active MUST come after a:hover in the CSS definition as follows:
<style type="text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
The following example demonstrates how to set the link color. Possible values could be
any color name in any valid format.
<style type="text/css">
a:link {color:#000000}
</style>
The following example demonstrates how to set the color of the visited links. Possible
values could be any color name in any valid format.
<style type="text/css">
a:visited {color: #006600}
</style>
<a href="/html/index.htm">Click this link</a>
3.9.3 Change the Color of Links when Mouse is Over
The following example demonstrates how to change the color of links when we bring a
mouse pointer over that link. Possible values could be any color name in any valid format.
<style type="text/css">
a:hover {color: #FFCC00}
</style>
The following example demonstrates how to change the color of active links. Possible
values could be any color name in any valid format.
<style type="text/css">
a:active {color: #FF00CC}
</style>
Lists are very helpful in conveying a set of either numbered or bulleted points. This chapter
teaches you how to control list type, position, style, etc., using CSS. We have the following five
CSS properties, which can be used to control lists:
➢ The list-style-type allows you to control the shape or appearance of the marker.
➢ The list-style-position specifies whether a long point that wraps to a second line should
align with the first line or start underneath the start of the marker.
➢ The list-style-image specifies an image for the marker rather than a bullet point or
number.
➢ The list-style serves as shorthand for the preceding properties.
➢ The marker-offset specifies the distance between a marker and the text in
the list.
The list-style-type property allows you to control the shape or style of a bullet point (also known
as a marker) in case of unordered lists and the style of numbering characters in ordered lists. Here
are the values, which can be used for an unordered list:
Value Description
None NA
disc A filled-in
(default) circle
Circle An empty
circle
Square A filled-in
square
Here are the values, which can be used for an ordered list:
Here is an example:
The output of the above program is :
Value Description
None NA
Inside If the text goes onto a second line, the text will wrap
underneath the marker. It will also appear indented to where
the text would have started if the list had a value of outside.
Outside If the text goes onto a second line, the text will be aligned
with the start of the first line (to the right of the bullet).
Example :
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<ul style="list-style-type:circle; list-stlye-
position:outside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul style="list-style-type:square;list-style-
position:inside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style-type:decimal;list-stlye-
position:outside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style="list-style-type:lower-alpha;list-style-position:inside;">
<li>Maths</li>
<li>Social Science</li><li>Physics</li>
</ol>
</body>
</html>
Tables are an excellent way to organize and display information on a page. You can set the
following properties of a table:
• The border-collapse specifies whether the browser should control the appearance of the
adjacent borders that touch each other or whether each cell should maintain its style.
• The border-spacing specifies the width that should appear between table cells.
• The caption-side captions are presented in the <caption> element. By default, these are
rendered above the table in the document. You use the caption-side property to control the
placement of the table caption.
• The empty-cells specify whether the border should be shown if a cell is empty.
• The table-layout allows browsers to speed up the layout of a table by using the first width
properties it comes across for the rest of a column rather than having to load the whole
table before rendering it.
This property can have two values collapse and separate. The following
example uses both the values:
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<style type="text/css">
table.one
{border-collapse:collapse;}
table.two
{border-collapse:separate;}
td.a
{
border-style:dotted;
border-width:3px;
border-color:#000000;
padding: 10px;
}
td.b
{
border-style:solid;
border-width:3px;
border-color:#333333;
padding:10px;
}
</style>
<table class="one">
<caption>Collapse Border Example</caption>
<tr><td class="a"> Cell A Collapse Example</td></tr>
<tr><td class="b"> Cell B Collapse Example</td></tr>
</table><br />
<table class="two">
<caption>Separate Border Example</caption>
<tr><td class="a"> Cell A Separate Example</td></tr>
<tr><td class="b"> Cell B Separate Example</td></tr>
</table>
</body>
</html>
The border-spacing property specifies the distance that separates the adjacent cells’
borders. It can take either one or two values; these should be units of length. If you provide one
value, it applies to both vertical and horizontal borders. Or you can specify two values, in which
case, the first refers to the horizontal spacing and the second to the vertical spacing:
NOTE: Unfortunately, this property does not work in Netscape 7 or IE 6.
Now let's modify the previous example and see the effect:
<style type="text/css">
table.one
{
border-collapse:separate; width:400px;
border-spacing:10px;
}
table.two
{
border-collapse:separate; width:400px;
border-spacing:10px 50px;
}
</style>
<table class="one" border="1">
<caption>Separate Border Example with border-spacing</caption>
<tr><td> Cell A Collapse Example</td></tr>
<tr><td> Cell B Collapse Example</td></tr>
</table><br />
<table class="two" border="1">
<caption>Separate Border Example with border-spacing</caption>
<tr><td> Cell A Separate Example</td></tr>
<tr><td> Cell B Separate Example</td></tr>
</table>
The empty-cells property indicates whether a cell without any content should have a border
displayed. This property can have one of the three values - show, hide, or inherit. Here is the
empty-cells property used to hide borders of empty cells in the <table> element.
Example:
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<style type="text/css">
table.empty
{
width:350px;
border-collapse:separate;
empty-cells:hide;
}
td.empty
{
padding:5px;
border-style:solid;
border-width:1px;
border-color:#999999;}
</style>
<table class="empty">
<tr>
<th></th>
<th>Title one</th>
<th>Title two</th>
</tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td><td class="empty">value</td></tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td><td class="empty"></td>
</tr>
</table>
</body>
</html>
You have seen the border that surrounds every box i.e. element, the padding that can appear inside
each box, and the margin that can go around them. In this chapter, we will learn how to change
the dimensions of boxes. We have the following properties that allow you to control the
dimensions of a box.
➢ The height property is used to set the height of a box.
➢ The width property is used to set the width of a box.
➢ The line-height property is used to set the height of a line of text.
➢ The max-height property is used to set a maximum height that a box can be.
➢ The min-height property is used to set the minimum height that a box can be.
➢ The max-width property is used to set the maximum width that a box can be.
➢ The min-width property is used to set the minimum width that a box can be.
The height and width properties allow you to set the height and width for boxes. They can take
values of a length, a percentage, or the keyword auto. Here is an example:
The line-height property allows you to increase the space between lines of text. The value of the
line-height property can be a number, a length, or a percentage. Here is an example:
The max-height property allows you to specify the maximum height of a box. The value of the
max-height property can be a number, a length, or a percentage.
NOTE: This property does not work in either Netscape 7 or IE 6. Here is an example:
The output of the above program is
The border-color property allows you to change the color of the border surrounding an
element. You can individually change the color of the bottom, left, top and right sides of an
element's border using the properties:
The border-style property allows you to select one of the following styles of border:
➢ none: No border. (Equivalent of border-width:0;)
➢ solid: Border is a single solid line.
➢ dotted: Border is a series of dots.
➢ dashed: Border is a series of short lines.
➢ double: Border is two solid lines.groove: Border looks as though it is carved into the
page.
➢ ridge: Border looks the opposite of groove.
➢ inset: Border makes the box look like it is embedded in the page.
➢ outset: Border makes the box look like it is coming out of the canvas.
➢ hidden: Same as none, except in terms of border-conflict resolution for
table elements.
You can individually change the style of the bottom, left, top, and right borders of
an element using the following properties:
Outlines are very similar to borders, but there are few major differences as well:
➢ An outline does not take up space.
➢ Outlines do not have to be rectangular.
➢ Outline is always the same on all sides; you cannot specify different values
for different sides of an element.
NOTE: The outline properties are not supported by IE 6 or Netscape 7. You can
set the following outline properties using CSS.
The outline-width property specifies the width of the outline to be added to the box. Its value
should be a length or one of the values thin, medium, or thick, just like the border-width attribute.
A width of zero pixels means no outline. Here is an example:
The outline-style property specifies the style for the line (solid, dotted, or dashed) that goes
around an element. It can take one of the following values:
➢ none: No border. (Equivalent of outline-width:0;)
➢ solid: Outline is a single solid line.
➢ dotted: Outline is a series of dots.
➢ dashed: Outline is a series of short lines.
➢ double: Outline is two solid lines.
➢ groove: Outline looks as though it is carved into the page.
➢ ridge: Outline looks the opposite of groove.
➢ inset: Outline makes the box look like it is embedded in the page.
➢ outset: Outline makes the box look like it is coming out of the canvas.
➢ hidden: Same as none.
Here is an example:
The above program will produce the following result:
The outline-color property allows you to specify the color of the outline. Its value should either
be a color name, a hex color, or an RGB value, as with the color and border-color properties. Here
is an example:
The margin property defines the space around an HTML element. It is possible to use
negative values to overlap content. The values of the margin property are not inherited by the
child elements. Remember that the adjacent vertical margins (top and bottom margins) will
collapse into each other so that the distance between the blocks is not the sum of the margins, but
only the greater of the two margins or the same size as one margin if both are equal.
We have the following properties to set an element margin.
➢ The margin specifies a shorthand property for setting the margin
properties in one declaration.
➢ The margin-bottom specifies the bottom margin of an element.
➢ The margin-top specifies the top margin of an element.
➢ The margin-left specifies the left margin of an element.
➢ The margin-right specifies the right margin of an element.
The margin property allows you to set all of the properties for the four margins in
one declaration. Here is the syntax to set margin around a paragraph:
<style type="text/css">
p {margin: 15px}
all four margins will be 15px
Here is an example:
The margin-bottom property allows you to set the bottom margin of an element. It can have a
value in length, %, or auto.
Here is an example:
The margin-left property allows you to set the left margin of an element. It can
have a value in length, %, or auto. Here is an example:
The above program will produce the following result
.
Unit -2
CSS Advanced
Learning objectives:
Paragraph Padding
Image height and width setting
Block level and inline elements
Pseudo-class and elements
Various attribute of image
Different attribute values of selectors
The padding-bottom property sets the bottom padding (space) of an element. This
can take a value in terms of length of %. Here is an example:
Page 41
4.1.2 The padding-top Property
The padding-top property sets the top padding (space) of an element. This can take a value in
terms of length of %. Here is an example:
Page 42
4.2 Setting Display Using CSS
The display property is the most important CSS property for controlling layout. The display
property specifies if/how an element is displayed. Every HTML element has a default display
value depending on what type of element it is. The default display value for most elements is
block or inline. A block element is often called a block-level element. An inline element is
always just called an inline element.
A block-level element always starts on a new line and takes up the full width
available (stretches out to the left and right as far as it can).
</div>
Page 43
CSS Syntax
Display: value;
Note: The problem with the <div> above occurs when the browser window is smaller than the
width of the element. The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small
windows. This is important when making a site usable on small devices:
Tip: Resize the browser window to less than 500px wide, to see
the difference between the two divs!
Relative positioning changes the position of the HTML element relative to where it normally
appears. So "left:20" adds 20 pixels to the element's LEFT position. You can use two values top
and left along with the position property to move an HTML element anywhere in an HTML
document.
NOTE: You can use the bottom or right values as well in the same way as top and
left.
Page 45
Here is an example:
An element with position: absolute is positioned at the specified coordinates relative to your
screen top-left corner. You can use two values top and left along with the position property to
move an HTML element anywhere in HTML document.
NOTE: You can use bottom or right values as well in the same way as top and
left.
Here is an example:
<div style="position: absolute; left: 80px; top: 20px; background-color:
yellow ;">
Page 46
This div has absolute positioning.
</div>
4.4.3 Fixed Positioning
Fixed positioning allows you to fix the position of an element to a particular spot on the page,
regardless of scrolling. Specified coordinates will be relative to the browser window. You can
use two values top and left along with the position property to move an HTML element anywhere
in the HTML document.
NOTE: You can use bottom or right values as well in the same way as top and
left.
Here is an example:
With CSS float, an element can be pushed to the left or right, allowing other elements to wrap
around it. Float is very often used for images, but it is also useful when working with layouts.
A line box is next to a float when there exists a vertical position that satisfies all of these four
conditions:
(a) At or below the top of the line box,
(b) At or above the bottom of the line box
(c) Below the top margin edge of the float, and
(d) Above the bottom margin edge of the float.
Elements are floated horizontally; this means that an element can only be floated left or
right, not up or down. A floated element will move as far to the left or right as it can. Usually this
means all the way to the left or right of the containing element. The elements after the floating
element will flow around it. The elements before the floating element will not be affected. If an
image is floated to the right, a following text flows around it, to the left:
Page 47
Example:
img { float:right; }
If you place several floating elements after each other, they will float next to each other if there
is room. Here we have made an image gallery using the float property:
Example
Elements after the floating element will flow around it. To avoid this, use the clear
property. The clear property specifies which sides of an element other floating elements
are not allowed. Add a text line into the image gallery, using the clear property:
Example:
.text_line { clear:both; }
The number in the "CSS" column indicates in which CSS version the
property is defined (CSS1 or CSS2).
Page 48
Page 49
4.6 Inline Block Properties
It has been possible for a long time to create a grid of boxes that fills the browser width
and wraps nicely (when the browser is resized), by using the float property. However, the
inline-block value of the display property makes this even easier. Inline-block elements are like
inline elements but they can have a width and a height.
Examples
The old way - using float (notice that we also need to specify a clear property for
the element after the floating boxes):
<!DOCTYPE html>
<html>
<head>
<style>
.floating-box
{
float: left;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
.after-box
{
clear: left;
border: 3px solid red;
}
</style>
</head>
<body>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="after-box">Another box, after the floating boxes...</div>
</body>
</html>
The above program will produce the following result
Page 50
4.7 Horizontal Alignment in CSS
To horizontally center a block element (like <div>), use margin: auto; Setting the width
of the element will prevent it from stretching out to the edges of its container. The element will
then take up the specified width, and the remaining space will be split equally between the two
margins:
Example:
.center
{
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Note: Center aligning has no effect if the width property is not set (or set to 100%).
Example:
Page 51
.center
{
text-align: center;
border: 3px solid green;
}
4.7.3 Center an Image
To center an image, use margin: auto; and make it into a block element:
img
{
display: block;
margin: auto;
width: 40%;
}
Example:
.right {
position:
absolute; right:
0px;
width: 300px;
border: 3px solid
#73AD21; padding: 10px;
}
Note: Absolute positioned elements are removed from the normal flow,
and can overlap elements.
Tip: When aligning elements with position, always define margin and padding for the <body>
element. This is to avoid visual differences in different browsers. There is also a problem with
IE8 and earlier, when using position. If a container element (in our case <div
class="container">) has a specified width, and the
!DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin
on the right side. This seems to be space reserved for a scrollbar. So, always set the
!DOCTYPE declaration when using position:
<!DOCTYPE html>
<html>
<head>
Page 52
<style>
body
{
margin: 0;
padding: 0;
}
.container
{
position: relative;
width: 100%;
}
.right
{
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
}
</style>
</head>
<body>
<h2>Right Align</h2>
<div class="container">
<div class="right">
<p><b>Note: </b>When aligning using the position property,
always include the !DOCTYPE declaration! If missing, it can
produce strange results in IE browsers.</p>
</div>
</div>
</body>
</html>
Page 53
4.8 Working with Combinatory
A combinatory is something that explains the relationship between the selectors. A CSS selector
can contain more than one simple selector. Between the simple selectors, we can include a
combinatory.
There are four different combinatory in CSS:
➢ descendant selector (space)
➢ child selector (>)
➢ adjacent sibling selector (+)
➢ general sibling selector (~)
4.8.1 Descendant Selector
The descendant selector matches all elements that are descendants of a specified element.
The following example selects all <p> elements inside <div> elements:
Example
div p
{
background-color: yellow;
}
Page 54
4.8.2 Child Selector
The child selector selects all elements that are the immediate children of a specified
element.
The following example selects all <p> elements that are immediate
children of a <div> element:
Example
<div > p {
background-color: yellow;
}
4.8.3 Adjacent Sibling Selector
The adjacent sibling selector selects all elements that are the adjacent siblings of a
specified element. Sibling elements must have the same parent element, and "adjacent"
means "immediately following". The following example selects all <p> elements that are
placed immediately after <div> elements:
Example:
div + p {
background-color: yellow;
}
Syntax
The syntax of pseudo-classes:
selector: pseudo-class
Page 55
{
property: value;
}
4.9.1 Anchor Pseudo-classes
Links can be displayed in different ways:
Example
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* selected link */
a:active {
color: #0000FF;
}
Note: a: hover MUST come after a: link and a:visited in the CSS definition in order to
be effective!a:active MUST come after a:hover in the CSS definition in order to be
effective! Pseudo-class names are not case- sensitive.
Page 56
p
{
display: none;
background-color: yellow;
padding: 20px;
}
div:hover p
{
display: block;
}
4.9.4 The: first-child Pseudo-class
The: first-child pseudo-class matches a specified element that is the first child of another
element.
4.9.5 Match the first <p> element
In the following example, the selector matches any <p> element that is the first child of
any element:
Example
p: first-child
{
color: blue;
}
Example
p i:first-child
{
color: blue;
}
Page 57
:empty p:empty Selects every <p> element
that has no children
Page 58
p:nth-of- Selects every <p> element
:nth-of-type(n) type(2) that is the second <p>
element of its parent
"required" attribute
Value Description
:first-line Use this element to add special styles to the first line of the text in a
selector.
:first- Use this element to add special style to the first letter of the text in a
letter selector.
:before Use this element to insert some content before an element.
Page 60
4.10.1 The: first-line pseudo-element
The following example demonstrates how to use the: first-line element to add special effects to
the first line of elements in a document.
The following example demonstrates how to use the: first-letter element to add special effect
to the first letter of elements in the document.
Page 61
The above program will produce the following result:
Page 62
4.10.3 The: before pseudo-element
The : before pseudo-element can be used to insert some content before the content of an
element. The following example demonstrates how to use :before element to add some
content before any element.
<style type="text/css">
p:before
{
content: url(/https/www.scribd.com/images/bullet.gif)
}
</style>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
The :after pseudo-element can be used to insert some content after the content of an
element. The following example demonstrates how to use: after element to add some
content after any element.
<style type="text/css">
p:after
{
content: url(/https/www.scribd.com/images/bullet.gif)
}
</style>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
The: selection pseudo-element matches the portion of an element that is selected by a user. The
following CSS properties can be applied to:
selection: color, background, cursor, and outline. The following example makes the selected text
red on a yellow background: Here is an example:
<!DOCTYPE html>
<html>
<head>
<style>
::-moz-selection { /* Code for Firefox */
color: red;
Page 63
background: yellow;
}
::selection {
color: red;
background: yellow;
}
</style>
</head>
<body>
<h1>Select some text on this page:</h1>
<p>This is a paragraph.</p>
<div>This is some text in a div element.</div>
<p><strong>Note:</strong> ::selection is not supported in Internet
Explorer 8 and earlier versions.</p>
<p><strong>Note:</strong> Firefox supports an alternative, the ::-
moz- selection property.</p>
</body>
</html>
The above program will produce the following result:
Page 64
4.11 Creating a navigation bar
A navigation bar (or navigation system) is a section of a graphical user interface intended
to aid visitors in accessing information. Navigation bars are implemented in file browsers, web
browsers and as a design element of some web sites. A set of buttons or images in a row or column
that serves as a control point to link the user to sections on a Web site. The navigation bar may
also be a single graphic image with multiple selections.
A navigation bar needs standard HTML as a base. In our examples we will build the navigation
bar from a standard HTML list. A navigation bar is basically a list of links, so using the <ul> and
<li> elements makes perfect sense: Example
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
Now let's remove the bullets and the margins and padding from the list:
ul
{
list-style-type:none;
margin:0;
padding:0;
}
Page 65
Example explained:
➢ list-style-type:none - Removes the bullets. A navigation bar does not need list markers
➢ Setting margins and padding to 0 to remove browser default settings
The code in the example above is the standard code used in both vertical, and horizontal
navigation bars.
4.11.2 Vertical Navigation Bar
To build a vertical navigation bar we only need to style the <a> elements, in
addition to the code above:
Example
a { display:block; width:60px; }
Example explained:
➢ display:block - Displaying the links as block elements makes the whole
link area clickable (not just the text), and it allows us to specify the width
➢ width:60px - Block elements take up the full width available by default.
We want to specify a 60 px width .
Note: Always specify the width for <a> elements in a vertical navigation bar. If you omit the
width, IE6 can produce unexpected results. You can also set the width of <ul>, and remove the
width of <a>, as they will take up the full width available when displayed as block elements. This
will produce the same result as our previous example:
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}
Add text-align:center to <li> or <a> to center the links. Add the border property to <ul> add a
border around the navbar. If you also want borders inside the navbar, add aborder-bottom to all
<li> elements, except for the last one:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
Page 66
padding: 0;
width: 200px;
background-color: #f1f1f1;
border: 1px solid #555;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li {
text-align: center;
border-bottom: 1px solid #555;
}
li:last-child {
border-bottom: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Page 67
The above program will produce the following result
There are two ways to create a horizontal navigation bar. Using inline or floating list items.
Both methods work fine, but if you want the links to be the same size, you have to use the floating
method. Create a basic horizontal navigation bar with a dark background color
and change the background color of the links when the user moves the mouse
over them:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
Page 68
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Page 69
4.12 Working with images
Images play an important role in any webpage. Though it is not recommended to include a lot of
images, but it is still important to use good images wherever required. CSS plays a good role to
control image display. You can set the following image properties using CSS.
The border property of an image is used to set the width of an image border. This property can
have a value in length or in %. A width of zero pixels means no border.
Here is an example:
Page 70
The above program will produce the following result:
The width property of an image is used to set the width of an image. This property can have a
value in length or in %. While giving value in %, it applies it in respect of the box in which an
image is available.
Here is an example:
Page 71
The above program will produce the following result:
Page 72
4.13 Working with attribute selectors
You can also apply styles to HTML elements with particular attributes. The style rule below will
match all the input elements having a type attribute with a value of text:
input[type="text"]
{
color: #000000;
}
The advantage to this method is that the <input type="submit" /> element is unaffected, and
the color applied only to the desired text fields.
There are following rules applied to attribute selector.
The [attribute] selector is used to select elements with a specified attribute. The following
example selects all <a> elements with a target attribute:
Example
a[target] {
background-color: yellow;
}
Page 73
4.13.3 CSS [attribute="value"] Selector
a[target="_blank"] {
background-color: yellow;
}
4.13.4 CSS [attribute~="value"] Selector
The [attribute~="value"] selector is used to select elements with an attribute value containing a
specified word. The following example selects all elements with a title attribute that contains a
space-separated list of words, one of which is "flower":
Example
[title~="flower"] {
border: 5px solid yellow;
}
The example above will match elements with title="flower", title="summer
flower", and title="flower new", but not title="my-flower" or title="flowers".
4.13.5 CSS [attribute|="value"] Selector
The [attribute|="value"] selector is used to select elements with the specified attribute starting
with the specified value. The following example selects all elements with a class attribute value
that begins with "top": Note: The value has to be a whole word, either alone, like class="top", or
followed by a hyphen( - ), like class="top-text"!
Example
[class|="top"] {
background: yellow;
}
The [attribute^="value"] selector is used to select elements whose attribute value begins with a
specified value.
The following example selects all elements with a class attribute value that begins with "top":
Note: The value does not have to be a whole word!
Example
[class^="top"] {
background: yellow;
}
Page 74
4.13.7 CSS [attribute$="value"] Selector
The [attribute$="value"] selector is used to select elements whose attribute value ends with a
specified value.
The following example selects all elements with a class attribute value that ends with "test":
Note: The value does not have to be a whole word!
Example
[class$="test"] {
background: yellow;
}
The [attribute*="value"] selector is used to select elements whose attribute value contains a
specified value. The following example selects all elements with a class attribute value that
contains "te":
Note: The value does not have to be a whole word!
Example:
[class*="te"] {
background: yellow;
}
Page 75