Applying CSS styles to all elements inside a DIV

HtmlCss

Html Problem Overview


I would like to apply a CSS file to a concrete DIV in my page. This is the page structure:

<link rel="stylesheet" href="style.css" />
...
<body>
   <div id="pagina-page" data-role="page">
   ...
   <div id="applyCSS">
      (all the elements here must follow a concrete CSS rules)
   </div>
   ...
</body>

I tried to apply the rules of the CSS file editing it like this (the CSS file is so large):

#applyCSS * {     (For all the elements inside "applyCSS" DIV:)
    .ui-bar-a {
       ...
       ...
    }
    .ui-bar-a .ui-link-inherit {
       ...
    }
    ...
}

But that solution doesn't work. So, how can I do that?

Html Solutions


Solution 1 - Html

#applyCSS > * {
  /* Your style */
}

Check this JSfiddle

It will style all children and grandchildren, but will exclude loosely flying text in the div itself and only target wrapped (by tags) content.

Solution 2 - Html

You could try:

#applyCSS .ui-bar-a {property:value}
#applyCSS .ui-bar-a .ui-link-inherit {property:value}

Etc, etc... Is that what you're looking for?

Solution 3 - Html

.yourWrapperClass * {
 /* your styles for ALL */
}

This code will apply styles all elements inside .yourWrapperClass.

Solution 4 - Html

I do not understand why it does not work for you, it works for me : http://jsfiddle.net/igorlaszlo/wcm1soma/1/

The HTML

<div id="pagina-page" data-role="page">
    <div id="applyCSS">
    <!--all the elements here must follow a concrete CSS rules-->
        <a class="ui-bar-a">This "a" element text should be red
            <span class="ui-link-inherit">This span text in "a" element should be red too</span>
        </a>      
    </div>
</div>

The CSS

#applyCSS * {color:red;display:block;margin:20px;}

Maybe you have some special rules that you did not share with us...

Solution 5 - Html

If you're looking for a shortcut for writing out all of your selectors, then a CSS Preprocessor (Sass, LESS, Stylus, etc.) can do what you're looking for. However, the generated styles must be valid CSS.

Sass:

#applyCSS {
    .ui-bar-a {
       color: blue;
    }
    .ui-bar-a .ui-link-inherit {
       color: orange;
    }
    background: #CCC;
}

Generated CSS:

#applyCSS {
  background: #CCC;
}

#applyCSS .ui-bar-a {
  color: blue;
}

#applyCSS .ui-bar-a .ui-link-inherit {
  color: orange;
}

Solution 6 - Html

Write all class/id CSS as below. #applyCSS ID will be parent of all CSS code.

For example you add class .ui-bar-a in CSS for applying to your div:

#applyCSS .ui-bar-a  { font-size:11px; } /* This will be your CSS part */

Below is your HTML part:

<div id="applyCSS">
   <div class="ui-bar-a">testing</div>
</div>

Solution 7 - Html

Alternate solution. Include your external CSS in your HTML file by

<link rel="stylesheet" href="css/applyCSS.css"/> 

inside the applyCSS.css:

   #applyCSS {
      /** Your Style**/
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1702964View Question on Stackoverflow
Solution 1 - Htmluser1467267View Answer on Stackoverflow
Solution 2 - HtmlArkanaView Answer on Stackoverflow
Solution 3 - HtmlAdem BüyükView Answer on Stackoverflow
Solution 4 - HtmlIgor LaszloView Answer on Stackoverflow
Solution 5 - HtmlcimmanonView Answer on Stackoverflow
Solution 6 - HtmlNileshView Answer on Stackoverflow
Solution 7 - HtmlRED.SkullView Answer on Stackoverflow