Archive for the ‘CSS’ Category

How to add a webpage fade-in effect using JQuery

Friday, August 28th, 2009

THE PROBLEM: HTML pages that have numerous and complex nested <div> tags and CSS can sometimes have a jumbled “assembling” appearance that happens as the page is loading and the various elements are being rendered by the browser. The appearance of this process can look quite unrefined and detract from professional appearance of the site.

A SOLUTION: JQuery includes a function to fade-in a hidden tag. If the content of the page is initially hidden using CSS, it then can be faded-in using JQuery once the whole page has been processed and is ready for display.

Step 1

Inside the page’s <body> tag, enclose the content in a <div> tag with a unique CSS id (see this post for the difference between CSS ids and classes).

<body>
 
<div id="content-wrapper">
 
The page content goes here!
 
</div><!-- content-wrapper -->
 
</body>

Step 2

Inside the page’s <head> tag, import the JQuery library (download it here) and create a fade-in effect on the <div> tag surrounding the page’s content. Key to this solution is that the effect gets placed inside JQuery’s $(document).ready() function, ensuring the fade begins after the document’s DOM has been completely parsed.

<script type="text/javascript" src="jquery.min.js"></script>
 
<script type="text/javascript">
$(document).ready(function() 
{
		// fade in content.
		$( '#content-wrapper' ).fadeIn("slow");		
});
</script>

Step 3

At this point the fade-in effect will not occur, because the content is not hidden to begin with. To correct this add some CSS styling underneath the JavaScript above to hide the page content initially.

<style type="text/css">
 
#content-wrapper
{
	display:none;	
}
 
</style>

Step 4

Now the fade-in effect will work, however, a problem exists. If someone visits the page and has JavaScript turned off in their browser, the CSS to hide the content will be applied, but the JQuery to fade it back into visibility will not run, leaving the page effectively blank. This is a serious accessibility issue. To correct this, instead of hardcoding the <div> tag that is wrapping the content, write the tag using JavaScript. That way if JavaScript is turned off, the tag will not be rendered and won’t hide its containing content. Change what was written in Step 1 to the following:

<body>
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '<div id="content-wrapper">' );
//--><!]]>
</script>
 
The page content goes here!
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '</div><!-- content-wrapper -->' );
//--><!]]>
</script>
 
</body>

Putting it all together you end up with this (remember to include the JQuery library in the same folder as this HTML page):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Fade-in effect example</title>
 
<script type="text/javascript" src="jquery.min.js"></script>
 
<script type="text/javascript">
$(document).ready(function() 
{
		// fade in content.
		$( '#content-wrapper' ).fadeIn("slow");		
});
</script>
 
<style type="text/css">
 
#content-wrapper
{
	display:none;	
}
 
</style>
 
</head>
 
<body>
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '<div id="content-wrapper">' );
//--><!]]>
</script>
 
The page content goes here!
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '</div><!-- content-wrapper -->' );
//--><!]]>
</script>
 
</body>
</html>

The difference between IDs and Classes in CSS

Saturday, December 20th, 2008

Cascading Style Sheet (CSS) styles are most commonly applied to elements (paragraphs, links, images, etc.) within a web page by one of two ways: either through ID selectors or class selectors. For example:

<div id="id-selector">style applied with ID<div>
<div class="class-selector">style applied with class<div>

When first learning to use CSS I think it can be confusing on when to use an ID and when to use a class as they seem on the surface to function identically. However, there is a fundamental difference, and an easy to remember one at that:

IDs may only appear once within a web page, while classes may appear multiple times.

Let’s take a closer look at the differences:

ID selector

  • Designated by a hash (#) preceding the style name.
    For example, #id-selector{font-size:12px;}.
  • Can only appear once within a web page.
    While this is not enforced by most, if not all, major web browsers it does not mean you should disregard this rule, as it potentially will cause problems if you decide to add JavaScript in the future. Your page will not pass validation by W3.org either.
  • Can only appear once within an element on a web page.
    For example, <div id="id-selector">.
  • Takes precedence over styles applied with a class.
    Web page elements can have a mix of ID and class supplied styles, for example, <div id="id-selector" class="class-selector">, however the styles declared in the ID selector will always take precedence over any conflicting styles declared in the class selector, unless the !important declaration follows the style declaration in the class. For example,
    #class-selector{font-size:18px !important;}.
  • Is used by JavaScript to gain access to an element.
    Using the getElementById method allows elements on a page to be accessed and manipulated via JavaScript.
  • Allows the browser to scroll to a particular element on the page if the ID name is appended to the end of the URL.
    For example, http://www.somedomain.com/yourpage.html#id-selector.

Class selector

  • Designated by a period (.) preceding the style name.
    For example, .class-selector{font-size:12px;}.
  • Can appear more than once within a web page.
    Unlike IDs, which should only appear once, classes can be applied to multiple elements on a web page.
  • Can appear more than once within an element on a web page.
    Providing a space separated list of class selectors within the class attribute of an HTML element allows multiple class styles to be applied at once. For example,
    <div class="class-selector another-class-selector">.

The following is a short example demonstrating all the above rules I have listed for ID and class selectors:

<style type="text/css">
 
#page-content
{
	width:300px;
	margin:10px;
	padding:10px;
	border:1px solid #00ff00;
	font-size:11px;
}
 
.page-element
{
	border:1px solid #ff0000;
}
 
.augmented-element
{
	border-top:none !important;
	border-bottom:none;
}
 
</style>
 
<div id="page-content" class="augmented-element">
	<div class="page-element">First</div>
	<div class="page-element augmented-element">Second</div>
	<div class="page-element">Third</div>	
</div>

Viewing this code in a web browser should render the following:

First
Second
Third

2 Essential FireFox plug-ins for CSS development

Saturday, September 13th, 2008

FireBug
Firebug allows the live editing of CSS, HTML, and JavaScript of any website. For tweaking the appearance of a webpage, this tool is indispensable for visually seeing the results of your changes to the source code of a webpage. The changes are made locally and are lost when the page is reloaded.

Dust-Me Selectors A thing that bothers me with CSS development is the feeling that there is a precarious ledge over which you are hanging, and as development progresses you are one step away from having a stylesheet that has become unmanageably disorganized and convoluted. This is particularly apparent when multiple people have a hand in editing the appearance of a page, or when you are creating a new stylesheet from an existing one. Dust-Me Selectors is a useful tool for finding CSS selectors that are no longer being used on a page. Additionally, it can spider a whole site to find the CSS selectors that are unused site-wide. The line number and stylesheet name of the unused selectors are provided, so that they can be manually removed. Clean and tidy is a wonderful thing!

The thing I miss about both these tools is the ability to apply the edits directly to the source code of the pages you are working on. A lot of time can be consumed manually applying the changes these tools find, but it is still quicker than making changes without these tools.

What other tools do you use?