Saturday, August 8, 2009

Class and ID in CSS with examples and differences

If you ever want to describe content of an HTML/XHTML document, then basic elements like , <h1>, <p>, </p><ul>, <li> will do the job. For example, if you like all <p> will have certain design then within CSS define the layout and all that will be appkied to all </p><p> tags.

But think about an example, in your database you have two </p><p> and you want to give two different layout for these two sections. With basic set of tags you can't cover every possible type of page element or layout choice. In order to define every possible type of page element or layout choice we must need to use ID and CLASS attribute. Like, </p><ul id="navigation1"> or <ul id="navigation2"> etc.

Then we define the style of these elements inside our CSS.

So, we can use both ID and CLASS inside html in order give style to them. But what is the difference between ID and CLASS? When we should use ID and when not? What is the advantages or differences between them? The following discussion will help you.

ID
- The most important concept of ID is IDs are unique.
- Each element can have only one ID.
- Each page can have only one element with that ID.

CLASS
- The most important concept of CLASS is CLASSes are not unique.
- You can use the same class on multiple elements.
- You can use multiple classes on the same element.

If you want to apply same style to different elements then assign same class to those elements.

Like you want all <ul> element has same class named Listed. Then write as,
<ul class="Listed">..... </ul>
<ul class="Listed">..... </ul>
<ul class="Listed">..... </ul> etc.

You can also assign multiple classes to an element. In that case put every classes as space delimited. For <ul> you can assign three classes named Listed, MyList, List1 as below.
<ul class="Listed MyList List1">..... </ul>

Now let us look into detail about ID and CLASS.
ID and CLASS tagging does not do anything by default
If you simply assign ID or CLASS tag to an element there will be no changes in the element style unless you have CSS to target them and apply styling. So, Classes and ID’s don’t have any styling information to them.

Which to choose ID or CLASS
The rule is - style that to be reusable should be kept in a class and style that is totally unique should be kept in an ID.

Special browser functionality of IDs
CLASS does not have any special abilities in the browser, but ID has one very important functionality. That is the "hash value" in the URL.
Suppose if you have the following lines
<b>There are 33 visitors online</b> inside your http://localhost/testing_id.html
and now you added an id element with it like below.
<b id="test">There are 33 visitors online</b>
Now whenever in the browser you type http://localhost/testing_id.html#test the browser will attempt to locate the element with an ID of "test" and will automatically scroll the page to show that element. This is another important concept of why ID needs to be unique in an HTML page because your browser need to know where to scroll!

Elements can have both ID and CLASS
There is no restriction to add both ID and CLASS. Sometimes it is very good idea to assign both ID and CLASS to an element. A good example is using comments.
Like,
<li id="comments-1123" class="item">
It has a class applied to it that is for styling all comments on the page, but it also has a unique ID value (which to be dynamically generated by your web language). This ID value is useful for direct linking.

CSS does not care about ID or CLASS
To CSS ID and CLASS are same. There is nothing you can do with CLASS but not with ID and vice-versa in CSS.

JavaScript cares about ID or CLASS
In case of JavaScript there is difference between these two. It depends on there being only one page element with any particular, or else the commonly used getElementById function wouldn’t be dependable. For those familiar with jQuery, you know how easy it is to add and remove classes to page elements. It is a native and built in function of jQuery. But there is no such function exists for ID’s.

Case Sensitivity
ID and CLASS are both case sensitive in their name. So myid and MyID is different.

CSS syntax for ID and CLASS
Class is specified by including a period (.) before the selector name.
For example, we have HTML element like <p class="box1">
To apply red colour into box1 class write your CSS entry as,

.box1 {
color:red;
}


Specifically you could use,
p.box1 {
color:red;
}


To render ID into CSS you need to include a number sign (#) before the selector name.
For example, we have HTML element like </p><p id="footer">
To apply red colour to footer ID write your CSS entry as,

#footer {
color:#red;
}


</p></li></ul></ul></ul></ul></li></ul></h1>

Wednesday, August 5, 2009

CSS links with example

If you ever notice the HTML links, you will see most of the HTML links are underlined inside my blog, which is default option. You can change those links colors, fonts, underline of HTML links. You can also define color based on whether the link is unvisited, visited, active, or whether the cursor is on the link. In this post I will discuss those with examples.

Before going into real part let's have an idea about pseudo-class. A pseudo class allows you to take into account different conditions or events when defining a property for an HTML tag.

You can define a link by using <a href=""> attribute.
The link property can have different states like,
-Link is visited.
-Link is not visited.
-Link is active.
-Cursor is over the link.
and you can represent these states by pseudo class a:visited, a:link, a:active, a:hover respectively.

