0

i'm trying to style any .header child divs that inside any parent divs with 'recipes' as its class.

i know i can just get .category-recipes .header but they'll be a few parent div will change depending on where the user is. eg. 'category-recipes on category page, or 'single-page-recipes' on single posts page.

here's a simplified version of what a page looks like

<div class="category-recipes">

  <div class="otherdiv"></div>
  <div class="otherdiv"></div>
  <div class="header"></div>

</div> <!-- end of category -->

i tried it and it didnt work.

div[class*="recipes"] .header{
  margin-bottom: 40px;
}

is it possible to get a child or parent in with this kind of selector?

wsgg
  • 934
  • 1
  • 20
  • 43

3 Answers3

1

This works: https://jsfiddle.net/09zkqatx/

Basically, I've done all the same way you described.

div div {
    width: 50px;
    height: 50px;
    border: 1px solid black;
}

div[class*="recipes"] .header{
  margin-left: 40px;
}
Artur Udod
  • 4,465
  • 1
  • 29
  • 58
  • thanks, not quite sure whats wrong with mine but it worked with div[id*="recipes"] .archive-header{ margin-bottom: 40px; } had to change the div from using a class to id – wsgg Jul 26 '15 at 09:58
0
div[class*="recipes"] div[class*="header"]{
  margin-bottom: 40px;
}

Is a spin off of what I got from https://stackoverflow.com/a/2094554/4871483

You could just use JavaScript and select like this

var parDivs = document.getElementsByClassName("category-recipes");
for(parDiv in parDivs){
    var child = parDiv.children[2];
}

And change the style from JavaScript

Community
  • 1
  • 1
rassa45
  • 3,482
  • 1
  • 29
  • 43
0

You can achieve what you want- Use a separate common class for 'recipes' For example,

<div class="category-recipes recipes">
  <div class="otherdiv"></div>
  <div class="otherdiv"></div>
  <div class="header"></div>
</div> <!-- end of category -->

And also for your 'single-page-recipes',

<div class="single-page-recipes recipes">
  <div class="otherdiv"></div>
  <div class="otherdiv"></div>
  <div class="header"></div>
</div> <!-- end of single page -->

For the above codes, use the css -

div.recipes > div.header {
    margin-bottom: 40px;
}

OR, if you can't add a common class, you can use small JS snippet to achieve this -

var el = document.getElementsByClassName('header')[0];
// first check if the parent node has class with text recipes
if(el.parentNode.classList.toString().match(/recipes/)) {
    //get the first child with class header and then style it
    el.style.marginBottom = "40px"; 
}

check out the JSFiddle - http://jsfiddle.net/xLzagpmv/4/

Rohit Kumar
  • 2,048
  • 1
  • 14
  • 28