w3schools Logo
Thursday 23 February 2012
w3schools.in Developer Network Made in INDIA

CSS Basic

CSS Introduction
CSS Positioning

CSS Selectors

Selectors Introduction
Tag Selectors Class Selectors ID Selectors Grouped Selectors Context Depandent Selectors

css Articles

CSS FontCSS TextCSS PaddingCSS MarginCSS BorderStyling tables with CSSHow To Build a Basic CSS LayoutHow to create css formsTableless Web Page Design Using CSS

 

Try-Yourself
CSS Tutorials Lessons

CSS Introduction

CssCSS is the acronym for Cascading Style Sheets.
CSS stands for Cascading Style Sheets. It is a way to divide the content from the layout on web pages.

  • CSS stands for Cascading Style Sheets
  • Styles define how to display HTML elements
  • Styles are normally stored in Style Sheets
  • Styles were added to HTML 4.0 to solve a problem
  • External Style Sheets can save you a lot of work
  • External Style Sheets are stored in CSS files
  • Multiple style definitions will cascade into one

What is style sheet

Before we begin describing how style sheets work, and how you use them, we should first answer the simple question, "what is a style sheet?"
As we saw in the last section, the W3C has made two major CSS recommendations:

  1. Cascading Style Sheets 1 (CSS 1)
  2. Cascading Style Sheets 2 (CSS 2) (which incorporates and extends CSS 1.)

But what are these recommendations in technical terms? They are language specifications, which define a simple grammar or language. The grammar specifies what types of statement can be made within a style sheet. Or, more plainly, it tells you what you can say and how you can say it.


Proprietary CSS Extensions

It's important to understand the difference here between what is in the recommendation and what is actually implemented in the various browsers. As we've talked about already, there are quite a few things that are in the specification but are not yet widely supported by browsers. There's no real harm in using features like this in your own work, as you know there is at least a commitment to their support at some time in the future. On the other hand, there are also a number of CSS-like proprietary extensions that may well "work" in a certain browser, but are not part of the CSS specification. Internet Explorer is the chief offender here. Less well known are a collection of Mozilla properties and values. These have been included in the code for the purposes of the Mozilla developers, who do not recommend you use them.
If you use Microsoft CSS extensions just be aware that they may not be supported by any browser other than Internet Explorer, and that there is no guarantee that they will be incorporated in future versions of the CSS specification. Our advice is to avoid them. For what it's worth, the following is a non-comprehensive list of Internet Explorer CSS extensions we have seen reference to. If you're in doubt about anything else you've come across, you can always do a quick check to see whether or not it is in the latest version of the CSS specification.

  • scrollbar-3dlight-color
  • scrollbar-arrow-color
  • scrollbar-base-color
  • scrollbar-darkshadow-color
  • scrollbar-face-color
  • scrollbar-highlight-color
  • scrollbar-shadow-color

CSS v HTML

So, at this level, a style sheet is simply a text file (which has a .css suffix), written according to the grammar defined in the CSS 1 or CSS 2 recommendations.

Example

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
body {font-family: Verdana, "Minion Web", Helvetica, sans-serif;
font-size: 1em;
text-align: justify; }

h1 {font-family: Verdana, sans-serif;
font-size: 1.3em; }

code {font-family: Courier, sans-serif;
font-size: 1em; }

.note {background-color: #003333;
border-width: thin;
border-color: black;
border-style: ridge;
color: white;
font-family: Verdana, Geneva, sans-serif;
font-size: .9em;
vertical-align: text-bottom; } 

Like an HTML document, a Cascading Style Sheet is just a simple text file. But unlike an HTML document, you don't need a special declaration at the top of the file to say that this is a style sheet. The name of the file should however end with a .css suffix.
In addition to being in .css files, style sheets can also be embedded into the <head> of HTML files. Later we'll take a closer look at linking and embedding style sheets, how to do it, what the difference is, and which is better.

With CSS, you will be able to:

  • define the look of your pages in one place rather than repeating yourself over and over again throughout your site. (Ever get tired of defining colors and fonts each time you start a new cell in a table? Those days are over with CSS!)
  • easily change the look of your pages even after they're created. Since the styles are defined in one place you can change the look of the entire site at once. (Ever get tired of replacing tags throughout your site when you want to change the look of a certain element? Those days are over with CSS!)
  • define font sizes and similar attributes with the same accuracy as you have with a word processor - not being limited to just the seven different font sizes defined in HTML.
  • position the content of your pages with pixel precision.
  • redefine entire HTML tags. Say for example, if you wanted the bold tag to be red using a special font - this can be done easily with CSS.
  • define customized styles for links - such as getting rid of the underline.
  • define layers that can be positioned on top of each other (often used for menus that pop up).


Your pages will load faster, since they aren't filled with tags that define the look. The style definitions are kept in a single CSS document that is only loaded once when a visitor enters your site.

 

Where Do Style Sheets Come From?

