Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a selector to exclude display: none elements?

Tags:

html

css

display

I want to select only <buttons> whose parents have display: block and exclude those <buttons> whose parents have display:none.

Is there any way to accomplish this?

like image 562
alex Avatar asked Oct 17 '17 09:10

alex


People also ask

How do you counter display none?

The Best Answer is. display: none doesn't have a literal opposite like visibility:hidden does. The visibility property decides whether an element is visible or not. It therefore has two states ( visible and hidden ), which are opposite to each other.

How do you exclude elements in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How do I select display none in CSS?

Actually there's a CSS3 solution to select elements that doesn't have a display:none style, or given an explicit style property: *:not([style*="display: none"]) button{ ... } It may be a solution, but not at all a good recommendation on standard as you are using inline styling.


3 Answers

Actually there's a CSS3 solution to select elements that doesn't have a display:none style, or given an explicit style property:

*:not([style*="display: none"]) button{ ... }

Demo:

*:not([style*="display: none"]) button{
    color:yellow;
}
<p style="display:block">
  My name is A.
  <button>
a
</button>
</p>
<p style="display: none">
  <button>
b
</button>
</p>
like image 81
cнŝdk Avatar answered Oct 04 '22 13:10

cнŝdk


If those display styles are declared inline then you can use the following selectors: div[style*="display: none;"] (if element has inline style attribute containing "display: none;" then apply style)

Attribute Selectors:

The CSS attribute selector matches elements based on the presence or value of a given attribute.

Src: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

Attribute Contains Selector:

When looking to find an element based on part of an attribute value, but not an exact match, the asterisk character, *, may be used within the square brackets of a selector. The asterisk should fall just after the attribute name, directly before the equals sign. Doing so denotes that the value to follow only needs to appear, or be contained, within the attribute value.

Src: https://learn.shayhowe.com/advanced-html-css/complex-selectors/

like image 23
UncaughtTypeError Avatar answered Oct 04 '22 12:10

UncaughtTypeError


No.

There are no selectors which select elements based on the values of properties that apply to them.


I don't think it would be practical for CSS to introduce such a feature either. Imagine:

:has-property-value(display: none) {
   display: block;
}
like image 31
Quentin Avatar answered Oct 04 '22 11:10

Quentin