Menu Content

Shopping Cart

cart
Your Cart is currently empty.

Currencies Accepted


Newsletter

Subscribe to our newsletter


Name:

Email:

Like it? Share it!

Find Us Elsewhere



Follow us on Twitter

Home Joomla Tips Understanding CSS Selectors When Editing Joomla Templates

Understanding CSS Selectors When Editing Joomla Templates PDF Print E-mail

When editing a Joomla! template one of the biggest challenges can be finding the css style rules that apply to a particular element. It really helps if you understand the basics of css selectors, and how they are applied to css style rules.

Simple Style Rules

A style rule consists of two parts, a selector, and a series of style declarations, for example

p.example{color:green; font-size:12px}

The selector is p.example, the style declaration is {color:green; font-size:12px}.

There are three types of selectors: elements, classes, and ids, for example

p{color:green; font-size:12px} /*element selector*/

#content{background-color: #ccccff; margin:2px} /*id selector*/

.featureditem{border:2px red solid} /*class selector*/

These can be combined:-

div#content p.featureditem{ color:green; font-size:12px;background-color: #ccccff; margin:2px;border:2px red solid; }

This style rule applies to all <p> elements with a class of featureditem, which lie inside a <div> element with the id content, eg the html would look like

<div id="content">

<p class="featureditem">This is some example text</p>

</div>

Multiple Style Rules

An important point to realise is that multiple style rules can apply to a particular element. In a Joomla template there may be dozens or even hundreds of style rules, and any given element may be affected by several of these at the same time. Sometimes these rules may conflict, for example one rule may specify a text color of blue, while another specifies a text colour of red. Which one is is actually applied will depend on the order of precedence of the css selectors

When editing a template, a common requirement is to change an attribute such as the font-color or size for a selected group of elements. A common problem occurs when a change is made to the style rule, and nothing happens! This is the result of applying the style rule to the wrong group of selectors.

When deciding how to display a particular element, a web browser will weigh up these style rules, and implement all that apply to the element. In the case where there are conflicts it will apply those rules that are most specific to the element.

For example the html element

<p class="featureditem" id="featured">This is some example text</p>

Suppose the stylesheet contains the following rules

p{color:green; font-size:12px} /*element selector*/

#featured{color: red; margin:2px} /*id selector*/

.featureditem{color:blue; border:2px red solid} /*class selector*/

 

It may not be immediately obvious in which color the text will appear: in fact it will be red. This is because id selectors are always weighed as being the most specific, followed by class selectors, then element selectors, which are the most general.

Similarly when you have the html

<div id="featured">

<p class="featureditem">This is some example text</p>

<p>This is some more example text</p>
</div>

And the style rules

#featured p{color: red; } /*id selector*/

p.featureditem{color:blue;} /*class selector*/

You might expect that the text will be blue for the first paragraph, and red for the second, in fact it will be red for both paragraphs, because the #featured css selector is more specific, and hence overrides the other style rules.

If you want to turn the first paragraph blue, you will need to use the style rules

#featured p{color: red; } /*id selector*/

#featured p.featureditem{color:blue;} /*class selector*/

 

Applying This to Joomla Templates

This issue arises quite commonly with Joomla templates, because it is common for designers to label parts of the web page with id attributes, and apply style rules to these parts. For example, a common (simplified) structure for a template is to have a left and right column, a main content area, plus a header and footer area. These areas may have the ids (for example): leftcol, rightcol, content, header and footer.

Suppose you want to change the color of h3 headings in your main content to blue, when these have the class 'featureditem'. You might try adding the following style rule to your template css:

h3.featureditem{color:blue;}

However you find to your frustration and annoyance that this does not work. The likely explanation is that the template designer has set up a style rule such as

#content h3{color:black}

and this is overriding your new style rule. The solution is to amend your rule to

#content h3.featureditem{color:blue;}

In practice it can be quite difficult to track down the exact style rule that is applied to an individual elements, hence it may be quite difficult to find a new rule that will have the effect that you want. If all else fails, then you can use the !important construct. For example,

h3.featureditem{color:blue !important;}

This will override other rules. Note that the !important is applied to the individual declaration, such as color:blue. However I would not recommend using this too widely - it disrupts the normal calculation of css properties, and if used a lot can create more problems than it solves.

 

 


Spiral Scripts
Written on Monday, 18 January 2010 18:06 by Spiral Scripts

Viewed 555 times so far.
Like this? Tweet it to your followers!

Latest articles from Spiral Scripts

  • Browser Detection in Joomla posted on Monday, 08 March 2010 11:19

    It is often the case that as web developers we need to add some extra…

  • Browser Detection in Joomla posted on Monday, 08 March 2010 11:19

    It is often the case that as web developers we need to add some extra…

  • A Stormy Sea posted on Friday, 05 March 2010 19:43

    A stormy sea at Spittal in Northumberland. The dark storm clouds with occasional patches of…

  • The Sea at Spittal posted on Friday, 05 March 2010 19:38

    A view of the sea at Spittal in Northumberland. I like the contrast between the…

  • On the Sea Front posted on Friday, 05 March 2010 19:21

    A view of the sea-front at Spitall in Northumberland. It was a bright but very…

Latest 'tweets' from Spiral Scripts

blog comments powered by Disqus

 
 
 

VirtueMart Featured Products Grid

Switch View

A module extension for the the Joomla 1.5 CMS. It displays a short excerpt from articles in a selected category or section, or from a specified list of articles, with link and optional thumbnail image.

£12.00


A module extension for the the Joomla 1.5 CMS. It displays a short excerpt from articles in a selected category or section, or from a specified list of articles, with link and optional thumbnail image.

£10.00


A module extension for the the Joomla 1.5 CMS. It plays a random list of mp3s using the Flash media player.

£12.00


A Flash puzzle game for the Joomla! content management system. This would be suitable for a site aimed at children.

£10.00



 
 

fitness