Unset css property in css. Is it possible?
Let me explain the problem with the css code.
We have two classes at hand: .whenRolledUp and .whenRolledDown.
I want my element to comply with the following rules:
1 When an element with .whenRolledUp class is positioned in DOM hierarchy and have an element with a class .rolledDown somewhere above itself in the hierarchy I would like to disable the element. Otherwise, I would like to do nothing to the display of the element. I am able to satisfy this with the help of this css:
.rolledDown .whenRolledUp {
display: none;
}
2 When an element with .whenRolledDown class is positioned in DOM hierarchy and have an element with a class .rolledDown somewhere above itself in the hierarchy I would like to do nothing to the display of element. Otherwise, I would like to have display: none for the element. I am able to satisfy the second part using this:
.whenRolledDown {
display: none;
}
But here is the catch. I can not satisfy the first part by just using this:
.rolledDown .whenRolledDown {
display: block;
}
Because here I am actually setting the display to block, while it may have a different value without this css.
It seems like the problem is impossible to solve with only css. Is it so?
You can use display: unset:
The unset CSS keyword resets a property to its inherited value if it
inherits from its parent, and to its initial value if not.
function add() {
document.querySelector('.target').classList.add('rolledDown');
}
.whenRolledUp {
display: none;
}
.rolledDown .whenRolledUp {
display: unset;
}
<div class="target">
<div class="whenRolledUp">whenRolledDown</div>
</div>
<button onClick="add()">Add rolledDown</button>
Related
Just wondering, can I make CSS inheritance skip a generation?
For example if I had this code:
Code:
.grandfather {
background-color: #fff;
}
.parent {
background-color: #333;
}
.child {
background-color: inherit;
}
<div class='grandfather'>
<div class='parent'>
<div class='child'>
Is there a way for the inherit command to take the background-color from the grandfather and completely ignore the parent? Or would i need to use a variable for that?
The short answer is no, you cannot. The inherit property always takes its value from its parent.
You can read more here: CSS inherit property
It touches on this issue with a specific note:
Note: Inheritance is always from the parent element in the document
tree, even when the parent element is not the containing block.
The simple solution is to have a class in the color you wish to use e.g. .blue {background: blue;} and add that into the HTML or use CSS custom properties (CSS variables) to effectively do that too.
I have a div within a webpage I am trying to target with the following code to create a flexbox:
div.my-div-class {
display: flex;
flex-wrap: wrap;
}
div.my-div-class > label {
fl
Originally I had a problem with the User Agent Styles overriding the div and causing it to automatically display block. I fix that per this question by adding the following code:
div {
display: inherit;
}
Which I assumed, perhaps naively, that this would cause the div to "inherit" the styles of what I set to the class.
I check the console, and sure enough see:
div { display: inherit; }
instead of what was there before for the User Agent which was:
div {display: block;}
Which is what I assumed was messing with my style originally.
I tried !important to see if that would at least cause a change and it didn't.
So I'm thinking I don't fully understand the behavior of inherit or how to target this particular div correctly.
Can someone explain this a little bit? I should mention this div is wrapped in a form, and the HTML of that form is like below:
<div id="form-container">
<form id="form">
<div class="my-div-class" id ="the-target-div">
/*Rest of the HTML*/
</div>
</form></div>
Generally you don't really need to target the div tag, you should instead use a class.
If you are creating your own CSS and not using some library, such as bootstrap, it's a good idea to use a CSS reset to make sure you are writing CSS on a clean slate. This is a popular one.
To answer your question, the inherit property sets a css property to inherit the value from its parent. A div tag by default is a block level element, so setting anything to inherit below it will also set it to display: block.
Just target whatever you need to be flex with the class name, such as:
.my-div-class {
display: flex
}
It seems that you didn't copy all of your html code, cos it looks like it's broken in the middle.
If you want to target this particular div you should do it by refering to it's class or id. Property value "inherit" inherits ONLY the property from its parent element that it is set as a value to.
For example:
.parentElement {
display: flex;
background: yellow;
height: 200px;
width: 100%;
}
.childElement {
display: flex;
height: 100px;
width: 70%;
background: blue;
}
.childElement:hover {
height: inherit;
}
<div class="parentElement">
<div class="childElement">
</div>
</div>
In this example when we hover over the child element we are setting height value to "inherit" which inherits the value ONLY for height property, but the width for example doesn't change.
In short: if you want your div to inherit all styles his parents has you should set "inherit" as a value for every property it's parent has.
I have the following code:
<div class = "badge">
<div class = "badge-header">
</div>
</div>
What's the proper way to style badge-header in css?
Is it
.badge .badge-header {
}
or
.badge-header {
}
also how do we structure our css names? How do we structure our divs and what selectors should we use for each above?
If you have a div inside a div, what is the naming convention that we should stick to in CSS?
Both
but the first will apply the style only when the "badge-header" is inside of a "badge"
in the second will apply for all "badge-header" elements.
See example: https://jsfiddle.net/6bvLtqLw/
CSS
.badge-header{
color:blue;
}
.badge .badge-header{
background-color: yellow;
}
HTML
<div class="badge">
<div class="badge-header">
inside
</div>
</div>
<div class="badge-header">
outside
</div>
Both work
.badge .badge-header {
}
above one applies style to those elements with class '.badge-header' AND are under elements with class '.badge'
.badge-header {
}
and the above one applies style to all the elements with class '.badge-header' regardless of element's position in DOM.
Both
but the first will apply the style only when the "badge-header" is inside of a "badge"
.badge .badge-header {
}
in the second will apply for all "badge-header" elements.
.badge-header {
}
The first variant obviously will only style .badge-header if used in the context of a .badge. The second will apply to all .badge-headerregardless of their context.
If you are certain .badge-header will never need to be used outside of .badge, you go with variant two as it is shorter and more concise.
If it might be useful to reuse the .badge-header in different context than only .badge, then use *both variants. Put all styles that all .badge-header have in common in .badge-header { ... }. Put the context-dependent styles in .badge .badge-header { ... }.
Here is the html layout
<div class="wrap">
<div class="section">
<span class="text">text1</span>
</div>
<span class="text">text2</span>
<span class="not-text">don't color me!</span>
</div>
Im trying to give a style to all "text" spans which are not in the "section" divs.
I tried this, but it doesn't seem to be working
.wrap :not(.section) .text
fiddle
Any help is greatly appreciated!
Edit: There are several workarounds for this case including the use of > operator like in ".wrap > .text", but I would like to know how to make the :not selector working, to make use of it in the future
When you use .wrap :not(.section) .text, this is what you're telling the browser:
Look for an element with a class of .text
which lives inside an element that does not have a class of .section
which lives inside
an element that has a class of .wrap
If you look closely at your markup, none of your elements meet that selector criteria.
In the markup you provided, I don't think you can specifically select those .text elements that are not descendants of .section using :not().
The closest you could get is by using a direct descendant selector, like this:
.wrap > .text
You could also select and style all .text descendants of .wrap (whether direct or not), and then cancel those styles for any .text elements inside of .section, like this:
.wrap .text {
// your styles here
}
.wrap .section .text {
// your cancelled styles here
}
You can probably use:
.wrap > span
.wrap > *:not(div)
.wrap > *:not(.section)
Best option given the constraints of CSS are to write a rule for all text and then override them back to the previous value for those within .section. So
.wrap .text {
// the styles you want
}
.wrap .section .text {
// styles undoing the styles above... depending on what the styles are it may not always be possible
}
Using a GWT web app, Firebug says that the following HTML
<table class="drop-zone drop-zone-column-66 multi-zone">
...
</table>
is using this CSS.
.maximized-gadget .drop-zone.multi-zone, .configure-tab a {
display: block;
}
What CSS do I need to write so that this <table> will have style, display: none?
I made 2 attempts: [EDIT - updated .multi-zone and display:none]
.drop-zone .drop-zone-column-66 .multi-zone {
display: none;
}
and
.maximized-gadget .drop-zone.multi-zone, .configure-tab a {
display: none;
}
but Firebug still gives me the CSS shown at the top.
Please advise me.
Strictly speaking, all you should need is:
.maximized-gadget .drop-zone.multi-zone {
display: none;
}
provided that that rule comes after the original rule you gave above:
.maximized-gadget .drop-zone.multi-zone, .configure-tab a {
display: block;
}
Depending on what the structure of the rest of your document is and what you're trying to do, you may need to add some specificity to that rule.
The problem with your first attempt is that your rule would apply to an element with a class of multi-zone which is a descendant of an element of class drop-zone-column-66, which in turn is a descendant of an element of class drop-zone. What you want is to target an element that has all three of those classes set on it, which you can do by chaining those selectors:
.drop-zone.drop-zone-column-66.multi-zone {
display: none;
}
which should set you right (though if I remember correctly this won't work in older versions of IE).