Let's now see example of each type of pseudo class CSS links.
1)Pseudo-class: link
The :link is for unvisited links. Suppose we want unvisited links will be displayed as green color. Then, do it inside CSS file as
a:link {
color: green;
}

2)Pseudo-class: visited
The :visited is used for links leading to pages that the user has visited. If you want that all visited page link will have color yellow. Then within CSS file write as,
a:visited {
color: yellow;
}

3)Pseudo-class: active
The :active is used for links that is active. If we want all active links will be displayed as background colored purple then write as,
a:active {
background-color: purple;
}

4)Pseudo-class: hover
The :hover is used when the mouse pointer goes over a link. If you want whenever mouse pointer goes over a link, link's color will be pink. Then write it as,
a:hover {
color: pink;

}

Finally, we can use http://arjudba.blogspot.com/2009/08/css-text-property-with-examples.html to add diversity of displaying links.

I tried to add some diversity in the following CSS and HTML. You run these in your environment to see effect.

Here is my mycss.css file.

a:link {
color: green;
text-decoration:none
}
a:visited {
color: yellow;
text-decoration:line-through;
}
a:active {
background-color: purple;
}
a:hover {
color: pink;
letter-spacing:40px;
text-transform:uppercase;
text-decoration:none;
}

And here goes a simple HTML,

<html>
<title>Links diversity with CSS
</title>
<link rel="stylesheet" href="mycss.css" type="text/css" />
<body>Here goes the links of
<a href="http://Arjudba.blogspot.com">Arju's Blog</a>
</body>
</html>

Related Documents

CSS Text property with examples.

CSS text property helps to add format and style to a text. Following CSS text properties are available.

1)text-indent
2)text-align
3)text-decoration
4)letter-spacing
5)text-transform

1)text-indent:
The property text-indent allows an indent to the first line of the paragraph. A simple example is shown below with indentation and without indentation.

<div style="text-indent:3em">
Here text-indent is set to 3em.
</div>
<div>
This text is without any indentation.
</div>


Here text-indent is set to 3em.


This text is without any indentation.


2)text-align:
Text-align can have value,
-Left :Text will be left justified.
-Right :Text will be right justified.
-Center :Text will be in center.
-justify : Text stretch in the line in such way so that both the right and left margins are straight.
Following is an example:

<div style="text-align:left">
<b>Left:</b> Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.
</div>
<div style="text-align:right">
<b>Right: </b>Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.
</div>
<div style="text-align:center">
<b>Center: </b>Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.
</div>
<div style="text-align:justified">
<b>Justified:</b> Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.
</div>

Output is,

Left: Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.


Right: Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.


Center: Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.


Justified: Register, post your question,
take part in regular discussion in forum http://www.arju-on-it.com/forum for free.


3)text-decoration
Text-decoration specifies whether there will be line over the text or under the text or through the text or none. It can have following values,
-Underline
-Overline
-Line-through
-None
Following is an example.

<div style="text-decoration:underline">
Line goes under the text.
</div>

<div style="text-decoration:overline">
Line goes over the text.
</div>

<div style="text-decoration:line-through">
Line goes through the text.
</div>

<div style="text-decoration:none">
No Line over/ under /through the text.
</div>

output is,

Line goes under the text.



Line goes over the text.



Line goes through the text.



No Line over/ under /through the text.

4)letter-spacing:
letter-spacing is the width between letter.
Example is,

<div style="letter-spacing:0.1em">
Space between letter is less.
</div>
<div style="letter-spacing:0.5em">
Space between letter is more.
</div>

output is,

Space between letter is less.


Space between letter is more.


5)text-transform:
The text-transform property controls the capitalization of a text.
It can have the following values.
-capitalize: Capitalizes the first letter of each word.
-uppercase: Converts all letters to uppercase.
-lowercase: Converts all letters to lowercase.
-none: No transformations
Following is an example.

<div style="text-transform:capitalize">
<b>Capitalize:</b> My name is Mohammad Abdul Momin Arju
</div>
<div style="text-transform:uppercase">
<b>Uppercase: </b>My name is Mohammad Abdul Momin Arju
</div>
<div style="text-transform:lowercase">
<b>Lowercase: </b>My name is Mohammad Abdul Momin Arju
</div>
<div style="text-transform:none">
<b>None: </b>My name is Mohammad Abdul Momin Arju
</div>

output is,

Capitalize: My name is Mohammad Abdul Momin Arju


Uppercase: My name is Mohammad Abdul Momin Arju


Lowercase: My name is Mohammad Abdul Momin Arju


None: My name is Mohammad Abdul Momin Arju


Related Documents

Tuesday, August 4, 2009

CSS Fonts with examples

In this post I will discuss about lists of fonts attributes/properties available in CSS along with their examples. With these properties you can control the fonts displayed in the web page.

The lists of font properties available in CSS are,

