ascading Style Sheets (CSS) are a powerful way to separate content from design in your Web Forms applications. Effective use of CSS is an easy way to maintain and consolidate the visual aspects of Web development. Cascading Style Sheets are a Web standard that have been in existence for a number of years. Most modern browsers support CSS, so their use in your .NET applications should pose no cross-browser compatibility issues. However, like most things in the Web world, various browsers may behave differently depending on the CSS you feed it.
In this article, you will learn about the mechanics and the practical uses of CSS that you will find valuable as a .NET developer. This is your first step to creating and designing sites using Web CSS standards.
Structure, Design, and Behavior
In .NET, Web Forms are comprised of three elements: structure, design, and behavior. Isolating each element reduces coupling and increases maintainability and clarity in your code.
A Web Form's structure is the actual HTML and WebControls markup of an ASPX page. As most programmers know, an HTML page must include certain elements and must include those elements in a certain order. This snippet shows an example of the structure of an HTML page.
This HTML markup must be structured as you see it. You could not put a <p>
tag outside of the <body>
, for example. Ideally, the only markups in your ASPX page at development time are HTML elements, user controls, and/or server controls.
Traditionally, developers have embedded design and layout in the HTML/ASPX pages in the form of tables, font tags, and other hard-coded elements. Removing all visual layout, color, positioning, and other aesthetic control to the style sheet reduces your structure code and gives you a single point-of-contact to update your site.
An example might be, instead of doing this:
You might simply use this:
Then, in the Style Sheet, you define <td>
to be a certain color. If you wish to make a change across all your <td>
tags, you only have to do so in one place.
To effectively use Cascading Style Sheets, you must keep in mind how cascading works within the browser. You might liken cascading to object inheritance. When you define a style up the chain, lower-defined styles may override values of the base style. A Style Sheet's power is unleashed when you set up the right type of cascade.
|Web Forms are composed of three elements: structure, design, and behavior. Isolating each element reduces coupling and increases maintainability and clarity in your code.|
Here is the order in which the styles are applied to elements on a page:
- Included files
- Head items (within the <style> tag)
- Inline styles
Included files are when you reference an external style in the <head>
tag using a <link>
tag, as shown in this next snippet.
Head items are style definitions found in the <style>
tag within the <title>
tag, as shown in the next snippet.
Inline styles are style definitions applied directly to an element via the style attribute. These are not recommended for use, as they are hard to maintain.
Application of Styles
You may apply styles to your document and elements in any of three ways: by element, by ID, or by class.
Often, you need to apply a style throughout your site to an existing HTML element. Perhaps you want all of your paragraphs to appear in the Arial font, have wide line spacing, and appear as black text. To style all the paragraphs on every page of your site without writing extra markup, associate the appropriate style to your paragraph tags (<p>
). The next snippet shows what you put into your Style Sheet so you can link that Style Sheet to all your pages.
|As your site grows, split up your styles into multiple files.|
Notice the beginning of this code block: the statement opens with the name of the HTML tag with no preceding characters. This is how you access elements within the HTML structure.
Every element in an HTML page may have an ID associated to it using the id
attribute. An element ID is a unique identifier for each element on the page. The IDs allow technologies like CSS and .NET to distinctly locate an element on a page. Setting aside .NET development for a moment, element IDs are only required if you choose to create a style that is specific for a certain element. The next code snippet demonstrates how to apply a style to an element on the page using an ID.
|Figure 1. ID Styles: An ID style allows you to target a specific named control on a page.|
<div id="divContainer">Styled Container</div>
Notice the opening of this code block. The statement begins with a pound sign (#).
The pound sign indicates to the browser that the following style is applied to an element on the page and is associated by its ID attribute value. Figure 1
shows the result of this HTML.
Although a style applied to an element by ID is only available to one element on the page, class styles may be reused over and over again. Class styles may be defined and applied to like or disparate types of elements on the page. To illustrate how you might use a class style, create two containers on a page that hold other controls. Each container should show up 25 pixels away from the left border of the browser, as shown in Figure 2
|Figure 2. Class Styles: Use class styles to set a standard left indentation on controls.|
The code to generate this Web page is shown below.
border:dashed 3px #000;
border:solid 1px #000;
<div class="container" id="divContainer1">
<div class="container" id="divContainer2">
Notice the opening of this code block. The statement begins with a period. The period or dot indicates to the browser that the following style is applied to an element on the page and is associated by its class attribute value.