1

I'm creating the following elements:

    .dh-tl-tl {}
    .dh-tl-tr {}
    .dh-tl-br {}
    .dh-tl-bl {}

    .dh-tr-tl {}
    ...

    .dh-br-tl {}
    ...

    .dh-bl-tl {}
    ...

Is there a way I could get a CSS selector to get the 4 items in each group?

e.g.

    [class=(?<=.dh-)(.*)(?=-bl)] {}
2
  • I believe this is possible. What is the expected output Commented Jul 26, 2016 at 20:15
  • 1
    I'd like to select all the .dh-*-tl, for example, or .dh-*-br. Commented Jul 26, 2016 at 20:18

3 Answers 3

10

You could use the ^= ('starts with') and $= ('ends with') attribute selectors.

[class^="dh-"][class$="-bl"]{}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer.
3

It sounds like you are trying to select all elements whose class name begins with an expression and also ends with another expression.

There are two CSS selectors that can be used for this

  • [attribute*="value"] - Attribute begins with selector
  • [attribute$="value"] - Attribute ends with selector

[class^="dh-"][class$="-tl"] { color: red; }
[class^="dh"][class$="-tr"] { color: orange; }
[class^="dh"][class$="-bl"] { color: green; }
[class^="dh"][class$="-br"] { color: blue; }
<p class="dh-tl-tl">This is group 1, it is colored red</p>
<p class="dh-tl-tr">This is group 2, it is colored orange</p>
<p class="dh-tl-bl">This is group 3, it is colored green</p>
<p class="dh-tl-br">This is group 4, it is colored blue</p>

<p class="dh-tr-tl">This is group 1, it is colored red</p>
<p class="dh-tr-tr">This is group 2, it is colored orange</p>
<p class="dh-tr-bl">This is group 3, it is colored green</p>
<p class="dh-tr-br">This is group 4, it is colored blue</p>

<p class="dh-bl-tl">This is group 1, it is colored red</p>
<p class="dh-bl-tr">This is group 2, it is colored orange</p>
<p class="dh-bl-bl">This is group 3, it is colored green</p>
<p class="dh-bl-br">This is group 4, it is colored blue</p>

<p class="dh-br-tl">This is group 1, it is colored red</p>
<p class="dh-br-tr">This is group 2, it is colored orange</p>
<p class="dh-br-bl">This is group 3, it is colored green</p>
<p class="dh-br-br">This is group 4, it is colored blue</p>

http://www.w3schools.com/cssref/sel_attr_begin.asp

Comments

1

use this

if element like div then use and class begin 'dh-', you can use.

div[class*='dh-']

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.