Archive for the ‘Beginner’ Category

Semantic uses of <img> HTML tag

Saturday, November 28th, 2009

What is it for?

According to W3C:

“The IMG element embeds an image in the current document at the location of the element’s definition.”

It is pretty clear therefore that we can use <img> tag in order to put images into our web pages.

This tag at one point revolutionised the web, as it brought images to it.

As time has moved on images and video dominate the web and the above simple guideline from W3C is nowhere near enough to tell us how we should be using this tag.

Appropriate uses

For content, not design

<img> tag should be used only for images which are directly related to the content of the given web page.

Any design related imagery, such as background tiling or component wrappers should be coded as part of an external CSS file and included via background CSS property.

Image source locations

Images are files which reside on server of some sort in order to be accessible by the end user.

In recent years it has become commonplace to use CDNs such as Flickr to serve all images within web pages.

There are known advantages and disadvantages to this approach.

The main advantage is that serving images can be much faster, as the CDN will have servers in particular country where the end user is based, hence serving of images will be faster for each user.

The main disadvantage is that images are outside the domain within which the given web site resides, making developers have to rely on the CDN to be up and running for the images to work within web pages.

Another disadvantage is that businesses are often concerned with giving their content away to third parties.

Main concern is that if the given CDN increases price of hosting, it may become more expensive to host images externally than internally.

There is also a big element of faith in hoping that the given CDN will be up and running forever (i.e. what happens if Yahoo goes bust and Flickr stops existing as a service?).

Content manageability

On large scale web sites which use teams of content editors, it is very important to consider those people within the work flow of a web site maintenance and updating processes.

Content editors usually have familiarity with HTML code, but not CSS, hence coding images as HTML backgrounds can sometimes create maintenance problem for non-technical staff.

Sometimes it is better to use <img> tag instead of CSS background in order to give content editors greater control over the content on the web site.

However these should be considered on individual bases through constructive communication and business interests should be taken into account (this usually means making sacrifices towards maintainability by non technical staff over semantics of the page).

Inappropriate uses

For layout

It was common in the past to use various ‘spacer’ images in order to control layout of web pages.

These spacers usually came in the form of a 1×1 pixel GIF image, which was stretched by developers using width and height attributes within the <img> tag.

This practice should be avoided by all means, as it unnecessarily bloats up the web page and creates very hard to maintain pages.

This practice is also bad because it misuses <img> tag for layout purposes rather than to bring good quality content into a web page.

Not using alt attribute

Images have accessibility issues associated with them – blind people and search indexers cannot see them.

This is the reason why there is an alt attribute present within <img> tag.

Sometimes however, in rush or through simple negligence, developers do not use alt attribute appropriate or at all.

alt attribute should always be used and should describe the image in the best way possible.

You should not be using an empty alt attribute simply to satisfy W3C code validator and make your code validate against the guidelines.

Using appropriate alt text is also important from Search Engine Optimisation perspective, as it is usually very relevant to have your web site images show up high in various search engine search results pages.

Semantic uses of <p> HTML tag

Monday, April 13th, 2009

What is it for?

<p> tag is a very nice, semantic tag simply intended for marking up paragraphs of text and that is it’s only purpose.

Appropriate uses

In a code block like this:

<p>This is an article about paragraph tag in HTML.</p>
<p>When we are marking up paragraphs in HTML we should use it for actual paragraphs of text and not for anything else. Paragraphs are only intended to be used for this purpose.</p>

we currently have two equally weighing paragraphs of text in terms of semantics.

However, as an author of this text, I actually intended first paragaph to be a description of the second paragraph.

Semantically speaking, changing the above code to:

<p class="description">This is an article about paragraph tag in HTML.</p>
<p>When we are marking up paragraphs in HTML we should use it for actual paragraphs of text and not for anything else. Paragraphs are only intended to be used for this purpose.</p>

adds a layer of additional semantics to the code giving the first paragraph a little more meaning and implying that it is a description of some sort.

The reason why I used a class instead of an id is because I could have many different descriptions on one page in any given system.

Inappropriate uses

Empty line break

Wrapping text into a <p> tag creates a natural line break after the text and some developers are often tempted to use a <p> tag to wrap a non-paragraph text into it or even use a blank <p> for the purposes of creating a line break.

Using paragraphs instead of a list

Sometimes you will come across a list of some sort being implemented as a series of paragraphs.

Consider the following code block:

<p>Semantics are good.</p>
<p>Semantics help the world.</p>
<p>Semantics are good for Internet.</p>
<p>Semantics create better solutions.</p>

Each one of the points made in this list is not really a paragraph.

A paragraph is usually a longer piece of text, while here we are looking at a list of short points about semantics, which belong to a same ‘group’ of thoughts or points.

The above list of points would therefore much more apropriately be marked up as a <ul> block, an unordered list of points about semantics.

Scope for semantic expression

Monday, April 6th, 2009

It has been a subject of many discussions I have had with various average developers that current technologies do not allow enough room for ‘semantic expression’.

People will complain that we do not have enough HTML tags in order to mark up everything that we need to build on web sites we work on.

People propose new ideas in specifications such as HTML, which are narrowed down too specifically onto web site types (see Web site types), forgetting that the web was built upon much more generic underpinning standards and principles.

Previously I wrote about Pareto Analysis of HTML and Pareto Analysis of CSS, both of which illustrated the fact that most web site types only require around 20% of the available technologies in order to be successfully implemented.

If HTML specifications were insufficient for proper semantic expression, we would be using 100% of HTML tags all the time and would be struggling to express the rest due to short falling of the specification.

