Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

Guidelines, Rules, and Web Standards

An organization's Web applications can benefit from establishing standards, coding guidelines, and interaction points and references for the way the UI code should be written and styled.

Rules To Code By

These are coding-quality and consistency standards much like those frequently placed on application developers; however, they now extend to their front-end code, which may be a new thing.

  • Any UI code should be built following basic Web standards-based best practices involving the use of POSH, CSS, and unobtrusive JavaScript (as described in Chapters 1, 2, and 3 respectively).
  • Browser independence, accessibility, and graceful degradation are key.
  • Programs should reference UI CSS classes and IDs by pulling them from deep within application logic to reachable points in the code, so that making changes to the presentation information poses minimal risk to the application. These CSS classes and IDs might be superficial properties of object classes, stored in configuration files, or application-level variables, so they can easily be changed later.
  • Avoid inline presentation styles or attributes at all costs.
  • Collaborate on JavaScript applications with front-end coders to share scripts as much as possible and avoid conflicts.
  • Distill the applications to the most simple and semantic markup possible.
  • Create basic, standard CSS rules for forms so that when new ones are added, they can have CSS applied without effort.

By following these guidelines and adopting clean, separated, Web standards-based code, you will ensure that applications and business-critical software won't need significant or risky modifications when a redesign is required.

Unfortunately, these sorts of guidelines can only go so far with software packages, tool kits, and code generated by IDEs or WYSIWYG editors. These tools let authors go only so far to remove inline presentation settings, push the settings into CSS classes, and so on. It may take some effort to examine alternative settings or experiment with one feature over another to get the tools to do what needs to be done. However, there are often simple steps to decouple application logic from backend code.

Better Forms with Modern Markup

Most Web-based applications include some variety of forms. It is not uncommon for Web authors to use an HTML table to obtain a nice layout for these forms.

While there is nothing inherently wrong with this practice, tables are for tabular data. Here there is no tabular data and no reason for the table. There are also presentational attributes such as bgcolor, align, and width, and no accessibility gains from any modern Web standards-based approaches. Additionally, often this form will include some server-side code to populate the values of the form (more on that later).

For example, here is a table being used to display a very simple data form (FIGURE 4.1):

Figure 4.1

Figure 4.1 Forms are typically coded using HTML tables and presentational markup.

<p>Please complete the following form:</p>
<p><b>User Information</b></p>
<form action="submit.php" method="post">
<table width="300" border="0">
<tr>
   <td bgcolor="#cccccc" width="30%" align="right">First Name:</td>
   <td><input type="text" name="txtFName" size="30" /></td>
</tr>
<tr>
   <td bgcolor="#cccccc" align="right">Last Name:</td>
   <td><input type="text" name="txtLName" size="30" /></td>
</tr>
<tr>
   <td bgcolor="#cccccc" align="right">Title:</td>
   <td><input type="text" name="txtTitle" size="30" /></td>
</tr>
<tr>
   <td colspan="2"><input type="submit" value="Go" /></td>
</tr>
</table>
</form>

Consider a newer version of code for essentially the same form (FIGURE 4.2):

<p>Please complete the following form:</p>

<form action="submit.php" method="post">
<div id="formBlock">
   <fieldset>
   <legend>User Information</legend>
   <p>
      <label for="txtFName">First Name:</label>
      <input type="text" id="txtFName" tabindex="1" />
   </p>
   <p>
      <label for="txtLName">Last Name:</label>
      <input type="text" id="txtLName" tabindex="2" />
   </p>
   <p>
      <label for="txtFName">Title:</label>
      <input type="text" id="txtTitle" tabindex="3" />
   </p>
   <p><input type="submit" value="Go" tabindex="4" /></p>
   </fieldset>
</div>
</form>
Figure 4.2

Figure 4.2 Modern markup makes it very easy to code simple forms.

The above can be paired with the following CSS, which could be applied to every form on a site to make them all follow the same setup. While this might seem like extra code, setting up a consistent way that forms are to be marked and styled, external to the program, is a huge benefit.