More than a decade before the world wide web, pioneers in the field of electronic documentation recognized the important distinction between what a document looks like (often referred to as its appearance) and the underlying structure of the document. Complex electronic publishing systems have since then been implemented in a way which separates information about how the document should appear from the document itself. When it was first imagined by Tim Berners-Lee, the web did provide a mechanism for this. But in the heated rush of the web's early explosion other, in some ways easier, but also problematic ways of coding page appearance took off. Among these were the <font> and <b> elements and other stylistic, or presentational HTML elements.
In a nutshell, CSS provides a means for web authors to separate the appearance of web pages from the content of web pages. We'll be learning a lot more about this in this introduction, how to do it, why it's good to do it, and why it's not so good to keep using the old ways.

The W3C

Cascading Style Sheets (CSS) is a recommendation of the World Wide Web Consortium (the W3C). The W3C is a consortium of web stakeholders: universities, companies such as Microsoft, Netscape and Macromedia, and experts in many web related fields. Founded by Tim Berners-Lee, it exists to enhance and promote the world wide web. One of its roles is to publish "recommendations". You can think of these W3C recommendations as a kind of standard. There are W3C recommendations covering diverse aspects of the web, from wireless content development to HTML 4.0 and XML.

A History of CSS Specifications
The W3C has published two major CSS recommendations: CSS 1 and CSS 2. As mentioned before, CSS 2 has now been updated to CSS 2, revision 1.

CSS 1 first became a recommendation in late 1996. Support for CSS 1 is extensive (if not complete) in Netscape 7, and acceptable in Internet Explorer, versions 4.5 for Macintosh and 5 and upwards for Windows. Opera 3.5 for Windows had very good support for CSS 1, and recent versions of this excellent browser have better support still.
CSS 2 became a recommendation in May of 1998. It extended CSS 1, so CSS 1 is a subset of CSS 2 with some very minor changes. Support for CSS 2 was almost nonexistent in Netscape Navigator 4.x, and very limited in Internet Explorer 4.5, 5 and 5.5 for Windows.

While browsers like Internet Explorer 6 and (more so) Netscape 7 and Opera 7 showed promising support for a lot of the basic aspects of CSS 2, it became obvious with time that some of its more complex (and perhaps even esoteric) features were unlikely to ever be implemented. For this reason, CSS 2, revision 1 was released in January 2003, with a number of the more high level aspects of CSS 2 now removed. It's possible that some of these features may find their way back into future iterations of the specification, but don't hold your breath waiting for the text-shadow property to reappear :-).

Developers often write to us expressing concerns about what features are in which version fo the specification. We're never sure what motivates this question, but our answer is always the same: it's quite simply not important from a development point of view. There is scant association between this and the level of browser support in older browsers, none at all in more recent. If you want to work with a property you have heard of the best thing to do is check to see if it is in this guide by looking for it in the contents, and then check its level of support in the browsers you are targetting.

The CSS Mobile Profile
The only specification for which you do need to be aware of what is and isn't included is the CSS Mobile Profile. We've included some information about this in this edition of the guide, in the Advanced CSS section.

Why Doesn't Everyone Use CSS?
CSS offers web developers a powerful tool that helps simplify the complex task of maintaining web sites, and provides sophisticated layout and design features for web pages without the need for plugins, long download times and expensive tools.

Why, then, after more than 7 years (CSS was first supported by Internet Explorer 3 in late 1996) are developers only just starting to take advantage of this fabulous web standard?
There are several important answers to this question.

First, CSS only really works at all in Netscape Navigator 4.0x and 4.5 and Internet Explorer 3 (a little), 4, 4.5 and 5. In the early days, this was considered to be a real issue. Developers felt that they had to develop for all those users still using "pre-CSS" browsers, so they avoided style sheets altogether. As we all know, the percentage of web users who use the latest version of a browser is always increasing. So what was almost unused 6 months ago, is widely used today. If you have resisted using or learning style sheets because it is an experimental technology, keep in mind that depending on your site visitors, a sizable majority of web browsers in common use today support style sheets very well. It is no longer experimental.

Secondly, even though the major browsers have supported style sheets for some time, this support is admittedly less than perfect. Many web developers believe that support is hopelessly inadequate, but this isn't true at all. In part, this is an excuse being used to avoid addressing a new technology. It was a valid reason a couple of years ago. Now, it's just an excuse.

What is true is that the process of trial and error which accompanies trying to make a style sheet work in all major browsers can be time consuming, and extremely frustrating. Again, though, as browsers like Internet Explorer 5 and 6 and Netscape Navigator 7 become more and more widely used, this becomes less and less of an issue.
One of the major reasons why we developed our CSS Editor Style Master was to help web developers address browser compatibility confusion. We also developed the Browser Compatibility Guide, to help those new to style sheets avoid many of the frustrating pitfalls of developing for inconsistent browser support. The information from this browser support guide is now incorporated into the paid version of this guide.

