Html If Statement

I just wanna know how to do an if-statement in simple HTML. Like the [if IE6] thingy

I'd like to do something like this

[IF 5>6]

How's the syntax? I can't seem to find anything but [If!IE6] and things like that, is that even possible?

Thanks a lot

Edit: I want to compare just scalar numbers, cause I have a dynamically created HTML. For example [If 4 == 6]. I DON'T WANT TO CHECK IE VERSIONS.

3

8 Answers

Not in HTML. Consider using JavaScript instead.

1

No, it's not possible. What you have seen is conditional comments in IE, which only checks version numbers of IE, and is typically used to inject IE specific style sheets to fix CSS bugs for that particular browser.

The <!--[if IE]> syntax only works in Internet Explorer. You'll need to use javascript or css to conditionally display html in other browsers.

If you want to check for browser versions:

<!--[if lt IE 7]>
    <!-- For LOWER than IE7 -->
<![endif]-->

<!--[if IE 6]>
    <!-- For JUST IE6 -->
<![endif]-->

<!--[if gt IE 7]>
    <!-- For HIGHER than IE7 -->
<![endif]-->

Other than that, you cannot use if statements in HTML, as it is a markup language and not a programming language. You need to do it either server side or with Javascript.

3

I believe this is the page you are looking for (v=vs.85).aspx

(I typed into google "IE conditional comments". It was the second result.)

4

The closest thing I can think of is *NgIf or using ternary expressions in your html using angular - however, the assignment of the variables and the logic of that ngif statement would be done in javascript.

[If lte IE 6]

This selects IE 5, 5.5 and 6. It's the closest to what you want.

If you insert Javascript into your code, then you can easily use the if statement. To insert you have to use script and /script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest