Entry Level PHP Web Application Development/HTML and CSS: Presentation to the User

&larr; Previous (PHP Web Application Development Environments) Next (Blank) &rarr;

HTML and XHTML
HTML and XHTML are content markup languages, and so for Web applications, they are what holds the content. This means that the markup that is given to the Web browser in HTML/XHTML form should look fairly plain when it is presented without any presentation information—that is, Cascading Style Sheets. This chapter focuses on building a simple Web page (without PHP) and showing how CSS can be used to increase the readability of the page while enabling HTML to hold the content.

Basic HTML Page
Let’s take the “Hello, World” example that we first presented to you:

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Hello, World!&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt; Hello, World! &lt;/p&gt; &lt;/body&gt; &lt;/html&gt;

This is a very basic page. In order to make this example a little more useful, let’s add some more markup to it:

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Hello, World!&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello, World!&lt;/h1&gt; &lt;p&gt; Hello, World! This is an &lt;acronym title="HyperText Markup      Language"&gt;HTML&lt;/acronym&gt; paragraph. &lt;/p&gt; &lt;p&gt; I have some things to tell you: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;HTML is the “language of the Web.”&lt;/li&gt; &lt;li&gt;PHP is an excellent server-side language.&lt;/li&gt; &lt;li&gt;CSS makes things look pretty.&lt;/li&gt; &lt;li&gt;&lt;em&gt;You want to make efficient use of your time…&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;strong&gt;So, you want to do things correctly.&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt;

Here is what this HTML looks like in Firefox 3. As you can see, it is rather plain looking:



Cascading Style Sheets are designed to make the job of modifying the look of HTML pages easy. Let’s change this a little bit so that the page looks quite different; the following is to be added directly below the …  line, in the head section:

&lt;style type="text/css"&gt; * {          margin: 0px; padding: 0px; background-color: #99ffff; font-size: 10px; }      body { margin-left: 5px; margin-right: 5px; }      h1 { text-align: center; font-family: "Times New Roman", serif; text-decoration: underline; font-weight: bold; color: #ffffff; margin-top: 5px; margin-bottom: 1em; font-size: 3em; }      p { margin-bottom: 0.4em; border: 1px dotted #000000; background-color: #ffffff; }      acronym { border-top: 1px solid #000000; border-bottom: 1px solid #000000; cursor: help; }      ul { margin-left: 2em; margin-right: 2em; font-family: "Times New Roman", serif; border: 1px dashed #000000; }      li { margin-left: 2em; font-size: 1.3em; }      strong { font-size: inherit; font-family: Arial, sans-serif; }      em { font-size: inherit; font-family: Arial, sans-serif; }    &lt;/style&gt;

What this does is tells the Web browser to decorate the text in various ways, depending on where it is inside of the HTML document. The format of CSS is:

SELECTOR1 SELECTOR2 ... SELECTORn { property1: value; property2: value; ...    propertyn: value; }

Where the selectors tell the browser what we wish to decorate, and the properties and values tell it how we want it decorated. Let’s take a look at the first CSS entry above:

* {          margin: 0px; padding: 0px; background-color: #99ffff; font-size: 10px; }

The selector here is the asterisk (“*”). This says “I want you to apply these formatting rules to everything.” The result is that all elements lose their default margin, padding, background-color, and font-size settings. It acts as a way to reset the Web browser’s default settings. Notice how the measurements are specified—as a number, and then a unit. There are many ways to pass measurements to the web browser. In this case, the measurement we are using is, which are pixels.

The next block of CSS tells the browser to apply some margin settings to the contents of the &lt;body&gt; tag, namely setting a left and right margin:

body { margin-left: 5px; margin-right: 5px; }

Again, we use  as the unit of measurement, stating that we want the left and right margins to be 5 pixels away from the edge of the browser’s viewing area—called the viewport.

CSS all works the same way; for the most part, it is quite readable, as well. Note that this is extremely basic CSS, which simply changes the way things look. Here is what the full HTML document now looks like:

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Hello, World!&lt;/title&gt; &lt;style type="text/css"&gt; * {          margin: 0px; padding: 0px; background-color: #99ffff; font-size: 10px; }      body { margin-left: 5px; margin-right: 5px; }      h1 { text-align: center; font-family: "Times New Roman", serif; text-decoration: underline; font-weight: bold; color: #ffffff; margin-top: 5px; margin-bottom: 1em; font-size: 3em; }      p { margin-bottom: 0.4em; border: 1px dotted #000000; background-color: #ffffff; }      acronym { border-top: 1px solid #000000; border-bottom: 1px solid #000000; cursor: help; }      ul { margin-left: 2em; margin-right: 2em; font-family: "Times New Roman", serif; border: 1px dashed #000000; }      li { margin-left: 2em; font-size: 1.3em; }      strong { font-size: inherit; font-family: Arial, sans-serif; }      em { font-size: inherit; font-family: Arial, sans-serif; }    &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello, World!&lt;/h1&gt; &lt;p&gt; Hello, World! This is an      &lt;acronym title="HyperText Markup Language"&gt;HTML&lt;/acronym&gt; paragraph. &lt;/p&gt; &lt;p&gt; I have some things to tell you: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;HTML is the “language of the Web.”&lt;/li&gt; &lt;li&gt;PHP is an excellent server-side language.&lt;/li&gt; &lt;li&gt;CSS makes things look pretty.&lt;/li&gt; &lt;li&gt;&lt;em&gt;You want to make efficient use of your time…&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;strong&gt;So, you want to do things correctly.&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt;

And in the Web browser:



Okay, so that’s not pretty, but it gets the point across: CSS is a pretty powerful mechanism because it lets you control the formatting, while leaving the content of the page alone.

Embedding CSS vs. Including it as a Resource
In the most recent example, we saw what CSS can do. However, there is one problem with it—we included the CSS directly in the Web page itself. If we have more than one Web page, it makes sense that we would want to be able to write the CSS once, and have those multiple Web pages make use of that same CSS. We could continue to include the CSS directly in the Web pages, but then what happens when we want to alter the appearance of all of our pages? We’d have to edit them all—and that is a lot of work.

One of the most important principles of software development, by the way, is known as Don't Repeat Yourself, or DRY. Having the CSS embedded in each and every HTML page violates this principle if we have several pages.

&larr; Previous (PHP Web Application Development Environments) Next (Blank) &rarr;