I am facing an issue with displaying random blocks in rows. Each time a block falls into a new row, I want it to have a different style. However, when the user clicks on a button to hide certain blocks using display:none
, the problem arises because the nth-child
selector still applies to hidden elements as well.
Is there a way to exclude those specific blocks from being styled, so that each row maintains a unique look? The provided code snippet demonstrates a similar scenario.
$('.hide-others').click(function () {
$('.css--all-photo').toggleClass('hidden');
})
.board-item--inner {
height:200px;
background:tomato;
text-align:center;
color:#fff;
font-size:33px;
margin-bottom:15px;
border:2px solid tomato;
}
@media (min-width:768px) and (max-width:991px) {
.board-item:nth-child(2n+1) .board-item--inner {
border:2px solid #000;
background:yellow;
color:#000;
}
}
@media (min-width:992px) and (max-width:1199px) {
.board-item:nth-child(3n+1) .board-item--inner {
border:2px solid #000;
background:yellow;
color:#000;
}
}
@media (min-width:1200px) {
.board-item:nth-child(4n+1) .board-item--inner {
border:2px solid #000;
background:yellow;
color:#000;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="form-group">
<button class="btn btn-info hide-others" type="button">Hide others</button>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">1</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">2</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item css--all-photo">
<div class="board-item--inner">3</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">4</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">5</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item css--all-photo">
<div class="board-item--inner">6</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">7</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item css--all-photo">
<div class="board-item--inner">8</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">9</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">0</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 board-item photos-board-item">
<div class="board-item--inner">10</div>
</div>
</div>
<div>
To understand my question better, please refer to the provided code snippet or visit this EXTERNAL FIDDLE.
I am seeking a CSS-only solution for this issue. Please include a fiddle link in your response! It is crucial to note that I cannot permanently remove these blocks, as the user needs the option to filter them via button click, resulting in scenarios where some blocks are hidden and shown.