Rediscovering generated content

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

In this article I’ll discuss some possible uses of generated content. Generated content is a powerful feature of CSS. Before going on, I’d like to explain what generated content is not. Let’s take a look at the following picture.

A puppy dog puppetFigure 1. A puppy dog puppet

The picture depicts a puppy dog puppet. Of course this is not a real puppy, but only an abstraction. By the same token, generated content is not real content but only an abstraction. More precisely, generated content doesn’t exist in the document tree nor is parsed with the rest of the document. We can say, in other words, that generated content is only pseudo-content. For example, when we insert a string through generated content, the text inserted is not selectable. That’s why generated content lives only in the style rules.

Table of contents

Inserting generated content

Generated content can be inserted before and after the real content of an element through the :before and :after pseudo-elements, respectively. To represent them, we can use the following fictional markup.

Listing 1. Fictional markup for :before and :after

  1. <p>
  2. <before>Start</before>
  3. Real content
  4. <after>End</after>
  5. </p>

And our CSS will be:

Listing 2: :before and :after in the CSS

  1. p:before {
  2. content: "Start";
  3. }
  4. p:after {
  5. content: "End";
  6. }

View the exampleView the screenshot

As we can see, the property that actually inserts the two strings is content. This property accepts the following values:

none, normal
The pseudo-content is not generated.
<string>
A textual string enclosed in matching quotes.
url()
This function allow us to insert an external resource (usually an image), as in the background-image property.
counter(), counters()
These functions insert counters. See below for more details.
attr(attribute)
This function allow us to insert the value of the attribute attribute of the given element.

Keep in mind that generated content takes up its own space on the page and its presence affects the space’s computation of the element that hosts it.

Inserting strings

In the previous example, we’ve inserted two simple strings before and after the real content of an element. Generated content allows us to insert also more complex symbols through escaping.

Listing 3. Inserting a symbol

  1. p:before {
  2. content: "\00A7";
  3. padding-right: 0.2em;
  4. }

View the exampleView the screenshot

The escaped sequence inside the double quotes is a hexadecimal Unicode value that refers to a paragraph symbol. We can also combine simple strings with Unicode symbols, as shown below.

Listing 4. Inserting a simple string and a symbol

  1. p:before {
  2. content: "( " "\00A7" " )";
  3. padding-right: 0.2em;
  4. }

View the exampleView the screenshot

Keep in mind that all the textual content inside the content property is treated literally, that is, spaces and tabulations inserted via the keyboard will be inserted in the page as well.

Inserting images

We can insert images through the url() function.

Listing 5. Inserting images

  1. a:before {
  2. content: url("../img/link.gif");
  3. padding-right: 0.2em;
  4. }

View the exampleView the screenshot

As we can see, this function works exactly as in the background-image property.

Inserting attribute values

An attribute value of an element can be inserted through the attr() function.

Listing 6. Inserting attribute values

  1. a[href]:after {
  2. content: "( " attr(href) " )";
  3. padding-left: 0.2em;
  4. color: #000;
  5. font: small "Courier New", Courier, monospace;
  6. }

View the exampleView the screenshot

We’ve just inserted the value of the href attribute that, as you can see, is a simple textual string.

Inserting counters

See my article on CSS counters.

Download examples

examples.zip

website designing delhi,website designing india,website designing,website design,websitedesignin.org,seo,search engine optimization,seo articels,website design delhi,website design india

Specifics on CSS Specificity

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This
This article was originally published on August 11, 2008. I am updating it now to fix some inaccuracies in how this concept was presented.

I’ve never specifically covered this subject before. (rimshot!)

The best way to explain it is to start with an example of where specificity gets confusing and perhaps doesn’t behave like you would expect. Then we’ll take a closer look at how to calculate the actual specificity value to determine which selector takes precedence.

 

Here is a simple unordered list:

<ul id="summer-drinks">     <li>Whiskey and Ginger Ale</li>     <li>Wheat Beer</li>     <li>Mint Julip</li>  </ul>

Now you want to designate one of these your favorite drink and change its styling a bit. You need a hook for this so you apply it via a class name on the list element.

<ul id="summer-drinks">     <li class="favorite">Whiskey and Ginger Ale</li>     <li>Wheat Beer</li>     <li>Mint Julip</li>  </ul>

Now you pop open your CSS and do your styling for your new class:

.favorite {    color: red;    font-weight: bold;  }

