Put css class directly on element or its parent - html

I have a div and I need to color one of its children div. I was thinking of setting a CSS class to the parent div instead of directly on the children div since I have a javascript class that already have a reference to the parent, so I don't need to lookup for the children.
Is it a bad pratice ? Could it cause me trouble in the future ?
Here an example http://codepen.io/anon/pen/pJgzJp?editors=110
html:
Is this better
<div class="parent1 whatIPrefer">
<div class="children1">
</div>
</div>
than
<div class="parent2">
<div class="children2 meh">
</div>
</div>
css
.parent1{
width:200px;
height:200px;
}
.children1{
width:200px;
height:200px;
}
.whatIPrefer .children1{
background-color: gold;
}
.parent2{
width:200px;
height:200px;
}
.children2{
width:200px;
height:200px;
}
.meh{
background-color: tomato;
}
More Context: I want to display problematic items in a red in my page. There could be many items colored that way, my javascript code to colour it look like this
for (var i = 0; i < this._report.WorksheetSectionIDs.length; i++) {
var worksheetSection = this._report.GetWorksheetSection(i);
if (worksheetSection._worksheet._grid.getColumns().length != columnsCount) {
this.Errors.push("Worksheet " + worksheetSection._sectionID + " doesn't have the right number of columns.");
worksheetSection.SetInErrorState();
}
}
where each worksheetSection has a reference to the parent element, so I can easily add a class to it.

Your situation is largely dependent on your use case. You did not provide much context, so this may be the best or not the best application for targeting your child div.
You can access the child of a div using the child selector for css:
.whatIPrefer > div { styles }
Here is an excellent article on selecting children of a parent element - check it out.
Hope this helps. Please comment below with any other questions. Thanks

It depends on web structure, and on elements, that you are styling. For example, if you know, that header will be just one, you can easilly style directly the header, but if there is more headers with different styles, for example header in body, header in head and so on. In that case it is good to style throw parent element.
In my projects, i always style throw parent elements, sometimes it is useless, but it is a good practise and you never know, when your web will need new elements.

Related

Add content to DOM elements added later w/o JavaScript

Is there a way to have same content (inner text/html) to DOM elements with a specific class that are added later.
Take the example of CSS:
.red {
background-color: red;
}
If a DOM element is added in future with a class .red, it's background color will be red.
Can I use a similar technique for the content of the DOM element. Say, all elements with a class of data-amount should have content 40. So if any element has this class, even if added later in the DOM, should show the content 40:
<div class="data-amount"></div>
Is this possible without using JavaScript?
You can use the CSS pseudo-element ::before:
.data-amount::before {
content: '40';
}

Reset the style of a HTML element

I have a webpage with elements, styles (imported and inline)
I want to reset the style for a specific element.
Example:
HTML:
<div class="parent">
This is the parent div, it colors the <strong>strong in red</strong>
makes a <small>small underlined</small>
<h4>sets a margin-left 10px for a H4</h4>
and many other stuff<br><br>
<div class="child">
this is the child element<br>
here a <strong>strong should not be red</strong><br>
<small>small should not be underlined</small>
<h4>H4 should not have a margin-left</h4>
and so on...
</div>
</div>
CSS:
.parent strong{
color:red;
}
.parent small{
text-decoration: underline;
}
.parent h4{
margin-left: 10px;
}
I want the child div to ignore the styles coming from his parents, including the html element
Here is an illustration of my example
The styles I gave here are just examples, there are much more
I cannot modify the parent CSS, is being dynamically generated
My child div is injected in the page, I can also inject any CSS I want
I cannot know in advance the content of the parent CSS
The only solution I found so far is including the child element in an Iframe, but is really really ugly!!
Any one can help how to achieve this? A JS solution is also acceptable.
.child strong{
color:pink !important;
}
1.You adjust the injecting code css via !important.
2.Even though you can't predict the css of the parents you can only have some basic CSS thing for your injected code.
Example
You can use css immediate child selector '>'
in your example
.parent>h4{
margin-left: 10px;
}
.parent>strong{
color:red;
}
check the updated demo
http://jsfiddle.net/WRDft/11/
Refer: http://msdn.microsoft.com/en-in/library/ie/aa358819(v=vs.85).aspx
CSS '>' selector; what is it?
This question has already been asked and discussed.
There is no way to blanket clear styles but there are work arounds.
Reset/remove CSS styles for element only
If I am understanding you correctly and if you know what content is being injected into your child div then the JQuery solution is very simple:
$(".child strong").css({"color":"black"});
$(".child small").css({"text-decoration":"none"});
$(".child h4").css({"margin-left":"0"});
The JQuery code can then be wrapped in any sort of function you desire.
Here is your fiddle with the JQuery added. Hope that helps.
Note: the JQuery selector - for example: $(".child strong") - can be as specific or as general as you like and you can add as many css rules as you like by using a comma separated list like this:
$(".child strong").css({"color":"black", "font-weight":"bold", "text-decoration":"underline", etc, etc});
Thank you all for your thoughts guys, unfortunately, the best way I managed to achieve this is by wrapping my content inside an IFrame
Advantage: Immediate and easy reset
Disadvantage: I cannot manipulate the elements outside of the IFrame

