Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Formatting in HTML Tags

Tags:

html

Is there any option for using IF-ELSE conditioning in HTML tags

 <if true>  do something   </if>  
 <else>     do something   </else>
like image 669
balaweblog Avatar asked Feb 02 '09 07:02

balaweblog


2 Answers

With acknowledgement to Kevin Loney and his javaScript solution, we should also consider CSS conditionals. If the conditional is based on browser-sensing/responsive design, then javaScripting can be bypassed:

<div class='if-part'>show something</div>
<div class='else-part'>show something</div>

"Show something" in the sense that you would merely hide the condition which does not apply. In the example below, "if-part" is shown on mobile devices only. "else-part" is shown on larger screens.

@media screen and (max-width:767px){
    .if-part{display:initial;}
    .else-part{display:none}
}
@media screen and (min-width:768px){
    .if-part{display:none;}
    .else-part{display:initial}
}

This answer offers even more CSS options to consider.

like image 51
Parapluie Avatar answered Sep 21 '22 17:09

Parapluie


Not to be pedantic, but HTML is a markup language, and isn't useful for conditional logic.

That being said, it sounds like what you're looking for is a bit of javascript. If you could add a bit more detail to your question, I could elaborate on how you could use javascript to do tasks with conditional logic.

like image 24
wilsoniya Avatar answered Sep 18 '22 17:09

wilsoniya