5

Is it possible to hide table rows using CSS, I have a project that required this concept. Here is my code:

style.css:

#hide-row { display:none; }

file.php

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
  <div id="hide-row">
     <?php foreach( $cops as $row ) { ?>
        <tr>
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
  </div>
</table>

But, It didn't work, the records still appear. Anybody help how to solve this case? Any help will be appreciated. Thank You in Advanced !

1
  • do you have multiple hide-row ids? Commented Mar 27, 2013 at 9:15

5 Answers 5

11

Use a class instead of an id:

.hide-row { display:none; }

And in your html/php file:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
     <?php foreach( $cops as $row ) { ?>
        <tr class="hide-row">
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
</table>

If you have to group your rows you could use a tbody tag instead of a div tag.

Can we have multiple <tbody> in same <table>?

 .hide-row tr { display:none; }

And in your html/php file:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
    <tbody class="hide-row">
     <?php foreach( $cops as $row ) { ?>
        <tr>
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
     </tbody>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Jantimon, it works perfectly. I choose the 2nd option. Actually I want to display it in smooth toggle via Jquery when the user click something that related to the iterations. -)
This is accepted answer but better solution is here stackoverflow.com/questions/1144123/… and the reason is that page position ( scrolling ) is different. With display:none is incorrect when table is big and hide/show button is under table. With visibility:collapse cursor is where it should be after rows are hidden.
3

You can't put divs as direct children of a < table> element. To hide single rows see jantimon's answer. If you want to group multiple rows use < tbody>:

css

.hide-row { display:none; }

php

<table>
    <tr>
        <th>Name</th>
        <th>Address</th>
    </tr>
    <tbody class="hide-row">
        <?php foreach( $cops as $row ) { ?>
            <tr>
                <td><?php echo $row->name; ?></td>
                <td><?php echo $row->address; ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>

1 Comment

+1 for no div child of a table (or tbody or thead or tfoot). Descendant, yes, if there tr in-between (I edited your answer to reflect the fact that you can have divs in a table, but in tr>td or tr>th)
2

You can't nest a div inside a table tag directly. You'd have to give your rows a class, then hide that. Something like:

.hidden {
    display: none;
}

<?php foreach( $cops as $row ) { ?>
    <tr class="hidden">
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->address; ?></td>
    </tr>
 <?php } ?>

Comments

2

you cannot have <div> outside <tr>.. give the class to <tr> and hide that..no need to create a <div> around it

html

<tr class="hide-row">
        <td><?php echo $row->name; ?></td>
        <td><?php echo $row->address; ?></td>
    </tr>

style.css

.hide-row { display:none; }

Comments

2

I would give each row you want hidden a hide-row class:

<tr class="hide-row">

Your CSS would then look like:

tr.hide-row { display: none; }

This then means you don't need the nested div.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.