h1 {
   margin: 0;
   font-weight: normal;
   font-size: 15px;
}
#formBlock {
   width: 300px;
}
#formBlock p {
   margin: 0 0 3px;
}
p {
   width: 100%;
}
label {
   background-color: #ccc;
   display: block;
   float: left;
   width: 28%;
   text-align: right;
   margin-right: 2px;
   padding: 2px 0;
}
input {
   float: left;
   width: 65%;
   margin-bottom: 4px;
   margin-top: 1px;
   padding: 1px;
}
input[type=submit] {
   width: 10%;
}
fieldset {
   border: none;
   padding: 0;
   margin: 0;
}
legend {
   font-weight: bold;
   margin-bottom: 12px;
}

Looking at the new XHTML standards-based approach reveals several fundamental enhancements over forms created without a semantic approach:

  • There is no longer a meaningless HTML table required for the markup of the form.
  • The form now features <label> elements that associate the text label with the actual form control—a great accessibility and, in general, user-centric feature supported by most browsers.
  • A tabindex is applied, to facilitate a tabbing order and increased keyboard accessibility.
  • The form is grouped into a <fieldset> and labeled with a <legend>, which groups and explains the form for greater accessibility.
  • Because it is a much cleaner piece of code, the form itself, which is bound to be tweaked by the application developers, is much easier to read and modify.

Server-Side Frameworks and Template Tools

Several Web scripting technologies have been around for some time, including PHP (PHP: Hypertext Preprocessor), Classic ASP (Active Server Pages), and Adobe (formerly Macromedia) ColdFusion, to name a few popular ones. These language platforms for server-side tasks have models that put front-end alongside backend code in the same files. On one line, authors will see programming logic inside technologies delimiters (examples include <%...%> and the like), and then the following line will have standard HTML. Additionally, the server-side code will use print statements to output HTML, oftentimes with inline presentation information.

There are also frameworks and coding techniques—such as the ColdFusion Fusebox (fusebox.org) framework and the PHP Smarty templating system (http://smarty.php.net)—that modularize these layers, attempting to pull the logic and front end apart, creating template files that include front-end code while the backend code is in different sets of files. In reality, the results are often mixed, because the bottom line is that the server-side code still must output a UI.

The question in the end is this: What is the quality of the markup for the UI even with it separated from business logic? All the application software tiers in the world will not help if the basic HTML code violates best practices or resides in files that the UI developers can't control or wade through.

Simple Steps to Better Server-Side Scripts

All frameworks or templating systems aside, some simple steps can be taken to limit the potential damage even with a barebones PHP or similar environment. In the most basic sense, any application data output or UI code being generated would be subject to the same rules that pertain to UI code that has nothing to do with databases. The only difference is that such code is simply generated, as opposed to coded by hand.

It should be noted that the challenges involved in producing clean separation of server-side business logic and presentation layers are not unique to PHP, as most server-side languages depend on good programming practices and discipline. PHP, "Classic ASP," and ColdFusion in particular share the characteristics of the application logic frequently being embedded into the same files as the front end.

<?php
// Printing results out
echo "<table border=\"1\" width=\"400\">";
while ($row = mysql_fetch_assoc($result)) {
   echo "<tr valign=\"top\">";
   echo "<td bgcolor=\"#ffffcc\"><b>$row['username']</b></td>";
   echo "<td>$row['firstname']</td>";
   echo "<td>$row['lastname']</td>";
   echo "<td><font color=\"grey\">$row['notes']</font></td>";
   echo "</tr>";
}
echo "</table>";
?>

The preceding code does a simple thing in PHP: It iterates through rows in a data set returned from a database query. Most scripting languages like this have similar techniques for outputting database results with looping structures. Obviously this is a small and simple example, but it could be buried inside other looping structures or complex business logic.

There are drawbacks to the above PHP (and to other similar server-side code):

  • Inline presentation elements are buried inside of the looping iteration.
  • Design changes require changes to the application. A programmer may need to be involved, instead of having it be a CSS change external to the application.
  • Any time there needs to be a change to the way the table looks, a programmer must locate the presentation code and make the modification directly inline inside the application logic.
  • The presentation attributes are escaped because of the quoted attributes in HTML, making the code difficult to manage.
  • The presentation aspects must be applied manually to every table on the site.

While simple, this example demonstrates intermingling of code with presentation attributes. Imagine that the results were in nested tables just to create borders and menus, and there were different presentation attributes per column... the code would begin to get quite hairy. A cleaner alternative is:

<style type="text/css">
#results {
   border: 1px solid #000;
   width: 400px;
}
#results td {
   border: 1px inset #000;
   vertical-align: top;
}
td.username {
   background-color: #ffc;
}
td.notes {
   color: gray;
}
</style>
<?php
echo "<table id=\"results\">";
while ($row = mysql_fetch_assoc($result)) {
   echo "<tr>";
   echo "<td class=\"username\">$row['username']</td>";
   echo "<td>$row['firstname']</td>";
   echo "<td>$row['lastname']</td>";
   echo "<td class=\"notes\">$row['notes']</td>";
   echo "</tr>";
}
echo "</table>";
?>

