<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Impressive WebsCSS Basics</title>
	
	<link>http://www.impressivewebs.com</link>
	<description>Web Design Articles &amp; Tutorials. To make the web impressive.</description>
	<lastBuildDate>Wed, 23 May 2012 10:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ImpressiveWebs-cssBasics" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="impressivewebs-cssbasics" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">ImpressiveWebs-cssBasics</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>CSS Selectors Defined</title>
		<link>http://www.impressivewebs.com/css-selectors/</link>
		<comments>http://www.impressivewebs.com/css-selectors/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 10:00:36 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=6301</guid>
		<description><![CDATA[If you're just getting started with CSS, it's good to have a fundamental understanding of what we mean when we refer to <strong>CSS selectors</strong>. In this post I'll briefly describe all the most well-known CSS selectors along with some examples.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re just getting started with CSS, it&#8217;s good to have a fundamental understanding of what we mean when we refer to <strong>CSS selectors</strong>. In this post I&#8217;ll briefly describe all the most well-known CSS selectors along with some examples.</p>
<p>A &#8220;selector&#8221; is the instruction in a CSS rule set that tells the browser what elements to &#8216;select&#8217; for styling. Look at the two examples below:</p>
<pre name="code" class="css">
.box {
	background-color: red;
}

form textarea {
	background-color: blue;
}
</pre>
<p>In each of those examples, the selector is the part of the rule set that appears before the opening curly brace. &#8220;.box&#8221; is the first selector and &#8220;form textarea&#8221; is the second selector.</p>
<h2>Selectors Defined with Examples</h2>
<p>Here is a brief definition of each type of selector, with examples:</p>
<p><strong>Universal Selector</strong><br />
This selector consists of the asterisk character, like this:</p>
<pre name="code" class="css">
* {
	background-color: red;
}
</pre>
<p>When used on its own like above, this selects every element on the page.</p>
<p><strong>Element Type Selector</strong><br />
Also called just the &#8220;type selector&#8221;, this selector matches HTML elements by tag name. Two examples:</p>
<pre name="code" class="css">
h2 {
	background-color: red;
}

div {
	background-color: red;
}
</pre>
<p><strong>Class Selector</strong><br />
This selects an element that matches a class name defined in a class attribute in the HTML.</p>
<pre name="code" class="css">
.element {
	background-color: red;
}
</pre>
<p>This is easily my favourite selector, and all good CSS developers should use it abundantly. You can put multiple classes separated by spaces on a single class attribute, which makes this selector quite powerful.</p>
<p><strong>ID Selector</strong><br />
This selects an element that matches an id defined in an id attribute in the HTML.</p>
<pre name="code" class="css">
#element{
	background-color: red;
}
</pre>
<p><strong>Descendant Selector</strong><br />
This selector is defined with a space character separating two selectors, and represents a child element, but not just immediate children, further nested ones as well.</p>
<pre name="code" class="css">
.parent .child {
	background-color: red;
}
</pre>
<p><strong>Attribute Selector</strong><br />
This selector targets an element based on an HTML attribute and/or attribute value. Both examples below are valid attribute selectors:</p>
<pre name="code" class="css">
div[style] {
	background-color: red;
}

input[type="text"] {
	background-color: red;
}
</pre>
<p>The first example targets any <code>&lt;div&gt;</code> element that has a &#8220;style&#8221; attribute. The second example targets any <code>&lt;input&gt;</code> element that has a &#8220;type&#8221; attribute with a value of &#8220;text&#8221;.</p>
<p><strong>Child Selector</strong><br />
This selects an element based on it being an immediate child of another element:</p>
<pre name="code" class="css">
.one > .two {
	background-color: red;
}
</pre>
<p>So this will not style a &#8220;.two&#8221; element unless it is an immediate child of a &#8220;.one&#8221; element. It can&#8217;t be nested inside another element, it has to be an immediate child element. And only the child is styled, not the parent.</p>
<p><strong>Adjacent Sibling Selector</strong><br />
This selector, which uses the plus sign, targets elements that are &#8220;adjacent&#8221; to each other, or immediate siblings, and they must have the same parent element.</p>
<pre name="code" class="css">
h2+p {
	background-color: red;
}
</pre>
<p><strong>General Sibling Selector</strong><br />
This uses the tilde character and is exactly the same as the adjacent sibling selector except the elements don&#8217;t have to be immediate siblings.</p>
<pre name="code" class="css">
h2~p {
	background-color: red;
}
</pre>
<p><strong>Pseudo-class</strong><br />
While technically falling under the category of &#8220;selectors&#8221;, these are not normally referred to as &#8220;selectors&#8221;, but just pseudo-classes. These select an element based on a state the element is in. Here are a few examples:</p>
<pre name="code" class="css">
a:visited {
	background-color: red;
}

input:focus {
	background-color: red;
}
</pre>
<p><strong>Pseudo-element</strong><br />
Again, not normally referred to as a selector, these actually represent elements in the HTML page that are not really part of the rendered HTML:</p>
<pre name="code" class="css">
p:first-letter {
	background-color: red;
}

p:before {
	content: "";
	background-color: red;
}
</pre>
<p>The &#8220;:before&#8221; and &#8220;:after&#8221; pseudo-elements are unique from other pseudo-elements in that they must be used along with the <code>content</code> property. For more on these, see <a href="http://coding.smashingmagazine.com/2011/07/13/learning-to-use-the-before-and-after-pseudo-elements-in-css/">my article on Smashing Magazine</a>.</p>
<p>If you enjoyed this article, you might also like a post I wrote defining a number of different <a href="http://www.impressivewebs.com/css-terms-definitions/">CSS Terms</a>.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=6301" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/css3-attribute-selectors-substring-matching/' rel='bookmark' title='CSS3 Attribute Selectors: Substring Matching'>CSS3 Attribute Selectors: Substring Matching</a></li>
<li><a href='http://www.impressivewebs.com/attribute-selectors/' rel='bookmark' title='Things Worth Noting About CSS Attribute Selectors'>Things Worth Noting About CSS Attribute Selectors</a></li>
<li><a href='http://www.impressivewebs.com/browser-support-css3-selectors/' rel='bookmark' title='Browser Support for CSS3 Selectors'>Browser Support for CSS3 Selectors</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/css-selectors/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Understanding CSS Shorthand</title>
		<link>http://www.impressivewebs.com/understanding-css-shorthand/</link>
		<comments>http://www.impressivewebs.com/understanding-css-shorthand/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 11:00:33 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=5624</guid>
		<description><![CDATA[Shorthand declarations can be tricky to master. In this post, I go through the basics of shorthand, and offer some suggestions for avoiding certain gotchas.]]></description>
			<content:encoded><![CDATA[<p>When declaring certain CSS properties, you have the option to use either the longhand, explicit declaration of a single CSS property, or a single-line shorthand declaration that combines a number of properties.</p>
<p>Let&#8217;s look at how shorthand works with some common examples, and I&#8217;ll try to explain some of the quirks associated with CSS shorthand.</p>
<h2>A Shorthand Example</h2>
<p>Declaring a background image is quite common in CSS. A <code>background</code> declaration in shorthand looks like this:</p>
<pre name="code" class="css">
.element {
	background: #fff url(../images/bg-image.png) no-repeat fixed 20px 20px;
}
</pre>
<p>To understand what&#8217;s happening in this shorthand declaration, here is the exact same CSS using longhand:</p>
<pre name="code" class="css">
.element {
	background-color: #fff;
	background-image: url(../images/bg-image.png);
	background-repeat: no-repeat;
	background-attachment: fixed;
	background-position: 20px 20px;
}
</pre>
<p>You can see already why most CSS developers prefer shorthand notation over longhand &#8212; the first example condenses five lines of CSS into one.</p>
<h2>Simple Shorthand Declarations</h2>
<p>Some shorthand declarations work in a very simple way. The example I showed you above with the <code>background</code> property is one of the more complex types (more on that below). But other shorthand declarations are quite easy to understand and work with.</p>
<p>The <a href="http://www.impressivewebs.com/difference-between-margins-padding-css/">margin and padding</a> properties are good examples of properties you&#8217;ll use often in their shorthand notation. And they&#8217;re not difficult to understand:</p>
<pre name="code" class="css">
.element {
	padding: 20px;
}
</pre>
<p>In this example, although I&#8217;ve left out three other values, those values are assumed to be equal to the one value shown. So with this code, all four sides of the targeted element will have 20px of padding.</p>
<p>We can also do this:</p>
<pre name="code" class="css">
.element {
	padding: 20px 10px;
}
</pre>
<p>In this example, we are declaring the top and right padding explicitly. The bottom and left padding, although not declared explicitly, will assume the same values as top and right &#8212; in that order. So the box will have 20px of top and bottom padding, and 10px of right and left padding.</p>
<p>And again, the long-hand equivalent of the example above would look like this:</p>
<pre name="code" class="css">
.element {
	padding-top: 20px;
	padding-right: 10px;
	padding-bottom: 20px;
	padding-left: 10px;
}
</pre>
<p>Margins and CSS3&#8242;s <a href="http://css3clickchart.com/?prop=border-radius">border-radius</a> property work exactly the same way, except with <code>border-radius</code> we&#8217;re dealing with corners instead of sides.</p>
<h2>The More Complex Shorthand Declarations</h2>
<p>Other shorthand declarations are a little more complicated, but with a few small exceptions, shouldn&#8217;t really cause you too many problems once you get the hang of them. The <code>background</code> shorthand declaration I showed you earlier is one example of these.</p>
<p>Other shorthand properties that may trip you up at times include:</p>
<ul class="body_list">
<li><code>list-style</code></li>
<li><code>font</code></li>
<li><code>border</code></li>
<li><code>outline</code></li>
</ul>
<p><a href="http://css3clickchart.com">CSS3 features</a> that use shorthand using the more complex style of notation include:</p>
<ul class="body_list">
<li><code>animation</code></li>
<li><code>border-image</code></li>
</ul>
<p>So what is it that makes these other shorthand properties more difficult to understand? Well, because these properties accept different types of values (sometimes keywords, sometimes units, etc.), leaving out certain values in a shorthand declaration will often have unexpected results.</p>
<p>Here&#8217;s an example using <code>background</code>:</p>
<pre name="code" class="css">
.element {
	background-color: blue;
	background: url(../images/bg-image.png) no-repeat;
}
</pre>
<p>With the code above, you would expect that the targeted element would have a non-repeating background image set over a blue background. But that will not happen unless we reverse the order of the properties. Because we&#8217;ve used <code>background</code> shorthand, this causes all the values associated with the <code>background</code> property to be reset to their initial, or default, state. In this case, the background color becomes &#8220;transparent&#8221;, overwriting the &#8220;blue&#8221;.</p>
<h2>Avoid Getting Tripped Up</h2>
<p>So it&#8217;s important to understand that anytime you use one of these more convoluted shorthand declarations, all omitted values associated with that shorthand declaration will be reset to their defaults.</p>
<p>Normally this is not a problem, because with properties associated with backgrounds, lists, padding, etc, those properties aren&#8217;t inheriting their values from parent elements. The only place where I have found this can cause headaches is in association with the <a href="http://www.impressivewebs.com/a-primer-on-the-css-font-shorthand-property/">font shorthand</a> property.</p>
<p>That&#8217;s why I did <a href="http://www.impressivewebs.com/screencast-why-you-shouldnt-use-css-font-shorthand/">this old screencast</a> (which is a bit out of date now) and created <a href="http://www.impressivewebs.com/css-font-shorthand-property-cheat-sheet/">this cheatsheet</a> that discusses some of the problems you might run into when using the shorthand <code>font</code> property. Basically, we expect certain typographical properties to inherit from their parents. But when we use <code>font</code> in shorthand, all the properties associated with that declaration are reset to their initial state, thus losing that natural inheritance.</p>
<p>Another thing that can happen with <code>font</code> shorthand and some other shorthand declarations is that if you omit a mandatory value, the entire line will be ignored by the browser. This is another thing to keep in the back of your mind in case you&#8217;re scratching your head wondering why a certain CSS property isn&#8217;t having any effect.</p>
<h2>Conclusion</h2>
<p>Overall, I highly recommend using shorthand whenever you can. The only one that I avoid like the plague is <code>font</code>. I&#8217;m perfectly content to use longhand for those, and I think many feel the same way.</p>
<p>It does help to use longhand when you&#8217;re first starting out, to get to know each of the individual properties. But for the most part you&#8217;ll stick to shorthand once you&#8217;ve got a good handle on the syntax for the property you&#8217;re using.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5624" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/longhand-padding-margins/' rel='bookmark' title='Should You Ever Use Longhand for Padding and Margins in CSS?'>Should You Ever Use Longhand for Padding and Margins in CSS?</a></li>
<li><a href='http://www.impressivewebs.com/difference-between-margins-padding-css/' rel='bookmark' title='What&#8217;s the Difference Between Margins and Padding?'>What&#8217;s the Difference Between Margins and Padding?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/understanding-css-shorthand/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Current Page Link Styles</title>
		<link>http://www.impressivewebs.com/current-page-link-styles/</link>
		<comments>http://www.impressivewebs.com/current-page-link-styles/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 11:00:22 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=5363</guid>
		<description><![CDATA[A simple post describing a couple of ways to indicate "current page" in your navigation using CSS.]]></description>
			<content:encoded><![CDATA[<p>One of the most common things you&#8217;ll see on any website is a navigation bar that has a different set of CSS styles applied to the link that represents the current page the user is on. (Maybe I&#8217;ll actually get around to doing this on my own site!) There are a few ways you can do this with HTML and CSS, which I&#8217;ll outline here.</p>
<h2>Using A Unique Link Class</h2>
<p>Probably the most common and easiest way to do this is by simply applying a different class with appropriate styles on the current page link. So in your HTML you would see something like this:</p>
<pre name="code" class="html">
&lt;ul class="nav"&gt;
	&lt;li&gt;&lt;a href="index.html"&gt;Home&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="about.html"&gt;About&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="services.html"&gt;Services&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="products.html" class="current"&gt;Products&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="contact.html"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>The CSS would then require something like the following:</p>
<pre name="code" class="css">
a:link, a:visited {
	color: blue;
}

a.current:link, a.current:visited {
	color: white;
	background-color: blue;
}
</pre>
<p>Now all links with a class of &#8220;current&#8221; will have white text on a blue background, while all other links will have just blue text.</p>
<p>The only thing you have to make sure of is that the location of the class gets changed depending on the page you&#8217;re on. For example, the HTML shown above would appear on the &#8220;Products&#8221; page, but on the &#8220;Contact&#8221; page the &#8220;current&#8221; class would be moved to the &#8220;Contact&#8221; link.</p>
<h2>Adding a Class to the &lt;body&gt;</h2>
<p>This next method is a lot messier, but is an option if you don&#8217;t mind more HTML. You can add a unique class to the <code>&lt;body&gt;</code> tag and then have a different class for each link. The body class would change depending on what page the user was viewing. So on the &#8220;About&#8221; page, your <code>&lt;body&gt;</code> tag would look like this:</p>
<pre name="code" class="html">
&lt;body class="about"&gt;
</pre>
<p>The HTML for your links would look like this:</p>
<pre name="code" class="html">
&lt;ul class="nav"&gt;
	&lt;li&gt;&lt;a href="index.html" class="home"&gt;Home&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="about.html" class="about"&gt;About&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="services.html" class="services"&gt;Services&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="products.html" class="products"&gt;Products&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="contact.html" class="contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>Then in your CSS you would have the following:</p>
<pre name="code" class="css">
a:link, a:visited {
	color: blue;
}

body.home a.home:link,
body.about a.about:link,
body.services a.services:link,
body.products a.services:link,
body.contact a.contact:link {
	color: white;
	background-color: blue;
}
</pre>
<p>This includes a separate selector for each nav item. So although the different unique link classes will appear in the HTML on every page, the unique &#8220;current page&#8221; styles will only take effect on the page that has the matching <code>&lt;body&gt;</code> class.</p>
<h2>Final Notes</h2>
<p>A few things can be noted about the technique outlined here:</p>
<ul class="body_list">
<li>If you are using PHP (or some other back-end language), you can generate the classes dynamically based on the current page</li>
<li>Although I&#8217;m applying the unique classes to the link elements, you can use the <code>&lt;li&gt;</code> elements if you want</li>
<li>I&#8217;ve seen people use a class of &#8220;active&#8221; instead of the class of &#8220;current&#8221; that I&#8217;m using here; I think &#8220;current&#8221; (or something else other than &#8220;active&#8221;) is better because CSS already has a pseudo-class called <code>:active</code> that means something completely different, so that might be confusing for beginners</li>
<li>I suppose you could use the <code>:nth-child()</code> pseudo-class to target the current link by number in the list, but that seems a little convoluted and not very convenient or maintainable</li>
<li>If you want to do this on a WordPress site, <a href="http://www.vanseodesign.com/wordpress/hightlight-current-page-wordpress/">this article</a> covers that topic</li>
</ul>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5363" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/text-shadow-links/' rel='bookmark' title='Using CSS3 Text Shadow for Active Link States'>Using CSS3 Text Shadow for Active Link States</a></li>
<li><a href='http://www.impressivewebs.com/scoped-styles-html5/' rel='bookmark' title='Scoped Styles in HTML5'>Scoped Styles in HTML5</a></li>
<li><a href='http://www.impressivewebs.com/best-way-add-css-web-page/' rel='bookmark' title='What&#8217;s the Best Way to Add CSS to a Web Page?'>What&#8217;s the Best Way to Add CSS to a Web Page?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/current-page-link-styles/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What’s the Difference Between Classes and IDs in CSS?</title>
		<link>http://www.impressivewebs.com/difference-class-id-css/</link>
		<comments>http://www.impressivewebs.com/difference-class-id-css/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 11:00:45 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=5108</guid>
		<description><![CDATA[When applying CSS styles to an element in your HTML, you'll be using different CSS selectors to target your elements.

Two of the most common selectors used in CSS are the "class" selector and the "ID" selector. There are many others, but this post will focus on these two, and I'll describe the difference so you'll know the potential effects of using either of these selectors.]]></description>
			<content:encoded><![CDATA[<p>When applying CSS styles to an element in your HTML, you&#8217;ll be using different CSS selectors to target your elements.</p>
<p>Two of the most common selectors used in CSS are the &#8220;class&#8221; selector and the &#8220;ID&#8221; selector. There are many others, but this post will focus on these two, and I&#8217;ll describe the difference so you&#8217;ll know the potential effects of using either of these selectors.</p>
<h2>The ID Selector is Very Specific</h2>
<p>An ID is defined in your HTML like this:</p>
<pre name="code" class="html">
&lt;div id="element"&gt;
&lt;!-- stuff goes here... --&gt;
&lt;/div&gt;
</pre>
<p>And in your CSS you can target the element, and apply styles to it, like this:</p>
<pre name="code" class="css">
#element {
    background: blue;
}
</pre>
<p>CSS styles are applied based on rules relating to the cascade and specificity. Because this is a post aimed at CSS beginners, I&#8217;m not going to go into the gory details of those subjects. If you want to delve into this, there&#8217;s a nice article <a href="http://coding.smashingmagazine.com/2010/04/07/css-specificity-and-inheritance/">on Smashing Magazine</a> by Inayaili de Leon that might be worth a peek.</p>
<p>The specificity of a selector can have a profound effect on the future maintenance and debugging of your CSS code. So, with that in mind, do realize that if you choose to use the ID selector, you <strong>may end up causing specificity issues</strong> that will force you later in development to use even stronger selectors. This could easily get out of hand and cause your stylesheet to be less maintainable and bloated.</p>
<p>Styles in a CSS file are supposed to &#8220;cascade&#8221;. So, theoretically, this should allow styles declared later in your stylesheet to override styles that are declared earlier. But because of more specific selectors (like IDs), this doesn&#8217;t happen as often as we&#8217;d like.</p>
<p>For example, if you had the following HTML:</p>
<pre name="code" class="html">
&lt;div id="element" class="element"&gt;
&lt;!-- stuff goes here... --&gt;
&lt;/div&gt;
</pre>
<p>And CSS:</p>
<pre name="code" class="css">
#element {
    background: blue;
}

body div.element {
    background: green;
}
</pre>
<p>Even though the <code>body div.element</code> selector appears after the <code>#element</code> ID selector, and despite the fact that the &#8220;body&#8221; element is included as part of the selector, the background of the element will be blue &#8212; not green &#8212; because the ID selector is more specific than the second selector, and <strong>the natural cascade has been overridden</strong>.</p>
<p>It may seem at first glance to be an advantage to have a very specific selector. But in the long run, this is not a good thing. Also, because an ID has to be unique and must only appear once per HTML page, <strong>you can&#8217;t reuse the styles on an ID selector</strong>, so your stylesheet will be much larger if you end up using a lot of IDs.</p>
<h2>Class Selectors are Much Easier to Work With</h2>
<p>Classes are applied in HTML like this:</p>
<pre name="code" class="html">
&lt;div class="element"&gt;
&lt;!-- stuff goes here... --&gt;
&lt;/div&gt;
</pre>
<p>And you target the element in your CSS like this (which, I&#8217;ve already demonstrated in the previous section, really):</p>
<pre name="code" class="css">
.element {
    background: blue;
}
</pre>
<p>Class selectors are, in my opinion, highly underrated and a much cleaner and easier to use option for applying styles to elements. There are a number of reasons for this.</p>
<p><strong>Classes can be reused</strong> (you&#8217;re not limited to a single class per page, like IDs). This allows you to avoid repeating styles unnecessarily. You can also include multiple classes using a single attribute, like this:</p>
<pre name="code" class="html">
&lt;div class="element class2 class3"&gt;
&lt;!-- stuff goes here... --&gt;
&lt;/div&gt;
</pre>
<p>You also have the option to chain your classes using selectors like <code>.element.class2.class3</code> (notice no spaces between the classes). While this is possible, you should know that this method is buggy in IE6, which is why these types of chained class selectors have been largely avoided for so many years.</p>
<p>And best of all, <strong>class selectors are not extremely specific</strong>. So a class of <code>.element</code> will override the less specific element selector (e.g. a plain <code>span</code> or <code>div</code> selector), but it will not override other classes &#8212; unless it appears after those classes in the CSS.</p>
<p>This is great for maintenance and debugging of your CSS, because when you add new classes to your CSS, you won&#8217;t be scratching your head wondering why the styles don&#8217;t take effect right away.</p>
<h2>Final Notes</h2>
<p>So my advice is: Don&#8217;t use IDs as CSS selectors unless you have really good, practical reasons for doing so. <strong>Use classes abundantly</strong> and allow the natural cascade to work its magic.</p>
<p>This doesn&#8217;t mean you won&#8217;t use IDs. You will still need IDs for in-page links (i.e. fragment identifiers), and for JavaScript and jQuery hooks.</p>
<p>You might think: What if there&#8217;s an element on the page that already has an ID (for non-CSS reasons), and it&#8217;s an element that you absolutely know is going to be unique and won&#8217;t have repeated or reusable styles? Well, in my opinion, even in this case, there is no reason to use the ID selector in your CSS, and you should still use a class instead.</p>
<p>If you take each one as a stand-alone selector, regardless of all other styles and selectors, there is no difference between <code>#element</code> and <code>.element</code>. But when you factor in the negative effects of an ID selector (the non-reusability, the high specificity, the detriment to the cascade), I think the class selector is much more powerful and should be an overwhelming choice.</p>
<p>Finally, if you want to get into some more good reasons to use classes heavily, you can check out my recent Smashing Magazine article covering <a href="http://coding.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/">Object Oriented CSS</a>.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5108" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/before-after-css3/' rel='bookmark' title='What&#8217;s the Difference Between &#8220;:before&#8221; and &#8220;::before&#8221;?'>What&#8217;s the Difference Between &#8220;:before&#8221; and &#8220;::before&#8221;?</a></li>
<li><a href='http://www.impressivewebs.com/difference-between-margins-padding-css/' rel='bookmark' title='What&#8217;s the Difference Between Margins and Padding?'>What&#8217;s the Difference Between Margins and Padding?</a></li>
<li><a href='http://www.impressivewebs.com/difference-block-inline-css/' rel='bookmark' title='The Difference Between &#8220;Block&#8221; and &#8220;Inline&#8221;'>The Difference Between &#8220;Block&#8221; and &#8220;Inline&#8221;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/difference-class-id-css/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The Difference Between “Block” and “Inline”</title>
		<link>http://www.impressivewebs.com/difference-block-inline-css/</link>
		<comments>http://www.impressivewebs.com/difference-block-inline-css/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 11:10:47 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4944</guid>
		<description><![CDATA[For the purpose of CSS styling, elements can be generally divided into a few different categories. Two of those categories are <em>block-level</em> elements and <em>inline</em> elements.

In my opinion, this is one of those areas that, once understood correctly, can help beginners to take their CSS skills to the next level.]]></description>
			<content:encoded><![CDATA[<p>For the purpose of CSS styling, elements can be generally divided into a few different categories. Two of those categories are <em>block-level</em> elements and <em>inline</em> elements.</p>
<p>In my opinion, this is one of those areas that, once understood correctly, can help beginners to take their CSS skills to the next level.</p>
<h2>Block-level Elements</h2>
<p>A block element is an element that has, but may not be limited to, the following characteristics:</p>
<ul>
<li>Without a set width, expands naturally to fill its parent container</li>
<li>Can have margins and/or padding</li>
<li>Without a set height, expands naturally to fit its child elements (assuming they are not floated or positioned)</li>
<li>By default, will be placed below previous elements in the markup (assuming no floats or positioning on surrounding elements)</li>
<li>Ignores the <code>vertical-align</code> property</li>
</ul>
<p>So, for a block element, it&#8217;s not necessary to give it a set width or to give it a <a href="http://www.impressivewebs.com/width-100-percent-css/">width of 100%</a> if you want it to fill its parent horizontally. In fact, doing either of those things may cause maintainability issues or other undesirable problems.</p>
<p>And, as the fourth item in the above list indicates, it&#8217;s also not necessary to &#8220;clear&#8221; a block element; assuming no floats are affecting the block element, it will be cleared automatically and will start on the next &#8220;line&#8221; in the page&#8217;s output.</p>
<p><strong>Examples of Block Elements:</strong><br />
<code>&lt;p&gt;</code>, <code>&lt;div&gt;</code>, <code>&lt;form&gt;</code>, <code>&lt;header&gt;</code>, <code>&lt;nav&gt;</code>, <code>&lt;ul&gt;</code>, <code>&lt;li&gt;</code>, and <code>&lt;h1&gt;</code>.</p>
<p><img src="http://cdn.impressivewebs.com/2011-11/block.jpg" alt="Block-level Elements" title="Block-level Elements" width="680" height="383" class="wide_image wi_new"></p>
<h2>Inline Elements</h2>
<p>An inline element has, but may not be limited to, the following characteristics:</p>
<ul>
<li>Flows along with text content, thus</li>
<li>Will not clear previous content to drop to the next line like block elements</li>
<li>Is subject to <a href="http://www.impressivewebs.com/css-white-space/">white-space</a> settings in CSS</li>
<li>Will ignore top and bottom margin settings, but will apply left and right margins, and any padding</li>
<li>Will ignore the <code>width</code> and <code>height</code> properties</li>
<li>If floated left or right, will automatically become a block-level element, subject to all block characteristics</li>
<li>Is subject to the <code>vertical-align</code> property</li>
</ul>
<p>The easiest way to picture an inline element is to think of it as <strong>a box that acts like text</strong>. What happens, for example, to text that&#8217;s not separated by other elements? It flows one letter after the other. If you put an inline element next to text, it will flow next to that text just like another piece of text.</p>
<p><strong>Examples of Inline Elements:</strong><br />
<code>&lt;a&gt;</code>, <code>&lt;span&gt;</code>, <code>&lt;b&gt;</code>, <code>&lt;em&gt;</code>, <code>&lt;i&gt;</code>, <code>&lt;cite&gt;</code>, <code>&lt;mark&gt;</code>, and <code>&lt;code&gt;</code>.</p>
<p><img src="http://cdn.impressivewebs.com/2011-11/inline.jpg" alt="Inline Element" title="Inline Element" width="680" height="383" class="wide_image wi_new"></p>
<h2>Other Notes</h2>
<p>You&#8217;ll notice that the examples of both block and inline elements are quite revealing: The block examples are structural elements, while the inline elements are text-based. This is an easy way to remember which is which, although at first you will sometimes be confused.</p>
<p>Generally speaking, you can put any block element inside another block element. You can also put any inline element inside a block element, as well as any inline element inside any other inline element. But you cannot put a block element inside an inline element &#8212; with (I believe) one exception: You can wrap an <code>&lt;a&gt;</code> element around any content, whether it be block eleemnts, or inline elements, or a mixure of both.</p>
<p>You have the option to change any block-level element to an inline element, and vice-versa, using the <code>display</code> property. So keep that in mind if you need to do this. But don&#8217;t use the wrong element for the wrong purpose. An SEO guy once told me to put an <code>&lt;h2&gt;</code> element inside of a paragraph, and style it to look like regular text, because he wanted an SEO boost. Not a good idea, that&#8217;s what <code>&lt;em&gt;</code> is for.</p>
<p>One final thing to note is that, although from a CSS perspective block and inline do exist, from an HTML5 perspective, the different types of elements have been re-categorized, so that they&#8217;re more specific. You can read about these <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/content-models.html">content models</a> in the WHATWG HTML5 spec.</p>
<h2>Bonus Tip: Replaced Elements</h2>
<p>At the beginning, I said that block and inline were just two types of content. Generally speaking, those are the two primary kinds, and you won&#8217;t have to worry too much about any other kinds.</p>
<p>But there are what are referred to as <a href="http://reference.sitepoint.com/css/replacedelements">replaced elements</a>. Basically, these are neither block nor inline. But you might classify them as something closer to inline, but with block-like structure.</p>
<p>I won&#8217;t provide a detailed explanation here; you can check out the SitePoint article linked in the previous paragraph for full details. But some examples of replaced elements include:</p>
<p><code>&lt;img&gt;</code>, <code>&lt;object&gt;</code>, <code>&lt;input&gt;</code>, and <code>&lt;select&gt;</code>.</p>
<h2>Conclusion</h2>
<p>If you&#8217;re just starting out with CSS, understanding the behaviour of block vs. inline elements will go a long way to helping you code sites that are efficient and maintainable and don&#8217;t rely on overuse of floats or other undesirable methods.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4944" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/difference-between-margins-padding-css/' rel='bookmark' title='What&#8217;s the Difference Between Margins and Padding?'>What&#8217;s the Difference Between Margins and Padding?</a></li>
<li><a href='http://www.impressivewebs.com/before-after-css3/' rel='bookmark' title='What&#8217;s the Difference Between &#8220;:before&#8221; and &#8220;::before&#8221;?'>What&#8217;s the Difference Between &#8220;:before&#8221; and &#8220;::before&#8221;?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/difference-block-inline-css/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Using Absolute Positioning in CSS</title>
		<link>http://www.impressivewebs.com/absolute-position-css/</link>
		<comments>http://www.impressivewebs.com/absolute-position-css/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 13:55:12 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4893</guid>
		<description><![CDATA[Positioning elements absolutely is a useful technique to be aware of. When used selectively, it can offer many more options for what might otherwise be troublesome layout options. Here I'll discuss the basics, with some tips for use.]]></description>
			<content:encoded><![CDATA[<p>Once you start to become more experienced with CSS, one concept that will help you greatly is use of the <code>position</code> property, in particular with a value of <code>absolute</code>.</p>
<p>Some of the benefits of using absolutely positioned elements in your layouts include:</p>
<ul>
<li>Aside from <a href="http://reference.sitepoint.com/css/position#compatibilitysection">a few IE bugs</a>, very good cross-browser support</li>
<li>Less dependence on floats, which can be problematic</li>
<li>Less dependence on margins, which can be a bit buggy in older IE</li>
</ul>
<p>Drawbacks of absolutely positioned elements include:</p>
<ul>
<li>The positioned elements are removed from the natural document flow</li>
<li>Can cause maintenance problems if you have to add other elements near the positioned one</li>
</ul>
<h2>The Basic Syntax</h2>
<p>To position an element absolutely, you need to do a few things. Here&#8217;s the code, then a basic explanation:</p>
<p>First, some basic HTML:</p>
<pre name="code" class="css">
&lt;div class="box"&gt;

	&lt;div class="inner-box"&gt;
	&lt;p&gt;dummy content&lt;/p&gt;
	&lt;/div&gt;

&lt;/div&gt;
</pre>
<p>Now the CSS:</p>
<pre name="code" class="css">
.box {
	position: relative;
}

.inner-box {
	position: absolute;
	top: 20px;
	left: 50px;
}
</pre>
<p>(In this example, I&#8217;ve omitted some values that would otherwise be necessary, like width and/or height.)</p>
<p>First, you need to create what&#8217;s referred to as a <strong>positioning context</strong>. In this case, the positioning context is the <code>.box</code> element. We do this by adding <code>position: relative</code> to that element.</p>
<p>Next, we need to absolutely position the inner element. We do this by setting <code>position: absolute</code>.</p>
<p>Lastly, we tell the browser where exactly we want the inner element to be placed in relation to the outer element. This is done using four possible properties: <code>top</code>, <code>right</code>, <code>bottom</code>, and <code>left</code>. In this example, I&#8217;m using two (which is enough). The inner element, by default, starts out in the top left corner of the outer element, then gets positioned 20px down from the top and 50px in from the left.</p>
<h2>What Happens if You Don&#8217;t Set a Positioning Context?</h2>
<p>Absolute positioning doesn&#8217;t require a &#8216;relative&#8217; element (as set on the <code>.box</code> element above). If you absolutely position an element without a positioning context, then the positioning will take place relative to the entire page. (The exception is if you don&#8217;t specify any top, bottom, left, or right values. In that case, even if there is no positioning context, the context will automatically become the immediate container element, and the element will still be in flow.)</p>
<p>The lack of positioning context is probably the biggest cause for problems with people new to CSS positioning. A CSS beginner may decide to try setting the position of an element to &#8220;absolute&#8221;, then after setting top and left values, notice that the element will either get pushed all the way to the left or all the way to the top, or both. And it will be outside of its container element. This might cause a beginner to give up and not ever use that property.</p>
<p>So understanding how the positioned parent affects the absolutely positioned child will help greatly because this is a very useful technique to be familiar with.</p>
<h2>Bonus Tip: Sizing Without Width or Height</h2>
<p>Here&#8217;s a bonus tip that probably not a lot of people know about. You can actually define the dimensions of any absolutely positioned element using the top, left, bottom, and right values alone, without any content in the element and without <code>width</code> or <code>height</code> values.</p>
<p>Here&#8217;s an example using the same HTML from above:</p>
<pre name="code" class="php">
.box {
	position: relative;
	width: 400px;
	height: 400px;
}

.inner-box {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
}
</pre>
<p>This will cause the inner element to fill the outer element&#8217;s content area. Here&#8217;s a JSBin with some exaggerated borders, so you can see how this works:</p>
<div class="button_holder">
<ul class="button">
<li><a href="http://jsbin.com/ilegur/edit#html,live" title="View Demo">View Demo</a></li>
</ul>
</div>
<h2>Conclusion</h2>
<p>Don&#8217;t be afraid of CSS positioning if you don&#8217;t understand it at first, or if you run into problems. It&#8217;s a useful technique that, when understood well, will help you avoid a number of problems that can occur from having too many floats or too many margin settings.</p>
<p>You can read more on positioning <a href="http://reference.sitepoint.com/css/positioning">on SitePoint&#8217;s reference</a>.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4893" width="1" height="1" style="display: none;" /><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/absolute-position-css/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>What’s the Difference Between Margins and Padding?</title>
		<link>http://www.impressivewebs.com/difference-between-margins-padding-css/</link>
		<comments>http://www.impressivewebs.com/difference-between-margins-padding-css/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 13:55:40 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4839</guid>
		<description><![CDATA[When you first start out with CSS development, you might be a bit confused about the difference between margins and padding. Here's a summary with some images to help visualize the difference.]]></description>
			<content:encoded><![CDATA[<p>For experienced CSS developers this is a very simple topic. But this being a <a href="http://www.impressivewebs.com">CSS for beginners</a> section of Impressive Webs, I thought it would be good to address this topic as briefly and as effectively as possible for those just starting out.</p>
<h2>What Are Margins?</h2>
<p>Margins in CSS make up the vertical and horizontal areas between elements. If elements have no margins around them, they will bump right up against each other. Some elements have margins by default, so often even if you haven&#8217;t added a margin, you may still see one.</p>
<p>Here&#8217;s a visual demonstration of CSS margins:</p>
<p><img src="http://cdn.impressivewebs.com/2011-11/margins-css.jpg" alt="CSS Margins" title="CSS Margins" width="680" height="400" class="wide_image wi_new"></p>
<p>It&#8217;s pretty simple. The space outside of, or between, elements is what comprises the margin areas. This area would also be outside of any borders set on elements.</p>
<p>I should point out there there could be other reasons for elements to have space between them &#8212; not just margins. But for the purpose of this simple post, we&#8217;ll just assume that all the vertical and horizontal space shown in that image is caused by margin settings.</p>
<p>Margins can be set using the <code>margin</code> property (which is shorthand, representing the four margin values for an element) or by the longhand <code>margin-top</code>, <code>margin-right</code>, <code>margin-bottom</code>, and <code>margin-left</code> properties. The order in shorthand is: top, right, bottom, left.</p>
<h2>What is Padding?</h2>
<p>So what about padding? Well, the padding of an element is the horizontal and vertical space that&#8217;s set around the content area of the targeted element. So padding is on the inside of a box, not the outside.</p>
<p>Here&#8217;s a visual demonstration of CSS padding:</p>
<p><img src="http://cdn.impressivewebs.com/2011-11/padding-css.jpg" alt="CSS Padding" title="CSS Padding" width="680" height="304" class="wide_image wi_new"></p>
<p>In this example, there&#8217;s a single element with text centered within that element. The centering could be done in a number of ways. We&#8217;ll assume it&#8217;s done via the CSS padding property. So the padding is the area inside the element that separates the content of the element from the element&#8217;s edges. In this example, the element could be a paragraph tag (<code>&lt;p&gt;</code>).</p>
<p>Similar to margins, padding is set using the shorthand <code>padding</code> property, and can also be done using longhand: <code>padding-top</code>, <code>padding-right</code>, <code>padding-bottom</code>, and <code>padding-left</code>.</p>
<h2>Summary</h2>
<p>So, in brief, margins separate elements from each other and away from page edges, adding space outside of elements. Padding adds space inside of an element, separating the element&#8217;s content from the edges of the targeted element.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4839" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/longhand-padding-margins/' rel='bookmark' title='Should You Ever Use Longhand for Padding and Margins in CSS?'>Should You Ever Use Longhand for Padding and Margins in CSS?</a></li>
<li><a href='http://www.impressivewebs.com/difference-block-inline-css/' rel='bookmark' title='The Difference Between &#8220;Block&#8221; and &#8220;Inline&#8221;'>The Difference Between &#8220;Block&#8221; and &#8220;Inline&#8221;</a></li>
<li><a href='http://www.impressivewebs.com/difference-class-id-css/' rel='bookmark' title='What&#8217;s the Difference Between Classes and IDs in CSS?'>What&#8217;s the Difference Between Classes and IDs in CSS?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/difference-between-margins-padding-css/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How Do I Target IE7 or IE8 Using CSS Hacks?</title>
		<link>http://www.impressivewebs.com/ie7-ie8-css-hacks/</link>
		<comments>http://www.impressivewebs.com/ie7-ie8-css-hacks/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 10:00:32 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4648</guid>
		<description><![CDATA[If you've dropped support for IE6 and/or IE7, then your repertoire of CSS hacks is probably pretty minimal. Here I outline the methods I feel are the best ways to write IE7- and IE8-specific CSS.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s very likely that you&#8217;ve dropped support for IE6 in your designs, seeing as the <a href="http://gs.statcounter.com/#browser_version-ww-monthly-201010-201110-bar">worldwide share</a> for that browser is below 5% and declining each month.</p>
<p>And if you&#8217;re developing websites or web apps in any technology-related niche (like web design), then IE6 visitors are probably even lower. For this website, IE6 visitors since January 1, 2011 account for about 0.5% of users, and in the last 30 days, that&#8217;s dropped to about 0.3%. You might have similar stats.</p>
<p>For this reason, it&#8217;s more than likely that the only CSS hacks you&#8217;ll need are for IE7 and IE8. Firstly, if you follow <a href="http://coding.smashingmagazine.com/2010/06/07/the-principles-of-cross-browser-css-coding/">some basic CSS coding principles</a>, then you might very well need no hacks or workarounds for IE7 and IE8. For the design of this blog, I currently have <del>zero</del> very few IE-specific hacks &#8212; so it&#8217;s obviously possible to get by without relying too strongly on hacks. (Update: I&#8217;m no longer supporting IE6 on this site, so that does help greatly).</p>
<p>Nonetheless, many beginners may need ways to target IE7 and/or IE8. Here I outline the best methods for doing so.</p>
<h2>Paul Irish&#8217;s Conditional Classes</h2>
<p>The best method, by far, is the <a href="http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/">conditional classes</a> technique popularized by Paul Irish. Here&#8217;s how you do it:</p>
<pre name="code" class="css">
&lt;!--[if IE 7]&gt;     &lt;html class="ie7"&gt; &lt;![endif]--&gt;
&lt;!--[if IE 8]&gt;     &lt;html class="ie8"&gt; &lt;![endif]--&gt;
&lt;!--[if !IE]&gt;&lt;!--&gt; &lt;html&gt;             &lt;!--&lt;![endif]--&gt;
</pre>
<p>This uses <a href="http://reference.sitepoint.com/css/conditionalcomments">conditional comments</a> to target certain versions of Internet Explorer, and the <code>&lt;html&gt;</code> element receives a custom class name for different versions of IE, and no class name when the browser is not IE.</p>
<p>Then in your CSS, you would target IE7 or IE8 like this:</p>
<pre name="code" class="css">
.element {
	margin-bottom: 20px;
}

.ie7 .element {
	margin-bottom: 10px;
}

.ie8 .element {
	margin-bottom: 15px;
}
</pre>
<p>Every browser will have a bottom margin of 20px on the element in question, but IE7 and IE8 will have a bottom margin of 10px and 15px respectively.</p>
<p>It&#8217;s important to note here that although targeting IE is necessary at times, I strongly suggest you try to find a better way to fix any browser differences, and <a href="http://www.impressivewebs.com/conditional-comments-classes-ie/">avoid using conditional classes</a> or any of the hacks mentioned below, if at all possible. Sometimes you just need to rethink your CSS and possibly your markup, and you can avoid this extra code from the start.</p>
<h2>CSS Hacks</h2>
<p>Another way to target IE7 or IE8 is using hacks.</p>
<p>First, how to target IE7 only. There are probably more than just two ways to do this, but here are two (which is really all you&#8217;ll need):</p>
<pre name="code" class="css">
* + html .element {
	margin-bottom: 10px;
}
</pre>
<p>(Thanks to <a href="http://www.impressivewebs.com/ie7-ie8-css-hacks/#comment-11190">Anthony</a> for the tip on this first one, above)</p>
<p>The hack is the part of the selector that comes before <code>.element</code>. No other browser will recognize that selector, so the 10px bottom margin will only appear to users of IE7.</p>
<p>Here&#8217;s another less memorable method:</p>
<pre name="code" class="css">
*:first-child+html .element {
	margin-bottom: 10px;
}
</pre>
<p>Yet another way to target IE7 is like this:</p>
<pre name="code" class="css">
.element {
	margin-bottom: 20px;
	*margin-bottom: 10px;
}
</pre>
<p>The &#8220;hack&#8221; is the asterisk at the start of the line of CSS that you want to apply to IE7. The only problem here is that this will also apply to IE6. So you should only use this if you know that this hack will look fine in IE6, or if you don&#8217;t care what it looks like in IE6. So if you&#8217;ve dropped support for IE6, then this should be fine.</p>
<p>From my research on this subject, there does not appear to be a way to target only IE8 (and not IE6, IE7, and IE9+) with a CSS hack. The only way seems to be using a conditional comment.</p>
<p>However, if you&#8217;ve dropped support for IE7, then you could use the following:</p>
<pre name="code" class="css">
.element {
	margin-bottom: 20px;
	margin-bottom: 10px\9;
}
</pre>
<p>The &#8220;hack&#8221; is the <code>\9</code> following the value and preceding the closing semicolon. This will target IE8 and below, so you should only use this as an IE8-only hack if you no longer support IE6 or IE7 (because both those versions will apply the style in this hack).</p>
<h2>Conditional Comments</h2>
<p>Finally, you could separate your CSS for IE7 and IE8 into separate files, using conditional comments, like this:</p>
<pre name="code" class="html">
&lt;!--[if IE 7]&gt;
&lt;link rel="stylesheet" href="ie7.css"&gt;
&lt;![endif]--&gt;

&lt;!--[if IE 8]&gt;
&lt;link rel="stylesheet" href="ie8.css"&gt;
&lt;![endif]--&gt;
</pre>
<p>But I do not recommend you use this method. This has two main problems: First, it separates your CSS into three different files, making it much harder to maintain. And second, you&#8217;re adding extra HTTP requests in two of the slowest browsers, making your pages load much more slowly.</p>
<p>So my suggestion is to use either the conditional classes or else a simple hack to target IE7 or IE8.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4648" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/difference-between-margins-padding-css/' rel='bookmark' title='What&#8217;s the Difference Between Margins and Padding?'>What&#8217;s the Difference Between Margins and Padding?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/ie7-ie8-css-hacks/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>How Do I Force the Browser to Use the Newest Version of my Stylesheet?</title>
		<link>http://www.impressivewebs.com/force-browser-newest-stylesheet/</link>
		<comments>http://www.impressivewebs.com/force-browser-newest-stylesheet/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 20:25:53 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4642</guid>
		<description><![CDATA[If you're a beginner and you're developing HTML and CSS using an external stylesheet, you might notice that in some browsers, under some circumstances, the changes you've made to your CSS don't take effect immediately.]]></description>
			<content:encoded><![CDATA[<div class="update-box">
<b>Update (Nov. 19/2011):</b><br />
This article started out as a basic tip but there have been some great comments added for those interested in delving into the topic of caching even more. So, although I do offer this as a recommendation, there are better ways to do this for larger apps, and there are some concerns to keep in mind should you choose to do this. So be sure to read the comments for links and further info on this topic.
</div>
<p>If you&#8217;re a beginner and you&#8217;re developing HTML and CSS using an external stylesheet, you might notice that in some browsers, under some circumstances, the changes you&#8217;ve made to your CSS don&#8217;t take effect immediately.</p>
<p>Sometimes it&#8217;s necessary to do a <a href="http://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache#Instructions_for_various_browsers">hard refresh</a> to see the updates take effect. But it&#8217;s unlikely that average web users know what a hard refresh is, nor can you expect them to keep refreshing the page until things straighten out.</p>
<p>So how can you ensure that any updates you&#8217;ve made to your CSS will take place immediately for all users? Here&#8217;s one way to do it:</p>
<pre name="code" class="html">
&lt;link rel="stylesheet" href="style.css?v=1.1"&gt;
</pre>
<p>Notice that I&#8217;m pointing to my CSS using <a href="http://www.impressivewebs.com/best-way-add-css-web-page/">the commonly known</a> <code>&lt;link&gt;</code> element. But I&#8217;ve also added what&#8217;s called a <a href="http://en.wikipedia.org/wiki/Query_string">query string</a> to the end of the file name.</p>
<p>The browser will view a file name of <code>style.css</code> as different from a file name of <code>style.css?v=1.1</code>, so it will generally force the browser to update the stylesheet. So, each time you update your CSS on the server, you can incrementally update your version number.</p>
<p>If you&#8217;re new to query strings, just know that the part before the equals sign is like a placeholder, and the part after the equals sign is the value put into that placeholder. This will have no effect on the CSS. It will only serve to make the browser think it&#8217;s a completely different file.</p>
<p>You could add many types of characters to the query string value, but numbers are a logical way to do it, because then you can just increase the number, and even add decimal places if you want. And of course, if you don&#8217;t change the value for a while, the browser will continue to cache (or preserve) the file, and won&#8217;t attempt to download it unless other factors force it to, or you end up updating the query string value.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4642" width="1" height="1" style="display: none;" /><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/force-browser-newest-stylesheet/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>What’s the Best Way to Add CSS to a Web Page?</title>
		<link>http://www.impressivewebs.com/best-way-add-css-web-page/</link>
		<comments>http://www.impressivewebs.com/best-way-add-css-web-page/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 16:08:58 +0000</pubDate>
		<dc:creator>Louis Lazaris</dc:creator>
				<category><![CDATA[CSS Basics]]></category>

		<guid isPermaLink="false">http://www.impressivewebs.com/?p=4625</guid>
		<description><![CDATA[In an HTML page, you can add CSS in a number of different ways, which I explain below. After I show you each method, I'll tell you which method is best, and why.]]></description>
			<content:encoded><![CDATA[<p>In an HTML page, you can add CSS in a number of different ways, which I explain below. After I show you each method, I&#8217;ll tell you which method is best, and why.</p>
<h2>Using HTML&#8217;s <code>&lt;link&gt;</code> Tag</h2>
<p>This is the best method to use (more details on this at the end of this post). Inside your web page, with the opening and closing <code>&lt;head&gt;</code> tags, you can include the following:</p>
<pre name="code" class="html">
&lt;link rel="stylesheet" href="style.css"&gt;
</pre>
<p>Prior to HTML5, developers were accustomed to adding a &#8220;type&#8221; attribute to the <code>&lt;link&gt;</code> element, to specify what type of file you were including. But this was never required by HTML validators, nor is it required by any browser or browser version.</p>
<h2>Using the <code>@import</code> At-Rule</h2>
<p>Instead of using the <code>&lt;link&gt;</code> element, you could alternatively include a reference to a CSS file like this:</p>
<pre name="code" class="html">
&lt;style&gt;
@import url(style.css);
&lt;/style&gt;
</pre>
<p>This method, referred to as an at-rule (this is just one type of at-rule) must be included either within <code>&lt;style&gt;</code> tags inside your document&#8217;s <code>&lt;head&gt;</code> element, or else inside your stylesheet (which could be referenced via the <code>&lt;link&gt;</code> element, or via another <code>@import</code> rule).</p>
<p>The <code>@import</code> declaration must appear before any other CSS rules, otherwise it will not work.</p>
<h2>Using the <code>&lt;style&gt;</code> Element</h2>
<p>You could include CSS anywhere in your web page by putting CSS rules inside <code>&lt;style&gt;</code> tags, like this:</p>
<pre name="code" class="css">
&lt;style&gt;
p {
	margin: 0 0 20px 0;
}
&lt;/style&gt;
</pre>
<p>Although this could be placed anywhere in your HTML document, it&#8217;s generally best to put it inside the <code>&lt;head&gt;</code> element, if you choose to use this method.</p>
<h2>Using Inline Styles</h2>
<p>Another way to add CSS to a web page is to use inline styles. With this method, the styles will only apply to the element on which they appear. Here&#8217;s an example:</p>
<pre name="code" class="css">
&lt;p style="color: red; padding: 20px;"&gt;Example paragraph text goes here.&lt;/p&gt;
</pre>
<h2>Which Method is Best?</h2>
<p>Generally speaking, the best method is the first one listed that describes how to use the <code>&lt;link&gt;</code> element. But there could be some instances where one of the other methods would come in handy, so you shouldn&#8217;t completely rule them out. I have used all the above methods over the years. The only method that I no longer use is the <code>@import</code> method.</p>
<p>The <code>@import</code> method fell out of popular use for various reasons. First, there was an issue with Internet Explorer that caused the page to <a href="http://bluerobot.com/web/css/fouc.asp/">temporarily appear unstyled</a> when this method was used in the <code>&lt;head&gt;</code> element. Also, <code>@import</code> was previously a good way to get really old browsers (like Netscape 4) to ignore CSS that they didn&#8217;t understand. But this is no longer necessary.</p>
<p>It should also be pointed out that the &#8220;Flash of Unstyled Content&#8221; does not occur if you use the <code>&lt;link&gt;</code> element, then refer to another file in your CSS via the <code>@import</code> method.</p>
<p>The other two methods I outlined (using the <code>&lt;style&gt;</code> element, and inline styles) should generally be avoided because they are not good for long-term maintenance and re-use of CSS. For example, if you have a bunch of CSS in the <code>&lt;head&gt;</code> of your document, then that CSS will only affect that document. Likewise, with inline styles, the styles will only apply to the elements they appear on.</p>
 <img src="http://www.impressivewebs.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4625" width="1" height="1" style="display: none;" /><p>Related posts:<ol>
<li><a href='http://www.impressivewebs.com/current-page-link-styles/' rel='bookmark' title='Current Page Link Styles'>Current Page Link Styles</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.impressivewebs.com/best-way-add-css-web-page/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 0.492 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-23 06:31:47 -->

