1

I need to apply a style to text in all children divs except the one that has a span with class .fa

.parentDiv > .column :not(.fa) {
  font-weight: bold
}
<div class="parentDiv">

  <div class="column">aaa</div>
  <div class="column">bbb</div>
  <div class="column">
    <span class="fa">ccc</span>
  </div>

</div>

I do need to keep CSS in one line as it's a part of a larger style sheet. How do I do that?

dippas
  • 58,591
  • 15
  • 114
  • 126
santa
  • 12,234
  • 49
  • 155
  • 255
  • 4
    obligatory [Is there a CSS parent selector?](http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) You need to override the style instead. something like `.parentDiv > .column { font-weight: bold }` and then `.parentDiv > .column .fa { font-weight: normal }` – Joseph Marikle Feb 09 '16 at 21:56
  • 4
    Any reason jQuery is included in the code example? – j08691 Feb 09 '16 at 21:56
  • Sorry, bad habbit. I use jQuery all the time in other issues. – santa Feb 09 '16 at 22:07

3 Answers3

1

The only way to do this is to use

.parentDiv > .column { font-weight: bold }
.parentDiv > .column > .fa { font-weight: normal }
Farzad Yousefzadeh
  • 2,461
  • 1
  • 20
  • 27
  • 1
    This assumes that if a `.column` has a `.fa`, it won't have other contents without `.fa`. – Oriol Feb 09 '16 at 21:58
1

You can use :not with :last-of-type

Snippet

.parentDiv > .column:not(:last-of-type) {
  font-weight: bold;
  color:red;
}
<div class="parentDiv">

  <div class="column">aaa</div>
  <div class="column">bbb</div>
  <div class="column">
    <span class="fa">ccc</span>
  </div>

</div>

EDIT based on @Oriol's comment

:not(:last-of-type) does not mean "doesn't contain a .fa

which makes sense, if your code could be dynamic.

here is another approach:

Snippet

.parentDiv > .column span:not(.fa) {
  font-weight: bold;
  color: red;
}
<div class="parentDiv">
  <div class="column">
    <span class="gsdfasd">text</span>
  </div>
  <div class="column">
    <span class="faadsasfa">some text</span>
  </div>
  <div class="column">
    <span class="fa">this will NOT BE red</span>
  </div>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
0

The way the not() selector works is that you have to target a specific element/class/ID. You would have to wrap the text from each column in a span and then add the :not(.fa) CSS to the span:

.parentDiv > .column span:not(.fa) {
  font-weight: bold
}
<div class="parentDiv">

  <div class="column">
    <span>aaa</span>
  </div>
  <div class="column">
    <span>bbb</span>
  </div>
  <div class="column">
    <span class="fa">ccc</span>
  </div>

</div>
APAD1
  • 13,509
  • 8
  • 43
  • 72