Showing posts with label css tutorials. Show all posts
Showing posts with label css tutorials. Show all posts
Monday, June 13, 2011

4 Types of CSS Hacks for Web Designers and Developers

CSS hacks are always useful for Web Designer and Developers. It become very handy when you are fixing up browser compatibility issues. If you know different types of CSS hacks you can save your time and increase your productivity.

If you are a Web Designer or Developer and looking for a CSS hacks then this post going to help you. In this post I am going to share 4 different types of CSS hacks you can use.

Here's the following list:

Conditional stylesheets


Following are the good example of Conditional stylesheet for Internet Explorer. Conditional comments used easily to specify stylesheets that should only be loaded in IE, or even in specific versions of that browser. Don't worry about Non-IE browsers, it treats conditional comments as HTML comment.

<!--[if for IE 8]><link rel="stylesheet" href="for-ie-8.css"><![endif]-->
<!--[if for IE 7]><link rel="stylesheet" href="for-ie-7.css"><![endif]-->
<!--[if for IE 6]><link rel="stylesheet" href="for-ie-6.css"><![endif]-->

Above CSS hack used differently in specific versions of IE, it would look something like this:

/* Main stylesheet */
.test { color: black; }

/* for-ie-8.css, for IE8 and older */
.test { color: red; }

/* for-ie-7.css, for IE7 and older */
.test { color: white; }

/* for-ie-6.css, for IE6 and older */
.test { color: black; }

Conditional classnames


If you don't want to use CSS hacks, you can apply conditional classnames to the <html> or <body> element. This approach allows you to write clean and hack-free CSS at the cost of adding hacks conditional comments to your HTML.

<!--[if lt IE 7]><html class="ie6"><![endif]-->
<!--[if IE 7]><html class="ie7"><![endif]-->
<!--[if IE 8]><html class="ie8"><![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]-->

This allows you to keep your browser-specific CSS in the same file:

.test { color: black; }
.ie8 .test { color: red; } /* IE8 */
.ie7 .test { color: white; } /* IE7 */
.ie6 .test { color: black; } /*IE6 and IE5*/

CSS hacks


Here’s an overview of the four most popular CSS hacks and which browsers they’re supposed to target:

.test {
color: black;
color: green\9; /* IE8 and older, but there’s more… */
*color: blue; /* IE7 and older */
_color: red; /* IE6 and older */
color: expression('red'); /* IE6 and above */
}

Using conditional classnames with CSS hacks


In the HTML, you can use a minimal version of the conditional classnames technique, like this:

<!--[if lt IE 9]><html class="for-ie8"><![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]-->

You can then use
.for-ie8
as a styling hook in CSS selectors to target IE8 and older versions. Combined with CSS hacks, you can finally target IE8 and older versions without also affecting IE9:

.test {
color: black;
}

.for-ie8 .test {
color: red; /* IE8 and older */
*color: green; /* IE7 and older */
_color: blue; /* IE6 and older */
}

If you have any opinion, please leave a comment or share this post.

Sunday, April 10, 2011

6 Techniques to write better CSS for your next project


Hello my dear readers. I am writing this post after a very long time as I was very busy in my projects so I didn't get time to write. This post is for whom who really want to write CSS style in better and manageable way. So if you are fresher or intermediate level developer you need to read this post very carefully.

In this post I going to explain how you can maintain or manage your CSS with some techniques. If you write your CSS in better way then it will help you to maintain it in future. So here's following tips to improve your CSS writing:

1. Defining Body and H1, H2 etc.

A very first and basic step of CSS to define body of your html doc. See image below how you can define your body and H1, H2, H3 etc. tags.

CSS Tips

2. Declaring Float, Margin, Padding, Border and Clear Both

In this 2nd step you need declare Float, Margin, Border and Clear Both for whole project. Once you declare these attributes alone you don't need write again and again. Look at the image below:

CSS Tips

3. Declaring styles for Links and Images

In this 3rd step now you can define style for your anchor link and image tags. This would help you to use these styles easily in your html document. Have look to below image:

CSS Tips

4. Declaring Typography styles

This is very important step your CSS. In this step you can declare whole typography styles for your html. Here you can define style for h1 to h6 tags and P tag for paragraph styling. Please see reffered image below:

CSS Tips

5. Declaring styles for Layout

In this 5th step you need declare styles for master layout like Header, TopNav, LeftNav or Right Nav and Conent wrapper using ID and classes as per your requirement. Look at image below for reference:

CSS Tips

6. Declaring styles for Footer

In this final step you have to define style for your master footer. You can define footer style by using ID. Please see image below:

CSS Tips

Here's final image of above explained steps for your reference:

CSS Tips

If you have any opinion, please leave a comment or share this post.

Sunday, January 24, 2010

10 Common CSS Browser Compatibility Issues/Bugs You Must Know

When you write a CSS for your project you never know what kind of bug or issue you will face at time of browser compatibility.

Internet Explorer (IE) and Mozilla Firefox are the standard browsers which are globally considerable at time of browser compatiblity. Other browsers such as Chrome, Safari and Opera have a significant presence as well.

In today's scenario browser bugs with CSS can be an incredible source of frustration for Web designers and developers. In this post I'm sharing some most common CSS browser compatibility Issues/Bugs. Here's the following list:

1. How To Attack An Internet Explorer (Win) Display Bug

Great article, tutorial on how to figure out IE bugs, including making sure it isn't a bug first, validating. Covers {position: relative;}, the peekaboo bug, dimensional bug fixing, the expanding box trick, the Holly hack, source code, and more. Easy to understand.

2. The IE/Win Disappearing List-Background Bug

It's another IE bug caused by placing a list with a background set within a floated <div> that has been relatively positioned. Problem and solutions discussed here.

3.IE6 Duplicate Characters Bug

This IE6 bug causes duplicate characters to appear, which is caused by HTML comments. Shouldn't happen, of course, but that's IE6 for ya! See this article/tutorial that explains the bug, how to fix it, and how to avoid it.

4. How to resolve browser compatibility issues while working with JavaScript, AJAX and XML

After going through this article, you will get an idea on resolving browser compatibility issues while working on JavaScript, AJAX and XML. When we run an application which you develop using AJAX and XML it behaves differently in different browsers like mostly in IE (Internet Explorer) and Mozilla Firefox.

5. Double-Margin Bug, Float-Width Bug, Pixel Gap Bug

This article covers CSS bugs such as Double-Margin Bug, Float-Width Bug and Pixel Gap Bug. A very clear and clean solution to reslove these issues.

6. CSS Min-Height Bug

This article describes how you can handle CSS Min-Height in different browsers. CSS: min-height with !important is tricky thing you can use.

7. IE White-Space Bug

There are cases in which IE treats the white space "\n" between and the next as a literal linefeed. That creates extra space between list items.

8. Cross Browser Compatible CSS Opacity

Shortest post ever! You can quickly and easily apply transparency to any supportive element by adding the following CSS code your stylesheet.

You can also look detail article by A List apart

9. Clearing floated images in body text

A problem that I’ve been running into more often in these days of CSS based layouts than I did back in the old table days is the “image-sticking-out-of-the-bottom-of-the-content-area” problem that often occurs when an image is floated left or right, and there isn’t enough text to exceed the height of the image. A problem that sometimes had no easy fix.

