Is it safe to omit and tags?

后端 未结 10 2059
广开言路
广开言路 2020-12-05 22:41

According to w3c and tags are optional, so the following table is perfectly valid.



        
                      
相关标签:
10条回答
  • 2020-12-05 23:05

    You should close your <TR> and <TD>tags if possible, but NOT ALWAYS because in some scenarios it may disturb you. This is because the <TR> and <TD>tags could be styled to display:none while the </TR> tag couldn't. It means that a scenario in which you want to extend / limit your display with media queries, will fail if you use </TR>. Consider the following code:

    <style>
    @media (max-width : 480px) {
    .hidden-class {display:none;}
    }
    </style>
    
    <table>
       <tr>
          <td>cell 1</td>
       </tr class="hidden-class">  <!-- this will fail! -->
       <tr class="hidden-class">
          <td>cell 2</td>
       </tr>                       <!-- this could stay -->
    </table>
    

    It means that you would write it as:

     <table>
       <tr>
          <td>cell 1</td>
       </tr>  <!-- this will now close the TR for every screen width, which will destroy your extenstion -->
       <tr class="hidden-class">
          <td>cell 2</td>
       </tr>                       <!-- this could stay -->
    </table>
    

    Which means that the only way to do so is by OMITTING THE in the first place:

         <table>
       <tr>
          <td>cell 1</td>
       <tr class="hidden-class">
          <td>cell 2</td>
       </tr>                       <!-- this could stay -->
    </table>
    
    0 讨论(0)
  • 2020-12-05 23:07

    There have been optional tags in HTML since the very beginning — they are a feature inherited from SGML, and so the early browsers must have been able to deal with them. While XHTML moved away from this and required a much more uniform syntax, this doesn’t affect you unless you explicitly tell the browser to parse in XML mode. I’ve never seen a problem when using the standard parsers of HTML 4/5.

    Since HTML5 so carefully describes when certain tags are optional, I would read that to imply that someone did lots of testing to make sure that in these cases, most browsers produce the same document tree.

    And while the space savings are negligible, I find that leaving off the closing tags of <p>, <li>, <td>, and <tr> simply makes my life easier when I’m working in the markup, and makes me less likely to make an error.

    0 讨论(0)
  • 2020-12-05 23:09

    It’s Ok with Static Pages not for Dynamic Pages …to debug

    0 讨论(0)
  • 2020-12-05 23:12

    It is safe, since optionality in the standard means that all the browsers (at least the ones which even remotely matter) would have implemented this - and the browser standards compliance usually runs to the opposite side, into trying to work correctly with even invalid HTML as opposed to failing on missing optional tags.

    Having said that, I find that omitting such tags makes things harder to read, which may or may not matter to you if the goal is size optimization.

    P.S. Also, if you have very large tables, I wonder whether there's any overhead incurred by the browser's HTML parser when dealing with such constructs? I am not sure without benchmarking or really deep thinking about how HTML parser works in detail, but it is something that could possibly be a factor if it happens.

    0 讨论(0)
  • 2020-12-05 23:16

    Personally I don't consider it good practice. Looking at the spec it didn't give a whole lot of information. I know it's required for XHTML so I looked up the HTML 5 spec. HTML 5 seems to take the same take on it as HTML 4 which is what you've linked to, but gives a little more information:

    A td element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.

    0 讨论(0)
  • 2020-12-05 23:24

    Close all tags in HTML for a few reasons:

    1. Not closing tags is tolerated, but it is not correct modern XHTML. It's deprecated much in the same way that HTML style attributes are in favor of CSS
    2. It's more readable for the next guy if you close the tags
    3. Browsers will actually have an easier time parsing your source if it more strictly adheres to the rules, even if that makes the source longer
    0 讨论(0)
提交回复
热议问题