Hide div using php

I am currently hiding a div based on an if statement. The method I use uses echoout a cssstyledisplay: none

Here is what I am doing specifically:

<style>
  #content{
    <?php
      if(condition){
          echo 'display:none';
      }
    ?>
  }
</style>
<body>
    <div id="content">
       Foo bar
    </div>
</body>

My question is: is this a good method for hiding a div? Is it possible that the browser caches the style and therefore ignores the echo-ed out style css?

+5
source share
6 answers

Using Php in CSS (Cascade Style Sheet) is not "correct",

Alternatively, you can use Php in your HTML:

<body>
    <?php if (condition){ ?>
        <div id="content">
           Foo bar
        </div>
    <?php } ?>
</body>

With this code, the div block is not displayed (and you are not using it with JavaScript), you can use this for a simply hidden div:

 <body>
    <div id="content" <?php if (condition){ echo 'style="display:none;"'; } ?>>
       Foo bar
    </div>
</body>
+11
source

Why not create a class:

<style>
    .hidden {
        display: none;
    }
</style>

PHP:

<div id="content" <?php print ( condition ? 'class="hidden"' : '' ); ?> >
+2

div. PHP , if div - CSS. , CSS, - JavaScript div , .

+1

:

<div runat="server" id="theDiv"> 

Code behind is
{
theDiv.Visible = False;
}

javascript

, :

Javascript, else div

0

, div. , , .

0

I usually try to avoid using PHP procedures inside CSS; especially embedded CSS (CSS that is on the same page).

I would save your CSS in my own CSS file and use the PHP condition to add the hide class to the DIV -OR- do not drop the DIV at all.

<link rel="stylesheet" type="text/css" href="style.css" />
<body>
    <div id="content" <?php if(conditional) : ?>class="hide"<?php endif;?>>
       Foo bar
    </div>
</body>

or alternatively

<?php $class = (conditional) ? "hide" : ""; ?>

<link rel="stylesheet" type="text/css" href="style.css" />
<body>

    <div id="content" class="<?=$class?>">
       Foo bar
    </div>
</body>

or

<link rel="stylesheet" type="text/css" href="style.css" />
<body>

    <?php if (conditional) : ?>
    <div id="content">
       Foo bar
    </div>
    <?php endif; ?>
</body>

Many times a div must be displayed so that it can be re-rendered using JavaScript (e.g. carousels, sliders, etc.).

0
source

All Articles