10. Firefox CSS Magic

Consider this post an evolving receptacle for Firefox-specific CSS tricks. This article covers: Change the color of highlighted text, Change the opacity of an element, Control item selection of an element, Round the corners of elements, Add an outline to an element, Add a multi-colored, multi-layered border to an element etc.

Recomendation:

Browser Compatibility Development Guide

9 Most Common IE Bugs and How to Fix Them

Do you know any CSS browser compatibility issue/bug, please leave a comment.

Monday, August 24, 2009

Let's make a nice vertical menu with 2kb CSS

In this post I am going to explain you that how to make a nice vertical menu with light weight CSS (i.e. 2kb). This menu is fully compatible with all standard browsers like IE6-7, Mozilla Firefox, Opera and Safari.

Some time back I wrote tutorial about How to Design a Nice Vertical Menu with CSS where I couldn't used working example. Now this time I am going to show you working example.

I have used one image, some lines of html and a 2kb CSS to make this menu. Here's final menu look that we are going to acheive:



Use following html code:

<div id="leftNav"><!--leftNav Start-->
<div class="header">WHAT DO YOU WANT TO DO?</div>
<ul><li ><a href="#">This is task one</a></li>
<li><a href="#">Task two</a></li>
<li><a href="#">Task three</a></li>
<li ><a href="#">Task four</a></li>
<li><a href="#">Task Five</a></li>
<li><a href="#">Task six</a></li>
<li><a href="#">Task seven</a></li>
<li style="border-bottom:none;"><a href="#">This is final result</a></li></ul></div><!--leftNav End-->


Attach following CSS in your page between <head></head> tag:

#leftNav {float: left; width: 231px; border: 1px solid #c9c9c7; margin-bottom: 10px; font-family:Geneva, Arial, Helvetica, sans-serif;}
#leftNav .header{float:left; width:231px; height:20px; padding-top:4px; background:url(Images/leftMenu-header-bg.gif) repeat-x; text-align:center; color:#344f6f; font-size:13px; font-weight:bold; border-bottom:1px solid #a7b4c3;}
#leftNav ul{margin:0px; padding:0px; width:231px;}
#leftNav ul li{margin:0px; padding:0px; font-size:11px; height:24px; width:231px; color:#364e70; border-bottom:1px solid #a7b4c3; float:left; list-style-type:none;background:#cad2df; border-top:1px solid #dbe3ec;}
#leftNav ul li a{ text-decoration:none;padding-top:5px; height:19px; width:226px; text-indent:14px; background:#cad2df; color:#364e70; list-style-type:none; position:absolute;border-left:5px solid #828fa6;}
#leftNav ul li a:hover{text-decoration:underline; font-weight:bold; padding-top:5px; height:19px; width:226px; background:#394a63; color:#cad2df; border-left:5px solid #1c3552;}


Copy following image in your image folder:



Thats all. Thanks.

Do you have any problem to implement, leave a comment.

Sunday, August 9, 2009

8 Good instances of z-index property

If you are new in CSS then you may find difficulty to understand about z-index property. In simple words z-index sets the stack order of specific elements. An element with greater stack order is always in front of another element with lower stack order.

In this post I'm sharing some good examples of z-index property to help upcoming web designers and developers. Here's the following list:

CSS z-index Property

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

z-index

How z-index works!

This page is to demonstrate how elements stack.

z-index

A Detailed Look at the Z-Index CSS Property

Most CSS properties that a web developer deals with regularly are instantaneous in their application to elements on the page.

z-index

OverlappingAndZIndex

There seems to be some confusion about how to get one element to appear on top of another. Do you add z-index: 1 or z-index: 1000? Why do some items overlap others, no matter how high the z-index value goes? This page hopes to clear this up.

z-index

Understanding CSS z-index

Usually HTML pages can be considered two-dimensional, because text, images and other elements are arranged on the page without overlapping. There is a single rendering flow, and all elements are aware of the space taken by others.

z-index

CSS2: z-index

Here is a post I wrote on webmasterworld.com to explain how z-index works. z-index goes hand-in-hand with the local stacking context. What is a stacking context? Whenever you specify a z-index, you create a new stacking context. Here's an example:

z-index

"Z's not dead baby, Z's not dead"

Developers I speak to often associate z-index with Dreamweaver’s layers feature. But in combination with alpha transparency support for PNG images in IE7 and full implementation of position property values, the stacking of elements with z-index is going to be big.

z-index

CSS Z-Index

The z-index property specifies the stack order of an element. In other words, when there is an overlap between two elements, the z-index value determines which one shows up on top. The element with a larger z-index value will appear on top.

z-index

Do you have any opinion, leave a comment.

Sunday, July 26, 2009

Simple CSS tips to styling a Table

