HTML/CSS Basics - Several precautions in HTML code writing (must read)

HTML/CSS Basics - Several precautions in HTML code writing (must read)

The warning points in this article have nothing to do with browser compatibility. They are mainly a summary of several small problems I encountered in the project. Although the problems are small, they are sometimes very troubling. I will record them here and continue to add them here if there are such problems later.

1. Space between inline tags

Under normal circumstances, when writing HTML code, there are habits such as line breaks and indentation, such as

XML/HTML CodeCopy content to clipboard
  1. < head >   
  2.      < meta   charset = "utf-8" >   
  3.      < style >   
  4. html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
  5. margin: 0;
  6. padding:0;
  7. }
  8. #myDIV {
  9. width: 200px;
  10. height: 200px;
  11. background-color: #ff0;
  12. }
  13. #myDIV > div{
  14. width: 50px;
  15. height: 50px;
  16. display: inline-block;
  17. background-color: #f00;
  18. }
  19.      </ style >   
  20.   
  21.    </ head >   
  22.    < body >   
  23.      < div   id = "myDIV" >   
  24.        < div > div1 </ div >   
  25.        < div > div2 </ div >   
  26.      </ div >   
  27.    </ body >   

The display effect is

There is a blank space in the middle. The reason is that if there are consecutive spaces, carriage returns, or line breaks between two inline tags (or when display: inline or inline-block is set), these symbols will be treated as a space symbol by default.

For example, if we add "ddd dd d" between two div tags, the effect is as follows. No matter how many consecutive blank characters there are, the final effect is only one space character.

This is similar to writing characters directly into an inline element.

However, inline elements will have leading and trailing whitespace removed .

So the reminder is:

When arranging inline elements, you need to keep the tags close together if you need to avoid whitespace between them.

When filling in the content of an inline element, try to use .innerText or .textContent (Firefox does not support innerText, but supports this property).

If you must write blanks in HTML code, please use HTML's space representation method&nbsp;

At this point, I think some people have a distorted understanding of inline elements. The so-called inline is the opposite of the so-called "block". Inline elements do not form blocks. They feel like flowing water, flowing around obstacles. For example, source code

XML/HTML CodeCopy content to clipboard
  1. < div   id = "myDIV" >   
  2.        < div > div1 </ div > ddd dd d < div > div2 </ div >   
  3.        < span > d dd d </ span >   
  4.      </ div >   

Display Effect

The content in span is divided into two sections and is no longer a complete block.

2. The default margin border of the body tag

There is nothing to say about this. Modern browsers (supporting CSS3) and IE8 have a default CSS style margin: 8px for the body. Some other tags are also like this, so I won’t give examples here. Many times we don't need this, and we need a similar setting at the beginning of the general project style.

XML/HTML CodeCopy content to clipboard
  1. html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
  2. margin: 0;
  3. padding:0;
  4. }

3. Special blank characters cause display abnormalities

For example, the following source code seems to have no problem

XML/HTML CodeCopy content to clipboard
  1. <!DOCTYPE html >   
  2. < html >   
  3.    < head >   
  4.      < meta   charset = "utf-8" >   
  5.      < style >   
  6. html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
  7. margin: 0;
  8. padding:0;
  9. }
  10. *{
  11. -webkit-box-sizing: border-box;
  12. -moz-box-sizing: border-box;
  13. box-sizing: border-box;
  14. }
  15. #myDIV {
  16. width: 200px;
  17. height: 40px;
  18. background-color: #ff0;
  19. }
  20. #myDIV a{
  21. float: left;
  22. width: 200px;
  23. background-color: #f00;
  24. }
  25.      </ style >   
  26.   
  27.    </ head >   
  28.    < body >   
  29.      < div   class = "tabbable"   id = "tabs"   style = "border:none;" >   
  30.        <!-- Page tag list -->   
  31.        < div   id = "myDIV"   style = "" >   
  32.        <   data-toggle = "tab"   href = "#tab-content-0"   > test0 </ a >   
  33.        </ div >   
  34.      </ div >   
  35.    </ body >   
  36. </ html >   

In fact, there is an abnormal blank character in front of the a tag, and the display effect is as follows

The width of a and the width of #myDIV should be the same, and a is floating, but the display effect is wrapped. This is too crazy, isn't it?

The normal display effect is

Let's take a look at what this abnormal blank is.

The first one is an abnormal space, and its URI component is encoded as "%E3%80%80"

The second is a normal space, whose URI component is encoded as "%20"

The third one is a normal Tab key, whose URI component is encoded as "%20%20%20%20", which is actually 4 spaces.

You see it. Therefore, sometimes the abnormal running effect of the code copied on the website may be caused by this reason.

To be continued. If I think of other points later, I will add them. I also hope that you can raise some related points, and I will definitely add them.

The above article "HTML/CSS Basics - Several Warning Points in the HTML Code Writing Process (Must Read)" is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

Original URL: http://www.cnblogs.com/chuaWeb/p/5053644.html

<<:  Solution to the IP address not being displayed under Linux

>>:  40 fonts recommended for famous website logos

Recommend

UDP simple server client code example

I won’t go into details about the theory of UDP. ...

WeChat applet realizes horizontal and vertical scrolling

This article example shares the specific code for...

Detailed tutorial on deploying Hadoop cluster using Docker

Recently, I want to build a hadoop test cluster i...

IE conditional comments for XHTML

<br />Conditional comments are a feature uni...

Use render function to encapsulate highly scalable components

need: In background management, there are often d...

Interpreting MySQL client and server protocols

Table of contents MySQL Client/Server Protocol If...

Html makes a simple and beautiful login page

Let’s take a look first. HTML source code: XML/HT...

JavaScript to implement random roll call web page

JavaScript writes a random roll call webpage for ...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

MySQL query syntax summary

Preface: This article mainly introduces the query...

Why is your like statement not indexed?

Preface This article aims to explain the most bor...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...