37

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?

9
  • If the display: block and the display: none parents are different classes, then you can select the buttons based on that.
    – Jesse
    Commented Oct 17, 2017 at 9:21
  • 3
    Why do you want that? If an elements parent is display: none then it simply won't render. It doesn't matter what other properties are applied to it.
    – Quentin
    Commented Oct 17, 2017 at 9:21
  • 2
    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) Commented Oct 17, 2017 at 9:26
  • 1
    try using is(":visible") with jquery. Commented Oct 17, 2017 at 9:26
  • 1
    @Amit yes, you're right, but I did mention that: "If those display styles are declared inline..." Commented Oct 17, 2017 at 10:05

6 Answers 6

43

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>

4
  • 4
    It may be a solution, but not at all a good recommendation on standard as you are using inline styling. Commented Oct 17, 2017 at 9:57
  • 2
    Well maybe it's a limitation of using inline style, but it's a solution, otherwise we will be using JavaScript or jQuery for it.
    – cнŝdk
    Commented Oct 17, 2017 at 9:59
  • 1
    @invinciblemuffi glad it helps you :)
    – cнŝdk
    Commented Nov 9, 2021 at 11:35
  • 1
    And "space" matters
    – Ivan Gusev
    Commented Jan 11, 2022 at 13:27
25

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/

4

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;
}
0
2

This is not possible with pure CSS so far, Unless you explicitly specify the inline css to style="display: none".

You could use some javascript to filter a set of buttons that are visible.

var buttons = document.querySelectorAll('.block button');

var visibleButtons = [];

buttons.forEach(function (element) {
  if (window.getComputedStyle(element.parentNode).display !== 'none') {
   visibleButtons.push(element);
  }
});

console.log(visibleButtons);
.block {
  display: block;
}

.hidden {
  display: none;
}
<div class="block">
  <button>btn 1</button>
</div>

<div class="block hidden">
  <button>btn 2</button>
</div>

<div class="block">
  <button>btn 3</button>
</div>

2
  • Actually it's possible with CSS3 to select elements using a style property.
    – cнŝdk
    Commented Oct 17, 2017 at 9:47
  • @cнŝdk Not really since the solution you link to requires inline styling. The poster, however, offers a universal solution as it works with computed style regardless with it is declared inline or in a head CSS element.
    – DDRRSS
    Commented Dec 28, 2021 at 17:28
1

There are no such selector available in CSS to select by their property values. You can try something with jquery by using :hidden selector to find buttons with display:none. Check below snippet for reference.

$( ".btnShow" ).click(function() {
  $( ".btn:hidden" ).show( "fast" );
});
.hidden{
  display:none;
}
.btnShow{
  display:block;
  margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="button 1" class="btn">
<input type="button" value="button 2" class="btn">
<input type="button" value="button 3" class="btn hidden">
<input type="button" value="button 4" class="btn">
<input type="button" value="button 5" class="btn hidden">
<input type="button" value="button 6" class="btn">
<input type="button" value="button 7" class="btn">
<input type="button" value="Show hidden buttons" class="btnShow">

3
  • Actually it's possible with CSS3 to select elements using a style property.
    – cнŝdk
    Commented Oct 17, 2017 at 9:48
  • As per my knowledge no one approach for inline style. Commented Oct 17, 2017 at 9:56
  • 2
    @chsdk — That selects elements based only on the value of a style attribute, not on the computed CSS property.
    – Quentin
    Commented Oct 17, 2017 at 10:15
-4

You can check with jquery. The code below means

"Get all buttons, filtered by ones whose parent is visible on page", loop through and print html of each one.

$(document).ready(function(){
$(":button").filter(function() { return $(this).parent().is(':visible') 
       }).each(function(){
		      console.log($(this).html());
		});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="intro" style="display:block">
    My name is someone.    
   <button>    a    </button>    <button>    b    </button>    
</p>
<p>I live somewhere.</p>    <p>My best friend is someone.</p>
Who is your favourite:    
<ul id="find" style="display:none">
  <li>One</li>      <li>Two</li>      <li><button>    x    </button>    
  <button>    y    </button></li>    
</ul>

Not the answer you're looking for? Browse other questions tagged or ask your own question.