Change styling when add new sibling in pure CSS

I have a container with children:
<div id="container">
<div></div>
<div></div>
<div></div>
</div>
Sometimes I want to add another child (server-side) into the container and change the styling of the other children to make room for their new sibling. It would become:
<div id="container">
<div id="special"></div>
<div></div>
<div></div>
<div></div>
</div>
Can I achieve this purely in CSS? Are there fancy new selectors (CSS4?) that I can use?
I want to avoid JavaScript or changing the attributes (classes) of any of the elements. I did read about the lack of parent selectors in CSS but the articles I read are a couple of years old, so I suspect there might be some brand new selectors I can take advantage of.
#container #special {
float: left;
background: yellow;
}
#container div {
/* already float: none; by default */
background: white;
}
is the simplest way to style elements differently in CSS: all div are styled because of the second rule (whether or not the special element exists or not) and the first rule will apply to one special element if it exists, with more specificity than the first one. Both rules will apply so properties in the first rule should override those in the second rule.
Now if you want to style the other divs differently when the special element exists:
In your example, no div precedes the special one so you don't need a preceding sibling selector; using the general sibling selector is sufficient:
#container div {
padding: 5px;
}
#container #special ~ div {
padding: 10px;
}
Could the special element be also created in 2nd, 3rd, etc position?
Then a similar trick to selecting first half of the elements could be used (combination of :nth-child()/:nth-last-child(), but it has limitations, like an upper bound of elements to be set and it won't work with more elements than that - and the selector will be looooong and relatively inefficient. Will gzip really really well though :) )
No.
This won't ever be supported by CSS. Even with advanced selectors in CSS4, they are selectors. They select something that is in the DOM. They can't produce a new HTML element.
To do this in jQuery, just use the before() method
$("div.special").before("#container div:first")
I'm guessing you could try some sibling selectors along with not selectors... something like:
.special+div:not(.special){ /*some special style here*/ }
.special+div+div:not(.special){ /*some special style here*/ }
.special+div+div+div:not(.special){ /*some special style here*/ }
up to n divs...
.special+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div+div:not(.special){ /*some special style here*/ }
if you have access to scss you can do something like:
#for $i from 1 through X { //REPLACE X WITH A NUMBER
.special #for $j from 1 through $i {+div}:not(.special):{/*some special style here*/};
}
I'm just guessing about the scss formula, but it should be something like that :D
Also, depending on what you want to achieve there might be other options. Could you be more specific on the styles you want to apply?

How can I set css for a class in a class?

With this HTML code.
<div class="noote">
<p class="first admonition-title">Noote</p>
<p class="last">Let's noote.</p>
</div>
How can I set the color of Noote to be red with css?
I mean, how can I set something for (div class="noote") and (p class="first") under it with css?
Try this:
/*this will apply for the element with class first
inside the element with class noot */
.noote .first{
color:red;
}
/* If you wanted to apply a rule for both individually
you can do: */
.noote, .first{
border:1px solid red;
}
div.note{
...
}
Refers to the div element that has the class note.
p.first{
...
}
Refers to the p element that has the class first.
div.note p.first{
...
}
Refers to the p element inside note that has the class first.
In addition, if you want to set an element child without setting a class to it,
div.note p:first-child{
/* it refers to the first p that contains noote */
}
#amosrivera's got it.
It's worth nooting that descendant selectors require more CPU. I always use more specific rules where possible. So instead of
.noote .first{
backgorund:red;
}
You could say
.noote > .first{
backgorund:red;
}
A nominal difference in most cases, but still a good habit.
Really?
Descendant selectors are
inefficient... The less specific the
key, the greater the number of nodes
that need to be evaluated.
— Google "Let's make the web
faster" docs
And
Descendent selectors are a major slow
down to the Safari browser
— John Sykes, May 2008
Some performance tests show little impact, and most writers agree that it only makes a difference on very large documents.
But mainly, I'm just going with my programmer instinct — say what you mean, and no more.

Styling clean div tags

In stylesheets you often see div#id { /* something */ } and div.class { /* ... */ } but how often do you see just div { /* something */ }?
Is it a bad idea to style div tags that have no #id or .class associated with them?
It's not necessarily bad practice, as long as you're sure you want to apply this styling to every single div in your document. You can always override and / or add further down the cascading style sheet.
It all depends on your purpose.
Styling all divs to be one specific style can be overridden.
So you may want to force height on all divs, but on divs with class hidden you want display none. Finally you may want a div with id = hello to have a red background.
Next you decide that you want a div with id=foo and class = bar to be have height:200.
div {
height:100px;
}
div.hidden {
display:none;
}
div#hello {
background-color:#FF0000;
}
div#id.bar {
height:200px;
}
Well...it depends on what you want. If you want every single div tag in your markup to have the same style then it makes sense to do a tag selector instead of a class or id selector.
As you normally use divs for a bunch of different stuff, I would answer "yes".