How to set one CSS rule for two tags?

45,104

Solution 1

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.

Solution 2

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;
}

Solution 3

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

Solution 4

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
{
indvi css for one
}

.footer #two .flag li .drop_down form div
{
indvi css for two
}

Solution 5

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;  
}
Share:
45,104
bonny
Author by

bonny

Updated on July 09, 2022

Comments

  • bonny
    bonny almost 2 years

    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;
        }