In reality, we are often not using, or in worse cases abusing, the specification and standards and therefore are unable to semantically express ourselves properly.

Here are some of the building blocks which, within the mainstream web technologies, allow us to express the meaning of information we are marking up on regular basis.

Meta data

Meta data is one of the least utilised aspects of any web page and one of the most misunderstood ones by everyone, including developers, users and search engines (who have arguably killed meta data purpose even more by surrendering to the fact that developer bad practices were the necessary fact of life).

The purpose of meta data is to describe the purpose and meaning of a given web page as a whole.

If the search engines like Google developed their page rank algorithm in such a way as to reward those systems which have good and properly implemented meta data standards, the web would be of much higher quality.

Meta data implementation is simple and straight forward, as well as supported by good CMS solutions.

With further extensions to meta data standards (see Dublin Core) it would be possible to implement any document exchange mechanism across various web systems, which would be enormously powerful leverage for semantic web.

CSS class names

Class names are also misunderstood by developers.

One of the main cornerstones of Object Oriented development is the notion that a class name becomes a living part of the system.

A reusable piece of code named ‘str2int’ for example, although short and concise isn’t necessarily easy to understand, remember and work with if it becomes widely accepted and reused.

Similarly naming CSS classes as ‘block’, ‘left’, ‘morePadding’ or ‘left_margin’ does not achieve longer term acceptability of name and does not enhance the overall understandability of the solution.

Class names like ‘product’, ‘hCard’, ‘organisation’ or ‘component’ are much more useful and can be made part of any system and reused over and over just as though they were part of the official HTML specification.

This is in line with general best practices of Object Oriented Software Development and makes both machine and human sense, for short, medium and long term.

HTML tags

HTML tags themselves are highly semantic and serve a very good purpose for web documents.

Developers would often abuse the tags, using paragraphs for layout, failing to mark up all lists properly, using <fieldset> for the purposes which <div>s are intended for and so on.

These abuses of specification inevitably lead towards bad semantics, lack of standards compliance and ‘lack of tags’ perception, even though most problems are more than resolvable through utilising proper coding principles.

HTML structure

Sometimes it is, by no means, enough to use one or two HTML tags in order to mark up a more complex piece of data.

In many instances we have situations where a paragraph of text contains a few acronyms which needs to be additionally marked up.

This is what HTML is all about, allowing us to combine atomic level tags into complex data structures.

Creating an XHTML page is essentially no different to creating an XML data structure.

It ought to be completely reusable at all levels and make as much sense as possible in and out of context.

This is the ultimate goal of excellent information architecture at technical and non-technical levels.

URL structure

Often very overlooked aspect of semantic expression is the very notion of the URL structure.

A link like www.flexewebs.com/p.php?id=12 does not mean anything, while www.flexewebs.com/about-us makes much more sense to humans and computers.

URL structures could be heavily standardised (see Cool URIs for the Semantic Web) and could help machines and humans navigate the web easier, faster and better.

URL structures are also incredibly powerful means of creating the overall web system architecture, which Flickr team utilised in order to create their system – they started the overall design from the URL designs.

XML

XHTML is a subset of XML and that’s the main reason why XHTML should be preferred technology choice over XHTML.

XHTML provides systems with a practical ability to offer ‘user interfaces as APIs’, where any system should be able to harvest data from a well constructed XHTML web system and reuse that information elsewhere if needed.

Knowing and understanding best practices in XML is therefore absolutely critical to creating great quality , scalable, semantic web user interfaces, which retain their value over a long period of time.

Content

Last but not least, the very content of the given widget is the cream on the cake of semantic expression.

After we have marked up our widget with all the necessary structural markup, the content which is contains gives the widget the final meaning and purpose.

Writing good and relevant content for web should therefore also be considered as an important part of building semantic web user interfaces.

The content ought make as much sense when taken out of context as well as the code and achieving that gives the final solution the real power.

The editor issue

Sunday, June 1st, 2008

One of the most important tools within web developer’s arsenal is the text editor.

It is a widely recognised and adopted convention among standards compliant, semantic web developers not to use tools which auto-generate HTML or CSS code.

I would strongly recommend to anyone not to use Dreamweaver or FrontPage to develop web pages.

These tools tend to create code which is either one or all of the following:

  1. Non-standards compliant
  2. Unnecessarily bloated
  3. Not cross-browser compliant
  4. Will not be sufficient when it comes to integration into larger systems (with PHP, Java, CFM or .NET back ends)

Use a plain text editor which does not write code automatically for you. It is OK if the text editor you use auto-suggests code to be used, but it is not good if the tool automatically writes code for you during your development process as you will never be 100% sure on whether the code the tool is generating is the code you want in the final web site.

I have been misfortunate enough to use early versions of tools like Dreamweaver and FrontPage and they have left such a sour taste in my mouth that I have (rightly so) made a decision never to go back to them again.

On a trial site I tried to build using Dreamweaver (probably somewhere around year 2001), the tool tended to inject so much unnecessary table related HTML that most of my time was spend taking out the code which the tool was putting in for me (even though I was in editor mode and not in visual design mode).

I got so annoyed with all this that I made a decision only to use straight, simple and free code editors, which only helped me with matters like code colouring, nice indentation, maybe some auto-suggestion and that’s it!

It has been one of the best development and process-related decisions I have ever made as a web developer.

If you are not able to write ‘hand coded’ HTML/CSS, then you really need to learn how to do it.

It is one of the big ways in which professionals are differentiated from amateurs within the industry and this is reflected within good web developer job ads, which will usually specify that ‘hand coding’ is an apsolute requirement.