Should I use the new HTML5 semantic elements? [closed]

本秂侑毒 提交于 2019-12-03 07:34:00

It's good practice to use both, for example

<nav>
    <div>
        <ul>
        <!-- etc -->
        </ul>
    </div>
</nav>

If you need to support those obsolete browsers, I wouldn't do anything more than that. The benefits, such as they are, are not worth the extra effort.

I do plan on supporting IE7 and IE8, but I know that these versions don't support the above semantic elements. I've read up about 'plugins' like Modernizr, Initializr and HTML5shiv, and I know that older browsers will then support the new elements IF JavaScript is enabled, but what am I supposed to do if it's not?

If JavaScript is not enabled, then while the content of the new elements will be shown, CSS will not be correctly applied to them. While in theory you could use a noscript element to trigger a redirect to a version of the page not using the new elements (via a meta refresh tag within the noscript), then you'd be maintaining two versions of your site.

For example, given this page: Live Copy

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>HTML5 Elements</title>
  <style>
    nav {
      color: green;
    }
  </style>
</head>
<body>
  <nav><ul><li>This text should be green</li></ul></nav>
</body>
</html>

...early versions of IE will show the text in the default color. Adding the HTML5 shiv prior to the style element:

<script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script>

...which as you know requires JavaScript, makes the text green: Live Copy

It's not necessary to follow the new semantic. New semantic is developed mostly for search engines, not for site functionality. If you really want to support IE, do it for IE.

If you really consider no-script cases and CSS is not enough for you, than all you can do is PHP/ASP magic.

One my friend works exclusively in Flash, because no js, totally client side, no cares about browsers... Who knows...

Jashwant

Should I use the new HTML5 semantic elements?

Yes.

IF JavaScript is disabled, what am I supposed to do about IE8 and below? The no-js class is added to the tag, so what exactly can I do with that?

You can do something like this,

HTML

  <div id='wrapper'>
     // Whole website coding here
    </div>
    <div id='old-browsers'>
      Use an upgraded browser
    </div>

CSS

    .no-js #wrapper {
      display: none;
    }

    #old-browsers {
     display: none;
    }

    .no-js #old-browsers {
      display:block;
    }

How can I use to my advantage here? I don't want to make pages too large with coding.

IE Consideration:

IE7 is 7 years old and most developers today do not support it. IE7 / IE8 users with js disabled is pretty low and you shouldnt develop for those exceptions. Instead you should give them a suggestion to upgrade with above method. You can use the noscript tag for the same usecase.

Joren

What you could do (and what a lot of big website's do) is to display a notice when javascript is disabled (optionally when the browser is IE7 or IE8 but you'll need a serverside check for that), that the website would not be displayed the way it is supposed to. See "How to detect if JavaScript is disabled?" on how to do so.

Only 5.3% (source) of internet users are using Internet Explorer < 8 and 0.25% to 2% (source) of the total users have Javascript disabled. You could spend a lot of time making a smooth solution for max 5% x 2% = 0,01% of your visitors or you could spend 5 minutes building the notice system I described.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!