0

I have a nested css class:

.a {
  b.c {
   color: red;
 }
}

How can I access to 'b.c' class? I am using <p class="a.b.c"></p>, but cannot work.

Thanks.

6
  • You can't nest classes in plain CSS Commented Aug 1, 2017 at 20:43
  • Yes, I am using Less Commented Aug 1, 2017 at 20:46
  • @alex I already use LESS, I want to know the solution. So, this is duplicate? Commented Aug 1, 2017 at 20:48
  • In CSS, you access a class like so .a. In HTML, it's like this class="a". Do you see the difference? Commented Aug 1, 2017 at 20:50
  • @alex his question is really an issue of syntax. Marking it as a duplicate won't reveal the true issue. Commented Aug 1, 2017 at 20:53

4 Answers 4

2

Your code:

.a {
  b.c {
   color: red;
 }
}

Can be accessed via

<div class="a"> <!-- parent with class "a" -->
  <b class="c">This text will be red</b> <!-- <b> tag with class "c" -->
</div>

Explanation

In less, when you nest as you are, you are saying that the nested rules only apply to elements that are inside an element that matches the selector of it's parent. The rule you wrote is looking for any <b> tag that has a class c which has a parent with class a

The CSS that would be output from your code would be

.a b.c {color: red;}

If you wanted to access one element, and as your broken HTML indicates, have the b be a class instead of an element selector, then you could make your LESS:

.a {
  &.b.c {
   color: red;
 }
}

Can be accessed via

<p class="a b c">
  This text will be red
</p>
Sign up to request clarification or add additional context in comments.

Comments

1

For what I know, you can't have dots in class names. To assign multiple class names to an element, seperate them by a space.

<p class="a b c"></p>

You can now access them like this:

.a.b.c {
    color: red;
}

Or like that:

.a {
   &.b.c {
    color: red;
   }
}

Comments

0

To select any elements with classes "b" and "c" such as

<p class='a b c'>hello</p>

Use this selector which selects it because it matches classes b and c

.b.c {
  color: red;
}

The . is used in CSS selectors, but in HTML don't use . in class names.

2 Comments

Where is a in your css define?
Maybe I'm not understanding your question. There doesn't have to be a selector for 'a' in the css because there are other classnames that can be used to select that element. What exactly are you trying to accomplish?
0

Your less isn't currently written in a way to accomplish what you want. The b isn't considered a class but an element, so it wouldn't do what you need. You need to change it like this:

.a {
  .b.c {
   color: red;
 }
}

But with this currently configuration, the only way to access it would be to do something like this <p class='a'><span class="b c">hello</span></p>.

If you want it to work like your example, then you should follow Chrisstar's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.