bonny
bonny

Reputation: 3247

How to set one CSS rule for two tags?

I would like to know how I can set one setting for two different classes.

To give an example:

.footer #one .flag li .drop_down form div{
    width: 80px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    text-align: left;
    line-height: 1.5;
    color: #ccc;
    font-weight:bolder;
    margin-left: 30px;
}
.footer #two .flag li .drop_down form div{
    width: 80px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    text-align: left;
    line-height: 1.5;
    color: #ccc;
    font-weight:bolder;
    margin-left: 30px;
}

Both rules have the same content. The difference is just the second tag. Is there a way to do something like this?

.footer >>>#one and #two<<<< .flag li .drop_down form div{
        width: 80px;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 11px;
        text-align: left;
        line-height: 1.5;
        color: #ccc;
        font-weight:bolder;
        margin-left: 30px;
    }

Upvotes: 44

Views: 62960

Answers (5)

Liam
Liam

Reputation: 29654

Separate with a comma:

.footer #one .flag li .drop_down form div,
.footer #two .flag li .drop_down form div
{
   shared css
}
    
.footer #one .flag li .drop_down form div
{
   css for element one only
}
    
.footer #two .flag li .drop_down form div
{
  css for element two only
}

Upvotes: 2

MikeTedeschi
MikeTedeschi

Reputation: 583

.footer #one .flag li .drop_down form div, 
.footer #two .flag li .drop_down form div{
    ... 
}

Upvotes: 4

Rakesh Vadnal
Rakesh Vadnal

Reputation: 985

Use a comma instead of separate CSS rules:

.footer #one .flag li .drop_down form div,  
.footer #two .flag li .drop_down form div {  
    width: 80px;  
    font-family: Arial, Helvetica, sans-serif;  
    font-size: 11px;  
    text-align: left;  
    line-height: 1.5;  
    color: #ccc;  
    font-weight:bolder;  
    margin-left: 30px;  
}

Upvotes: 1

Pez Cuckow
Pez Cuckow

Reputation: 14412

In CSS you use a , (comma), unfortunately this is on the entire selector rather than just a section of it.

If you really wanted to make it cleaner you could give each of the form div's a unique id and then just #form1, #form2.

You can find further info in "Shorten the comma separated CSS selectors".

For example:

.footer #one .flag li .drop_down form div,
.footer #two .flag li .drop_down form div {
    width: 80px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    text-align: left;
    line-height: 1.5;
    color: #ccc;
    font-weight:bolder;
    margin-left: 30px;
}

Upvotes: 8

James Allardice
James Allardice

Reputation: 165951

Separate the selectors with a comma:

.footer #one .flag li .drop_down form div,
.footer #two .flag li .drop_down form div {
    /* Rules */
}

From the selectors level 3 spec:

A comma-separated list of selectors represents the union of all elements selected by each of the individual selectors in the list. (A comma is U+002C.) For example, in CSS when several selectors share the same declarations, they may be grouped into a comma-separated list. White space may appear before and/or after the comma.

Upvotes: 67

Related Questions