0

i have an image in the background. i need to decide the color of some divs based on that. How can i achieve it directly in CSS instead of any script Or CSS framework

if (isBackImgTower){
    color:#fff; //(then i want to apply the color #fff)
}
else{
    color:#000;
}
Manoj Kumar
  • 19
  • 1
  • 3

3 Answers3

4

Simply NO. CSS does not support logics.

So you will need a css preprocessor like SASS/SCSS for simple logics like this.

Sample SCSS [This might not give the exact answer - But just to get an idea],

$isBackImgTower: true;

div {
  @if $isBackImgTower == true {
    color: #fff; 
  } @else {
    color: #000;
  }
}

But why not simply use a class with background-image property? Is there any reason?

Example,

div {
    color: #000;
}
div.isBackImgTower {
    background-image:url('images/tower.jpg');
    color: #fff;
}
Jerad Rutnam
  • 1,536
  • 1
  • 14
  • 29
3

You can't. But you can use this pattern to achieve that:

.background-tower{
    background-image:url('tower.jpg');
}

.background-sky{
    background-image:url('sky.jpg');
}

.background-tower, .background-tower a /* <--a tags inside tower */{
    color:#fff;
}

.background-sky {
    color:#000;
}

And almost your html should be like:

<div class='background-tower'>
    <a href="#">some text</a>
    some other text,...
</div>
Mohi
  • 1,776
  • 1
  • 26
  • 39
0

Looking first css medai query. If you cant find your wants, you use jquery or alternative. For example

if ($("#samplediv").attr("data-state") == "a value") {
$("#samplediv").addClass("class-for-a-value");
} else {
$("#samplediv").addClass("class-for-not-a-value");
}

that is a logical change css class. You cannot do it use only css