-3

Is it possible to hide a part of website like DIV or any tag with opacity & z-index in PHP instead of CSS

CSS Code:

#unwantedposition{
    opacity:0;
    z-index:9999;
    position:absolute;
    }

I am trying to rewrite my CSS code with PHP

PHP Code:

header("Content-type: text/css; charset: UTF-8");

   $opacity = "0";
   $z-index = "9999";
   $position = "absolute";

How can I fix this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Stellan Coder
  • 323
  • 1
  • 11
  • yes you can use if statement for that – Jerson Dec 28 '21 at 18:15
  • 1
    PHP has no access to DOM. Opacity, z-index and position are CSS properties. You can, however, output valid CSS with PHP. – Markus AO Dec 28 '21 at 18:19
  • 2
    @Jerson Can you show me example. how i can write `if statement` for `opacity` and `z-index`. – Stellan Coder Dec 28 '21 at 18:19
  • @MarkusAO so there is no way to use `opacity` and `z-index` with PHP? – Stellan Coder Dec 28 '21 at 18:20
  • As noted, they are **CSS properties**. The "p" in PHP stands for "preprocessor". You can use PHP to produce valid HTML, CSS, Javascript, and anything else that the browser understands. PHP is not a front-end language and does not run in the browser. – Markus AO Dec 28 '21 at 18:23
  • @MarkusAO i know what PHP means. i am just asking if any way to make content transparent using PHP – Stellan Coder Dec 28 '21 at 18:25
  • Have you understood what PHP does, in general, though? – Markus AO Dec 28 '21 at 18:26
  • @shanmugapradeep, you can make content transparent using PHP by adding CSS properties to `style` attribute or adding CSS class with required properties to target element – 7-zete-7 Dec 28 '21 at 18:41
  • @7-zete-7 can you show some example? how can i write these statement with PHP – Stellan Coder Dec 28 '21 at 18:42
  • @shanmugapradeep, watch on answer – 7-zete-7 Dec 28 '21 at 18:47
  • 2
    I'm finding it very unclear what you want to achieve. From comments in some of the answers it sounds as though you are trying to make sure that a user cannot view your source code. This is not possible - but is it what you are wanting to achieve? Also the concept of 'opacity in PHP' has no meaning. – A Haworth Dec 28 '21 at 19:09

4 Answers4

0

Try to achieve this with if statement example under the body the variable $hide set it on top of your html page

<style type="text/css">
    .disabled-div {
        pointer-events: none;
        opacity: 0.4;
    }
</style>
<body>
    
    <div class="<?php if($hide === true){ ?>disabled-div <?php } ?>">this is my hidden div</div>
    
    <div>some content</div>
</body>
0

You could simply override that CSS class.

<?php
echo '<style>
    #unwantedposition{
    opacity:0;
    z-index:9999;
    position:absolute;
}
</style>';

That should be placed in your header tag, below your CSS file.

Reza Saadati
  • 5,018
  • 4
  • 27
  • 64
  • 1
    But it same thing. i am looking for alternative solution without use of CSS. – Stellan Coder Dec 28 '21 at 18:35
  • The easiest way to do this, is with JavaScript. If you want to do it with PHP only, it is possible but way too heavy. First, you will need to read your CSS file. With Regex you will need to find the string that you are looking for, then with PHP replacing it with the new string. Once that is done, save the new string in the same file and load the page. – Reza Saadati Dec 28 '21 at 18:44
0

You can conditionally show HTML tag:

<?php if ($isNeedToShow): ?>
  <div>...</div>
<?php endif ?>
7-zete-7
  • 712
  • 4
  • 12
  • 1
    It just another way of using CSS using PHP but i am looking for same kind of function with PHP without CSS because i do not want opacity code in view-source. – Stellan Coder Dec 28 '21 at 18:40
  • @shanmugapradeep, PHP can only avoid the rendering of the HTML tag. To hide an element you must use CSS. – 7-zete-7 Dec 28 '21 at 18:45
  • 1
    Writing in CSS will not hide the element in view-source :(. anyone can click view source in browser and read the CSS with opacity mentioned or the CSS link which is called in the page. – Stellan Coder Dec 28 '21 at 18:47
  • @shanmugapradeep, in this case, you just need to not render the HTML tag. Watch the changed answer – 7-zete-7 Dec 28 '21 at 18:50
  • 1
    If it appears in the DOM/HTML, it will obviously be present in the "view-source" (with CSS or otherwise). Even if you render it using javascript, the generated HTML will still be readable with basic developer tools. – Markus AO Dec 28 '21 at 18:58
  • @MarkusAO, I don't even want to bring up the topic of Shadow DOM. Although even it can be viewed :) – 7-zete-7 Dec 28 '21 at 19:04
  • @shanmugapradeep, you can use WebAssemply, Canvas, Shadow DOM or etc to "hide" elements, but one way or another, they will still be visible because HTML + CSS + JS - open source – 7-zete-7 Dec 28 '21 at 19:07
  • 1
0

I don't understand what you mean. However, opacity and z-index are CSS properties. However, PHP doesn't have access to the DOM Element, it can only use a mix of HTML and CSS.

Then the question is, how to use PHP to mix HTML and CSS?

You can print <tag style="css-property: value;"></tag> Example :

<div style="opacity: 0; z-index: 9999; position: absolute"></div>

Did this answer help? If you have other questions you can add comments about your questions.

syahid246
  • 109
  • 2
  • 7