Thirdly, and very importantly, style sheets can be a little tricky to understand at first. Cascading style sheets work quite differently from the style sheets you might be familiar with, like those in word processors and page layout applications such as Quark Express and PageMaker. To address this third major hurdle, we have developed this Guide, a tutorial, self paced courses, and many other resources, all of which you can find at the House of Style.

How it works:

  • How do Style Sheets Work?
  • A style is a definition of fonts, colors, etc.
  • Each style has a unique name: a selector.
  • The selectors and their styles are defined in one place. In your HTML contents you simply refer to the selectors whenever you want to activate a certain style.

For example:
Instead of defining fonts and colors each time you start a new table cell, you can define a style and then, simply refer to that style in your table cells.

Classic HTML

Code:
1
2
3
4
5
6
7
8
9
10
11
12
<table>

<tr><td bgcolor="#FFCC00" align="left"><font face="arial" size="2" color="red">
<b>this is line 1</b></font></td></tr>

<tr><td bgcolor="#FFCC00" align="left"><font face="arial" size="2" color="red">
<b>this is line 2</b></font></td></tr>

<tr><td bgcolor="#FFCC00" align="left"><font face="arial" size="2" color="red">
<b>this is line 3</b></font></td></tr>

</table>

With CSS (assuming that a selector called subtext is defined)

Code:
1
2
3
4
5
<table>
<tr><td class="subtext">this is line 1</td></tr>
<tr><td class="subtext">this is line 2</td></tr>
<tr><td class="subtext">this is line 3</td></tr>
</table> 



W3
2009-11-15 14:11:45 829day

CSS3: What’s New for CSS? 

Alright, this one’s for the fellow web developers. I’d like to talk a bit about CSS3. But first,
a bit of review: 

CSS, short for Cascading Style Sheets, is a method to add formatting and styling to any
markup language. It’s most commonly used in web pages programmed in HTML and XHTML
but it can be used in XML documents as well.
It includes elements such as color, fonts, page layout, backgrounds,borders, and MUCH more. 

The most current release of CSS is CSS3. It’s currently in development, but some browsers are
already implementing certain elements for use in web pages. 
However, since not all browsers support CSS3 and it’s not guaranteed that everyone will be
running one of the latest supported browsers, it’s STRONGLY recommended that you don’t use
many of the elements to design major portions of your web page. But that’s a real shame,
because CSS3 has some really cool new features. Some of which, easily allow web developers
to create some stylish and functional web page elements. 

These are just a few of the cool new features that CSS3 is offering: 

Web Fonts 

This feature allows developers to embed fonts in their web pages, rather than relying on the
same old boring â€śweb safe” serif or sans-serif fonts. This feature was included in CSS2,
but only in Internet Explorer and it only supported fonts that were Embedded Open Type(.oet)
format. However, in CSS3, you can use any licensed TrueType (.ttf) or OpenType (.otf)
font in your page. Below is an example of the code needed to import custom fonts into a web page: 

@font-face { 
    font-family: Delicious; 
    src: url('Delicious-Roman.otf'); 


After importing it, you can call it using font-family: 

h2 { font-family: Delicious, sans-serif; }Of course, before using any fonts in your page,
much like images and other media, you MUST check the licensing. 

Rounded Corners (on borders) 
The border-radius function is one of my personal favourites. It simply allows the coder to
add nicely rounded corners of any radius to any border. It works the same as the existing
border element, in that it can be applied to all four corners with the generic statement,
or can be corner-specific by adding, you guessed it: topleft, topright, bottomleft or
bottomright.

This simple little piece of code can be seen 

in the example below: 

.box { 
    background-color: #ccc; 
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px; 
    border: 1px solid #000; 
    padding: 10px; 


Multiple Backgrounds 
In CSS2, you were restricted to use one background image per element. This made it a bit
tricky to apply multiple backgrounds, in that you needed to layer divs and play around with all
that nonsense. However, in CSS3, it’s as simple as separating multiple backgrounds with commas: 

body { 
    background: url(body-top.gif) top left no-repeat, 
    url(body-bottom.gif) bottom left no-repeat, 
    url(body-middle.gif) left repeat-y; 


These three are among the MANY new features in CSS3. If you’re interested in reading more
about the new features in CSS,  

 

Sharfu
2010-02-14 18:02:24 738day

good

 

Mohan
2010-03-02 12:03:18 722day

it's good.but i want with output.

 

Carol
2010-04-21 01:04:38 673day

Great stuff.  I would like a way to be able to test my knowledge while I'm learning.

 

Ramya
2010-05-11 18:05:32 652day

It is good
  We need output also..Try to give some more examples.

 

Karthikeyan
2010-03-31 13:03:52 693day

it's good,if there is some option for us to test our means it will still more better

 

Sahir
2010-06-26 23:06:46 606day

nice yar

 

CHINNI
2010-07-28 18:07:48 574day

Hi Gautam

Your site is very nice. To add the out put of the source code.

 

Karthikraja
2010-11-16 14:11:38 463day

ya, very nice .For W3school is good teacher

 


Add Comments / Scripts
Name:
E-mail:
Comment:
 


Can't read the image? click here to refresh