0

Lets say I have:

<div class="container">
<p>blah blah blah</p>
</div>

would I be able to use 2 :before pseudo classes in that same container div?

like so:

  .container:before{
    content: " ";
    position: absolute;
    top: 0;
    left: 0;
    width: 31%;
    height: 61%;
    background-color: black;
  }

.container:before{
    content: " A String ";
    position: absolute;
    top: 0;
    left: 0;
    background-color: black;
  }

Ignore the actual css but the question here is would that be valid inserting 2 pseudo classes before that container div? and if not is there a way that it can be?

Lucas Santos
  • 1,359
  • 3
  • 24
  • 43
  • 1
    Why don't you try and let us know? – Amar Syla Apr 12 '15 at 22:01
  • Well I tried and it did not come out correctly but I ask because there may be something someone knows that can make it work or some info on that.. Just getting more info on people with experience with that pseudo class. – Lucas Santos Apr 12 '15 at 22:04
  • Duplicate: http://stackoverflow.com/questions/6065958/defining-css-properties-twice – nZeus Apr 12 '15 at 22:13

2 Answers2

3

No, you can't use two :before instanced in a single element. Here is a non-working jsfiddle, which makes use of two :before instances on a single div. As you can see, the second :before statement overrides the first one's styles.

http://jsfiddle.net/Luwhf700/

Now, here, we have a working one, using :before and :after, which would be similar to using two :before statements (only for absolute positioning elements).

http://jsfiddle.net/Luwhf700/1/

Amar Syla
  • 3,523
  • 3
  • 29
  • 69
  • I see ok thanks, just out of curiosity on that jsfiddle you used left:auto and it positioned the div all the way to the rite. why is that? what did auto do to left? – Lucas Santos Apr 12 '15 at 22:14
  • I used `left: auto` for the first case, where two `:before` uses didn't work. If we remove `left: auto` from the second `:before` use, it will inherit `left: 0` from the first :before use, and `right: 0` won't work to position the element to the right. So, I am resetting the left value with `left: auto`, so I can position the element to the right. If this answer helped, can you please vote up and / or mark as correct answer? – Amar Syla Apr 12 '15 at 22:16
  • so left: auto positions the elements as if it were to be right:0; ? – Lucas Santos Apr 12 '15 at 22:18
  • No, I used `left: auto` just to reset the `left: 0` which was applied to the first `:before` I used. As you can see, in the second `:before`, I've used `right: 0`, but if we don't add `left: auto, it won't be position to the right, because `left` will have the value of `0`, from the upper `:before`. I hope I am clear. If not, you can make a new question, and I can add a more detailed answer to it. – Amar Syla Apr 12 '15 at 22:22
  • Ahh I see ok now I understand. Thanks a ton Amar appreciate it! – Lucas Santos Apr 12 '15 at 22:25
0

It cannot be done, since the css is overwritten, you can see the same here Also Amar Syla answered the same

if not is there a way that it can be?

You can use java script/ jquery to add a class or an id to the div based on your condition and the set the css.

For example:

#before1.container{
    content: " ";
    position: absolute;
    top: 0;
    left: 0;

    background-color: red;
  }

#before.container{
    content: " A String ";
    position: absolute;
    top: 100px;
    left: 0;
    background-color: green;
  }

Then you just need to set the id like below, using jquery/javascript

<div id="before" class="container">
<p>blah blah blah</p>
</div>


<div id="before1" class="container">
<p>blah blah blah</p>
</div>

To know how to set div id dynamically, check this link It has both java script and jquery solutions

Community
  • 1
  • 1
Polynomial Proton
  • 5,020
  • 20
  • 37