Styling a Tables in CSS is complicated by the fact that there are two different models available:

  • Collapsing borders

  • Seperate borders


  • The Collapsing borders allows one border to appear while the Seperate border allows multiple borders. The Seperate border is the default. Suppose we have the table:

    <table title="Membership of W3C" summary="Test Table">
    <caption>W3C Membership</caption>
    <thead>
    <tr>
    <th>Type</th><th>America</th><th>Europe</th><th>Pacific</th><th>Total</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <th>Full</th><td>62</td><td>29</td><td>17</td><td>108</td>
    </tr>
    <tr>
    <th>Affiliate</th><td>240</td><td>123</td><td>47</td><td>410</td>
    </tr>
    <tr>
    <th>Total</th><td>302</td><td>152</td><td>64</td><th>518</th>
    </tr>
    </tbody>
    </table>


    If the style sheet is:

    table {border-collapse:seperate; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;}
    td{border:solid blue 1pt;}
    th{border:solid red 1pt;}


    The result would be:

    TableStyling1

    By changing the styling to:

    table {border-collapse:seperate; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; border:1px dashed green;}
    td{border:solid blue 1pt;}
    th{border:solid red 1pt;}


    The result becomes:

    TableStyling2

    Now lets apply some other CSS styles:

    table {border-collapse:collapse; background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; border:1px dashed green;}
    td{border:solid blue 1pt;}
    th{border:solid red 1pt;}
    colgroup.a{background-color:#FFFF00;}
    tr.three{background-color:#FFFFFF;}
    col.col1{background-color:navajowhite;}
    col.col2{background-color:navajowhite;}
    col.col3{background-color:navajowhite;}
    col.col4{background-color:navajowhite;}
    col.col5{background-color:navajowhite;}
    tbody.second{background-color:powderblue;}
    colgroup.b{background-color:lime;}
    tr.two{background-color:pink;}
    th.grand{background-color:#FFFF00;}


    Here's the table that we have to apply above CSS style:

    <table>
    <colgroup class="a"><col class="col1" /><col class="col2" /><col class="col3" /><col class="col4" /><col class="col5" /></colgroup>
    <thead>
    <tr>
    <th class="grand">Type</th><th class="grand">America</th><th class="grand">Europe</th><th class="grand">Pacific</th><th class="grand">Total</th>
    </tr>
    </thead>
    <tbody class="second">
    <tr class="two">
    <th>Full</th><td>62</td><td>29</td><td>17</td><td>108</td>
    </tr>
    </tbody>
    <tbody class="second">
    <tr>
    <th>Affiliate</th><td>240</td><td>123</td><td>47</td><td>410</td>
    </tr>
    </tbody>
    <tr>
    <th>Total</th><td>302</td><td>152</td><td>64</td><th>518</th>
    </tr>
    </table>


    The result would be:

    TableStyling3

    Applying a CSS styles to a table not easy but having a good knowledge of CSS makes it easy.

    Do you have any opinion, leave a comment.

    Tuesday, July 7, 2009

    15 Good CSS Forums for Web Designers and Developers

    If you are intermediate in CSS and looking to enhance your CSS knowledge then this post going to help you. By joining any of the following CSS forums will help you to boost your CSS knowledge.

    In this post I'm have shared some good CSS Forums that every Web Designers and Developers must join.

    Here's the following CSS forums:

    1. CSS Drive

    CSS Drive

    2. Dynamic Drive

    DynamicDrive

    3. CSS Tricks

    CSS Tricks

    4. WELOVECSS - CSS Forum for everyone

    WeLoveCSS

    5. CSS Forums

    CSSForums

    6. Sitepoint CSS Forum

    Sitepoint CSS Forums

    7. Webmaster World

    WebmasterWorld

    8. CSS Creator

    CSS Creator

    9. forums.devshed.com

    Devshed CSS Forum

    10. BYTES

    Bytes

    11. Webmaster-Talk

    webmaster-----

    12. WebdesignerForum.co.uk

    webdesigner Forum

    13. CSS Chat

    CSS Chat

    14. TalkDev

    TalkDev

    15. TUTORIALFX

    TutorialFX

    Do you have any opinion, leave a comment.

    Tuesday, June 23, 2009

    5 Handy tips to fix space beneath floated elements

    When two elements are floated in a div, chances are parent element no longer contains it because the float is removed from the flow. Here I am sharing five quick tips to fix this problem.

    Tip 1

    Remind the containing block to wrap its children using the overflow property. Example code where #inner is floated:

    <div id="outer">
    <div id="inner"><p>big floating contents</p></div>
    <p>Main Content</p></div>


    Set the outer's style to:

    #outer {

    overflow: auto;
    height: 1%;

    }


    Tip 2

    In the CSS:

    .clearfix:after {content: "."; display: block; height: 0; font-size: 0; clear: both; visibility: hidden;}

    .clearfix {display: inline-block;}
    /* Hides from IE5/Mac \*/
    * html .clearfix {height: 1px;}
    .clearfix {display: block;}
    /* End hide from IE5/Mac */


    XHTML:

    <div class="clearfix">
    <div class="floater">This text won't extend past the bottom of the "clearfix" div.</div>
    </div>


    No non-semantic XHTML. Just some clever CSS rules.

    Tip 3

    <div class="spacer"></div>
    div.spacer {clear: both;}


    This should work, and as DIV has no intrinsic height you can style it to have as much or as little vertical space as you like. But, IE5 and IE6 have several nasty bugs that can appear when a DIV is used to clear floats. They appear only if various conditions are met, so they may or may not occur on your page. Also, Gecko does not render empty divs, so this will not always work. Sticking a non-breaking space in there, or a comment, may help, but not always.

    Tip 4

    br {clear: both;}


    This is safe and reliable, but BR also carries some default behaviour of a certain amount of vertical space that you cannot alter. You can always create classes of BR's, and each could behave differently.

    The following are good styles to apply to a br or div element to make it take up no space when clearing:

    .clearfloat {clear:both; height:0; font-size: 1px; line-height: 0px;}


    Tip 5

    CSS:

    .newBFC {overflow: hidden;
    _overflow: visible;
    _overflow-x:hidden;
    _height: 0;
    }

    /*\*//*/
    .newBFC {display: inline-block;}
    /**/


    XHTML:

    <div class="newBFC">
    <div class="floater">
    This text won't extend past the bottom of the "newBFC" div.
    </div>
    </div>


    This solution creates a new block formatting context in all browsers which should assure a similar display across the board.

    Do you have any opinion, leave a comment.

    Source:http://css-discuss.incutio.com

    Monday, May 25, 2009

    How to Design a Nice Vertical Menu with CSS

    In this tutorial I would like to teach you how to design a Vertical Menu with the help of little CSS. This is very nice Vertical Menu style tutorial for beginners.

    In this Vertical Menu you'll see mouse over effect on sub menus, which is very interesting and you'll always love to learn through out this tutorial.

    Here's a image of Vertical Menu that we are going to learn:



















    For this I am using following two images with css:


    Menu Top bg

    Menu Selected



    I am using following CSS to design a Vertical Menu:


    .main_cont
    {
    float:left;
    width:220px;
    background-color:#4d7986;
    padding:10px;
    }

    .menu_top_bg
    {
    width:223px;
    background:url(../grfx/menu_top_bg.gif) repeat-x;
    height:22px;
    padding-top:8px;
    font-family:Verdana, Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#FFFFFF;
    font-weight:bold;
    text-align:center;
    margin-bottom:1px;
    }

    .sub_menu ul
    {
    padding:0px;
    margin:0px;
    }

    .sub_menu ul li
    {
    font-family:Arial, Helvetica, sans-serif;
    font-size:11px;
    color:#FFFFFF;
    line-height:32px;
    border-bottom:1px dotted #93bcc3;
    list-style-type:none;
    text-indent:8px;
    }

    .sub_menu ul li a
    {
    text-decoration:none;
    color:#FFFFFF;
    }

    .sub_menu ul li a.selected
    {
    background:url(../grfx/menu_selected.gif) no-repeat;
    float:left;
    width:242px;
    height:32px;
    }

    .sub_menu ul li a:hover
    {
    background:url(../grfx/menu_selected.gif) no-repeat;
    float:left;
    width:242px;
    height:32px;
    }



    Use following code to attach above CSS manually to HTML document:

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


    Use following HMTL to get a final result:

    <div class="main_cont">
    <div class="menu_top_bg">TOP HEADER TEXT</div>
    <div class="sub_menu">
    <ul>
    <li><a href="#" class="selected">Menu Text 1</a></li>
    <li><a href="#">Menu Text 2</a></li>
    <li><a href="#">Menu Text 3</a></li>
    <li><a href="#">Menu Text 4</a></li>
    <li><a href="#">Menu Text 5</a></li>
    <li><a href="#">Menu Text 6</a></li>
    </ul>
    </div>
    </div>


    Now you are done. If you find any difficulty to implement above code please leave a comment.

    Saturday, May 9, 2009

    Six Common Mistakes DotNet Guys Always Does

    I have been working with DotNet team from last 2 years. I am responsible for all UI (User Interface) related problems. I have been noticed that my team mates always stuck on the same kind of issues while handling UI. These are very common HTML, CSS and Browser related issues of everyday exercise.

    I feel that DotNet guys don't give such importance to UI part as they give to their controls and functions part. They always keep UI part bottom in their priority list. At last when application going to launch they stuck in UI related issues. Now the solution is just review following mistakes at initial stage:

    1. HTML Tags

    One and only most common mistake is HTML Tags. I agree that application code is very lengthy but forget to closing (</tag>) a tag is big mistake. So keep close HTML Tag at the end of your code. If you have used so many tables in a page then try to handle all tables through CSS.

    2. Table vs Div

    Now the second most common issue is Tables and Divs. Make sure what to use either Table or Div. It always create big problem at time of browser compatibility. So, don't mix-up Table code with Div.

    Example:

    <table>
    <tr>
    <td><div></div></td>
    <td></td>
    </tr>
    </table>


    Using a tabular data in a table is correct but using a non-tabular data in table is semantically wrong.

    3. Understand CSS units of measurement

    I have been noticed some times while fixing UI related issues that DotNet guys never care about units of measurements. Some times they use px (pixel), % (percentage), em, and pt altogether which is not correct. My advice is to review CSS properly before start.

    Read more about CSS units of measurement

    4. Non-Standard GUI Controls

    Basic GUI widgets like command links and buttons, radio buttons and check-boxes, scrollbars, close boxes, and so on — are the lexical units that form dialog designs vocabulary. Changing the appearance or behavior of these units will confuse readers.

    5. Nested Tables

    Nested tables always cause a problem when you are using a so many tables in page. It is very difficult for a UI design to fix nested tables at time of browser compatibility. Use CSS for good HTML.

    6. Violating Design Conventions

    One thing should keep in mind that don't lose consistency because consistency is one of the most powerful usability principles: when things always behave the same, users don't have to worry about what will happen. Instead, they know what will happen based on earlier experience.

    Do you have any suggestions? Leave your comment.

    Thursday, April 30, 2009

    10 Great CSS Selectors you must know

    Selectors define which part(s) of your (X)HTML document will be affected by the declarations you’ve specified. Several types of selectors are available in CSS. Note that some of them are not supported in all browsers.

    Element Selectors

    The most basic of all selectors is the element selector (you may have heard them called tag selectors). It is simply the name of an (X)HTML element, and—not surprisingly—it selects all of those elements in the document. Let’s look at the example:

    h1 {color: blue;}
    h2 {color: green;}


    We’ve used h1 and h2 as selectors. These are element selectors that select h1 and h2 elements within the (X)HTML document, respectively. Each rule indicates that the declarations in the declaration block should be applied to the selected element. So, in the previous example, all h1 elements in the page would be blue and all h2 elements would be green. Simple enough, right?

    Note: Although this book is about using CSS to style (X)HTML documents, CSS can be used for other types of documents as well (notably XML). Therefore, it’s entirely possible that you will run across element selectors that are not valid (X)HTML elements.


    Class Selectors

    So far we’ve been assigning styles exclusively to (X)HTML elements, using element selectors. But there are several other types of selectors, and the class and ID selectors may be next in line as far as usefulness. Modern markup often involves the assigning of classes and IDs to elements. Consider the following:

    <h1 class="warning">Be careful!</h1>
    <p class="warning">Every 108 minutes, the button must be pushed. Do not attempt to use the computer for anything other than pushing the button.</p>


    Here, we’ve specified a class of warning to both the h1 element and the p (paragraph) element. This gives us a hook on which we can hang styles that is independent of the element type. In CSS, class selectors are indicated by a class name preceded by a period (.); for example:

    .warning {color: red; font-weight: bold;}

    This CSS will apply the styles noted in the declaration (a red, bold font) to all elements that have the class name warning. In our markup, both the h1 and the p elements would become red and bold. We can join an element selector with a class selector like this:

    p.warning {color: red; font-weight: bold;}

    This rule will assign the red color and bold weight only to paragraph elements that have been assigned the class warning. It will not apply to other type elements, even if they have the warning class assigned. So, the h1 in our previous markup would be ignored by this style rule, and it would not become red and bold. You can use these rules in combination to save yourself some typing. Take a look at this block of CSS code. We’ve got two style rules, and each has several of the same declarations:

    p.warning {
    color: red;
    font-weight: bold
    font-size: 11px;
    font-family: Arial;
    }
    h1.warning {
    color: red;
    font-weight: bold
    font-size: 24px;
    font-family: Arial;
    }
    A more efficient way to write this is
    .warning {
    color: red;
    font-weight: bold
    font-size: 11px;
    font-family: Arial;
    }
    h1.warning {
    font-size: 24px;
    }


    Class selectors can also be chained together to target elements that have multiple class names. For example:

    <h1 class="warning">Be careful!</h1>
    <p class="warning help">Every 108 minutes, the button must be pushed. Do not attempt to use the computer for anything other than pushing the button.</p>


    <p class="help">The code is 4-8-15-16-23-42.</p>

    A .warning selector will target both the h1 and first p elements, since both have the class value warning. A .help selector will target both p elements (both have a class value of help). A chained selector such as .warning.help will select only the first paragraph, since it is the only element that has both classes (warning and help) assigned to it.

    ID Selectors

    ID selectors are similar to class selectors, but they are prefaced by a pound sign (#) instead of a period. So, to select this div element:

    <div id="main-content">
    <p>This is the main content of the page.</p>
    </div>


    we would need a selector like this:

    #main-content {width: 400px;}

    or this:

    div#main-content {width: 400px;}

    Note: You may ask yourself why you’d ever need to join an element selector with an ID selector, since IDs are valid only once in each (X)HTML document. The answer lies in the fact that a single style sheet can be used over many documents. So, while one document may have a div element with the ID of content, the next might have a paragraph with the same ID. By leaving off the element selector, you can select both of these elements. Alternatively, you can ensure that only one of them is selected by using the element selector in conjunction with your ID selector.


    ID selectors cannot be chained together, since it is invalid to have more than one ID on a given element in (X)HTML. However, it is possible to chain class selectors and ID selectors, such as div#main-content.error.

    Descendant Selectors

    Descendant selectors, sometimes called contextual selectors, allow you to create style rules that are effective only when an element is the descendant of another one. Descendant selectors are indicated by a space between two elements. As an example, you may want to style only li elements that are descendants of ul lists (as opposed, say, to those who are part of ol lists). You’d do so like this:

    ul li { color: blue; }

    This rule will make li text blue—but only when the li is contained within a ul element. So, in the following code, all li elements would be blue:

    <ul>
    <li>Item one</li>
    <li>Item two</li>
    <li>Item three</li>
    <li>Item four has a nested list
    <ol>
    <li>Sub-item one</li>
    <li>Sub-item two</li>
    </ol>
    </li>
    </ul>


    Even though the nested list in item four is an ol element, the blue color will still be applied to its list items because they are the descendants of a ul. Descendant selectors can be useful in targeting items deep in your (X)HTML structure, even when they don’t have an ID or class assigned to them. By stringing together many elements, you can target strong elements inside cite elements inside blockquote elements inside div elements:

    div blockquote cite strong {color: orange;}

    You can combine these with your class and ID selectors to get even more specific. Perhaps we want only li elements in ul elements with a class of ingredients inside our div with the id value recipes:

    div#recipes ul.ingredients li {font-size 10px;}

    As you can imagine, descendant selectors are powerful, and it’s no coincidence that descendant selectors are among the most-used types of CSS selectors.

    Child Selectors

    Child selectors are similar to descendant selectors, but they select only children rather than all ancestors. Child selectors are indicated by a greater-than sign (>).

    Note: Microsoft Internet Explorer 6 and below does not support child selectors.


    Consider our example markup from earlier:

    <ul>
    <li>Item one</li>
    <li>Item two</li>
    <li>Item three</li>
    <li>Item four has a nested list
    <ol>
    <li>Sub-item one</li>
    <li>Sub-item two</li>
    </ol>
    </li>
    </ul>


    Whereas our descendant selector, ul li {color: blue;}, targeted all li elements in this example, a similar child selector would only select the first four li elements, as they are direct children of a ul element:

    ul > li {color: blue}

    It would not target those li elements in item four’s nested ol list.

    Adjacent Sibling Selectors

    Adjacent sibling selectors allow you to target an element that immediately follows—and that has the same parent as—another element.

    Note: Microsoft Internet Explorer 6 and below does not support adjacent sibling selectors.


    The concept of adjacent sibling selectors may sound a bit convoluted at first, but consider this example:

    <body>
    <h1>This is a header</h1>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    </body>


    Paragraphs, by default, have a margin of 1 em above and below them. A common style effect is to remove the top margin of a paragraph when it is immediately after a header. The adjacent sibling selector, which is indicated by a plus sign (+), solves this problem for you:

    h1 + p {margin-top: 0;}

    Although this is incredibly useful in theory, it’s not so useful in the real world. Internet Explorer version 6 and older doesn’t support this selector (Internet Explorer 7 does offer support for it, though). Including it in your styles doesn’t hurt anything—IE will simply ignore it. But those visitors using IE won’t see your adjacent sibling style rules, either. This selector doesn’t do a lot for you in the real world at the time of this writing, but it’s still smart to be aware of it, as someday it will come in handy.

    Attribute Selectors

    This selector, which is indicated by square brackets ([]), allows you to target elements based on their attributes. You can select elements based on the presence of

    • An attribute in an element
    • An exact attribute value within an element
    • A partial attribute value within an element (for example, as a part of a URL)
    • A particular attribute name and value combination (or part thereof) in an element

    Note: Microsoft Internet Explorer 6 and below does not support attribute selectors.


    Particular Attribute Selector

    Perhaps better named the “equal to or starts with” attribute selector, the partial attribute selector—with its pipe-equal (|=) syntax—matches attribute values that either match exactly or begin with the given text. For example:

    img[src|="vacation"] {float: left;}

    would target any image whose src value begins with vacation. It would match vacation/photo1.jpg and vacation1.jpg, but not /vacation/photo1.jpg.

    Attribute selectors, like adjacent sibling selectors, would be more valuable if Internet Explorer 6 and lower supported them (again, they are supported in IE 7). Since it doesn’t, many web developers are forced to admire them from afar.

    Daisy-Chaining Selectors

    It’s important to note that all types of selectors can be combined and chained together. For example, take this style rule:

    #primary-content div {color: orange}

    This code would make for orange-colored text in any div elements that are inside the element with an id value of primary-content. This next rule is a bit more complex:

    #primary-content div.story h1 {font-style: italic}

    This code would italicize the contents of any h1 elements within divs with the class value story inside any elements with an id value of primary-content. Finally, let’s look at an over-the-top example, to show you just how complicated selectors can get:

    #primary-content div.story h1 + ul > li a[href|="http://ourcompany.com"] em { font-weight: bold; }

    This code would boldface all em elements contained in anchors whose href attribute begins with http://ourcompany.com and are descendants of an li element that is a child of a ul element that is an adjacent sibling of an h1 element that is a descendant of a div with the class named story assigned to it inside any element with an id value of primary-content. Seriously. Read it again, and follow along, right to left.

    Grouping Selectors

    You can also group selectors together to avoid writing the same declaration block over and over again. For example, if all your headers are going to be bold and orange, you could do this:

    h1 {
    color: orange; font-weight: bold;
    }
    h2 {
    color: orange; font-weight: bold;
    }
    h3 {
    color: orange; font-weight: bold;
    }
    h4 {
    color: orange; font-weight: bold;
    }
    h5 {
    color: orange; font-weight: bold;
    }
    h6 {
    color: orange; font-weight: bold;
    }


    Or, for more efficiency, you could comma-separate your selectors and attach them all to a single declaration block, like this:

    h1, h2, h3, h4, h5, h6 {color: orange; font-weight: bold;}

    Obviously this is much more efficient to write, and easier to manage later, if you decide you want all your headers green instead of orange.

    Conclusion

    CSS selectors range from simple to complex, and can be incredibly powerful when you begin to understand them fully. The key to writing efficient CSS is taking advantage of the hierarchical structure of (X)HTML documents. This involves getting especially friendly with descendant selectors. If you never become comfortable with the more advanced selectors, you’ll find you write the same style rules over and over again, and that you add way more classes and IDs to your markup than is really necessary.

    Source: Ebook (Pro CSS Techniques) by Jeff Croft, Ian Lloyd, and Dan Rubin

    Tuesday, April 14, 2009

    3 Gifts for User Interface in CSS3

    Box-sizing and box-model

    Box-sizing let’s you change the behavior of the browser in calculating the width of an element. By default, box-sizing is set to content-box. With that set, it calculates width and height as specified by CSS 2.1, adding the border-width and border-height and the padding to the size of the box. By setting it to border-box, you can force the browser to instead render the box with the specified width and height, and add the border and padding inside the box.

    Firefox has this feature prefixed with -moz-, resulting in -moz-box-sizing. Safari 3 / WebKit uses -webkit-box-sizing and Opera just accepts plain box-sizing.

    Browser Support

    Mozilla, Safari, Opera support box sizing proerty but IE doesn't support.

    The resize property in CSS3

    It allows you to specify if a box is resizable. WebKit implemented it in the latest nightlies. Below is an example box, which is resizable if your browser supports resize:

    Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text


    The code for this is very simple:

    div.resize {width: 100px; height: 100px; border: 1px solid; resize: both; overflow: auto;}


    Now it supports resize: both; like the box above, but also resize: horizontal; and resize: vertical;. Here’s the horizontal one, which has a max-width, so you won’t break the layout completely, and a min-width so you can’t make it to small either:

    Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text


    And the vertical one. with a max-height and a min-height:

    Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text Sample text sample text sample text sample text sample text sample text


    Outline

    Outlines have been expanded in CSS3 to include the outline-offset property. This allows the offset to be rendered away from the edge of the specified element. In the example below, the offset is specified as outline-offset: 12px; and the outline as outline: 2px solid blue;

    div.example {width: 150px; height: 150px; border: 1px solid black; outline: 1px solid blue; outline-offset: 12px;}


    Example

    This div has both a black border, and a blue outline, with an offset of 12 pixels.



    This currently works in Opera, Safari and Firefox.



    Source:css3.info

    Monday, March 23, 2009

    Understanding CSS Units of Measure

    In this post I'd like to share some facts about CSS units of measurements for Length units, Percentage units & Color units:

    1. Length Units

    <length> indicates a number followed by a unit of measure:24px.

    The number can be an integer or a decimal fraction, and can be preceded by + or -.

    Units can be absolute or relative:

    Absolute: mm, cm, in, pt, pc (millimeters, centimeters, inches, points, picas)

    Relative: em, ex, px (the element's font height, the element's x-height, pixels)

    Font size may be defined in points, pixels, inches, or centimeters (pt, px, in, cm) or as a percentage.

    <absolute-size> can be:xx-small, x-small, small, medium, large, x-large, xx-large.

    <relative-size> can be:larger, smaller.

    2. Percentage Units

    <percentage> indicates a number followed by a % sign:50%.

    In the text-indent, margin, padding, and width properties, percentage values are relative to the width of the parent element.

    In the font-size property, percentage values are relative to the font size of the parent element.

    In <color> values, percentages can be used to express RGB values.

    3. Color Units

    <color> can represent either <color-name> or <rgb> values, as defined below:

    <color-name> can be: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, yellow

    <rgb> can be:

    #<hex><hex><hex>

    rgb(<number>, <number>, <number>)

    rgb(<percentage>,<percentage>,<percentage>)

    <hex> represents a hexidecimal value, <number> a decimal value, and <percentage> a percentage.

    source:webmonkey.com

    Wednesday, March 18, 2009

    Five CSS Shorthands you always love

    There are many shorthands of writing CSS but I'm going to share which I love the most. Here's the following shorthands of CSS:

    Styles Normal Shortcut
    Font font-size: 1em; line-height: 2.5em; font-weight: bold; font-style: italic; font-family: arial; font: 1em/2.5em bold italic arial;
    Background background-color: #fff; background-image: url(image.gif); background-repeat: no-repeat; background-position: top left; background: #fff url(image.gif) no-repeat top left;
    Lists list-style: #fff; list-style-type: disc; list-style-position: outside; list-style-image: url(image.gif); list-style: disc outside url(image.gif);
    Margin margin-top: 10px; margin-right: 1px; margin-bottom: 15px; margin-left: 4px; margin: 10px 1px 15px 4px (top, right, bottom, left);
    Border border-width: 1px; border-color: black; border-style: solid;
    border-right-width: 1px; border-right-color: black; border-right-style: solid;
    border: 1px black solid;
    border-right: 1px black solid;


    Want to read more about CSS click here

    Monday, March 9, 2009

    10 Most common questions always asked in an interview

    I have worked with almost 7 oragnisations during my career and there's lots of questions asked in interview related to the CSS & HTML. I'm going to share some most common questions that always asked in an interview:

    Qestion 1 - How many types of CSS we use?

    Answer: There are three types of CSS we generally use - External CSS, Embedded CSS and Inline CSS.

    Examples:

    External CSS
    <link href="/css/test.ccs" rel="stylesheet" type="text/css" />

    Embedded CSS
    <style TYPE="text/css">
    <!--
    body {
    background: black;
    color: #80c0c0 }
    a:link { color: #ff8080 }
    a:visited { color: #ff0000 }
    a:active { color: #a05050 }
    -->
    </style>


    Inline CSS
    This can be use with <span> tag:
    <span style="width:50%; height:40px; font:arial 12px"></span>



    Qestion 2 - What is the difference between id and class?

    Answer: IDs identify a specific element and therefore must be unique on the page – you can only use a specific ID once per document. Many browsers do not enforce this rule but it is a basic rule of HTML/XHTML and should be observed.

    Classes mark elements as members of a group and can be used multiple times, so if you want to define a style which will be applied to multiple elements you should use a class instead.



    Qestion 3 - What is the difference between gif and jpeg?

    Answer: JPEG (Joint Photographic Experts Group) is a standardised image compression mechanism. JPEG is designed for compressing either full-colour (24 bit) or grey-scale digital images of "natural" (real-world) scenes.

    GIF (Graphics Interchange Format) It is an 8 bit format which means the maximum number of colours supported by the format is 256.



    Qestion 4 - What is the major difference between CSS2 and CSS3?

    Answer: Click here



    Qestion 5 - What is the major difference Web 2.0 and Web 3.0?

    Answer: Click here



    Qestion 6 - What is the difference between HTML and XHTML?

    Answer: Click here



    Qestion 7 - How to show two events on a single click by using Onclick event in HTML?

    Answer: Click here



    Question 8 - How we can define a new font in CSS?

    Answer: .fontstyle{font-family:"courier new"}



    Question 9 - What do you mean by clear:both in CSS?

    Answer: Image and text elements that appear in another element are called floating elements. The clear property sets the sides of an element where other floating elements are not allowed.



    Question 10 - Why we should use DIVs instead of tables?

    Answer: It is semantically wrong to show non-tabular stuff in a table.

    5 Tips of Defining Vertical Centering With CSS

    There are a multiple ways to defining vertical centering objects with CSS, but it can be difficult to select the easiest one. I’m going to share you all the best ways I’ve seen.

    Vertical centering with CSS is not a every's cup of tea. There are many different ways that may not compatible with some browsers. Let’s review 5 different ways to vertically centering objects:

    Method 1

    This method sets some <div>s to display like a table, so we can use the table’s vertical-align property (which works very differently for other elements).

    <div id="wrapper">
    <div id="cell">
    <div class="content">
    Content goes here
    </div>
    </div>
    </div>


    #wrapper {display:table;}
    #cell {display:table-cell; vertical-align:middle;}


    Method 2

    This method will use an absolutely positioned div, which has the top set to 50% and the top margin set to negative half the height of the content. This means the object must have a fixed height, that is defined by CSS.

    Because it has a fixed height, you may want to set overflow:auto; to the content div, so if there is too much content to fit in, a scrollbar will appear, instead of the content continuing on outside the div!

    <div id="content">
    Content Here
    </div>

    #content {position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */}


    Method 3

    In this method, we will insert a div above the content element. This will be set to height:50%; and margin-bottom:-contentheight;. The content will then clear the float and end up in the middle.

    <div id="floater">
    <div id="content">
    Content here
    </div>
    </div>


    #floater {float:left; height:50%; margin-bottom:-120px;}
    #content {clear:both; height:240px; position:relative;}


    Method 4

    This method uses a position:absolute; div with a fixed width and height. The div is then told to stretch to top:0; bottom:0;. It can’t because of the fixed height, so margin:auto; will make it sit in the middle. This is similar to using the very common margin:0 auto; to horizontally centre block elements.

    <div id="content">
    Content here
    </div>


    #content {position:absolute; top:0; bottom:0; left:0; right:0;
    margin:auto; height:240px; width:70%;}


    Method 5

    This method will only centre a single line of text. Simply set the line-height to the height of the object, and the text sits in the middle

    <div id="content">
    Content here
    </div>


    #content {height:100px; line-height:100px;}


    This method is very useful on small elements, such as to centre the text inside a button or single-line text field.

    Sunday, March 8, 2009

    CSS EXPLAINED: 6 Excellent tips to become a Master in CSS

    I’m going to share that how to write CSS in perfection. This is explained version of CSS found to be very useful. Below is a list of tips to become a master in CSS:

    ORGANIZE WELL

    One of the most important factors in effective style sheet design is effective organization - it is a bad idea to separate styles into different sections: for example, into separate sections for fonts, margins and colors.

    You should organize your style sheet by separating your rulesets into those that specify the formatting of elements (e.g., H1 {color: red}), those that specify the formatting of classes (e.g., P.copyrightnotice {color: red} or .left P {margin-top: 1em}), and those that specify the formatting of ids (e.g., #main {width: 60%}).

    You should then place each section in alphabetical order.

    You will now have a style sheet that looks something like this:

    /* Elements */

    A:link {}

    ADDRESS {}

    BODY {}

    DIV {}

    H1 {}

    H2 {}

    H3 {}

    H4 {}

    LI {}

    OL {}

    P {}

    UL {}

    /* Classes */

    A.offsite:link {}

    P.introduction {}

    /* IDs */

    DIV#body {}


    SHORT IS ADVANTAGE

    Above all else, the most important factor in writing effective style sheets is brevity - try to get rid of the superfluous garbage that clutters up your style sheet.

    Here is an example of what I mean:

    BODY {font-variant: normal;
    font-stretch: none;
    letter-spacing: normal;
    word-spacing: normal;
    text-decoration: none}

    H1 {text-decoration: none;
    font-style: normal}


    This kind of thing is indicative of a bloated style sheet - no style sheet-supporting browser has any text-decoration on any element other than <A>, so text-decoration: none on BODY has no effect whatsoever, and neither does it have an effect on H1. Equally, all the other declarations here are totally useless, and are serving no purpose except to increase download times for viewers of your page.

    Therefore, to improve your site at a stroke, cut all this garbage out of your style sheet, and just watch those download times plummet (and dedicated hosting costs, if you have to pay per gigabyte downloaded).

    COMBINE, COMPRESS AND CONSOLIDATE

    The next thing to do is to try to consolidate your style sheet by considering whether you really want all the styles that you have, and also by combining styles.

    However, it is a bad idea to group elements (as in H1, H2 {...}), since this tends to complicate style sheets. Here's some examples:

    A:active {
    color: #FFFF66;
    background: #9966CC;
    }


    Becomes:

    A:active {color: #FF6; /* Each digit is repeated, so this is the same as FF FF 66 */ background: #96c}


    And:

    BLOCKQUOTE {
    border-style : solid;
    border-width : 1px;
    font-style : italic;
    line-height : 1.58em;
    margin-bottom : .75em;
    margin-left : .58em;
    margin-right : 1.3em;
    margin-top : .75em;
    padding : .3em;
    border-color: #c06565;
    }


    Becomes:

    BLOCKQUOTE {border: 1px solid #c06565;
    font-style: italic;
    line-height : 1.58em;
    margin: .75em 1.3em .75em .58em}


    With:

    H1,H2,H3,H4,H5,H6 {
    font-style : normal;
    line-height : 1.33em;
    margin-bottom : .33em;
    text-decoration : none;
    font-family: "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif
    }

    H1, H2 {
    margin-left: -7%;
    }

    H3, H4, H5 {
    margin-left: -3%;
    }

    H6 {
    margin-left: -1%
    }

    H1, H2, H3, H4, H5, H6 {
    color: #66CCCC;
    }

    H1 {
    font-size : 2em;
    margin-top : 1.33em;
    }

    H2 {
    font-size : 1.75em;
    margin-top : 1.3em;
    }

    H3 {
    font-size : 1.58em;
    margin-top : 1.3em;
    }

    H4 {
    font-size : 1.24em;
    margin-top : 1.24em;
    }

    H5 {
    font-size : 1.17em;
    margin-top : 1.17em;
    }

    H6 {
    font-size : 1em;
    margin-top : 1em;
    }


    Becoming:

    H1 {font: 2em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1.3em 0 .33em -7%;
    color: #6CC}

    H2 {font: 1.75em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1.3em 0 .33em -7%;
    color: #6CC}

    H3 {font: 1.58em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1.3em 0 .33em -3%;
    color: #6CC}

    H4 {font: 1.24em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1.24em 0 .33em -3%;
    color: #6CC}

    H5 {font: 1.17em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1.17em 0 .33em -3%;
    color: #6CC}

    H6 {font: 1em/1.33em "Arial Rounded MT Bold", "Twa TUR", "Trebuchet MS", Arial, Impact, sans-serif;
    margin: 1em 0 .33em -3%;
    color: #6CC}


    The importance of doing this cannot be overstated - it will improve download times, improve maintainability by making it easier to spot bugs, and it will make it easier to use browser detection or browser avoidance techniques because there is less to change.

    STYLE SHEET PERFECTION

    The next thing to consider is that your style sheet should contain all of the declarations below (i.e., each and every block element must have declarations for margin, padding and line height, and CODE, BODY, PRE and CODE also require font size and family), although not necessarily the same values.

    It is vitally important that you do this - if you fail to, it is inevitable that some users will encounter problems when viewing your page (the number of pages that I have had problems with as a result of the authors not following these simple principles grows ever greater).

    If you are designing a style sheet from scratch, you should start with the style sheet below. This will ensure that no conflicts or differences will occur, whatever browser you use. If you are not starting from scratch, simply ensure that your style sheet includes these declarations (not necessarily these values) on each element:

    Base style sheet

    /* Elements */

    /* Fonts - change 'sans-serif' to your preferred font */
    ADDRESS, BLOCKQUOTE, BODY, CAPTION, CENTER, DD, DIR, DIV, DL, DT, FORM, H1, H2, H3, H4, H5, H6, MENU, OL, P, TD, TH, UL {font-size: 1em;
    font-family: sans-serif}

    /* Real styles */

    A {cursor: auto}

    A:link {color: blue;
    background: transparent;
    text-decoration: none}

    A:visited {color: red;
    background: transparent;
    text-decoration: none}

    A:active {color: green;
    background: transparent;
    text-decoration: none}

    A:hover {text-decoration: underline}

    ADDRESS {margin: 0em 0% 0em 0%;
    font: italic 1em/1 sans-serif}

    BIG {font-size: 1em}

    BLOCKQUOTE {margin: 0em 0% 0em 0%;
    line-height: 1}

    BODY {margin: 0em 0% 0em 0%;
    font: 1em/1 sans-serif;
    background: white;
    cursor: default}

    BUTTON {cursor: auto}

    CAPTION {margin: 0em 0% 0em 0%;
    line-height: 1}

    CODE {font-size: 1em;
    font-family: monospace} /* To avoid overriding boldness/italicity by using font */

    DIV {margin: 0em 0% 0em 0%;
    line-height: 1}

    DD {margin: 0em 0% 0em 0%;
    line-height: 1}

    DL {margin: 0em 0% 0em 0%}

    DT {margin: 0em 0% 0em 0%;
    line-height: 1}

    H1 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    H2 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    H3 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    H4 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    H5 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    H6 {margin: 0em 0% 0em 0%;
    font: 1em/1 serif}

    HR {margin: 0em 0% 0em 0%}

    HTML {margin: 0;
    background: white}

    INPUT {cursor: auto}

    KBD {font: 1em monospace}

    LI {margin: 0em 0% 0em 0%;
    line-height: 1}

    OL {margin: 0em 0% 0em 5%;
    list-style: decimal}

    UL UL OL OL, UL OL UL OL, OL UL OL OL, OL OL UL OL, OL UL OL, OL OL {list-style: lower-alpha}

    OL UL OL OL, OL OL UL OL, UL OL OL OL, OL OL OL {list-style: lower-roman}

    OL OL OL OL {list-style: decimal}

    P {margin: 0em 0% 0em 0%;
    line-height: 1}

    PRE {margin: 0em 0% 0em 0%;
    font: 1em/1 monospace}

    SAMP {font: 1em monospace}

    SMALL {font-size: 1em}

    SUB {font-size: 1em}

    SUP {font-size: 1em}

    TD {line-height: 1}

    TEXTAREA {cursor: text}

    TH {font: bold 1em/1 sans-serif}

    TT {font: 1em monospace}

    UL {margin: 0em 0% 0em 5%;
    list-style: disc}

    UL UL, OL OL UL UL, OL UL OL UL, UL OL OL UL, OL UL UL, UL OL UL {list-style: circle}

    OL UL UL UL, UL OL UL UL, UL UL OL UL, UL UL UL {list-style: square}

    UL UL UL UL {list-style: disc}


    Set this up as your style sheet, and test it on your pages using IE 5 or Opera (Netscape 4.* is no use because it is too buggy; Internet Explorer 4 is tolerable, but is buggy on margin %).

    There is no chance whatsoever that this base style sheet will produce what you want, but what it does do is force you to make style decisions for each element - although taking an approach that doesn't specify every style on every element might look o.k. in the browser that you are using, a different browser with different default settings might have it looking differently. The point of it is to make sure everything looks exactly correct when your users come to view the page - if you specify every relevant property, then there is no chance for problems.

    To give an example, I was looking at a page that set margins on BODY but not on H1-6. In my user style sheet, I set margins on BODY and H1-6. The H1-6 had negative margin-lefts set that were designed to fit the margin I had on BODY. Since the page set margins on BODY, my BODY's margins were overridden, but since H1-6 were not set, the result (since the page had much smaller margins on BODY than I did) was that that part of the page's headings were obscured.

    As a result, it is important that you never remove any of the declarations in the template - only alter them. This will ensure that there is consistency whoever is reading your page. It is also important to note that even though you might never use DT (or whatever), you should still keep its ruleset, since if you use it in future, you will then be forced into styling it appropriately.

    In addition, you should not attempt to consolidate rulesets (e.g., by having H1, H2), since this tends to cause confusion. Furthermore, you should not mix classes in with the the element rulesets, but you should have them at the end of the style sheet.

    There are a few points of note about the style sheet itself:

    1. The use of the margin shorthand; although in each case margin: 0em 0% 0em 0% is expressable as margin: 0, I specify it like this to emphasize two things: firstly that you should specify vertical margins in ems and horizontal margins in %, and secondly that each side can be specified. The order is top right bottom left - margin: 0 5% 0 7% gives a left margin of 7%, top and bottom margins of 0, and a right margin of 5%. If you wish, you may change them all to margin: 0, but I find that it acts as a useful reminder.

    2. The font declarations at the top; this should be set to the values you prefer - the declaration is setting the font family and size for the document. It is set like this to avoid the possibility of browsers applying their own defaults on any element.
    The use of the font shorthand; this allows fonts' boldness, variant (i.e., small-caps or not), italicity, size, line height and typeface to be specified.

    3. The typeface and size are both required; for example, font: 16px Arial is valid, but font: 16px is not. If line height is specified, it must follow the size separated by a / - e.g., font: 2em/1 sans-serif, which specifies a font size equal to twice that of the parent element and a line height equal to the font size, in a sans serif font. If bold, small-caps or italic is specified, they must come before the size - font: italic 16px Arial is valid, but font: 16px bold Arial is not.

    4. You should not alter HTML's ruleset - it is likely to cause problems if you do; its official status with regard to backgrounds, etc., is poorly established, and its use is to be avoided.

    Style sheet design checklist

    Here is your style sheet design checklist:

    1. Use shorthand properties where possible.

    2. Avoid grouping elements unless all the elements have exactly the same styles (e.g., avoid H1, H2 {x} H1 {y} H2 {z}., but o.k. is H1, H2 {x}).

    3. Always keep all declarations relating to an individual element in the same place.

    4. Remove all useless declarations (such as font-style: normal).

    5. Always declare margin on every block element.

    6. Evaluate each declaration - does it have any useful effect?

    7. Always use capital letters for the element_name in selectors - P, not p.

    8. Separate into sections - elements, classes and ids, making sure that the elements are first.

    9. Sort each section into alphabetical order.

    10. Try adopting the compact but attractive and easy-to-read layout style of:

    P {declaration: value;
    declaration: value;
    declaration: value}


    Notice how compact this is. This is a definite advantage because it makes style sheets much easier to read, and makes it far easier to spot problems.

    11. Try to always include the type selector when using classes (i.e., P.copyrightnotice, not .copyrightnotice) so that your classes are self-documenting (N.B. this doesn't apply to ids, since they don't work in Netscape or Internet Explorer when it is included).

    12. Always use descriptive identifiers and elements that fit the content - bad is SPAN.pink {color: pink}, good is STRONG.attentiongrabber {font-weight: normal; color: pink}. As a reminder of what each element is used for, here's a list of the inline elements and their uses:

    ABBR - Abbreviations
    Acronym - Marking acronyms - IE 4 & 5 and Mozilla 5 only
    CITE - Marks citations
    Code - An instance of computer code
    Dfn - Definitions
    Em - Emphasis
    KBD - Keyboard input
    Q - An inline quotation
    Samp - Sample output
    Span - Anything not covered by these other elements
    Strong - Strong emphasis
    Var -A variable
    And here's the block elements and their uses:
    ADDRESS - Used for marking up addresses
    BLOCKQUOTE - Block-level quotations
    DD - Definition description; can only be used inside a DL; should follow the term that it is describing
    DIV - Used for making logical divisions for styling purposes
    DL - Definition list - container for DT and DD
    DT - Definition term - term to be defined; can only be used inside a DL
    H1 - Heading, level 1
    H2 - Heading, level 2
    H3 - Heading, level 3
    H4 - Heading, level 4
    H5 - Heading, level 5
    H6 - Heading, level 6
    LI - List item; can only occur inside OL or UL
    OL - Ordered list; signifies that the LI that occur inside it should be numbered
    P - Paragraph
    PRE - Preformatted text (i.e., text in which the spacing is preserved)
    UL - Unordered list; signifies that the LI that occur inside it should not be numbered

    If you have followed this checklist successfully, the most complex style sheet will not exceed 3k for its elements (i.e., excluding classes and ids), and if it does, it is almost always a sign of poor design.

    HTML DESIGN PRINCIPLES

    It is important that your HTML is of reasonably high quality; for example, you should always think in terms of elements, not tags. E.g.:

    <BODY>
    <H1>
    A heading
    </H1>
    The text after the heading.
    <P>
    Some more text
    <P>
    And some more.


    This in an example of bad HTML, since 'The text after the heading' is not part of a P element, and therefore cannot be styled using the P selector. Better would be:

    <BODY>
    <H1>
    A heading
    </H1>
    The text after the heading.
    <P>
    Some more text
    <P>
    Some more text
    <P>
    And some more.


    This is better since every paragraph is now enclosed by a P element. If there is too much space between H1 and P, simply adjust H1's margin-bottom.

    The next major point to consider is how you markup special text.

    You should always do this with an element that is designed for the purpose; failing which, you should use SPAN or DIV with a class. In addition, most importantly of all, you should never use <FONT> tags.

    source:richinstyle.com

     

    Copyright © 2009 - tutorialfeed.org