While this shows only the CSS in a <style> block for convenience, it demonstrates that to change the look of the table and the alignment of the table cell content or font settings, not a single line of PHP would need to be touched. A rudimentary example, to be sure, but the point is clear: The more complex the business logic, the more benefit to the application logic in not having inline presentation information.

The Problem

The benefit of technologies such as PHP or even Classic ASP is that the Web programmer has full control over the UI code being produced—an advantage that shouldn't be undervalued. Upkeep and maintenance of an application's user interface can get difficult in a team environment, where it is possible that the UI code was not written by the programmer. In these cases, communication and simple iterations of review are critical. Obviously, these are business procedures as opposed to a coding technique, but these critical processes often do not happen.

The problem is, in today's world there are many newer products and technologies gaining wide acceptance, which make reviews and UI development involvement in the backend phases harder than ever. For complex business applications, scripting languages like those discussed so far have fallen out of favor in some circles because the framework itself doesn't impose layered application architectures. The onus is on the programmers to follow structured design patterns that enforce good programming practices. Older tools such as PHP and ColdFusion were often more accessible to UI designers or developers than some of the newer technologies.

The fundamental problem remains, or has even gotten worse, in most new server-side coding environments. It can apply whether it is inline server-side scripting such as PHP or Classic ASP, or now ASP.NET, which has a layered approach that attempts to separate business logic and backend code. It can also come up where XSLT is being used to generate XHTML or HTML from XML.

So, what is this fundamental problem? While the software engineers were building with more mature and tiered backend to front-end environments such as Java, ASP.NET, or XML/XSLT, the front-end designers and developers don't know the front-end portions of these software environments—and frequently never will.

Typically the front-end portions of these software platforms just output the same bad legacy nonstandard code for the front end that they always have. The challenge is in pushing Web standards into the front end of these applications.

Beware Server-Generated Code

Some more modern application environments have features designed to help remove the "burden" of generating HTML or other UI code from the application developers' plate. The concept is that HTML (or XHTML) can be easily abstracted and then dynamically generated by commands passed into the programming language of the tool kit. These are usually properties assigned to data sets being returned from database queries, or other similar structures that control the dynamic output.

The problem with this is that both the design and the ultimate code that is outputted are largely at the mercy of the writers of the software framework that abstracts and generates the code. Success also depends on the level of effort put in by the programmer to exploit whatever UI features are available. Different frameworks have different levels of quality. Authors will need to "view-source" and actually observe how the markup code is structured when it is output as opposed to just how it looks, because the markup is dynamically generated.

Nothing can replace the human element in most cases. It just takes that extra step of seeing what the code is doing and figuring out how to mold it. Sometimes there are things that can be done; sometimes there aren't. Having made it this far into this book, it should be obvious that getting the front end standards-compliant can be a significant benefit.

  • + Share This
  • 🔖 Save To Your Account