Reply
 
Thread Tools
Old 09-19-2005, 11:00 PM   #1
Alex
Senior Member
 
Alex's Avatar
 
Join Date: Sep 2005
Posts: 204
CSS Semantics & Optimisation
Part One: Semantics

Semantics mark the difference between an expert coder and a talented amateur, while they can both make the same webpage you will find that the professional page has clearly indented code with a fluid style and clear progression from section to section, without semantics you get a jumble of indecipherable code that no one but the author can comprehend.

A large part of semantics is learning how to indent, this seperates areas of code and defines layers on which you can build. While you can make code like this:

Code:
html {padding: 0 0 0 2px;background: #dadada url(images/bg.gif) repeat-y center;text-align: center;font: 70% tahoma, arial, verdana, sans-serif;color: #000;}
That is not useful at all to someone trying to understand the code, by laying it out like this:

Code:
html { 
padding: 0 0 0 2px; 
background: #dadada url(images/bg.gif) repeat-y center; 
text-align: center; 
font: 70% tahoma, arial, verdana, sans-serif; 
color: #000; 
}
The second method is far clearer to an observer, each attribute has its own line and is instantly recognisable and the element it is describing frames the lot never going on the same line to allow it to look like a box, a method that helps the viewer to comprehend the code no end.

The single '}' closing the element may seem a waste but in coding tems it is an elegant closer. When elements are placed together it leaves an almost empty line that makes the next element stand out to make it easier to identify the elements down the page.

In one way Semantics and Optimisation go hand in hand and that is in that the lines must be compacted into it's parents where there is more than one line of the same type, taking the same example this same code can be shown in two ways, the uncompacted:

Code:
html { 
padding-left: 2px; 
padding-right: 0; 
padding-top: 0; 
padding-bottom: 0; 
background-color: #dadada; 
background-image: url(images/bg.gif); 
background-repeat: repeat-y; 
background-position: center;
text-align: center; 
font-size: 70%; 
font-family: tahoma, arial, verdana, sans-serif; 
color: #000; 
}
and the aforementioned compacted:

Code:
html { 
padding: 0 0 0 2px; 
background: #dadada url(images/bg.gif) repeat-y center; 
text-align: center; 
font: 70% tahoma, arial, verdana, sans-serif; 
color: #000; 
}
The second of these is MUCH easier to read since the imformation for each CSS describer is on one line and can be more easily grouped and identified, if they are on seperate lines as they are in the first example a reader must read each line and then once they have found all the relevant ones piece them together, in the second this is done already.

On the optimisation side it also uses much less space, a whole stylesheet which is uncompacted can be two or three times larger just by a lack of semantics! Another common semantics mistake is the missuse of id="" and class="" some using one exclusively and some using both at inappropriate times. id="" (#div in the CSS) should ALWAYS be used if there is only one of that div, whereas class="" (div.div in the CSS) should ALWAYS be used if there is more than one of that CSS definition in the page. Where in doubt it is better to use class="" but in most cases you will already know which ones are to be used once only.

Headings are an important part of semantics, ALWAYS have h1 before h2 as you would anywhere else, the headers help to lay out the page and should be in a logical pattern to help readers follow the page.




Part Two: Optimisation

The other distinguishing mark of a professional is the ability to make a webpage with the least code possible, you can make a site with 200 divs and a huge CSS document, but often you can make the same layout with four or five divs and a tiny CSS document. This is the importance of OPTIMISATION, this means using only what you have to and nothing more. A good way to optimise is to find two elements with the same details for example these:

HTML Code:
div.topbox { 
background: url(images/topbox.jpg) repeat-y; 
width: 722px; 
height: 6px; 
} 
#topbox_top { 
background: url(images/top.jpg) no-repeat;  
width: 722px; 
height: 6px; 
}  
<div class="topbox">   
<p>    
Text   
</p>
</div>  
<div id="topbox_top">   
<p>    
Text   
</p>
</div>
Instead of having these as they are you can instead remove all but background from #topbox_top and instead have it as:

HTML Code:
div.topbox { 
background: url(images/topbox.jpg) repeat-y; 
width: 722px; 
height: 6px; 
}  
#topbox_top { 
background: #000; 
}  
<div class="topbox">   
<p>    
Text   
</p>
</div>  
<div class="topbox" id="topbox_top">   
<p>    
Text   
</p>
</div>
While in this case that only removes a few bytes in large CSS documents this can cut huge chunks off through clever use of overriding styles. Beyond this the best way to learn is to practice, learn from others and remember to validate your code always

