Menu Content

Currencies Accepted


Shopping Cart

cart
Your Cart is currently empty.

How to Download A Product

For most of our products, including the free ones, you will need to go through checkout and place an order.

Read More

 

Find Us Elsewhere

Follow us on Twitter

Spiral Scripts on Facebook

rapidssl_ssl_certificate
2Checkout.com is a worldwide leader in payment services

Google ads

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 5175 times so far.
Like this? Tweet it to your followers!

Latest articles from Spiral Scripts

  • New Image Slideshow for Joomla 1.6/1.7/2.5 posted on Monday, 23 January 2012 17:27

    We are happy to announce the release of our new image slideshow module for Joomla…

  • Mod Panel Slider Documentation posted on Saturday, 21 January 2012 10:42

    Our Panel Slider Module displays your images in an attractive and unusual scrolling slideshow. Display…

  • New Tool for Joomla Developers posted on Saturday, 14 January 2012 11:51

    Self-Replicating Form Field for Joomla There are times as a Joomla extension developer when it…

  • Variable Form Field Documentation posted on Thursday, 12 January 2012 13:24

    The variable field form field can be used to generate self-replicating fields of most of…

  • Virtuemart 2.0 Released posted on Thursday, 05 January 2012 11:16

    On 19th December the much anticipated Virtuemart 2.0.0 was released. This is a great achievement…

Latest 'tweets' from Spiral Scripts

  • Some useful Joomla mailing lists http://t.co/lBNEoAcK Link Wednesday, 01 February 2012 12:31
  • New Image Slideshow for Joomla 1.6/1.7/2.5 http://t.co/z9Utb8z9 Link Monday, 23 January 2012 18:14
  • New Image Slideshow for Joomla 1.6/1.7/2.5: We are happy to announce the release of our new image slideshow modu... http://t.co/bWb5VBPh Link Monday, 23 January 2012 17:57
  • New Tool for Joomla Developers http://t.co/jm4F3OlS Link Saturday, 14 January 2012 13:51
  • New Tool for Joomla Developers: Self-Replicating Form Field for Joomla There are times as a Joomla extension dev... http://t.co/myKPsmLh Link Saturday, 14 January 2012 12:17
blog comments powered by Disqus

 
 
 

VirtueMart Featured Products Grid

Switch View

This module can be used as a replacement for the Virtuemart featured products, top products, random products or recent products modules. It displays product images as a 3 Dimensional Flash slideshow.

£15.00


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

£12.00
This module 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.

£14.00


This module 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