0
<div id="tt">test1</div>
    <div id="blabla">test2</div>
<div id="test">
    <div id="blabla">test3</div>
    <div id="tt">test4</div>
</div>
<style>
    #tt {color:blue;}
    #blabla {color:green;}
    #test #tt, #test #blabla etc... {color:red;}
</style>

Is there a way to avoid repeating #test?

Thanks ;)

4
  • 1
    Is your example right? Duplicate id's and a reference to #t which isn't in your html. What are you trying to accomplish?
    – Hawxby
    Commented Feb 7, 2011 at 11:23
  • 3
    You should probably use a class.
    – nc3b
    Commented Feb 7, 2011 at 11:26
  • Hmm could you be more specific please? :) It's interesting
    – Somebody
    Commented Feb 7, 2011 at 11:30
  • You should be more specific. Your code is invalid in it's current state. as nc3b suggests, you should be using a class
    – Ross
    Commented Feb 7, 2011 at 11:59

4 Answers 4

2

For the particular example that was the question before it underwent major revisions:

* { color: red }

CSS is very much about context though. It isn't very good when dealing with hypotheticals that have little resemblance to the real code.

1
  • :D it will select all. I was talking about selecting variant.
    – Somebody
    Commented Feb 7, 2011 at 11:27
0

Depends on what you want to do. If you want all the text nodes to be red and the parent element is #test, then you just need #test {color:red}. If you need to be more specific, and give a color only for a child, then you can use .child {color:blue}, but if you want to color a child of #test, then you will need to specify the ancestor descendant, in other words #test .child {color:green}


Edit

Example according my comment http://jsbin.com/udoya3

4
  • I want to lessen code and avoid conflicts with # names of the rest css files. I have listed conflict situation in the example. If you will remove each #test in the styles, it will overlap the previous set of css rules
    – Somebody
    Commented Feb 7, 2011 at 11:29
  • You avoid "conflicts" if you don't apply multiple times rules with with the same properties. In your example there is not reason to write #test #tt, #test #blabla etc... {color:red;}, you just need #test * {color:red} and will be red the tt and blabla. Also keep in mind that in your HTML document you can use only time time an id, if you want to use it multiple times (such as tt & blabla you need the class attribute). Check the link my post.
    – Sotiris
    Commented Feb 7, 2011 at 11:45
  • And if there is somewhere another <div class="test"> about which you don't know and can't change it? :)
    – Somebody
    Commented Feb 9, 2011 at 9:40
  • OK i got the idea to use id once and then class after it.
    – Somebody
    Commented Feb 9, 2011 at 9:43
0

Instead of using id here, you should use id and class. An id should be unique : one id, one element. Class aren't unique. A class can have more than one element.

<style>
    .tt,.t {color:red;}
</style>

<div class="tt">test1</div>
<div id="test">
    <div class="tt">test2</div>
</div>
1
  • You didn't get the idea mate :) i know how to use css and that id should be uniq by standart
    – Somebody
    Commented Feb 7, 2011 at 11:46
0

#test, #t, #tt { color:red; }

is perfectly valid code.

an #id takes precedence over any other styling (ie a class) which can have adverse effects, but may also be desired.

what are you actually trying to do? save a few characters when writing your css?

Not the answer you're looking for? Browse other questions tagged or ask your own question.