1)font-family
2)font-style
3)font-variant
4)font-weight
5)font-size
6)font


Some descriptions with examples are shown below.
1)font-family:
With font-family property it is specified several fonts so that browser can choose the text font of the element in an order. If first font in the order is not available then browser use second font in the order to display the item and so on.

There are two types of names used to categorize fonts: family-names and generic families.

Family name denotes a specific font name.

Generic family denotes a group of family name.
Family name and their generic family is displaying HTML code and result is given below.

<ul>
<b><u><div style="font-famimy:serif; padding-bottom:.2em">Following is an example of Generic family serif</div></u></b>
<div style="font-family:Times New Roman">Times New Roman</div>
<div style="font-family:Garamond">Garamond</div>
<div style="font-family:Georgia">Georgia</div>
</ul>
<ul>
<b><u><div style="font-famimy:sans-serif;padding-bottom:.2em">Following is an example of Generic family sans-serif</div></u></b>
<div style="font-family:Trebuchet">Trebuchet</div>
<div style="font-family:Arial">Garamond</div>
<div style="font-family:Verdana">Georgia</div>
</ul>
<ul>
<b><u><div style="font-famimy:serif;padding-bottom:.2em">Following is an example of Generic family Monospace</div></u></b>
<div style="font-family:Courier">Courier</div>
<div style="font-family:Courier New">Courier New</div>
<div style="font-family:Andele Mono">Andele Mono</div>
</ul>

Output is,


    Following is an example of Generic family serif

    Times New Roman

    Garamond

    Georgia



    Following is an example of Generic family sans-serif

    Trebuchet

    Garamond

    Georgia



    Following is an example of Generic family Monospace

    Courier

    Courier New

    Andele Mono



You can also specify font-family as below.
font-family: arial, verdana, sans-serif;
If Arial font is not available to browser it will try to display as font verdana. If verdana not available then it will display a font from sans-serif family.

2)font-style:
font-style can be defined as normal, italic or oblique.
Following is an example.


<ul>
<b><u><div style="font-famimy:serif; padding-bottom:.2em">Following is an example of font-style of Generic family serif</div></u></b>
<div style="font-family:Times New Roman;font-style:italic">Times New Roman</div>
<div style="font-family:Garamond; font-style:oblique">Garamond</div>
<div style="font-family:Georgia; font-style:normal">Georgia</div>
</ul>



    Following is an example of font-style of Generic family serif

    Times New Roman

    Garamond

    Georgia



3)font-variant:
Font-variant value can be normal or small-caps. A small-caps font is a font that displays all letters like capital letters but for uppercase size is greater than lower.

Example is,

<ul>
<b><u><div style="font-famimy:serif; padding-bottom:.2em">Following is an example of font-style of Generic family serif</div></u></b>
<div style="font-family:Times New Roman;font-variant:small-caps">Times New Roman</div>
<div style="font-family:Garamond; font-style:normal">GarAmond</div>
<div style="font-family:Georgia; font-variant:small-caps">GeoRGia</div>
</ul>



    Following is an example of font-style of Generic family serif

    Times New Roman

    GarAmond

    GeoRGia



4)font-weight:
font-weight defines how bold or heavy a font will be. It can have value normal or bold. Also it can have weight between 100 to 900 to describe the weight of a font.

Example is shown below.

<ul>
<b><u><div style="font-famimy:serif; padding-bottom:.2em">Following is an example of font-style of Generic family serif</div></u></b>
<div style="font-family:Times New Roman;font-weight:700">Times New Roman</div>
<div style="font-family:Garamond; font-weight:bold">GarAmond</div>
<div style="font-family:Georgia; font-weight:normal">GeoRGia</div>
</ul>


    Following is an example of font-style of Generic family serif

    Times New Roman

    GarAmond

    GeoRGia



5)font-size:
font-size describes how big a font will be. It can be expressed in various unit like pixel size(px), percentage(%), adjust based on screen(em).
Following is an example,


<ul>
<b><u><div style="font-famimy:serif; padding-bottom:.2em">Following is an example of font-style of Generic family serif</div></u></b>
<div style="font-family:Times New Roman;font-size:2.2em">Times New Roman</div>
<div style="font-family:Garamond; font-size:30px">GarAmond</div>
<div style="font-family:Georgia; font-size:120%">GeoRGia</div>
</ul>


    Following is an example of font-style of Generic family serif

    Times New Roman

    GarAmond

    GeoRGia



6)font:
The font is a short hand property. It covers all the different font properties in one single property.

Suppose you have defined you font styles as below,
font-style: italic;
font-weight: bold;
font-size: 10px;
font-family: arial, sans-serif;

Instead of writing these, you simply can use font like below.

font: italic bold 10px arial, sans-serif;

The order of values for font is:

