<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Life on Lars &#187; Coding</title>
	<atom:link href="http://lifeonlars.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifeonlars.com</link>
	<description>Design, the Universe and Everything</description>
	<lastBuildDate>Fri, 26 Aug 2011 01:59:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Ten things you should know about CSS (that I wish I knew when I started)</title>
		<link>http://lifeonlars.com/coding/css-xhtml/ten-things-you-should-know-about-css/</link>
		<comments>http://lifeonlars.com/coding/css-xhtml/ten-things-you-should-know-about-css/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 02:39:45 +0000</pubDate>
		<dc:creator>larfa</dc:creator>
				<category><![CDATA[CSS & XHTML]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.lifeonlars.com/?p=357</guid>
		<description><![CDATA[Over the years I&#8217;ve had quite a few eureka moments when it comes to CSS, when pieces fall into place and I realise a much better way of doing things. It&#8217;s both extremely satisfying but also somewhat annyoing since having that knowledge earlier could have saved me hours and hours of frustration and effort. 1. [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years I&#8217;ve had quite a few eureka moments when it comes to CSS, when pieces fall into place and I realise a much better way of doing things. It&#8217;s both extremely satisfying but also somewhat annyoing since having that knowledge earlier could have saved me hours and hours of frustration and effort.</p>
<p><span id="more-357"></span></p>
<h2>1. Understanding specificity, inheritance and cascading</h2>
<p>Having a clear understanding of which styles will apply in which context will save you a lot of time and headache. I can&#8217;t tell you how many times I&#8217;ve scratched my head trying to figure out why a style wasn&#8217;t being applied correctly to an element despite the fact that it was clearly in the stylesheet.</p>
<h4>Specificity</h4>
<p>Specificity determines which CSS rule is applied by the browser, depending on where a selector fits in specificity hierarchy it will either be ignored or override an less specific selector. When a rule isn&#8217;t being applied by the browser but you think it should then usually specificity is to blame (altough there is the occasional doh moment where you edit or preview the wrong file). Using Firebug or Chrome Code inspector can help you identify whether a style is picked up by the browser and which styles may be overriding it.</p>
<h4>Inhertiance</h3>
<p>When you define styles on an element then these styles will typically be inherited by any elements contained within that element. So if you for example set the &#8220;font-family:Helvetica,Arial,sans-serif&#8221; on the body tag then everything on the page will inherit and use that font unless you override that with a more specific rule.  Not all properties are inherited by default, but you can force inheritance by adding a property-value pair with the inherit value.</p>
<h3>Cascading or &quot;The Cascade&quot;</h3>
<p>The idea of a “cascade” is at core of CSS (Cascading Style Sheets). The Cascade ties in with Specificity and Inheritance and adds the source order into the mix to determine what rules are applied to what elements. So not only does specificity of selectors matter but also the order of the selectors and even the order the style sheets are included when using multiple stylesheets. When it comes to source order the last one to the party gets the last word.</p>
<h4>Additional reading</h4>
<p><a href="http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html" target="_blank">CSS Specificity Wars</a>. Great article explaining Specificity using Star Wars characters and analogies.<br />
<a href="http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/" target="_blank">Smashing Magazine: CSS Specificity &#8211; Things You Should Know</a><br />
<a href="http://coding.smashingmagazine.com/2010/04/07/css-specificity-and-inheritance/" target="_blank"> Smashing Magazine: CSS Specificity and inheritance</a><br />
<a href="http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-understanding-css-specificity/" target="_blank">NetTuts: Quick Tip Understanding CSS Specificity</a><br />
<a href="http://www.w3.org/TR/css3-cascade/" target="_blank">W3 CSS3 Cascade</a></p>
<h2>2. Understanding of how use CSS with the DOM (Document Object Model)</h2>
<p>I still remember the eureka moment I had when I first came to the realisation that I didn&#8217;t have to apply a class to every single element I wanted to style. It was during a practical test in a job interview when I was forced to style a page based on a PSD mock-up, but I wasn&#8217;t allowed to touch any of the existing HTML. For the first time I had to re-think my approach, instead of my then bad habits of slapping a class on pretty much every element, and look at the structure of the HTML document. The hierachy of and structure of the document, what existing ids or classes could I use and how were the elements inside those structured. After the excercise I felt like my eyes had just been opened and I finally understood much more about what CSS was all about. </p>
<p>Instead of creating CSS completely reliant on classes or IDs you can free yourself up to a handful of &#8220;hooks&#8221; on container elements.</p>
<p>For example instead using specific classes for everything</p>
<pre class="brush: css; title: ; notranslate">
.header .heading {}
.content .heading {}
.content .bluetext {}
</pre>
<p>You could use a single class as starting point then refer to all elements of a specific type within that container e.g. specify that all &#8220;h1&#8243;, &#8220;h2&#8243;, &#8220;p&#8221; or &#8220;div&#8221; inside a container should be styled differently. If you add a class to the body of the page or the main wrapper/container to uniquely idenity the page you can also use that to create different styles for individual pages just by adding a single class. </p>
<pre class="brush: css; title: ; notranslate">
.header h1 {}
.container h1 {}
.content div p {}
body.aboutpage h1 {}
</pre>
<h2>3. Understanding the box model, margins and padding </h2>
<p>One of the first things you should come to grips with when starting out with HTML and CSS is the box model.</p>
<blockquote cite="http://www.w3.org/TR/CSS2/box.html"><p>
The CSS box model describes the rectangular boxes that are generated for elements in the document tree and laid out according to the visual formatting model.
</p></blockquote>
<p>The main components of the box model are:</p>
<ul>
<li>Height</li>
<li>Width</li>
<li>Position</li>
<li>Margin</li>
<li>Border</li>
<li>Padding</li>
</ul>
<p>The slightly tricky bit to come to grips with for beginners is the dimensions for a block element in the box model. When you define the width as 200px then you&#8217;d expect that this would be the width. This is true, however if you have a 1px border around the block element then you have to add 1px + 1px, then if you have 10px padding you have to add another 10px + 10px so the total width would be 222px.</p>
<p>Almost everything you do in CSS and HTML will in one way or another be directly or indirectly impacted by the box model so I highly recommend taking some time to read up and understand it properly. It&#8217;ll save you a lot of trouble down the road.</p>
<h4>Additional Reading</h4>
<p><a target="_blank" href="http://css-tricks.com/2841-the-css-box-model/">The CSS Box Model</a><br />
<a target="_blank" href="http://www.w3.org/TR/CSS2/box.html">W3 Box model</a><br />
<a target="_blank" href="http://www.addedbytes.com/for-beginners/the-box-model-for-beginners/">Box model for beginners</a><br />
<a target="_blank" href="http://reference.sitepoint.com/css/boxmodel">Sitepoint: Box Model reference</a></p>
<h2>4. Using CSS floats</h2>
<p>Float is a CSS position property and has been a common way of creating CSS layouts since the move away from table based layouts sometime back in the last millenium. It&#8217;s a powerful but very unintuitive way of doing this that can be hard to get your head around. The concept is similar to that of print layout where you have images set into the page with text wrapping around, but with CSS and HTML there are a lot more uses for it and quite a few pitfalls to look out for.</p>
<ul>
<li>You can float elements left or right (or set it to float: none;)</li>
<li>When floating an element it will automatically be considered a block element</li>
<li>Floated elements will float to the edge or the container elemnt they are in</li>
<li>Floating elements does not trigger hasLayout, this can cause unwanted layout issues with floated elements overflowing their containers</li>
<li>Defining a width or height and setting overflow:hidden or overflow:auto on a container element can overcome the hasLayout and overflow issue</li>
<li>To center floated elements use <strong>margin: 0 auto;</strong></li>
<li>If there is room in the container all floated elements will try to fit side by side on the same &#8220;row&#8221; unless you &#8220;clear&#8221; the floats</li>
<li>Use clear:left, clear:right; or clear:both; to force a floated element to appear on the next &#8220;row&#8221;</li>
</ul>
<p>These are just some of the things to keep in mind when using floats, don&#8217;t worry after a few tries and probably a few more errors you&#8217;ll get the hang of it. </p>
<h4>Additional Reading</h4>
<p><a target="_blank" href="http://css-tricks.com/795-all-about-floats/">All about floats</a><br />
<a target="_blank" href="http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/">Smashing Magazine: Float Theory, things you should know</a><br />
<a target="_blank" href="http://www.alistapart.com/articles/css-floats-101/">A List Apart: CSS Floats 101</a><br />
<a target="_blank" href="http://www.quirksmode.org/css/clearing.html">Clearing Floats</a><br />
<a target="_blank" href="http://css-tricks.com/snippets/css/clear-fix/">CSS Float Clearfix</a></p>
<h2>5. Display:inline-block  </h2>
<p>I&#8217;ll be honest I&#8217;m starting to really love using inline-block over floats for layouts. Sure there are a few issues with it but the workarounds aren&#8217;t that bad and in comparison to using floats for everything it feels so much more elegant.</p>
<h3>Benefits</h3>
<ol>
<li>Easy to center elements both horizontally and vertically. <em>I cannot stress how much easier this can make your life for certain types of layouts</em></li>
<li>You can vertically align blocks top or bottom. <em>Awesome when using blocks with variable content size</em></li>
<li>No overflow issues. Elements containing inline-block elements will trigger hasLayout without any problems.</li>
<li>Possible to use margins, border, padding and all other block element rules.</li>
</ol>
<h3>Downsides</h3>
<ol>
<li>Not supported in IE7 or below. <em>However there is a relatively easy work around of IE7 and a similar but less effective workaround for IE6.</em> </li>
<li>Inline-block elements honor whitespace. <em>Typically this isn&#8217;t a problem but if you want multiple block elements to be side by side you may end up with a 4px spacing between them. You can either use negative margins or remove the whitespace in your HTML code</em></li>
</ol>
<h4>Additional reading</h4>
<p><a href="http://www.sitepoint.com/give-floats-the-flick-in-css-layouts/"  target="_blank">Sitepoint: Give Floats the Flick in CSS Layouts</a><br />
<a href="http://www.ternstyle.us/blog/float-vs-inline-block" target="_blank">Ternstyle: Float vs. Inline-Block</a><br />
<a href="http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/" target="_blank">Cross Browser Inline-Block</a><br />
<a href="http://www.brunildo.org/test/InlineBlockLayout.html" target="_blank">IE7/Win inline-block and hasLayout</a></p>
<h2>6. The value of keeping your CSS properly formatted and well structured</h2>
<p>One often overlooked aspect of CSS is code formatting and naming standards. Just like with PHP or Java having a set of code standars for CSS can help with maintenance, debugging or just make it easier come up with useful names for selectors. When collaborating with other people on CSS, the need for a common standard for naming and structuring your CSS becomes even more apparent.</p>
<p>To be honest it&#8217;s not really that important what coding standard you choose. Whether it&#8217;s one that your company prefers, just personal preference or a standard you find online. The important thing is that there is a a standard and that people that are contributing CSS are aware of it. Even if you&#8217;re the only one writing CSS, if you write and structure your code in a consistent manner it will help you in the long run when to comes to maintaining old code or re-using some CSS from one site to another.</p>
<h4>Additional reading</h4>
<p><a href="http://www.procssor.com" target="_blank">ProCSSor<a> is an advanced CSS Prettifier that lets you format CSS in the exact way you want<br />
<a href="http://codex.wordpress.org/CSS_Coding_Standards" target="_blank">WordPress CSS Coding Standards</a></p>
<h2>7. Go beyond the standard class and ID selectors</h2>
<p>Most of us are familiar with the standard .class and #id selectors in CSS and of course the plain tag selector but it doesn&#8217;t end there. There are :pseudoClasses, descendant selectors, adjacent selectors (x + y), selectors for direct children (x > y), attributes selectors y[attribute] and many many more. Learning a few more (or all of these) will open up for a lot more ways targeting specific areas with CSS rules without having to add additional classes to the HTML.</p>
<h4>Additional reading</h4>
<p><a href="http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/" target="_blank">NetTuts: 30 CSS selectors you must memorize</a></p>
<h2>8. Using CSS shorthand</h2>
<p>CSS supports several different ways or writing the same rules, by using  </p>
<p>The most common CSS short codes are:</p>
<ul>
<li>background: colour url(&#8230;.) no-repeat top left; e.g. background:#ccc;</li>
<li>font: font-style font-variant font-weight font-size/line-height font-family;  e.g. font: normal 0.8em/1.5em verdana, helvetica, sans-serif; </li>
<li>border: 1px solid #ccc;</li>
<li>padding: top right bottom left; e.g. padding:5px 10px 0 10px; or just padding: 5px; = padding: 5px 5px 5px 5px;</li>
<li>margin: top right bottom left; e.g. margin: 10px 0 5px 0; or just margin: 10px = margin: 10px 10px 10px 10px;
</ul>
<p>Personally I don&#8217;t use the font one that often as it&#8217;s a lot stricter than others and harder to read and I don&#8217;t often have to declare all the values for fonts. When do use the font short hand it&#8217;s usually on the body or main font style for the site and then everything else after that is overriding that with more specific rules for font-family, font-size, font-weight etc.</p>
<p>When overriding specificity more often than not you end up using more individual rules e.g. </p>
<ul>
<li>border-top:none;</li>
<li>font-weight:normal</li>
<li>padding-left: 0;</li>
<li>background-color:#eee;</li>
<li>and so on&#8230;</li>
</ul>
<h4>Additional Reading</h4>
<p><a href="http://codex.wordpress.org/CSS_Shorthand" target="_blank">WordPress CSS shorthand</a></p>
<h2>9. Stacking classes</h2>
<p>A HTML element is not restricted to having a single class or ID. You can apply multiple classes to the same HTML element but you can also define multiple selectors for the same CSS rule. Obviously this is a very simplified example but the general idea is that you if you can define a generic class or ID for a type of element and add an extra class for more specific instances of that type that needs to be styled differently. Instead of repeating all the property-value pairs for each rule your element will inherit the styles from the generic class and then add to or override it with the more specific class. </p>
<pre class="brush: css; title: ; notranslate">
.container {
    display:inline-block;
    margin-right:10px;
    padding:10px;
    vertical-align:top;
    width:400px;
}
.container.wide {
    width:960px;
    margin:0;
}
</pre>
<p>Note there is no space between the two class names in the example above. This means that it applies only to elements that have both the container and wide classes applied but it will still inherit all the styling from .container so there&#8217;s no need to repeat those property-value pairs. E.g.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;container wide&quot;&gt;
  content
&lt;/div&gt;
</pre>
<h4>Additional Reading</h4>
<p><a href="http://css-tricks.com/5690-multiple-class-id-selectors/" target="_blank">Multiple Class and ID selectors</a></p>
<h2>10. Using some kind of CSS reset</h2>
<p>The aim of using a CSS reset is to limit or reduce inconsistencies across browsers when it comes to things like default margins, font sizes of headings, line heights etc. Whilst the need for CSS resets is diminishing with modern browsers it&#8217;s still handy to include if your site needs to comply to older browsers.</p>
<pre class="brush: css; title: ; notranslate">
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-weight: inherit;
	font-style: inherit;
	font-size: 100%;
	font-family: inherit;
	vertical-align: baseline;
}
/* remember to define focus styles! */
:focus {
	outline: 0;
}
body {
	line-height: 1;
	color: black;
	background: white;
}
ol, ul {
	list-style: none;
}
/* tables still need 'cellspacing=&quot;0&quot;' in the markup */
table {
	border-collapse: separate;
	border-spacing: 0;
}
caption, th, td {
	text-align: left;
	font-weight: normal;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: &quot;&quot;;
}
blockquote, q {
	quotes: &quot;&quot; &quot;&quot;;
}
</pre>
<p>As important as it is to note which elements are included in the most popular CSS reset available today. It’s also important to note some of the elements that are deliberately excluded from this list:</p>
<ul>
<li>input</li>
<li>button</li>
<li>hr</li>
</ul>
<p>These elements were excluded because their cross-browser differences are so vast that you would have to completely unstyle them to create a &quot;bulletproof&quot; element. They’re so weird, that even then, there’s no guarantee.</p>
<h4>Additional reading</h4>
<p><a href="http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/" target="_blank">CSS Reset Reloaded by Eric Meyer</a><br />
<a href="http://html5boilerplate.com/"  target="_blank">HTML5 Boilerplate is a great starting point for new HTML5 projects.</a> It includes a CSS reset based on the latest reset by Eric Meyer.</p>
<h2>Bonus: IE6 must die</h2>
<p>Internet Explorer is the bane of Web Designers across the world, hell even Microsoft wants it gone. </p>
<p>The Triton rendering engine (Internet   Explorer) has a nifty little feature that allows you to use IE specific HTML to target specific versions of the browser and then load a special style sheet just to deal with those   issues.</p>
<pre>&lt;!--[if IE]&gt;
Target all versions of IE
&lt;![endif]–&gt;    

&lt;!--[if lte IE 7]&gt;
Target all versions of IE that are less than or equal to &quot;7&quot;
&lt;![endif]–&gt;    

&lt;!--[if IE 6]&gt;
Target IE 6
&lt;![endif]–&gt;</pre>
<p>Using conditional comments to target IE and cut out your  hacks, will slim down your main style sheet, and help load the page quicker in browsers that don’t need the correction code. </p>
<p>Some people argue that you should never need to use hacks, workarounds or special stylesheets if you just create standards compliant code. To that I have the following to say:</p>
<ol>
<li>IE6, IE7 and hell even IE8 are not standards compliant browsers.</li>
<li>It is in theory possible to create HTML and CSS that will work in all browsers but to do so you&#8217;d limit yourself to the lowest common denominator and have to use older CSS and HTML. Your site may look the same or at least similar in older browser but it won&#8217;t win any innovation or design awards.</li>
<li>Personally I think keeping a separate stylesheet for IE6 and/or IE7 is a much simpler, cleaner and elegant way of enabling at least basic support for IE without all the bells and whistles of CSS3 but still allowing your site to strut its stuff in modern browsers</li>
</ol>
<p>If you&#8217;re not convinced consider progressive enhancement and graceful degradation techniques to allow your site to take advantage of features in modern browsers but degrade well for older browsers.</p>
<h2>What are your favourite CSS tips or Eureka moments?</h2>
<p>I hope you found this article helpful. If you&#8217;ve had your own aha moments when learning CSS feel free to share them in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://lifeonlars.com/coding/css-xhtml/ten-things-you-should-know-about-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why you should be using semantically correct HTML mark-up</title>
		<link>http://lifeonlars.com/coding/css-xhtml/using-semantically-correct-html/</link>
		<comments>http://lifeonlars.com/coding/css-xhtml/using-semantically-correct-html/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 10:31:52 +0000</pubDate>
		<dc:creator>larfa</dc:creator>
				<category><![CDATA[CSS & XHTML]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Best Practice]]></category>
		<category><![CDATA[Semantic HTML]]></category>
		<category><![CDATA[Web Standards]]></category>

		<guid isPermaLink="false">http://www.lifeonlars.com/?p=141</guid>
		<description><![CDATA[Using correct HTML tags within your markup not only helps with SEO but also promotes web standards and for some gives us a warm fuzzy feeling in our stomach knowing things are the way they were meant to be. But what exactly does semantically correct HTML mean? HTML is a markup language, and as with [...]]]></description>
			<content:encoded><![CDATA[<p>Using correct HTML tags within your markup not only helps with SEO but also promotes web standards and for some gives us a warm fuzzy feeling in our stomach knowing things are the way they were meant to be.</p>
<p>But what exactly does semantically correct HTML mean? HTML is a markup language, and as with other languages, HTML tags and attributes can have a meaning (semantics) attached to them. Typically HTML elements can roughly be categorised into the following:</p>
<p><span id="more-141"></span></p>
<ul>
<li><strong>Semantic elements</strong> (cite, code, em, p, strong etc.); have specific meaning attached to them</li>
<li><strong>Presentational elements</strong> (b, i, font, br etc.); do not have any meaning attached to them and only alter visual presentation</li>
</ul>
<p>When using semantic markup your content can be read and <em>interpreted</em> by software. Why is this important? Humans can easily distinguish elements on a web page purely from their visual attributes, and it&#8217;s normally easy to identify a header or a paragraph on a web page. For software it&#8217;s not that easy, in order for content of a web page to be machine friendly it needs to be contained within the relevant semantic elements. E.g. p for paragraphs, ul for unordered lists, h1, h2 etc. for headings and so on.</p>
<h2>Common Structural Semantic Elements</h2>
<table width="600" cellspacing="0" cellpadding="0" border="0">
<tr>
<th><strong>Element</strong></th>
<th><strong>Usage</strong></th>
</tr>
<tr>
<td>h1, h2, h3, h4, h5, h6</td>
<td>Headings</td>
</tr>
<tr>
<td>p</td>
<td>Paragraphs</td>
</tr>
<tr>
<td>blockquote</td>
<td>Quoted text</td>
</tr>
<tr>
<td>ul, ol</td>
<td>Unordered and ordered lists</td>
</tr>
<tr>
<td>table, th, hr, td</td>
<td>Tabular information</td>
</tr>
<tr>
<td>em, strong</td>
<td>Emphasized words and phrases</td>
</tr>
<tr>
<td>cite</td>
<td>Citations (e.g. book titles)</td>
</tr>
<tr>
<td>abbr, acronym</td>
<td>Abbreviations and acronyms</td>
</tr>
</table>
<h2>Benefits of semantic markup</h2>
<ul>
<li><strong>SEO benefits;</strong> semantic markup makes it easier for search engines to find and index the content on your pages and increases the chances of your site being ranked better for relevant topics.</li>
<li><strong>Accessibility;</strong> your website is more accessible; semantic markup .</li>
<li><strong>Flexibility;</strong> easier to change the layout with CSS, the markup is simpler and easier to handle.</li>
<li><strong>Maintenance;</strong> plain, structured code makes it easier to edit and make changes rather than searching through multiple nested tables.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://lifeonlars.com/coding/css-xhtml/using-semantically-correct-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