Alex Elliott (http://alex-elliott.com/)
Alex is offline   Reply With Quote
Old 09-19-2005, 11:42 PM   #2
Terminator1138
Server Captain
 
Terminator1138's Avatar
 
Join Date: Mar 2005
Location: 40.85 Lat. 96.75 Long.
Posts: 5,318
Nice tutorial there..
Terminator1138 is offline   Reply With Quote
Old 09-20-2005, 02:40 PM   #3
Forest
Super Moderator
 
Forest's Avatar
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 687
Sorry, I have some questions. It seems to me that a critical attitude is required when talking about this semantics stuff. so:

1) Isn't semantics all about using the correct html tag for a the correct part of your content? Meaning < hn > for headings < p > for paragraphs < li > for lists, < table > for tables? etc? About asking yourself howto encode an address when it is not that of the site owner? About whether to use < abbr > or < acronym > ?

If I am correct then I do not see what css has to do with semantics.At least not in the same manner as (x)html can be linked to this subject.

In my mind what you are talking about is just good coding practices. Which is obviously important. I agree with all you tips and would like to add one:
- always code css properties in the same order. It helps. (perhaps always margin first then padding?)

2) Your example of clean coding. I would do it like this:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en">
<head>
<title>Coding</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<style type="text/css" media="screen">
.box {
width: 722px; 
background: #ccc;
color: white; 
}  
.top { 
background: #000; 
}  
</style>
</head>
<body>
<h1>heading</h1>   
<p class="box">Some text.</p>
<p class="top box">More text</p>
</body>
</html>
What do you think?
__________________
"The reasonable man adapts himself to the world. The unreasonable man persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - G.S. Shaw

Last edited by Forest : 09-20-2005 at 03:10 PM.
Forest is offline   Reply With Quote
Old 09-20-2005, 02:47 PM   #4
Terminator1138
Server Captain
 
Terminator1138's Avatar
 
Join Date: Mar 2005
Location: 40.85 Lat. 96.75 Long.
Posts: 5,318
In general, semantics (from the Greek semantikos, or "significant meaning," derived from sema, sign) is the study of meaning, in some sense of that term. Semantics is often opposed to syntax, in which case the former pertains to what something means while the latter pertains to the formal structure/patterns in which something is expressed (e.g. written or spoken).


soo...my thought is
semantics is the structure and syntax is the meaning....am I wrong?

Last edited by Terminator1138 : 09-20-2005 at 02:51 PM.
Terminator1138 is offline   Reply With Quote
Old 09-20-2005, 03:08 PM   #5
Forest
Super Moderator
 
Forest's Avatar
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 687
In which case... I would be wrong. I need to research
__________________
"The reasonable man adapts himself to the world. The unreasonable man persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - G.S. Shaw
Forest is offline   Reply With Quote
Old 09-20-2005, 03:09 PM   #6
Alex
Senior Member
 
Alex's Avatar
 
Join Date: Sep 2005
Posts: 204
1. Well I take a slightly looser definition. I mean to say that the structure of the code allows for the meaning to be expressed in better terms. The structure of the second example as opposed to the first gives it a clear and defined structural meaning whereas the first has no obvious structural meaning and muse be worked out in your mind after the meaning of the text has been formulated.

Meh basically I agree with Term but on a looser basis

2. Thank you, I had not seen that before to be honest with you! :P
Alex is offline   Reply With Quote
Old 09-20-2005, 03:19 PM   #7
Forest
Super Moderator
 
Forest's Avatar
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 687
Originally Posted by dictionary
Semantics | Sem*an"tics |
n. sing. or pl. Gr. shmantikos having
meaning, from sh^ma a sign.

1. the study of the meanings of words and of the sense
development of words; -- formerly called semasiology.
PJC

2. a doctrine and philosophical approach to language and its
relationship to thought and behavior, developed by Alfred
Korzybski (1879-1950), which holds that the capacity to
express ideas and thereby improve one's interaction with
others and one's environment is enhanced by training in
the more critical use of words and other symbols; -- also
called general semantics.
PJC

3. the meanings of words as they are used to achieve an
effect; especially, the multiple meanings of words or the
multiplicity of words having the same meaning; -- used in
referring to the confusion that can be caused
(intentionally or unintentionally) by multiple meanings;
as, there's no real difference, it's only a matter of
semantics.
PJC
I do not believe I was all that wrong...

@PD: what did you notice?
__________________
"The reasonable man adapts himself to the world. The unreasonable man persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - G.S. Shaw
Forest is offline   Reply With Quote
Old 09-20-2005, 03:21 PM   #8
Alex
Senior Member
 
Alex's Avatar
 
Join Date: Sep 2005
Posts: 204
Originally Posted by Forest
@PD: what did you notice?
huh???
Alex is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 05:43 AM.


Design by Vjacheslav Trushkin, color scheme by ColorizeIt!.
Powered by vBulletin Version 3.6.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Abused Productions and Blue Valley Design
[Output: 81.69 Kb. compressed to 76.87 Kb. by saving 4.83 Kb. (5.91%)]