font-style | font-variant | font-weight | font-size | font-family

Related Documents

Monday, August 3, 2009

CSS color and backgrounds

In this post I will discuss about CSS color and list of background attributes.
Following is the list of foreground and background color attribute exist in CSS.
1)color
2)background-color
3)background-image
4)background-repeat
5)background-attachment
6)background-position
7)background
In the following section there is some discussions about these properties/attributes.

1)color: The color property is for foreground color of an HTML element. The color attribute can be entered through three ways.

i)Hexadecimal value: example is, color: #ff00ee; where first pair ff represents red, second pair 00 represents green and last pair ee represents blue.)

ii)Naming the color: You can name the color with color attribute. Like you can describe blue color by color:blue; red one by color:red; etc.

iii)Third way is define the color in rgb notation. Like color:rgb(100,123,233) where first one represents red, second os green and third one is blue. The measurement is between zero(0) to 255 where 255 represents fully red/green/blue and 0 represents no red/green/blue.

2)background-color: The background-color property describes the background color of an element. The value can be entered same as color attribute. To make blue background we can write, background-color: blue;

3)background-image: With background-image property we can insert background image for a web page. To do this you need to apply the background-image property to <body> and specify the location of the image.
Example is,
body {
background-image: url("myimage.jpg");
}


4)background-repeat: If you set your background image using background-image property by default the image is repeated both horizontally and vertically to cover the entire screen. However, there will be many times where you may not wish to repeat your background image. You can control this by using background-repeat property.

There is four different values for background-repeat.
i)background-repeat: repeat-x - The image is repeated horizontally
ii)background-repeat: repeat-y - The image is repeated vertically
iii)background-repeat: repeat - The image is repeated both horizontally and vertically
iv)background-repeat: no-repeat - The image is not repeated

5)background-attachment: The property background-attachment specifies whether a background picture is fixed or scrolls along with the containing element.

This property can have two values.

i)Background-attachment: scroll - The image scrolls with the text of the web page - unlocked

ii)Background-attachment: fixed - The image is locked. That is background image will not move with the text when a reader is scrolling the page.

6)background-position: By default, a background image will be positioned in the top left corner of the screen.

But you may want to put the background image anywhere on your screen. The property background-position allows you to do this.

There are various ways to set the values of background-position.

i)background-position:200px 300px; indicates the background image to be positioned 200px from the left side and 300px from the top of the browser window.

ii)background-position: top right; indicates that the image is positioned in the top-right corner of the page.

iii)background-position: 50% 25%; indicates that the image is centrally positioned and one fourth down the page.

7)background:
The property background is the short hand way for all the background properties that is discussed above.

With simply background attribute we can list all background properties in one lines and thus shorten the background property usage.

For example, we need to set up following attributes for our CSS.
background-color: #FF0022;
background-image: url("myimage.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: right bottom;


Using background the same result can be achieved in just one line of code:


background: #FF0022 url("myimage.jpg") no-repeat fixed right bottom;

Remember the list of orders used in this background attribute,

[background-color] | [background-image] | [background-repeat] | [background-attachment] | [background-position]

And if any property is left out, it will automatically be set to its default value.

Related Documents
Diferent ways to apply CSS to an HTML document

Sunday, August 2, 2009

Diferent ways to apply CSS to an HTML document

There is actually three different ways by which we can apply CSS to an HTML element. I will discuss here all three ways. But it is recommended the last one. That is put CSS in a file and then from HTML make a link to the style sheet. This is because in that case all CSS can be easily monitored just one file and we can easily use that file along the whole site. It is flexible too.

Way 01: In-line (the attribute style)
One way to apply CSS to HTML is by using the HTML attribute "style". Suppose we need to set color of a text. Using CSS color we can do that. But before that put the color attribute within style like below,
<span style="color:red;" > This is red color</span>
And it will look like,
This is red color
If we run above code we will notice color becomes red.

Way 02: Internal (the tag style)
Second way of putting CSS elements is inside title tag of HTML. Within title tag using <style type="text/css"> CSS elements can be entered and then those CSS elements will be applied to whole page HTML in the page.
<html>
<head>
<title>Just an CSS example</title>
<style type="text/css">
body {color: red;}
</style>
</head>
<body>
This is red color
</body>
</html>

Way 03: External (link to a style sheet)
A third and good way of putting CSS things into a file. Like let's name the file as style.css and put all CSS things inside the file. Its contents are below.
body {
color: red;
}

And then the trick is to create a link from the HTML file to the style sheet (style.css). The following code inside HTML does the thing.

<link rel="stylesheet" type="text/css" href="style.css" >

My full HTML code will look like,
<html>
<head>
<title>My document</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
This color becomes red because of stylesheet.
</body>
</html>

Related Documents