Archive for the ‘CSS’ Category

CSS: Making a Box for a Web Page

Wednesday, July 1st, 2009

I needed to make a box on the right hand of my Web site, to hold some information. It turned out to be pretty easy: I used the CSS Box Model.

First, add the CSS box model specifications in the header of your html document:

<style type=”text/css”>
#box {
width: 200px;
height: 650px;
float: left;
margin: 1px;
border-color: black;
border-style: solid;
border-width: 30 px;
background-color: #F00;
padding: 1em 2em;
}
</style>
</head>

Note: You can also put them in a separate CSS stylesheet, but for simplicity’s sake, we’ll place it in the document this time around.

Then, add in the box where you need it in the HTML part of the document:

<div id=”box”>
Test in the box goes here.
</div>
</body>
</html>

I’ve shamelessly lifted these examples from a fine Tech Republic tutorial.

The full set of box model attributes can be found in W3C’s specification. You can get quite fancy with placement, fonts and whatnot.

Now you’d think you could use “align” to put the box’s “div” tag, right? Wrong! It’s been deprecated. Use “float” and place it in the CSS spec instead.

For a crude example of this in action, see the code for my home page.

–Joab Jackson





HTML: The Difference Between id and class

Thursday, April 30th, 2009

In HTML markup, an id is an attribute you can graft onto a element markup tag, one that identifies that element with a word:

<p id=”sampletest”>This is the sample test</p>

The W3C defines id as the “document-wide unique id.” You can use the name of an id only once, and in a single type of tag, on a Web page. Ryan Fiat writes, “Simply put, an ID is like the sticker that says ‘you are here’ on a mall directory, and there can only ever be one of those.”

Very much like the id, a class can also be grafted onto an element, in a very similar fashion:

<p class=”sampletest”>This is the sample test</p>

The class attribute specifies a classname for an element, says the W3C. You can add your own class to most elements (except base, head, html, meta, param, script, style, and title).

A class can be used to give a name to a particular part of a web page, i.e. <p class=”preamble”>. This can be useful for manipulating that part of the HTML document, through CSS (to stylize that section), or Javascript (to process the content of that section).

Unlike an id, a class can be used across multiple tags. For example, you could have both of these elements in a document:

<h3 class=”legalese”>title</h3>
<div class=”legalese”>content</div>

You could call only one of the classes (i.e. “div.legalese”), or define legalese, and it will apply in both instances. (Click here for working example. View source to see how it works)

ALSO, another cool thing about classes is that more than one can be used in a single element. They are separated by spaces within the quotes:

<p class=”legalese preamble”>content<p>

In this case, if you have CSS stylesheet definitions for both legalese and preamble, then they would both apply to the content above.

In a CSS style sheet, for instance, you could embed these style elements in your document via CSS:

.preamble { font: bold; }
.legalese { font-size: 325%;}

And any content marked with these classes (i.e. <div class=”preamble legalese”>) would be stylized accordingly (Click here for working example. View source to see how it works).


One nice thing you can do with an id that you can’t do with a class: Internal navigation. Anchor linking, it’s called. A browser can use an id as a sort of internal navigation by attributing it to a markup tag of some sort:

<a href=”#content”>This will take you over yonder</a>

<p>hilldale</p>

<p>hilldale</p>

<p>hilldale</p>

<p>hilldale</p>

<p>hilldale</p>

<p id=”content”>Yonder!!</p>

(Click here for working example. View source to see how it works).

–Joab Jackson





CSS: Adding a background color to a Web page

Sunday, February 22nd, 2009

Since, the last time I built a Web page, the rules have changed for how to adding a background color.It used to be the case that you added an attribute onto the body tag. No longer! That is being deprecated….These days, you use Cascading Style Sheets (CSS).

The easiest way to do this is to embed some CSS code directly into the page itself, in the head section.

<Style type=”text/css”>
body {background-color: #FFEBCD}
</style>

Note, the color above is encoded (“#FFEBCD”) in hexadecimal….Go here to get the hexadecimal codes for your favorite colors.

Some of this info was taken from here.

–Joab Jackson