CSS – Beginners
CSS is used to control the style of a web document
in a simple and easy way.
CSS stands for Cascading Style Sheet.
This tutorial gives complete understanding on CSS.
Before your
begin:
Before you begin, it's important that you know Windows or Unix.
A working knowledge of Windows or Unix makes it much easier to learn HTML.
You should be familiar with:
·
Basic word processing using any text editor.
·
How to create directories and files.
·
How to navigate through different directories.
·
Basic understanding on internet browsing using a browser like
Internet Explorer or Firefox etc.
·
Basic understanding on developing simple Web Pages using HTML or
XHTML.
If you are new to HTML
and XHTML then I would suggest you to go through our HTML
Tutorial or XHTML
Tutorial. Anyone of HTML or XHTML is enough to proceed.
What is CSS?
Cascading Style Sheets, fondly referred to as CSS, 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 is easy to learn and understand but it provides powerful
control over the presentation of an HTML document. Most commonly, CSS is
combined with the markup languages HTML or XHTML.
Advantages
of CSS:
·
CSS saves time - You can write CSS once and then reuse same sheet in multiple
HTML pages. You can define a style for each HTML element and apply it to as
many Web pages as you want.
·
Pages load faster - If you are using CSS, you do not need to write HTML tag
attributes every time. Just write one CSS rule of a tag and apply to all the
occurrences of that tag. So less code means faster download times.
·
Easy maintenance - To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
·
Superior styles to
HTML - CSS has a much wider array of attributes
than HTML so you can give far better look to your HTML page in comparison of
HTML attributes.
·
Multiple Device
Compatibility - Style sheets allow
content to be optimized for more than one type of device. By using the same
HTML document, different versions of a website can be presented for handheld
devices such as PDAs and cell phones or for printing.
·
Global web
standards - Now HTML attributes
are being deprecated and it is being recommended to use CSS. So its a good idea to start using CSS in all the HTML
pages to make them compatible to future browsers.
Who Creates
and Maintains CSS?
CSS is created and maintained through a group of people within
the W3C called the CSS Working Group. The CSS Working Group creates documents
called specifications. When a specification has been discussed and officially
ratified by W3C members, it becomes a recommendation.
These ratified specifications are called recommendations because
the W3C has no control over the actual implementation of the language.
Independent companies and organizations create that software.
NOTE: The World Wide Web Consortium, or W3C is a group that makes
recommendations about how the Internet works and how it should evolve.
CSS
Versions:
Cascading Style Sheets, level 1 (CSS1) was came out of W3C as a
recommendation in December 1996. This version describes the CSS language as
well as a simple visual formatting model for all the HTML tags.
CSS2 was became a W3C recommendation in May 1998 and builds on
CSS1. This version adds support for media-specific style sheets e.g. printers
and aural devices, downloadable fonts, element positioning and tables.
css -
syntax
A CSS comprises of style rules
that are interpreted by the browser and then applied to the corresponding
elements in your document. A style rule is made of three parts:
·
Selector: A selector is an HTML tag at which style
will be applied. This could be any tag like <h1> or <table> etc.
·
Property: A property is a type of attribute of HTML
tag. Put simply, all the HTML attributes are converted into CSS properties.
They could becolor or border etc.
·
Value: Values are assigned to properties. For
example color property can have value either red or #F1F1F1 etc.
You can put CSS Style Rule
Syntax as follows:
selector { property: value }
|
Example: You can define a table border as follows:
table{ border :1px solid #C00; }
|
Here
table is a selector and border is a property and given value 1px solid #C00 is the value of that property.
You can define selectors in
various simple ways based on your comfort. Let me put these selectors one by
one.
The Type
Selectors:
This is the same selector we
have seen above. Again one more example to give a color to all level 1 headings
:
h1 {
color: #36CFFF;
}
|
The
Universal Selectors:
Rather than selecting elements
of a specific type, the universal selector quite simply matches the name of any
element type :
* {
color: #000000;
}
|
This rule renders the content
of every element in our document in black.
The
Descendant Selectors:
Suppose you want to apply a
style rule to a particular element only when it lies inside a particular
element. As given in the following example, style rule will apply to <em>
element only when it lies inside <ul> tag.
ul em {
color: #000000;
}
|
The Class
Selectors:
You can define style rules
based on the class attribute of the elements. All the elements having that
class will be formatted according to the defined rule.
.black {
color: #000000;
}
|
This
rule renders the content in black for every element with class attribute set to black in our document. You can make it a bit
more particular. For example:
h1.black {
color: #000000;
}
|
This
rule renders the content in black for only <h1> elements with class
attribute set to black.
You can apply more than one
class selectors to given element. Consider the following example :
<p class="center bold">
This para will be styled by the classes center and bold.
</p>
|
The ID
Selectors:
You can define style rules
based on the id attribute of the elements. All the elements having that id will
be formatted according to the defined rule.
#black {
color: #000000;
}
|
This
rule renders the content in black for every element with id attribute set to black in our document. You can make it a bit
more particular. For example:
h1#black {
color: #000000;
}
|
This
rule renders the content in black for only <h1> elements with id
attribute set to black.
The true power of id selectors
is when they are used as the foundation for descendant selectors, For example:
#black h2 {
color: #000000;
}
|
In this
example all level 2 headings will be displayed in black color only when those
headings will lie with in tags having id attribute set toblack.
The Child
Selectors:
You have seen descendant
selectors. There is one more type of selectors which is very similar to
descendants but have different functionality. Consider the following example:
body > p {
color: #000000;
}
|
This rule will render all the
paragraphs in black if they are direct child of <body> element. Other
paragraphs put inside other elements like <div> or <td> etc. would
not have any effect of this rule.
The
Attribute Selectors:
You can
also apply styles to HTML elements with particular attributes. The style rule
below will match all input elements that has 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-".
Multiple
Style Rules:
You may need to define multiple
style rules for a single element. You can define these rules to combine multiple
properties and corresponding values into a single block as defined in the
following example:
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
|
Here
all the property and value pairs are separated by a semi colon (;). You can keep
them in a ingle line or multiple lines. For better readability we keep them
into separate lines.
For a
while don't bother about the properties mentioned in the above block. These
properties will be explained in coming chapters and you can find complete
detail about properties in CSS
References.
Grouping
Selectors:
You can apply a style to many
selectors if you like. Just separate the selectors with a comma as given in the
following example:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
|
This define style rule will be
applicable to h1, h2 and h3 element as well. The order of the list is irrelevant.
All the elements in the selector will have the corresponding declarations
applied to them.
You can
combine various class selectors together as shown below:
#content, #footer, #supplement {
position: absolute;
left: 510px;
width: 200px;
}
|
CSS - Inclusion
There are four ways to
associate styles with your HTML document. Most commonly used methods are inline
CSS and External CSS.
Embedded CSS
- The <style> Element:
You can put your CSS rules into
an HTML document using the <style> element. This tag is placed inside
<head>...</head> tags. Rules defined using this syntax will be
applied to all the elements available in the document. Here is the generic
syntax:
<head>
<style type="text/css" media="...">
Style Rules
............
</style>
</head>
|
Attributes:
Attributes associated with
<style> elements are:
Attribute
|
Value
|
Description
|
type
|
text/css
|
Specifies
the style sheet language as a content-type (MIME type). This is required
attribute.
|
media
|
screen
tty tv projection handheld braille aural all |
Specifies
the device the document will be displayed on. Default value is all. This is optional
attribute.
|
Example:
Following is the example of
embed CSS based on above syntax:
<head>
<style type="text/css" media="all">
h1{
color: #36C;
}
</style>
</head>
|
Inline CSS - The style Attribute:
You can
use style attribute of any HTML element to
define style rules. These rules will be applied to that element only. Here is
the generic syntax:
<element style="...style rules....">
|
Attributes:
Attribute
|
Value
|
Description
|
style
|
style
rules
|
The
value of style attribute is a combination of style
declarations separated by semicolon (;).
|
Example:
Following is the example of
inline CSS based on above syntax:
<h1 style ="color:#36C;"> This is inline CSS </h1>
|
This will produce following
result:
This is inline CSS
|
External CSS
- The <link> Element:
The <link> element can be
used to include an external stylesheet file in your HTML document.
An
external style sheet is a separate text file with .css extension. You define all the Style
rules within this text file and then you can include this file in any HTML
document using <link> element.
Here is the generic syntax of
including external CSS file:
<head>
<link type="text/css" href="..." media="..." />
</head>
|
Attributes:
Attributes associated with
<style> elements are:
Attribute
|
Value
|
Description
|
type
|
text/css
|
Specifies
the style sheet language as a content-type (MIME type). This attribute is
required.
|
href
|
URL
|
Specifies
the style sheet file having Style rules. This attribute is a required.
|
media
|
screen
tty tv projection handheld braille aural all |
Specifies
the device the document will be displayed on. Default value is all. This is optional
attribute.
|
Example:
Consider
a simple style sheet file with a name mystyle.css having the following rules:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
|
Now you
can include this file mystyle.css in any HTML document as follows:
<head>
<link type="text/css" href="mystyle.css" media="all" />
</head>
|
Imported CSS
- @import Rule:
@import is used to import an
external stylesheet in a manner similar to the <link> element. Here is
the generic syntax of @import rule.
<head>
<@import "URL";
</head>
|
Here URL is the URL of the
style sheet file having style rules. You can use another syntax as well:
<head>
<@import url("URL");
</head>
|
Example:
Following is the example
showing you how to import a style sheet file into HTML document:
<head>
@import "mystyle.css";
</head>
|
CSS Rules
Overriding:
We have discussed four ways to
include style sheet rules in a an HTML document. Here is the rule to override
any Style Sheet Rule.
·
Any inline style sheet takes highest priority. So it will
override any rule defined in <style>...</style> tags or rules
defined in any external style sheet file.
·
Any rule defined in <style>...</style> tags will
override rules defined in any external style sheet file.
·
Any rule defined in external style sheet file takes lowest
priority and rules defined in this file will be applied only when above two
rules are not applicable.
Handling old
Browsers:
There are still many old
browsers who do not support CSS. So we should take care while writing our
Embedded CSS in an HTML document. The following snippet shows how you can use
comment tags to hide CSS from older browsers:
<style type="text/css">
<!--
body, td {
color: blue;
}
-->
</style>
|
CSS
Comments:
Many times you may need to put
additional comments in your style sheet blocks. So it is very easy to comment
any part in style sheet. You simple put your comments inside /*.....this is a
comment in style sheet.....*/.
You can use /* ....*/ to
comment multi-line blocks in similar way you do in C and C++ programming
languages.
Example:
/* This is an external style sheet file */
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
/* end of style rules. */
|
css_units
Before
we start actual exercise, I would like to give a brief idea about the CSS Measurement Units.
CSS
supports a number of measurements including absolute units such as inches,
centimeters, points, and so on, as well as relative measures such as percentages
and em units. You need these values while specifying various measurements in
your Style rules e.gborder="1px solid red".
We have listed out all the CSS
Measurement Units alogwith proper Examples:
Unit
|
Description
|
Example
|
%
|
Defines
a measurement as a percentage relative to another value, typically an
enclosing element.
|
p
{font-size: 16pt; line-height: 125%;}
|
cm
|
Defines
a measurement in centimeters.
|
div
{margin-bottom: 2cm;}
|
em
|
A
relative measurement for the height of a font in em spaces. Because an em
unit is equivalent to the size of a given font, if you assign a font to 12pt,
each "em" unit would be 12pt; thus, 2em would be 24pt.
|
p
{letter-spacing: 7em;}
|
ex
|
This
value defines a measurement relative to a font's x-height. The x-height is
determined by the height of the font's lowercase letter x.
|
p
{font-size: 24pt; line-height: 3ex;}
|
in
|
Defines
a measurement in inches.
|
p
{word-spacing: .15in;}
|
mm
|
Defines
a measurement in millimeters.
|
p
{word-spacing: 15mm;}
|
pc
|
Defines
a measurement in picas. A pica is equivalent to 12 points; thus, there are 6
picas per inch.
|
p
{font-size: 20pc;}
|
pt
|
Defines
a measurement in points. A point is defined as 1/72nd of an inch.
|
body
{font-size: 18pt;}
|
px
|
Defines
a measurement in screen pixels.
|
css -
colors
CSS uses color values to
specify a color. Typically, these are used to set a color either for the
foreground of an element(i.e., its text) or else for the background of the
element. They can also be used to affect the color of borders and other
decorative effects.
You can specify your color
values in various formats. Following table tells you all possible formats:
Format
|
Syntax
|
Example
|
Hex
Code
|
#RRGGBB
|
p{color:#FF0000;}
|
Short
Hex Code
|
#RGB
|
p{color:#6A7;}
|
RGB
%
|
rgb(rrr%,ggg%,bbb%)
|
p{color:rgb(50%,50%,50%);}
|
RGB
Absolute
|
rgb(rrr,ggg,bbb)
|
p{color:rgb(0,0,255);}
|
keyword
|
aqua,
black, etc.
|
p{color:teal;}
|
These formats are explained in
more detail in the following sections:
CSS Colors -
Hex Codes:
A hexadecimal is a 6 digit
representation of a color. The first two digits(RR) represent a red value, the
next two are a green value(GG), and the last are the blue value(BB).
A hexadecimal value can be
taken from any graphics software like Adobe Photoshop, Jasc Paintshop Pro or
even using Advanced Paint Brush.
Each hexadecimal code will be
preceded by a pound or hash sign #. Following are the examples to use
Hexadecimal notation.
Color
|
Color HEX
|
|
#000000
|
|
#FF0000
|
|
#00FF00
|
|
#0000FF
|
|
#FFFF00
|
|
#00FFFF
|
|
#FF00FF
|
|
#C0C0C0
|
|
#FFFFFF
|
CSS Colors -
Short Hex Codes:
This is a shorter form of the
six-digit notation. In this format, each digit is replicated to arrive at an
equivalent six-digit value; For example: #6A7 becomes #66AA77.
A hexadecimal value can be
taken from any graphics software like Adobe Photoshop, Jasc Paintshop Pro or
even using Advanced Paint Brush.
css
backgrounds
This tutorial will
teach you how to set backgrounds of various HTML elements. You can set
following background properties of an element:
·
The background-color property is used to set the background
color of an element.
·
The background-image property is used to set the
background image of an element.
·
The background-repeat property is used to control the
repetition of an image in the background.
·
The background-position property is used to control the
position of an image in the background.
·
The background-attachment property is used to control
the scrolling of an image in the background.
·
The background property is used as shorthand to specify
a number of other background properties.
Set the background color:
Following is the
example which demonstrates how to set the background color for an element.
<p style="background-color:yellow;">
This text has a yellow background
color.
</p>
|
This will produce
following result:
This text has a
yellow background color.
|
Set the background image:
Following is the
example which demonstrates how to set the background image for an element.
<table style="background-image:url(images/pattern1.gif);">
<tr><td>
This table has background image
set.
</td></tr>
</table>
|
Repeat the background image:
Following is the example which demonstrates how to repeat the background
image if image is small. You can use no-repeat value forbackground-repeat property
if you don't want to repeat an image, in this case image will display only
once.
By default background-repeat property will have repeat value.
<table style="background-image:url(images/pattern1.gif);
background-repeat:
repeat;">
<tr><td>
This table has background image
which repeats multiple times.
</td></tr>
</table>
|
Following is the
example which demonstrates how to repeat the background image vertically.
<table style="background-image:url(images/pattern1.gif);
background-repeat:
repeat-y;">
<tr><td>
This table has background image set
which will repeat vertically.
</td></tr>
</table>
|
Following is the
example which demonstrates how to repeat the background image horizontally.
<table style="background-image:url(images/pattern1.gif);
background-repeat:
repeat-x;">
<tr><td>
This table has background image set
which will repeat horizontally.
</td></tr>
</table>
|
Set the background image position:
Following is the
example which demonstrates how to set the background image position 100 pixels
away from the left side.
<table style="background-image:url(images/pattern1.gif);
background-position:100px;">
<tr><td>
Background image positioned 100
pixels away from the left.
</td></tr>
</table>
|
Following is the
example which demonstrates how to set the background image position 100 pixels
away from the left side and 200 pixels down from the top.
<table style="background-image:url(images/pattern1.gif);
background-position:100px
200px;">
<tr><td>
This table has background image
positioned 100
pixels away from the left and 200
pixels from the top.
</td></tr>
</table>
|
Set the background attachment:
Background
attachment determines whether a background image is fixed or scrolls with the
rest of the page.
Following is the
example which demonstrates how to set the fixed background image.
<p style="background-image:url(images/pattern1.gif);
background-attachment:fixed;">
This parapgraph has fixed
background image.
</p>
|
Following is the
example which demonstrates how to set the scrolling background image.
<p style="background-image:url(images/pattern1.gif);
background-attachment:scroll;">
This parapgraph has scrolling
background image.
</p>
|
Shorthand property :
You can use the background property to set all the
background properties at once. For example:
<p style="background:url(images/pattern1.gif)
repeat fixed;">
This parapgraph has fixed repeated
background image.
</p>
|
css -
fonts
This tutorial will teach you
how to set fonts of a content available in an HTML element. You can set following
font properties of an element:
·
The font-family property is used to change the face of
a font.
·
The font-style property is used to make a font italic
or oblique.
·
The font-variant property is used to create a
small-caps effect.
·
The font-weight property is used to increase or decrease how bold or light a font
appears.
·
The font-size property is used to increase or
decrease the size of a font.
·
The font property is used as shorthand to
specify a number of other font properties.
Set the font
family:
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>
|
This will produce following
result:
This text is rendered in either georgia, garamond, or
the default
serif font depending on which font you have at your system. |
Set the font
style:
Following
is the example which demonstrates how to set the font style of an element.
Possible values are normal,
italic and oblique.
<p style="font-style:italic;">
This text will be rendered in italic style
</p>
|
This will produce following
result:
This text will be rendered in italic style
|
Set the font
variant:
Following
is the example which demonstrates how to set the font variant of an element.
Possible values are normal and
small-caps.
<p style="font-variant:small-caps;">
This text will be rendered as small caps
</p>
|
This will produce following
result:
This text will be renedered as small
caps
|
Set the font
weight:
Following
is the example which demonstrates how to set the font weight of an element. The
font-weight property provides the functionality to specify how bold a font is.
Possible values could be normal,
bold, bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900.
<p style="font-weight:bold;">
This font is bold.
</p>
<p style="font-weight:bolder;">
This font is bolder.
</p>
<p style="font-weight:900;">
This font is 900 weight.
</p>
|
This will produce following
result:
This font is bold.
This font is bolder.
This font is 900 weight.
|
Set the font
size:
Following
is the example which demonstrates how to set the font size of an element. The
font-size property is used to control the size of fonts. Possible values could
be xx-small, x-small, small,
medium, large, x-large, xx-large, smaller, larger, size in pixels or in %
<p style="font-size:20px;">
This font size is 20 pixels
</p>
<p style="font-size:small;">
This font size is small
</p>
<p style="font-size:large;">
This font size is large
</p>
|
This will produce following
result:
This font size is 20 pixels
This font size is small
This font size is large
|
Set the font
size adjust:
Following is the example which
demonstrates how to set the font size adjust of an element. This property
enables you to adjust the x-height to make fonts more legible. Possible value
could be any number.
<p style="font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
|
This will produce following
result:
This text is using a font-size-adjust value.
|
Set the font
stretch:
Following is the example which
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>
|
This will produce following
result:
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.
|
Shorthand
property :
You can
use the font property to set all the font
properties at once. For example:
<p style="font:italic small-caps bold 15px georgia;">
Applying all the properties on the text at once.
</p>
|
This will produce following
result:
Applying
all the properties on the text at once.
|
css –
text
This tutorial will
teach you how to manipulate text using CSS properties. You can set following
text properties of an element:
·
The color property is used to set the color of a text.
·
The direction property is used to set the text
direction.
·
The letter-spacing property is used to add or subtract
space between the letters that make up a word.
·
The word-spacing property is used to add or subtract
space between the words of a sentence.
·
The text-indent property is used to indent the text of
a paragraph.
·
The text-align property is used to align the text of a
document.
·
The text-decoration property is used to underline,
overline, and strikethrough text.
·
The text-transform property is used to capitalize text
or convert text to uppercase or lowercase letters.
·
The white-space property is used to control the flow
and formatting of text.
·
The text-shadow property is used to set the text shadow
around a text.
Set the text color:
Following is the
example which demonstrates how to set the text color. Possible value could be
any color name in any valid format.
<p style="color:red;">
This text will be written in red.
</p>
|
This will produce
following result:
This text will be written in red.
|
Set the text direction :
Following is the example which demonstrates how to set the direction of
a text. Possible values are ltr or rtl.
<p style="direction:rtl;">
This text will be renedered from
right to left
</p>
|
This will produce
following result:
This text will be renedered from
right to left
|
Set the space between characters:
Following is the example which demonstrates how to set the space between
characters. Possible values are normal or a number specifying space..
<p style="letter-spacing:5px;">
This text is having space between
letters.
</p>
|
This will produce
following result:
This text is
having space between letters.
|
Set the space between words:
Following is the example which demonstrates how to set the space between
words. Possible values are normal or a number specifying space..
<p style="word-spacing:5px;">
This text is having space between
words.
</p>
|
This will produce
following result:
This text is having space between
words.
|
Set the text indent:
Following is the example which demonstrates how to indent the first line
of a paragraph. Possible values are % or a number specifying indent
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>
|
This will produce
following result:
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. |
Set the text alignment:
Following is the example which demonstrates how to align a text.
Possible values are left, right, center, justify..
<p style="text-align:right;">
This will be right aligned.
</p>
<p style="text-align:center;">
This will be center aligned.
</p>
<p style="text-align:left;">
This will be left aligned.
</p>
|
This will produce
following result:
This will be
right aligned.
This will be
center aligned.
This will be left aligned.
|
Decorating the text:
Following is the example which demonstrates how to decorate a text.
Possible values are none, underline, overline, line-through, blink..
<p style="text-decoration:underline;">
This will be underlined
</p>
<p style="text-decoration:line-through;">
This will be striked through.
</p>
<p style="text-decoration:overline;">
This will have a over line.
</p>
<p style="text-decoration:blink;">
This text will have blinking effect
</p>
|
This will produce
following result:
This will be underlined
This will have a over line.
This
text will have blinking effect
|
Set the text cases:
Following is the example which demonstrates how to set the cases for a
text. Possible values are none, capitalize, uppercase, lowercase..
<p style="text-transform:capitalize;">
This will be capitalized
</p>
<p style="text-transform:uppercase;">
This will be in uppercase
</p>
<p style="text-transform:lowercase;">
This will be in lowercase
</p>
|
This will produce
following result:
This Will Be Capitalized
THIS WILL
BE IN UPPERCASE
this will be in lowercase
|
Set the white space between text:
Following is the example which demonstrates how white space inside an
element is handled. Possible values are normal, pre, nowrap.
<p style="white-space:pre;">This
text has a line break
and the white-space pre setting
tells the browser to honor it
just like the HTML pre
tag.</p>
|
This will produce
following result:
This text has a line break
and the white-space pre setting
tells the browser to honor it
just like the HTML pre tag.
|
Set the text shadow:
Following is the
example which demonstrates how to set the shadow around a text. This may not be
supported by all the browsers.
<p style="text-shadow:4px
4px 8px blue;">
If your browser supports the CSS
text-shadow property,
this text will have a blue shadow.</p>
|
This will produce
following result:
If your browser supports the CSS
text-shadow property, this text will have a blue shadow.
|
CSS –
images
Images are very
important part of any Web Page. Though it is not recommended to include lot of
images but it is still important to use good images wherever it is required.
CSS plays a good
role to control image display. You can set following image properties using
CSS.
·
The border property is used to set the width of an
image border.
·
The height property is used to set the height of an
image.
·
The width property is used to set the width of an
image.
·
The -moz-opacity property is used to set the opacity of
an image.
The image border Property:
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 the
example:
<img
style="border:0px;" src="images/logo.gif" />
<br />
<img style="border:3px dashed
red;" src="images/logo.gif" />
|
This will produce
following result:
|
The image height Property:
The height property of an image is used to set the
height 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 the
example:
<img style="border:1px
solid red; height:100px;"
src="images/logo.gif" />
<br />
<img style="border:1px
solid red; height:50%;"
src="images/logo.gif" />
|
This will produce
following result:
|
The image width Property:
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 the
example:
<img style="border:1px
solid red; width:100px;"
src="images/logo.gif" />
<br />
<img style="border:1px
solid red; width:100%;"
src="images/logo.gif" />
|
This will produce
following result:
|
The -moz-opacity Property:
The -moz-opacity property of an image is used to set
the opacity of an image. This property is used to create a transparent image in
Mozilla. IE uses filter:alpha(opacity=x) to create transparent
images.
In Mozilla (-moz-opacity:x)
x can be a value from 0.0 - 1.0. A lower value makes the element more
transparent (The same things goes for the CSS3-valid syntax opacity:x).
In IE
(filter:alpha(opacity=x)) x can be a value from 0 - 100. A lower value makes
the element more transparent.
Here is the
example:
<img
style="border:1px solid
red;-moz-opacity:0.4;filter:alpha(opacity=40);"
src="images/logo.gif" />
|
This will produce
following result:
|
CSS –
LINK
This tutorial will teach you
how to set different properties of a hyper link using CSS. You can set
following properties of a hyper link:
We will revisit same properties
when we will discuss Pseudo-Classes of CSS.
·
The :link Signifies unvisited hyperlinks.
·
The :visited Signifies visited hyperlinks.
·
The :hover Signifies an element that currently
has the user's mouse pointer hovering over it.
·
The :active Signifies an element on which the user
is currently clicking.
Usually these all properties
are kept in the header part of 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>
|
Now we will see how to use
these properties to give different effects to hyperlinks.
Set the
color of Links:
Following is the example which
demonstrates how to set the link color. Possible value could be any color name
in any valid format.
<style type="text/css">
a:link {color:#000000}
</style>
<a href="index.html">Black Link</a>
|
This will produce following
black link:
Set the
color of Visited Links:
Following is the example which
demonstrates how to set the color of visited links. Possible value could be any
color name in any valid format.
<style type="text/css">
a:visited {color: #006600}
</style>
<a href="index.html">Click this link</a>
|
This will produce following
link. Once you will click this link, it will change its color to green.
Change the
color of links when mouse is over:
Following is the example which
demonstrates how to change the color of links when we bring a mouse pointer
over that link. Possible value could be any color name in any valid format.
<style type="text/css">
a:hover {color: #FFCC00}
</style>
<a href="index.html">Bring Mouse Here</a>
|
This will produce following
link. Now you bring your mouse over this link and you will see that it changes
its color to yellow.
Change the
color of active links:
Following is the example which
demonstrates how to change the color of active links. Possible value could be
any color name in any valid format.
<style type="text/css">
a:active {color: #FF00CC}
</style>
<a href="index.html">Click This Link</a>
|
This will produce following
link. This will change it color to pink when user clicks it.
css –
tables
This tutorial will teach you
how to set different properties of an HTML table using CSS. You can set
following properties of a table:
·
The border-collapse Specifies whether the browser
should control the appearance of 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 Specifies whether the border
should be shown if a cell is empty.
·
The table-layout Allows browsers to speed up 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.
Now we will see how to use
these properties with examples.
The border-collapse
Property:
This
property can have two values collapse and separate.
Following is the example to show both values:
<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>
|
This will produce following
result:
|
The
border-spacing Property:
The border-spacing property
specifies the distance that separates adjacent cells. borders. It can take
either one or two values; these should be units of length.
If you provide one value it
will 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:
<style type="text/css">
/* If you provide one value */
table.example {border-spacing:10px;}
/* This is how you can provide two values */
table.example {border-spacing:10px; 15px;}
</style>
|
Now let's modify 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>
|
This will produce following
result:
|
The
caption-side Property:
The caption-side property
allows you to specify where the content of a <caption> element should be
placed in relationship to the table. The table that follows lists the possible
values.
This
property can have one of the four values top,
bottom, left or right. Let us see following
example to show each value:
NOTE:These
properties may not work with your IE Browser.
<style type="text/css">
caption.top {caption-side:top}
caption.bottom {caption-side:bottom}
caption.left {caption-side:left}
caption.right {caption-side:right}
</style>
<table style="width:400px; border:1px solid black;">
<caption class="top">
This caption will appear at the top
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="bottom">
This caption will appear at the bottom
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="left">
This caption will appear at the left
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="right">
This caption will appear at the right
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
|
This will produce following
result:
|
The
empty-cells Property:
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.
<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>
|
This will produce following
result:
|
The
table-layout Property:
The table-layout property is
supposed to help you control how a browser should render or lay out a table.
This
property can have one of the three values fixed,
auto or inherit.
Here is the example to show the
difference between these properties.
NOTE:This
property is not supported by many browsers so do not rely on this property.
<style type="text/css">
table.auto
{
table-layout: auto
}
table.fixed
{
table-layout: fixed
}
</style>
<table class="auto" border="1" width="100%">
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
<br />
<table class="fixed" border="1" width="100%">
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
|
This will produce following
result:
|
css –borders
The border properties allow you to specify how the
border of the box representing an element should look. There are three
properties of a border you can change
·
The border-color Specifies the color of a border.
·
The border-style Specifies whether a border should be
solid, dashed line, double line, or one of the other possible values.
·
The border-width Specifies the width of a border.
Now we will see how
to use these properties with examples.
The border-color Property:
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:
·
border-bottom-color changes the color of bottom border.
·
border-top-color changes the color of top border.
·
border-left-color changes the color of left border.
·
border-right-color changes the color of right border.
Here is the example
which shows effect of all these properties:
<style
type="text/css">
p.example1{
border:1px solid;
border-bottom-color:#009900; /* Green */
border-top-color:#FF0000; /* Red */
border-left-color:#330000; /*
Black */
border-right-color:#0000CC; /*
Blue */
}
p.example2{
border:1px solid;
border-color:#009900; /*
Green */
}
</style>
<p
class="example1">
This example is showing all borders
in different colors.
</p>
<p
class="example2">
This example is showing all borders
in green color only.
</p>
|
This will produce
following result:
This example is showing all borders in different
colors.
This example is showing all borders
in green color only.
|
The border-style Property:
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 following properties:
·
border-bottom-style changes the style of bottom border.
·
border-top-style changes the style of top border.
·
border-left-style changes the style of left border.
·
border-right-style changes the style of right border.
Following is the
example to show all these border styles:
<p style="border-width:4px;
border-style:none;">
This is a border with none width.
</p>
<p style="border-width:4px;
border-style:solid;">
This is a solid border.
</p>
<p style="border-width:4px;
border-style:dashed;">
This is a dahsed border.
</p>
<p style="border-width:4px;
border-style:double;">
This is a double border.
</p>
<p style="border-width:4px;
border-style:groove;">
This is a groove border.
</p>
<p style="border-width:4px;
border-style:ridge">
This is aridge border.
</p>
<p style="border-width:4px;
border-style:inset;">
This is a inset border.
</p>
<p style="border-width:4px;
border-style:outset;">
This is a outset border.
</p>
<p style="border-width:4px;
border-style:hidden;">
This is a hidden border.
</p>
<p style="border-width:4px;
border-top-style:solid;
border-bottom-style:dashed;
border-left-style:groove;
border-right-style:double;">
This is a a border with four
different styles.
</p>
|
This will produce
following result:
This is a border with none width.
This is a solid border.
This is a dahsed border.
This is a double
border.
This is a groove
border.
This is aridge
border.
This is a inset border.
This is a outset border.
This is a hidden border.
This is a a border with four
different styles.
|
The border-width Property:
The border-width property allows you to set the width of an element
borders. The value of this property could be either a length in px, pt or cm or
it should be set to thin, medium or thick.
You can
individually change the width of the bottom, top, left, and right borders of an
element using the following properties:
·
border-bottom-width changes the width of bottom border.
·
border-top-width changes the width of top border.
·
border-left-width changes the width of left border.
·
border-right-width changes the width of right border.
Following is the
example to show all these border width:
<p style="border-width:4px;
border-style:solid;">
This is a solid border whose width
is 4px.
</p>
<p style="border-width:4pt;
border-style:solid;">
This is a solid border whose width
is 4pt.
</p>
<p
style="border-width:thin; border-style:solid;">
This is a solid border whose width
is thin.
</p>
<p
style="border-width:medium; border-style:solid;">
This is a solid border whose width
is medium;
</p>
<p
style="border-width:thick; border-style:solid;">
This is a solid border whose width
is thick.
</p>
<p
style="border-bottom-width:4px;
border-top-width:10px;
border-left-width: 2px;
border-right-width:15px;
border-style:solid;">
This is a a border with four
different width.
</p>
|
This will produce
following result:
This is a solid border whose width
is 4px.
This is a solid border whose width
is 4pt.
This is a solid border whose width
is thin.
This is a solid border whose width
is medium;
This is a solid border whose width
is thick.
This is a a border with four different width.
|
Border Properties Using Shorthand:
The border property
allows you to specify color, style, and width of lines in one property:
Following is the
example to show to use all the three properties into a single property. This is
the most frequently used property to set border around any element.
<p style="border:4px solid
red;">
This example is showing shorthand
property for border.
</p>
|
This will produce
following result:
This example is showing shorthand
property for border.
|
css –
margins
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 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.
There are following
four properties to set an element margin.
·
The margin 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.
Now we will see how
to use these properties with examples.
The margin Property:
The margin property
allows you 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 p {margin: 10px 2%} top and bottom margin will be 10px, left and right margin will be 2% of the total width of the document. p {margin: 10px 2% -10px} top margin will be 10px, left and right margin will be 2% of the total width of the document, bottom margin will be -10px p {margin: 10px 2% -10px auto} top margin will be 10px, right margin will be 2% of the total width of the document, bottom margin will be -10px, left margin will be set by the browser </style> |
Here is the
example:
<p
style="margin: 15px; border:1px solid black;">
all four margins will be 15px </p> <p style="margin:10px 2%; border:1px solid black;"> top and bottom margin will be 10px, left and right margin will be 2% of the total width of the document. </p> <p style="margin: 10px 2% -10px; border:1px solid black;"> top margin will be 10px, left and right margin will be 2% of the total width of the document, bottom margin will be -10px </p> <p style="margin: 10px 2% -10px auto; border:1px solid black;"> top margin will be 10px, right margin will be 2% of the total width of the document, bottom margin will be -10px, left margin will be set by the browser </p> |
This will produce
following result:
all four margins will be 10px
top and bottom margin will be 10px, left and right margin will be 2%
of the total width of the document.
top margin will be 10px, left and
right margin will be 2% of the total width of the document, bottom margin
will be -10px
top margin will be 10px, right
margin will be 2% of the total width of the document, bottom margin will be
-10px, left margin will be set by the browser
|
The margin-bottom Property:
The margin-bottom
property allows you set bottom margin of an element. It can have a value in
length, % or auto.
Here is the
example:
<p
style="margin-bottom: 15px; border:1px solid black;">
This is a paragraph with a specified bottom margin </p> <p style="margin-bottom: 5%; border:1px solid black;"> This is another paragraph with a specified bottom margin in percent </p> |
This will produce
following result:
This is a
paragraph with a specified bottom margin
This is another
paragraph with a specified bottom margin in percent
|
The margin-top Property:
The margin-top
property allows you set top margin of an element. It can have a value in
length, % or auto.
Here is the
example:
<p
style="margin-top: 15px; border:1px solid black;">
This is a paragraph with a specified top margin </p> <p style="margin-top: 5%; border:1px solid black;"> This is another paragraph with a specified top margin in percent </p> |
This will produce
following result:
This is a paragraph with a specified top margin
This is another paragraph with a specified top margin in percent
|
The margin-left Property:
The margin-left
property allows you set left margin of an element. It can have a value in
length, % or auto.
Here is the example:
<p
style="margin-left: 15px; border:1px solid black;">
This is a paragraph with a specified left margin </p> <p style="margin-left: 5%; border:1px solid black;"> This is another paragraph with a specified top margin in percent </p> |
This will produce following
result:
This is a paragraph with a
specified left margin
This is another paragraph with a
specified top margin in percent
|
The margin-right Property:
The margin-right
property allows you set right margin of an element. It can have a value in length,
% or auto.
Here is the
example:
<p
style="margin-right: 15px; border:1px solid black;">
This is a paragraph with a specified right margin </p> <p style="margin-right: 5%; border:1px solid black;"> This is another paragraph with a specified right margin in percent </p> |
This will produce
following result:
This is a paragraph with a
specified right margin
This is another paragraph with a
specified right margin in percent
|
css –
lists
Lists are very
helpful in conveying a set of either numbered or bulleted points. This tutorial
teaches you how to control list type, position, style etc. using CSS
There are 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.
Now we will see how
to use these properties with examples.
The list-style-type Property:
The list-style-type property allows you to control the
shape or style of bullet point (also known as a marker) in the 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 (default)
|
A filled-in
circle
|
circle
|
An empty circle
|
square
|
A filled-in
square
|
Here are the values
which can be used for an ordered list:
Value
|
Description
|
Example
|
decimal
|
Number
|
1,2,3,4,5
|
decimal-leading-zero
|
0 before the
number
|
01, 02, 03, 04,
05
|
lower-alpha
|
Lowercase
alphanumeric characters
|
a, b, c, d, e
|
upper-alpha
|
Uppercase
alphanumeric characters
|
A, B, C, D, E
|
lower-roman
|
Lowercase Roman
numerals
|
i, ii, iii, iv, v
|
upper-roman
|
Uppercase Roman
numerals
|
I, II, III, IV, V
|
lower-greek
|
The marker is
lower-greek
|
alpha, beta,
gamma
|
lower-latin
|
The marker is
lower-latin
|
a, b, c, d, e
|
upper-latin
|
The marker is
upper-latin
|
A, B, C, D, E
|
hebrew
|
The marker is
traditional Hebrew numbering
|
|
armenian
|
The marker is
traditional Armenian numbering
|
|
georgian
|
The marker is
traditional Georgian numbering
|
|
cjk-ideographic
|
The marker is
plain ideographic numbers
|
|
hiragana
|
The marker is
hiragana
|
a, i, u, e, o,
ka, ki
|
katakana
|
The marker is
katakana
|
A, I, U, E, O,
KA, KI
|
hiragana-iroha
|
The marker is
hiragana-iroha
|
i, ro, ha, ni,
ho, he, to
|
katakana-iroha
|
The marker is
katakana-iroha
|
I, RO, HA, NI,
HO, HE, TO
|
Here is the
example:
<ul
style="list-style-type:circle;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul style="list-style-type:sqaure;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol
style="list-style-type:decimal;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol
style="list-style-type:lower-alpha;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol
style="list-style-type:lower-roman;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
|
This will produce
following result:
o
Maths
o
Social Science
o
Physics
·
Maths
·
Social Science
·
Physics
1.
Maths
2.
Social Science
3.
Physics
a.
Maths
b.
Social Science
c.
Physics
i.
Maths
ii.
Social Science
iii.
Physics
|
The list-style-position Property:
The list-style-position property indicates whether the
marker should appear inside or outside of the box containing the bullet points.
It can have one the two values:
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).
|
Here is the
example:
<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:sqaure;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>
|
This will produce
following result:
o
Maths
o
Social Science
o
Physics
·
Maths
·
Social Science
·
Physics
1.
Maths
2.
Social Science
3.
Physics
a.
Maths
b.
Social Science
c.
Physics
|
The list-style-image Property:
The list-style-image allows you to specify an image so
that you can use your own bullet style. The syntax is as follows, similar to
the background-image property with the letters url starting the value of the
property followed by the URL in brackets. If it does not find given image then
default bullets are used.
Here is the
example:
<ul>
<li
style="list-style-image:
url(images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol>
<li
style="list-style-image:
url(images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
|
This will produce
following result:
·
Maths
·
Social Science
·
Physics
1.
Maths
2.
Social Science
3.
Physics
|
The list-style Property:
The list-style allows you to specify all the list
properties into a single expression. These properties can appear in any order.
Here is the
example:
<ul style="list-style:
inside square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style:
outside upper-alpha;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
|
This will produce
following result:
§ Maths
§ Social Science
§ Physics
A.
Maths
B.
Social Science
C.
Physics
|
The marker-offset Property:
The marker-offset property allows you to specify the
distance between the marker and the text relating to that marker. Its value
should be a length as shown in the following example:
Unfortunately,
however, this property is not supported in IE 6 or Netscape 7.
Here is the example:
<ul style="list-style:
inside square; marker-offset:2em;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style:
outside upper-alpha; marker-offset:2cm;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
|
This will produce
following result:
§ Maths
§ Social Science
§ Physics
A.
Maths
B.
Social Science
C.
Physics
|
css –
padding
The padding property allows you to specify how much
space should appear between the content of an element and its border:
There are following
five CSS properties which can be used to control lists:
The value of this
attribute should be either a length, a percentage, or the word inherit. If the
value is inherit it will have the same padding as its parent element. If a
percentage is used, the percentage is of the containing box.
You can also set
different values for the padding on each side of the box using the following
properties:
·
The padding-bottom Specifies the bottom padding of an
element.
·
The padding-top Specifies the top padding of an
element.
·
The padding-left Specifies the left padding of an
element.
·
The padding-right Specifies the right padding of an
element.
·
The padding Serves as shorthand for the preceding
properties.
Now we will see how
to use these properties with examples.
The padding-bottom Property:
The padding-bottom property sets the bottom padding
(space) of an element. This can take a value in terms of length of %.
Here is the
example:
<p style="padding-bottom:
15px; border:1px solid black;">
This is a paragraph with a
specified bottom padding
</p>
<p style="padding-bottom:
5%; border:1px solid black;">
This is another paragraph with a
specified bottom padding in percent
</p>
|
This will produce
following result:
This is a paragraph with a
specified bottom padding
This is another paragraph with a
specified bottom padding in percent
|
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 the
example:
<p style="padding-top:
15px; border:1px solid black;">
This is a paragraph with a
specified top padding
</p>
<p style="padding-top: 5%;
border:1px solid black;">
This is another paragraph with a
specified top padding in percent
</p>
|
This will produce
following result:
This is a paragraph with a
specified top padding
This is another paragraph with a
specified top padding in percent
|
The padding-left Property:
The padding-left property sets the left padding (space)
of an element. This can take a value in terms of length of %.
Here is the example:
<p style="padding-left:
15px; border:1px solid black;">
This is a paragraph with a
specified left padding
</p>
<p style="padding-left:
15%; border:1px solid black;">
This is another paragraph with a
specified left padding in percent
</p>
|
This will produce
following result:
This is a paragraph with a
specified left padding
This is another paragraph with a
specified left padding in percent
|
The padding-right Property:
The padding-right property sets the right padding
(space) of an element. This can take a value in terms of length of %.
Here is the
example:
<p style="padding-right:
15px; border:1px solid black;">
This is a paragraph with a
specified right padding
</p>
<p style="padding-right:
5%; border:1px solid black;">
This is another paragraph with a
specified right padding in percent
</p>
|
This will produce
following result:
This is a paragraph with a
specified right padding
This is another paragraph with a
specified right padding in percent
|
The padding Property:
The padding property sets the left, right, top and
bottom padding (space) of an element. This can take a value in terms of length
of %.
Here is the
example:
<p
style="padding: 15px; border:1px solid black;">
all four padding will be 15px </p> <p style="padding:10px 2%; border:1px solid black;"> top and bottom padding will be 10px, left and right padding will be 2% of the total width of the document. </p> <p style="padding: 10px 2% 10px; border:1px solid black;"> top padding will be 10px, left and right padding will be 2% of the total width of the document, bottom padding will be 10px </p> <p style="padding: 10px 2% 10px 10px; border:1px solid black;"> top padding will be 10px, right padding will be 2% of the total width of the document, bottom padding and top padding will be 10px </p> |
This will produce
following result:
all four paddings will be 15px
top and bottom paddings will be
10px, left and right paddings will be 2% of the total width of the document.
top padding will be 10px, left and
right padding will be 2% of the total width of the document, bottom padding
will be 10px
top padding will be 10px, right
padding will be 2% of the total width of the document, bottom padding and top
padding will be 10px
|
css -
dimension
You have seen the border that
surrounds every box ie. element, the padding that can appear inside each box
and the margin that can go around them. In this tutorial we will how we can
change the dimensions of boxes.
There are 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:
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 the example:
<p style="width:400px; height:100px;border:1px solid red;
padding:5px; margin:10px;">
This paragraph is 400pixels wide and 100 pixels high
</p>
|
This will produce following
result:
This paragraph is 400pixels
wide and 100 pixels high
|
The
line-height Property:
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 the example:
<p style="width:400px; height:100px;border:1px solid red;
padding:5px; margin:10px;line-height:30px;">
This paragraph is 400pixels wide and 100 pixels high
and here line height is 30pixels.This paragraph is 400 pixels
wide and 100 pixels high and here line height is 30pixels.
</p>
|
This will produce following
result:
This paragraph is 400pixels
wide and 100 pixels high and here line height is 30pixels.This paragraph is
400 pixels wide and 100 pixels high and here line height is 30pixels.
|
The
max-height Property:
The max-height property allows you to specify 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 the example:
<p style="width:400px; max-height:10px;border:1px solid red;
padding:5px; margin:10px;">
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
</p>
<img alt="logo" src="images/css.gif" width="95" height="84" />
|
This will produce following
result:
This paragraph is 400px wide
and max height is 10px This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px This paragraph is 400px
wide and max height is 10px
|
The
min-height Property:
The min-height property allows you to specify minimum
height of a box. The value of the min-height property can be a number, a
length, or a percentage.
Here is the example:
<p style="width:400px; min-height:200px;border:1px solid red;
padding:5px; margin:10px;">
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
</p>
<img alt="logo" src="images/css.gif" width="95" height="84" />
|
This will produce following
result:
This paragraph is 400px wide
and min height is 200px This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px This paragraph is 400px
wide and min height is 200px
|
The
max-width Property:
The max-width property allows you to specify maximum
width of a box. The value of the max-width 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 the example:
<p style="max-width:100px; height:200px;border:1px solid red;
padding:5px; margin:10px;">
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
</p>
<img alt="logo" src="images/css.gif" width="95" height="84" />
|
This will produce following
result:
This paragraph is 200px high
and max width is 100px This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px This paragraph is 200px
high and max width is 100px This paragraph is 200px high and max width is
100px
|
The
min-width Property:
The min-width property allows you to specify minimum
width of a box. The value of the min-width 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 the example:
<p style="min-width:400px; height:100px;border:1px solid red;
padding:5px; margin:10px;">
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
</p>
<img alt="logo" src="images/css.gif" width="95" height="84" />
|
This will produce following
result:
This paragraph is 100px high
and min width is 400px This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px This paragraph is 100px
high and min width is 400px This paragraph is 100px high and min width is
400px
|
css -
scrollbars
There may be a case when an
element's content might be larger than the amount of space allocated to it. For
example given width and height properties did not allow enough room to
accommodate the content of the element.
CSS
provides a property called overflow which tells the browser what to do if
the box's contents is larger than the box itself. This property can take one of
the following values:
Value
|
Description
|
visible
|
Allows
the content to overflow the borders of its containing element.
|
hidden
|
The
content of the nested element is simply cut off at the border of the containing
element and no scrollbars is visible.
|
scroll
|
The
size of the containing element does not change, but the scrollbars are added
to allow the user to scroll to see the content.
|
auto
|
The
purpose is the same as scroll, but the scrollbar will be shown only if the
content does overflow.
|
Here is the example:
<style type="text/css">
.scroll{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:scroll;
}
.auto{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:auto;
}
</style>
<p>Example of scroll value:</p>
<div class="scroll">
I am going to keep lot of content here just to show
you how scrollbars works if there is an overflow in
an element box. This provides your horizontal as well
as vertical scrollbars.
</div>
<br />
<p>Example of auto value:</p>
<div class="auto">
I am going to keep lot of content here just to show
you how scrollbars works if there is an overflow in
an element box. This provides your horizontal as well
as vertical scrollbars.
</div>
|
This will produce following
result:
Example of scroll value:
I
am going to keep lot of content here just to show you how scrollbars works if
there is an overflow in an element box. This provides your horizontal as well
as vertical scrollbars.
Example of auto value:
I
am going to keep lot of content here just to show you how scrollbars works if
there is an overflow in an element box. This provides your horizontal as well
as vertical scrollbars.
|
Basic CSS Implementation and Positioning
This article is designed to
be a starting point for basic CSS concepts and I hope many will find it useful.
The topics we will cover here are answers to questions I get every day on the
forum and I felt whipping up a comprehensive guide of CSS fundamentals could
benefit our members. Along the way, I've included some helpful links and
various tips and tricks that might come in handy for anyone looking to learn
more about CSS.
Example Files
I've put together a set of
example files for the exercises here. They include all the HTML and CSS files
for the examples. There is also a blank HTML file linked to a blank CSS file,
if you would like to follow along as we go. You can download them here.
Table of Contents
This article covers a lot
of ground, we'll go over the basics first. If you're familiar with the basics,
feel free to jump ahead to the positioning examples:
Why CSS is Important
Basically, anything related
to the look of an element on a web page is controlled via CSS. CSS is attached
to HTML elements and is used to control what HTML elements look like - how big
or small, how it is spaced, where it appears, what color it is, ECT. This is
necessary as HTML by itself doesn't offer many options in terms of looks,
positioning, or style for that matter. It basically goes up and down, side to
side and isn't very exciting in it's raw form. A primary purpose of HTML is to
present content, and CSS exists purely to style that content.
Implementing styles
In order to use CSS, styles
must be applied to HTML elements. The most preferable way is to add CSS to a
style sheet also known as a css file. Our templates, and pretty much every
template out there, come with a number of style sheets built in that you can
access and modify to suit your needs. If you're using Gantry, check out this article I wrote a
few months back, it covers adding a custom style sheet to a Gantry
template.
If you're on Joomla and
feeling adventurous, check out Andy Miller's article on
building a CSS plugin, or you can just download it and use it as
well. This allows you to place custom CSS directly in your articles.
More reference on inserting
CSS - Three Ways to Insert CSS
If you would like to try
the examples without too much effort, feel free to use the blank.html and
blank.css files in the example files. They are linked together so you can use
them right away.You can download them here.
The other way of applying
styles, is using inline styles directly inside of your HTML tags. We'll go over
why this is method is frowned upon.
Inline Styles
An inline style is where
you use the style attribute in your HTML to define css
properties. While inline styles can offer an easy and quick solution, they can
be difficult to update later and they can interfere with other style sheets
that may be in use. For example, take this inline style that creates a box:
This is my box
This approach can work in some cases, but it's best to stay
away from this method if possible. For example, say you're working on a website
that has 200 articles in Joomla, and each one needs this box. Later you decide
the backgound should be green. Now you'll need to go into each article and
update the style individually for each one. This would become a very time
consuming process that would leave a lot of room for error.
Unfortunately, these types
of styles are commonly generated by WYSIWYG (What You See Is What You Get) or
Visual editors that are used to input content in CMS programs. They are often
added to your content unknowingly and can be the cause of formatting
inconsistencies between template styles and content styles. What you see may be
what you get - but it's not always what you want. In many cases, you can't see
all the extra code that is added in, and these problems can usually be
corrected by looking at the code and stripping out the inline styles.
Also in this months
Magazine, Rocket Theme Moderator David Goode has put together an article
addressing these types of issues in content editors. Take a look here, it
covers some things to look out for and ways to input identifiers. Which we will
go over next.
Element Identifiers
Since we now know that
using the style attribute in our HTML is frowned upon,
we'll discuss the alternative. The better solution is to use either the id or class attributes in our HTML to create an
element identifier. This is used to identify a single HTML element (id), or
group of elements (class), by a specific name of your choosing. This name can
then be used in the style sheet to apply CSS properties. Then, the elements
identified by that name in the HTML will have those styles applied to them.
We'll go over how that works.
We'll go back to the box
example to show how this works. Instead of using an inline style, I'll use the
class attribute to give the box a class name of my-box:
Notice how the code is much
simpler to work with. Of course, it will not do much for us until we add in the
CSS into the style sheet. We'll do that now. Add the styles for the box to your
style sheet by using the class name identifier, like so:
1..my-box
{width:150px;
height:150px;
margin:0
auto;
border:1px
blacksolid;
background:#ddd;
text-align:center;}
You may notice there is a
dot, or period, before the name in the CSS. This is called a selector,
specifically a class selector in this case. To use the class name, place the
dot with the name immediately following it. Id selectors use the # hashmark
before the name. Then any CSS properties you add will go inside the curly
braces. We won't go too deep into CSS syntax here, you can check out this link
for some basics - CSS Syntax.
A few important points do
apply here:
§ The names you use
must be unique and they must exactly match in both the HTML and CSS to work.
§ An element that uses
an ID can only appear once on a page, otherwise causing an HTML validation
error. For that reason, ID's are used for primarily for grouping, or
identifying, specific parts of your site - header, sidebar, footer for example.
§ Classes can be used
as many times as you like and applied to different types of elements freely.
For this reason, they are often used for content elements and other repeatable
elements on your page.
For the purposes of this
article we'll deal mostly with classes. Unless you have a specific need to use
an id, using a class name will work just fine.
There are some distinct
advantages to working this way. Think about the previous example of using the
box div in 200 separate articles. If we used the class name and style sheet,
instead of the inline style, and we needed to change the style of all the
elements later, we just need to edit one line of CSS code. All of the elements
using the my-box class name will be updated in one shot. By taking an extra
step at the beginning, you can save hours of time later.
There is a lot more to CSS
selectors than what we have covered here, if you'd like to see what else is
possible, this is a handy link from the w3schools.com website, CSS Selector Reference. This website offers a
simple and comprehensive guide to CSS properties, so poke around a bit if you
want to learn more.
For now, we'll move on and
take a look at a few simple ways of using CSS in our content to overcome some
common situations and challenges.
CSS Positioning
Positioning is one of the
most powerful, yet often misunderstood, aspects of CSS. While it can be used to
create complex layouts, like the Gantry Framework for example, we'll just cover
the basics and show how it can be used in a content type setting.
Floats
and Clears
Floats are a useful way to
position elements to the left or right side. When an element has a float
applied, it basically gets out of the way and all of the nearby non-floated
elements will flow around it, similar to wrapping text around an image in a
word processor or a newspaper. Floats are fairly easy to implement, but they
also come with their own set of challenges, we'll address those as we go. Feel
free to reference css-floats.html and css-floats.css in the example files, all
of the code for these examples can be found there.
Floats
For example, this could be
a common layout issue for an article or post that appears on a website using a
CMS program. You have an image and a paragraph of text inside an article. The
article itself is a div, generated through the template or framework, and the
content from the editor appears inside. Without using floats or additional CSS,
the image will appear above the text in this case, as this is how HTML handles
these elements by default.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec quis varius libero. Mauris sem elit, laoreet
vel porttitor nec, rhoncus quis eros. Duis quis euismod ipsum. Vestibulum
iaculis mi tincidunt leo semper egestas.
As we can see, this isn't
the most visually appealing layout. It would make more sense to have the text
occupying the space next to the image, instead of sitting below it. Using a
float, that is exactly what we'll do. A float takes an element out of the
default flow of HTML, so the paragraph text will move up to wrap around the
image.
First, we'll create two new
class styles in the CSS that use the left and right float properties. While you
can choose any names you like for the classes, it can be helpful to use names
that make sense and are easy to remember. Be sure to use the dot before the
name to designate it as a class selector, like so:
1..float-left{
float:left;}
2..float-right{
float:right;}
Now that we have defined
our classes in the CSS, we'll add one of them to the HTML class attribute
inside the image tag, depending on which way you want it to float. In this
example, we'll go left. Since we have set up class names for both left and
right floats, it's easy to change your mind or experiment simply by updating
the class attribute in the HTML.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec quis varius libero. Mauris sem elit, laoreet
vel porttitor nec, rhoncus quis eros. Duis quis euismod ipsum. Vestibulum
iaculis mi tincidunt leo semper egestas.
We can see the result might
not be what was expected. The purpose of this example is to show both the power
and pitfalls of applying a float. As you can see, this moved the image to left
and allowed the text to flow into the horizontal space the image was occupying
previously, which was the intended goal. We've solved one problem, but
simultaneously created another.
Since the floated image has
been taken out of the normal flow, it is no longer affecting the height of the
containing div. The height of the div is not defined, so the div height is
determined by the height of the content inside of the div, and we would like
for it to stay that way. This is so it will remain flexible should we choose to
add or remove additional content later - that's why defining a fixed height for
the div itself wouldn't be a good solution. In this case, the div has condensed
around the paragraph text, creating an overlap of the image and the bottom of
the container. If the text was longer this might not be as much of an issue,
but in this case, I intentionally used a short snippet of text to illustrate this
example. Thankfully, there is a simple solution.
Clearing
This brings us to clearing.
A clear is used to get around, or cancel a float. It basically bypasses a
floated element and restores the normal flow. Clears, like floats, can be
applied to the left or right. They can also be applied to both sides - which
usually does the trick in most situations.
In our example, we wish to
preserve the normal height of our containing div, so we need to create a clear
below our content so the float doesn't affect the container. To create a
clearing element, add this to your style sheet:
1..clear
{clear:both;}
Then we'll add a clearing
div in the HTML under our content, but inside the containing div, that uses the
"clear" class name, like so:
And we can see the result
here
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec quis varius libero. Mauris sem elit, laoreet
vel porttitor nec, rhoncus quis eros. Duis quis euismod ipsum. Vestibulum
iaculis mi tincidunt leo semper egestas.
Using an empty div to clear
is a standard technique. It has no height unless you specify otherwise or put
content inside it, thus remaining invisible. In this case, it was specifically
created for the purpose of clearing the float. It restores the flow and
eliminates the bunched up container issue nicely.
Finishing Touches
Now that we have the layout
of our content working, let's take a look at the content inside. One issue is
that the paragraph text is pushed up against the image. We can fix that easily
by applying a margin to the image in the CSS. Since we are already using a
class for the image to apply the float, we could easily add our margin to that
same class in the style sheet to make the adjustment. Since we're learning some
tricks, we'll instead create a new class that will be specific to the images in
the article content. This is a good practice, particularly if you plan to use,
or are currently using, the original float classes in other places. In real
life, if you happened to be using that class elsewhere, any changes made to it
would affect all the elements using it, which may not be a desired effect.
By using a new class name,
we can fix this specific spacing issue, without affecting anything else, and
add additional styles to spruce up the look of the content as well. In the
style sheet, we'll create a new identifier class called
"article-img". Add this to the style sheet:
1..article-img
{float:left;
margin-right:
10px;
border:1px
black
solid;padding:
2px;
background:#666; }
In your HTML, be sure to
update the image class attribute to use the new identifier:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec quis varius libero. Mauris sem elit, laoreet
vel porttitor nec, rhoncus quis eros. Duis quis euismod ipsum. Vestibulum
iaculis mi tincidunt leo semper egestas.
We see the final result.
The right margin we applied to the image pushed the paragraph to the right a
bit, fixing the spacing issue. I also added a border, a background color and a
bit of padding to create a framing effect, giving the image a bit more pop.
Feel free to add more styles and experiment with the look if you like, there is
certainly more that could be done.
Now that you have your
image class identifier and style worked out, you can use that class name in all
of the images in similar articles and achieve the same result with minimal
effort. Should a unique layout circumstance come up, you can simply create a
new identifier, change the style to suit your needs and update the HTML class
attribute accordingly. Once the identifiers and HTML are in place, you'll be
able to easily experiment with various styles and CSS properties. All the
changes will happen directly in the style sheet and the only real limit is your
creativity.
Using
Floats to Create a Horizontal Menu
Let's take a quick look at
a more advanced use for a float. One of the most common questions on the forum
is about making vertical menus horizontal. Since Joomla moved on from version
1.5, the horizontal menu is no longer an option in the default menu module,
much to the dismay of many. Fortunately, it's an easy task to accomplish by
using a float. The only real difference between a vertical menu and a
horizontal menu is that the list items inside use a float.
Take this for example, this
is a basic HTML list, most menus are made from HTML list elements. This is a
stripped down version of what the structure for a Joomla menu looks like.
1.<ul
class="menu">
2.<li>Menu Item
one</li>
3.<li>Menu Item
two</li>
4.<li>Menu Item
three</li>
5.<li>Menu Item
four</li>
6.</ul>
With no styling, it looks
like this:
§ Menu Item one
§ Menu Item two
§ Menu Item three
§ Menu Item four
Using Descendant
Selectors
You can see the ul
(unordered list) element uses the "menu" class as an identifier (as
all default menus in Joomla do), and there are li's (list items) inside with
our menu contents. To accomplish our goal, we need to apply a float to the list
items inside the ul element. The list items themselves do not have identifiers,
but they are inside of the ul element which does have a class name identifier.
We can use that class name to apply styles to the content inside. Remember, the
"C" in CSS stands for Cascading, part of the cascading concept is
descendant selecting. Basically, an element that contains other elements is
considered a parent, and the elements inside are children, or descendants, of
the parent element. In this case, the ul is the parent, and the li's are the
children. By using a descendant selector we can target the list items
specifically. This is actually easier than it sounds, the CSS would look like
this:
1..menu li{float:
left;
margin:0
10px;}
We have used the menu class
name in the same way as before, but by adding a single space and the name of
the child element afterward, we have created a descendant selector. Meaning the
styles are specifically applied to all of the list items inside the menu class.
In this case, we have floated the list items left, and added some margins on
the side to create space between them. One simple line of CSS will do the
trick, it just has to be applied in the right way. Below, you can see the
result.
·
Menu Item one
·
Menu Item two
·
Menu Item three
·
Menu Item four
To take the concept one
step further, if you applied that code to your site and you were using more
than one menu, the change would be applied to all menus with the class name of
"menu". That could be a problem, especially if you had another menu
that needs to stay vertical. Using descendant selectors again is the answer,
there is no limit on how many levels of descendance you can use, you just need
to identify a parent element of the menu you wish to change. For the sake of
argument we'll say the menu you want to change is in the footer position of a
Rocket Theme template. All the primary module / widget positions in Gantry are
divs with ID's, so you can use those ID's to target the modules inside. For
example:
1.#rt-footer .menu
li{float:
left;
margin:0
10px;}
Now you are only applying
the style to all the list items inside the menu class that are inside the
rt-footer element - any menu list items not inside of rt-footer will be
unaffected. You could get more specific in Joomla by adding a module class
suffix in your module settings and using that as an identifier, thus applying
the style only to a specific module, like:
1..module-class-suffix
.menu li{float:
left;
margin:0
10px;}
Understanding how to single
out the elements you wish to change is a huge key to success in CSS. Being
handy with Firebug or a Web Inspector will serve you well, as looking at the
source code is often the easiest way to determine the name identifiers of
elements and the parent elements they are inside. Once you have that
information, jotting down a line of properly targeted CSS in your style sheet
will make the specific changes you need, where you need them, and nowhere else.
CSS
Positioning Properties
Understanding the how
positioning properties work in CSS can be challenging at first, somewhat due to
the terminology and the context of how elements are positioned within each
other, and in some cases, within your browser window. While it can be a bit
confusing at first, the power that comes with understanding them will give you
complete control over the placement of your elements and allow you to create
more depth in your designs.
There are four position
properties in CSS. By default, all elements use Static Positioning. There
really isn't much to understand with this one, it doesn't need to be defined
anywhere and it's pretty much what you are seeing on the screen already. Static
positioned elements follow the normal flow of HTML, that's really all there is
to know about it. This is the only positioning property that cannot be moved
with the top, right, bottom, left properties or have z-index applied to it, only
the other three can make use these properies, we'll cover all that in a bit.
Fixed Positioning
The next property, Fixed Positioning, is not used
a whole lot, but it can accomplish a few specific tasks. Elements that use
fixed positioning are taken out of the normal flow and positioned relative to
the browser window. The
element is then "fixed" in that position within your window,
regardless of page scrolling, it just sticks in that spot permanently. This
makes it a less than ideal solution for basic positioning tasks, as everyone
has a different monitor size, browser window size ECT. The results can be
inconsistent from one computer to the next, and changing the size of the
browser window can move the element as well.
The power of fixed
positioning is usually put to work when trying to create a fixed header, a
fixed footer, or on annoying websites that place an ad on the screen that stays
in a fixed location regardless of the page being scrolled up or down. Most of
the time it's in a vertical setting, placed at the very top or very bottom. For
example, you have a footer you wish to stay at the bottom of the page at all
times. Fixed Positioning is the right tool for the job.
Due to the way it works,
it's difficult to place an example here, as it would be stuck on the page we
are seeing right now. A good reference is our Zephyr Theme, take a look here: Zephyr Theme. Notice the fixed footer at the
bottom, it stays there even when you scroll down or resize the browser window.
This is fixed positioning in action, the CSS that makes this happen is this:
1..fixedfooter-1
#rt-footerbar {
2.position:
fixed;
3.width:
100%;
4.bottom:
0;
5.left:
0;
6.z-index:
1000;
7.}
The position is set to
fixed, then it is positioned by the left and bottom properties. Since bottom is
set to zero, it is placed directly at the very bottom of the browser window,
that is the notable property in this case. You may also notice the z-index
property, which is used to stack positioned elements. The high z-index number
ensures it stays on top and isn't covered by other elements, more on this
later. Of course, in this example, there is more happening behind the scenes,
as the fixedfooter-1 element is generated via Javascript and controlled by
template parameters, but it serves to illustrate the point. Besides this type
of application, there aren't many other practical uses for fixed positioning.
Relative Positioning
This is possibly the most
confusing of the positioning properties for beginners, but understanding how it
works is critical when using CSS to precisely position elements. The term
"relative" is what throws most folks off, it implies that the element
is positioned relatively to something else. This is not the case. In fact,
adding position:relative; to an element doesn't change the
position of the element at all. The real power of relative positioning is
slightly counter-intuitive at first. When applied, it transforms the element
into a positioning context that can be used as a reference point to position
other elements.
Positioning Context
Basically, by applying
position relative to an element, you're not changing the position of that
element. Instead, you have made it the relative position, or positioning
context, that other elements can then use as a basis for their positioning.
This comes in handy when working with elements that use absolute positioning.
Relative and absolute positioning go hand in hand. Often, trying to use
absolute positioning without the context of a relatively positioned element can
be messy and produce unexpected results. As I mentioned, this can be a bit
confusing until you see it in action, we'll get to an example soon.
Absolute Positioning
Absolute positioning is
similar to fixed positioning in that absolutely positioned elements are
completely removed from the flow of the HTML. The main difference between them
is the positioning context. While fixed position elements use the browser
window as a positioning context, absolutely positioned elements use relative
positioned elements as their positioning context. In the case that there are no
relatively positioned elements available, it uses the roothtml element as it's context. The notable
point here is that the positioning context is attached to the html elements,
and the context can be set anywhere on the page by using position relative,
this makes it infinitely more useful than fixed positioning. By using absolute
positioning inside of a relatively positioned element you can make precise
positioning adjustments to place your elements anywhere you like.
Positioning in Action
In this simple example
we'll go over how all of this actually works together. Feel free to open the
css-positioning.html and css-positioning.css files in the examples files to
follow along. Or jump to css-positioning-final.html and
css-positioning-final.css files to see the end results.
This
is the Header
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Phasellus ut dui sed mi sagittis faucibus
porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec posuere iaculis
ante, sit amet faucibus quam condimentum ac. Sed placerat turpis neque. Nulla
ac elit sem. Donec porta suscipit mollis. Ut volutpat lobortis dictum. Duis
lacinia rhoncus tortor, non porttitor tellus aliquam non.
Here, we have two divs, one
inside the other. The outer div will simulate an HTML page, as with some of the
CSS we are about to do here might have some unwanted effects on this actual
webpage. Basically, it just has some dimensions and position:relative; applied to it from the start. Here is
the HTML.
As you see, there are two
divs both with unique class names. The inner div contains content, a h2 header,
2 images and a paragraph, each with unique class names as well. I've done this
to create a way to position each element individually through the CSS. The base
CSS looks like this:
01./* Positioning
Example */
02.
03..positioning-container{position:relative;
04.width:575px;
margin:0
auto;
border:1px
black
solid;
padding:50px;background:#eee;}
05.
06..inner-container{border:1px
black
solid;
background:#fff;padding:10px;}
07.
08..demo-header{padding:20px;
color:#fff;
background:
gray;
border:1pxblack
solid;
width:200px;
margin-top:0;
09.-moz-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
10.-webkit-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
11.box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);}
12.
13..inner-container
img{border:1px
black
solid;
14.-moz-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
15.-webkit-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
16.box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);}
17.
18..sample-image-01{}
19..sample-image-02{}
20.
21..sample-content{}
Since we have our classes
set up in both the HTML and the CSS, making changes from here will only require
editing the style sheet. Some of the content elements have some styles, and
others haven't been styled yet, but we'll fill them in as we go. Notice that I
used a descendant selector to add a border and CSS3 box shadow to the both the
images in the inner container in one shot, instead of using the same CSS
properties twice inside the two individual identifiers, this works well if you
want the all the elements of a certain type to share properties. We'll do more
with that in a minute. Lets get to the positioning.
We'll start with exploring
how relative positioning works by applying absolute positioning to the h2
demo-header. In the CSS, add position:absolute; with top and left positions of 0 to
the demo-header class:
1..demo-header {
position:absolute;
top:0;
left:0;
2.padding:20px;
color:#fff;
background:
gray;
border:1px
black
solid;width:200px;
margin-top:0;}
This
is the Header
Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut dui sed mi
sagittis faucibus porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec
posuere iaculis ante, sit amet faucibus quam condimentum ac. Sed placerat
turpis neque. Nulla ac elit sem. Donec porta suscipit mollis. Ut volutpat
lobortis dictum. Duis lacinia rhoncus tortor, non porttitor tellus aliquam non.
Basically, we have removed
the element from the HTML flow, and positioned it at the very top left of the
positioning context, which is the outer div that has position:relative applied
to it. Even though the element is inside the inner div, the outer div is the
positioning context that the absolute positioning will use. If we didn't have
an outer div with position:relative, the header would be at the very top left
of the web page, which would not be preferable in most cases.
To illustrate this point
further, in the CSS for the inner-container, add position:relative;
1..inner-container{
position:
relative;
2.border:1px
black
solid;
background:#fff;
padding:10px;}
This
is the Header
Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut dui sed mi
sagittis faucibus porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec
posuere iaculis ante, sit amet faucibus quam condimentum ac. Sed placerat
turpis neque. Nulla ac elit sem. Donec porta suscipit mollis. Ut volutpat
lobortis dictum. Duis lacinia rhoncus tortor, non porttitor tellus aliquam non.
Now the header is
positioned at the top left of the inner div, as we have changed the positioning
context by adding position relative to that element. Absolutely positioned
elements will use the closest relatively positioned element as their home base
so to speak.
Hopefully, the relative
positioning concept is starting to make sense at this point. Now that we have
our defined our positioning context as the inner-container div, we can use the
positioning properties in the absolutely positioned element to be a bit more
creative with our layout. The top, left, bottom, right values set the
coordinates in pixels for the distance from that particular side. A common use
of absolute positioning is to break elements "outside the box" so to
speak, creating a three dimensional effect. This can be done by using negative
values in the positioning properties, update your CSS like so:
1..demo-header {
position:absolute;
top:-25px;
left:-25px;
2.padding:20px;
color:#fff;
background:
gray;
border:1px
black
solid;width:200px;
margin-top:0;
3.}
This
is the Header
Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut dui sed mi
sagittis faucibus porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec
posuere iaculis ante, sit amet faucibus quam condimentum ac. Sed placerat
turpis neque. Nulla ac elit sem. Donec porta suscipit mollis. Ut volutpat
lobortis dictum. Duis lacinia rhoncus tortor, non porttitor tellus aliquam non.
This adds a bit of
dimension to the page and makes the header pop out a bit. The CSS3 box shadow
helps this effect come to life. CSS is powerful and offers many options, don't
be afraid to get creative with it.
Now that we have a nicely
positioned header, we can move on to the images. Just to illustrate some
different ways of using the positioning, lets stack them on the right side.
Remember the descendant selector we used to create the borders - we'll add position:absolute; to that to apply the property to both
images in one shot, then input different coordinates in the individual classes
to place them in different locations. Update the CSS for the images like so:
1..inner-container
img{
position:absolute;
2.border:1px
black
solid;
3.-moz-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
4.-webkit-box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);
5.box-shadow:
5px
5px
5px
rgba(0,0,0,0.5);}
6.
7..sample-image-01{
top:
10px;
right:10px;}
8..sample-image-02{
bottom:
10px;
right:10px;}
Remember that when we
position elements absolutely, it removes them from the flow, almost as if they
didn't exist at all. In this case, our inner div will collapse when the images
have the positioning applied. Previously, we used a clear to fix this, but in
this case we aren't using a float so that won't work. This time, we'll define a
height for the inner div to reserve the space and contain the images inside the
box. The images here are 150px tall, so around 300px will do the trick. In this
case, I'll make it a bit smaller so we can experiment with z-index, as the
images will slightly overlap. Update the CSS for the inner-container:
1..inner-container{
position:
relative;
height:275px;
2.border:1px
black
solid;
background:#fff;
padding:10px;}
This
is the Header
Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut dui sed mi
sagittis faucibus porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec
posuere iaculis ante, sit amet faucibus quam condimentum ac. Sed placerat
turpis neque. Nulla ac elit sem. Donec porta suscipit mollis. Ut volutpat
lobortis dictum. Duis lacinia rhoncus tortor, non porttitor tellus aliquam non.
Now the images are on the
right, 10px in from each side, and the bottom image is covering the top image.
Let's switch this so the top image covers the bottom image by using z-index.
We'll also stagger the right positioning by 5px in opposite directions on each
image, just to mix it up a bit. Once again update the CSS for the image
classes:
1..sample-image-01{
top:
10px;
right:15px;
z-index:10;}
2..sample-image-02{
bottom:
10px;
right:5px;
z-index:
1;}
This
is the Header
Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut dui sed mi
sagittis faucibus porttitor rutrum ipsum. Integer mattis volutpat turpis. Donec
posuere iaculis ante, sit amet faucibus quam condimentum ac. Sed placerat
turpis neque. Nulla ac elit sem. Donec porta suscipit mollis. Ut volutpat
lobortis dictum. Duis lacinia rhoncus tortor, non porttitor tellus aliquam non.
By assigning image-01 a
higher z-index value than image-02, we have placed it in the front. The z-index
property is fairly simple to understand. To use z-index, an element must first
have a positioning property applied to it - other than static of course. Then
the stacking order is determined by the z-index number, higher numbers appear
in the front, lower numbers appear in the back. This can be handy to know
should you come across a situation where an element on your page is "on
top" of something it shouldn't be.
Finally, we'll fit our
paragraph text in there so we can actually read it. Since everything else has
been moved out of the flow, portions of it are covered up, which isn't ideal
for a content setting. At this point we don't need to do anything too crazy, we
just need to get it to fit in the space that is left. We can apply a margins to
the paragraph to accomplish this. In this particular instance, absolute
positioning might not be the best way to go. While it is a powerful tool, be
careful not to overuse it or otherwise abuse it. Margins in themselves are a
powerful positioning tool as well. For now, update the sample-content class,
like so:
1..sample-content{
margin:
75px
200px
0
25px;}
This
is the Header
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Phasellus ut dui sed mi sagittis faucibus porttitor rutrum ipsum. Integer
mattis volutpat turpis. Donec posuere iaculis ante, sit amet faucibus quam
condimentum ac. Sed placerat turpis neque. Nulla ac elit sem. Donec porta
suscipit mollis. Ut volutpat lobortis dictum. Duis lacinia rhoncus tortor, non
porttitor tellus aliquam non.
Here we've added margins in
a shorthand fashion - the order goes top, right, bottom, left - clockwise from
the top is the easy way to remember. We've pushed it down 75px from the top,
pushed it in from the right 200px - to get past the width of the images, the bottom
doesn't really matter in this case, and 25px in from the left just for some
space.
While this is a pretty
basic example, it illustrates the basic concepts of positioning. What we're
left with is a bit more stylish than what we started with, and it's easy to see
how complex design ideas can become reality on a web page with a just small bit
of CSS. Understanding how it works is the key to success.
By using element
identifiers (class names) in our HTML from the start, we were able to transform
the content purely using CSS. Once the original HTML was put in place, it
didn't need to be updated throughout the course of the example. The benefits of
this method include saving time, lighter code, and more styling flexibility.
The Bottom Line
We've covered a ground lot
here, but these basic concepts really only scratch the surface of what can be
done with CSS positioning, or CSS in general. Hopefully, the information here
provided a fundamental grasp on using HTML and CSS together and a general
understanding of positioning concepts in CSS. Really, we didn't use too much
code in any of the examples and I hope you've seen how easy it is to address
general layout and spacing issues with just a touch of code applied in the
right place.
While I took care to
include as much detail as possible, there is much we couldn't cover in this
single article. There are plenty of resources and tutorials online and lots of
great books on the topic. The great thing about knowing CSS is that no matter
whether you're using Joomla, Wordpress, Drupal, or something else - CSS is CSS,
and everything that is web based uses it. The knowledge can be applied to any
framework, or any situation. It's all about understanding how it works, from
there your creativity is the only limit.
Getting Started with
CSS: A Practical Exercise
Learning how to write CSS
is an essential lesson in any web design classroom. For you who are just
starting out, here’s a beginner’s guide to help you learn the basics in proper
CSS execution.
Preface
Performing even the most
basic CSS task requires an eye for details. Code is poetry and not something
for the restless. In this beginner’s guide tutorial we will show an example of
how to create an html web page using div tag markup and later defining them in
an external stylesheet.
Step 1 –
The Markup and Use of Tags
The HTML markup comes first
and you should always leave page styling (CSS) until the markup is completely
finished.
HTML stands for Hypertext
Mark-up Language and is the means to describe the structure of text-based
information of a web page. Before you learn how to apply styling through CSS
you will have to also learn how to apply the elements of structure.
When doing a markup you use
“tags” that are surrounded by angle brackets. Tags are used so that you may define
what you want on the web page. The CSS is later applied to define where the
content of each tag should appear in the web browser window, and how it should
look.
Elementary tags in web page
are:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
It is within the
<body> tag you then use more tags to markup web page element and content.
A very important tag that
is used to divide and define structure is the <div>. Using this tag you
may divide your web page into boxes. Within a div tag you can then add more
tags to define its content (links, text, images etc.), later to be styled in
the CSS.
Use an image editor
(Photoshop recommended) to draw a markup the boxes:
In code, this web page
markup would look like below, as an example on how to use div tags:
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<div id="header">
</div><!--Header-->
<div id="navigation">
</div><!--Navigation-->
<div id="menu">
</div><!--Menu-->
<div id="content">
</div><!--Content-->
<div id="footer">
</div><!--Footer-->
</div>
</body>
</html>
As you can see, we have
given the div tag an ID. This is to later be able to define every individual
tag in the stylesheet. “ID” is used to mark a tag for unique styling, while
“class” is used for reoccurring design elements.
Also, the first div tag in
the markup; the “wrapper” we use to wrap around all other tags. This is to
later be able to position the web page as we wish.
Step 2 –
Linking to Stylesheet
As established, the CSS
stylesheet is where you define design elements for your web page. You can
choose to create an internal stylesheet or an external. In this tutorial we
will show you how to use an external stylesheet.
If you preview your web
page in your web browser now, nothing will appear. That is simply because you
have not yet given the tags any color or form. And since the stylesheet will be
a separate external file, the first thing you have to do is to make sure your
web page includes a path to the CSS.
Add this code between
<head></head>:
<link rel="stylesheet" type="text/css" href="style.css" />
Step 3 –
Creating the CSS
The CSS syntax consists of
the selector, property and value. The selector is the tag you want to define in
design, the property what kind of attribute you wish to add, and value how the
property is added.
The selector, except the
body tag, is written to the CSS with either “#” or “.” before the selector
name; # defining selector ID and . (dot) defining selector class. A selector
can be defined by any number of properties and to work has to be wrapped within
{ }.
#selector {
property:value;
property:value;
property:value;
}
We will now define the
following tags as selectors in the stylesheet, according to a number of
different properties and values.
Selectors to be defined in
the CSS:
·
body
·
wrapper
·
header
·
navigation
·
menu
·
content
·
footer
Properties and possible
values in the CSS:
The background property can
be defined with an image or color, or both. To display an image, the value has
to include a path to the image source. To display a color only, a hexadecimal
is used. For both, the hexadecimal is defined before the image source.
The color property is used
to define the color of text of a selector. The value is written as hexadecimal
(e.g. #FFFFFF for white)
The font-family property
lets you decide what font type the web page text should have. It is standard to
include at least three font-family types, so if the web browser does not
support the first, it has more to choose from (e.g. Trebuchet MS, Arial, Times
New Roman)
The font-size property
defines the size of the font and is written in pixels.
The margin property is used
to define the position of the selector. The values are written to define the
selector’s sides’ distance from the browser’s window frame, as top, left,
bottom, right. As we want to position the web page in the center, we write 0px
auto 0px. All the four values do not however always have to be defined.
The width property defines
the desired width in pixels we want our selector box to have.
The height property defines
the height in pixels
The float property allows
us to position elements inside selectors, mainly to the left or right.
Now, to create the CSS
file, open your favorite text editor (EditPlus recommended) and save the new
document as style.css (the name we gave the css file when creating the path in
the web page head tag) in the folder where your web page are located. Then add
the following code:
body {
background: #f3f2f3;
color: #000000;
font-family: Trebuchet MS, Arial, Times New Roman;
font-size: 12px;
}
#wrapper {
background: #FFFFFF;
margin: 60px auto;
width: 900px;
height: 1024px;
}
#header {
background: #838283;
height: 200px;
width: 900px;
}
#navigation {
background: #a2a2a2;
width: 900px;
height: 20px;
}
#menu {
background: #333333;
float: left;
width: 200px;
height: 624px;
}
#content {
background: #d2d0d2;
width: 900px;
height: 624px;
}
#footer {
background: #838283;
height: 180px;
width: 900px;
}
Conclusion
You have now defined each
tag/selector by the properties mentioned above. Previewed in the browser, your
webpage should now look like this.
How To Create a Pure
CSS Dropdown Menu
With the help of some
advanced selectors a dropdown menu can be easily created with CSS. Throw in
some fancy CSS3 properties and you can create a design that was once only
achievable with background images and Javascript. Follow this tutorial to see
the step by step process of building your own pure CSS dropdown menu.
The menu we’ll be creating
features two sub categories that appear once the parent link is activated by a
hover. The first series of sub-links appear underneath main nav bar, then the
second series of links fly out horizontally from the first dropdown. Take a
look at the CSS dropdown menu demo to see it all in action.
The HTML
Structure
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tutorials</a></li>
<li><a href="#">Articles</a></li>
<li><a href="#">Inspiration</a></li>
</ul>
</nav>
First up we’ll need to
create the HTML structure for our CSS menu. We’ll use HTML5 to house the
navigation menu in a
<nav>
element, then add the primary
navigation links in a simple unordered list.<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tutorials</a>
<ul>
<li><a href="#">Photoshop</a></li>
<li><a href="#">Illustrator</a></li>
<li><a href="#">Web Design</a></li>
</ul>
</li>
<li><a href="#">Articles</a>
<ul>
<li><a href="#">Web Design</a></li>
<li><a href="#">User Experience</a></li>
</ul>
</li>
<li><a href="#">Inspiration</a></li>
</ul>
</nav>
The first sets of sub-menus
can then be added under the “Tutorials” and “Articles” links, each one being a
complete unordered list inserted within the
<li>
of its parent menu option.<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tutorials</a>
<ul>
<li><a href="#">Photoshop</a></li>
<li><a href="#">Illustrator</a></li>
<li><a href="#">Web Design</a>
<ul>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Articles</a>
<ul>
<li><a href="#">Web Design</a></li>
<li><a href="#">User Experience</a></li>
</ul>
</li>
<li><a href="#">Inspiration</a></li>
</ul>
</nav>
The secondary sub-menu is
nested under the “Web Design” option of the first sub-menu. These links are
placed into another unordered list and inserted into the “Web Design”
<li>
.
So far this leaves us with
a neat layout of links with the sub-menus having a clear relation to their
parents.
The CSS
Styling
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
Let’s begin the CSS by
getting the basic dropdown functionality working. With CSS specificity and
advanced selectors we can target individual elements buried deep in the HTML
structure without the need for extra IDs or classes. First hide the sub menus
by targeting any UL’s within a UL with the
display:none;
declaration.
In order to make these menus reappear they need to be converted back to block
elements on hover of the LI. The >
child selector makes sure only the
child UL of the LI being hovered is targeted, rather than all sub menus
appearing at once.nav ul {
background: #efefef;
background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: -webkit-linear-gradient(top, #efefef 0%,#bbbbbb 100%);
box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
padding: 0 20px;
border-radius: 10px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: ""; clear: both; display: block;
}
We can then start to style
up the main navigation menu with the help of CSS3 properties such as gradients,
box shadows and border radius. Adding
position:relative;
will allow us to absolutely position
the sub menus according to this main nav bar, then display:inline-table
will condense the width of the menu to
fit. The clearfix style rule will clear the floats used on the subsequent list
items without the use of overflow:hidden
,
which would hide the sub menus and prevent them from appearing.nav ul li {
float: left;
}
nav ul li:hover {
background: #4b545f;
background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
background: -webkit-linear-gradient(top, #4f5964 0%,#5f6975 40%);
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block; padding: 25px 40px;
color: #757575; text-decoration: none;
}
The individual menu items
are then styled up with CSS rules added to the
<li>
and the nested anchor. In the browser
this will make the link change to a blue gradient background and white text.nav ul ul {
background: #5f6975; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li a:hover {
background: #4b545f;
}
The main navigation bar is
now all styled up, but the sub menus still need some work. They are currently
inheriting styles from their parent elements, so a change of background and the
removal of the border-radius and padding fixes their appearance. To make sure
they fly out underneath the main menu they are positioned absolutely 100% from
the top of the UL (ie, the bottom).
The LI’s of each UL in the sub menu don’t need floating side by side, instead they’re listed vertically with thin borders separating each one. A quick hover effect then darkens the background to act as a visual cue.
The LI’s of each UL in the sub menu don’t need floating side by side, instead they’re listed vertically with thin borders separating each one. A quick hover effect then darkens the background to act as a visual cue.
nav ul ul ul {
position: absolute; left: 100%; top:0;
}
The final step is to
position the sub-sub-menus accordingly. These menus will be inheriting all the
sub-menu styling already, so all they need is to be positioned absolutely to
the right (left:100%) of the relative position of the parent
<li>
.
No comments:
Post a Comment