0

How can I hide the header div when there are no rows in the table. My css is not working.

.myTable:empty ~ .header {
  display: none;
}
<div class="row">
  <div class="header">ADDITIONAL ITEMS:</div>
  <div class="clearfix"></div>
  <div class="table-responsive m-t">
    <table class="myTable">
    </table>
  </div>
</div>

Any suggestions, Please!

5
  • 2
    There is no parent selector in CSS, you need to use JS Commented Aug 23, 2017 at 13:11
  • But jquery is not working in my PDF view. that's why I m trying to do like this Commented Aug 23, 2017 at 13:12
  • Are you looking for this? Commented Aug 23, 2017 at 13:14
  • Okay,thank you will check the link Commented Aug 23, 2017 at 13:16
  • you can hide the whole table with table:has(>tbody:empty) { display:none; } Commented May 17, 2023 at 19:13

3 Answers 3

1

Updated Snippet :

if($(".myTable tr").length > 0){
	alert('row present')
}else{
	$('.header').hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="header">ADDITIONAL ITEMS:</div>
  <div class="clearfix"></div>
  <div class="table-responsive m-t">
    <table class="myTable">
     <!--<tr></tr>-->
    </table>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

But jquery won't work in my PDF view :(
0

You can not use pure css,you in css can not first check childrens of table,if table have children(in here tr) show .header else hide .header,because css have not backward.you must use jquery.

$(document).ready(function(){
  if(!$('.myTable').find('tr').length) 
    $('.header').hide();
})

$(document).ready(function(){
  if(!$('.myTable').find('tr').length) 
    $('.header').hide();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<div class="row">
  <div class="header">ADDITIONAL ITEMS:</div>
  <div class="clearfix"></div>
  <div class="table-responsive m-t">
    <table class="myTable">
    </table>
  </div>
</div>

1 Comment

But jquery won't work in my PDF view :(
0

You can't select a previous element because CSS does not work backwards (Hence, Cascading Style Sheet)

BUT, If you really want to use only CSS, you can use flex:

https://jsfiddle.net/0d7402sm/

#flex { 
  display: flex; 
  /* Optional, if you want the DIVs 100% width: */ 
  flex-direction: column;
}

.myTable:empty ~ .header {
  display:none;
}

#flex > .myTable { order: 2; }
#flex > .header { order: 1; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.