Then you take a look at your work, but alas, it didn’t work! The text of your favorite drink didn’t turn red or go bold! Something fishy is at work here.

Poking around more in the CSS, you find this:

ul#summer-drinks li {     font-weight: normal;     font-size: 12px;     color: black;  }

There is your trouble right there. Two different CSS selectors are telling that text what color and font-weight to be. There is only one statement for font-size, so clearly that one will take effect. These aren’t “conflicts” per-say, but the browser does need to decide which one of these statements to honor. It does so by following a standard set of specificity rules.

I think this confuses some beginners because they haven’t quite gotten this sorted out yet. They might think because the .favorite statement is “further down in the CSS” or because the is “closer to the actual text” in the HTML that will be the one that “wins”.

In fact, the order of selectors in your CSS does play a role and the “further down” one does in fact win when the specificity values are exactly the same. For example:

.favorite {     color: red;  }  .favorite {     color: black;  }

The color will be black… but I digress.

The point here is you want to be as specific as it makes sense to be every chance you get. Even with the simple example presented above, it will become obvious to you eventually that simply using a class name to target that “favorite drink” isn’t going to cut it, or won’t be very safe even if it did work. It would have been smart to use this:

ul#summer-drinks li.favorite {    color: red;    font-weight: bold;  }

That is what I’m calling “being as specific as it makes sense to be”. You could actually be way more specific and use something like this:

html body div#pagewrap ul#summer-drinks li.favorite {    color: red;    font-weight: bold;  }

But that is over the top. It makes your CSS harder to read and yields no real benefits. Another way to juice up the specificity value for your “.favorite” class is to use the !important declaration.

.favorite {    color: red !important;    font-weight: bold !important;  }

I once heard it said that !important is like the Jedi mind trick for CSS. Indeed it is, and you can force your will over the styling of elements by using it. But !important imposes that will through drastically increasing the specificity of that particular selectors property.

The !important declaration can be easily misused if misunderstood. It is best used to keep your CSS cleaner, in examples where you know elements with a particular class selector should use a certain set of styling no matter what. Conversely, not used just as a quick crutch to override the styling of something instead of figuring out how the CSS was structured and working by the original author.

One of my classic examples is:

.last {     margin-right: 0 !important;  }

I often use that in situations where there are multiple floated blocks, for the last block on the right in a row. That ensures the last block doesn’t have any right margin which would prevent it from butting snugly against the right edge of its parent. Each of those blocks probably has more specific CSS selectors that apply the right margin to begin with, but !important will break through that and take care of it with one simple/clean class.

Calculating CSS Specificity Value

Why is that our first attempt at changing the color and font-weight failed? As we learned, it was because simply using the class name by itself had a lower specificity value and was trumped by the other selector which targeted the unordered list with the ID value. The important words in that sentence were class and ID. CSS applies vastly different specificity weights to classes and IDs. In fact, an ID has infinitely more specificity value! That is, no amount of classes alone can outweigh an ID.

Let’s take a look at how the numbers are actually calculated:

In otherwords:

  • If the element has inline styling, that automatically1 wins (1,0,0,0 points)
  • For each ID value, apply 0,1,0,0 points
  • For each class value (or pseudo-class or attribute selector), apply 0,0,1,0 points
  • For each element reference, apply 0,0,0,1 point

You can generally read the values as if they were just a number, like 1,0,0,0 is “1000″, and so clearly wins over a specificity of 0,1,0,0 or “100″. The commas are there to remind us that this isn’t really a “base 10″ system, in that you could technically have a specificity value of like 0,1,13,4 – and that “13″ doesn’t spill over like a base 10 system would.

Sample calculations





Important Notes

  • The universal selector (*) has no specificity value (0,0,0,0)
  • Pseudo-elements (e.g. :first-line) get 0,0,0,1 unlike their psuedo-class brethren which get 0,0,1,0
  • The !important value appended a CSS property value is an automatic win. It overrides even inline styles from the markup. The only way an !important value can be overridden is with another !important rule declared later in the CSS and with equal or great specificity value otherwise. You could think of it as adding 1,0,0,0,0 to the specificity value.

Resources

website designing delhi,website designing india,website designing,website design,websitedesignin.org,seo,search engine optimization,seo articels,website design delhi,website design india
website designing delhi,website designing india,website designing,website design,websitedesignin.org,seo,search engine optimization,seo articels,website design delhi,website design india