search
HomeWeb Front-endHTML TutorialIntroduction to the space symbols (nbsp; ensp; emsp;) in html and a detailed explanation of the method of Chinese alignment implementation

一:不同空格符合的区别

  •   半角的不断行的空白格(推荐使用)

  •    半角的空格 

  •    全角的空格

详细的含义:

 :这是我们使用最多的空格,也就是按下space键产生的空格。在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个)。要使用html实体表示才可累加。该空格占据宽度受字体影响明显而强烈。在inline-block布局中会搞些小破坏,在两端对齐布局中又是不可少的元素。

 :此空格有个相当稳健的特性,就是其占据的宽度正好是1/2个中文宽度,而且基本上不受字体影响。

  :此空格也有个相当稳健的特性,就是其占据的宽度正好是1个中文宽度,而且基本上不受字体影响。

二:使用场景

对于在一些中文排版对齐方面可以使用,如下html代码:

<ul>
    <li class="li">姓&emsp;&emsp;名:<input type="text" /></li>
    <li class="li">手&ensp;机&ensp;号:<input type="text" /></li>
    <li class="li">电子邮箱:<input type="text" /></li></ul>

实现的效果如图所示:

值得注意的是:上面的空白字符中文对齐方法在IE6下不能完全兼容。(现在谁还在兼容IE6呢,所以还是非常有用的。)

三:空格新成员3000

大多数编辑器中空格是透明滴,很容易就被删掉;另外,HTML压缩时候,空格也会被删除掉,所以需要转换书写形式。

在web页面上,一般有3种书写:

  • 直接,例如搜狗输入法输入“版权” – ©.

  • web字符,&<a href="http://www.php.cn/wiki/1297.html" target="_blank">copy</a>;

  • charCode表示:©

而上面的就是具有特定名称的web字符。但是,恕我寡闻,我并不清楚全角空格是否有对应& + 关键字示意,所以,就使用工具转成了charCode字符表示,也就是这里的 

  •  → <a href="http://www.php.cn/code/3792.html" target="_blank">002</a>;

  •  → 

字符使用技巧:

1. HTML中字符输出使用配上charCode值;
2. 在JavaScript文件中为防止乱码转义,则是\u配上charCode值;
3. 而在CSS文件中,如CSS伪元素的content属性,直接使用\配上charCode值。

因此,想在HTML/JS/CSS中转义“我”这个汉字,分别是:

  • \u6211, 如console.log('\u6211');

  • \6211, 如.xxx<a href="http://www.php.cn/wiki/977.html" target="_blank">:before</a> { content: '\6211'; }

考虑到直接 这种形式暴露在HTML中,可能会让屏幕阅读器等辅助设备读取,从而影响正常阅读流,因此,我们可以进一步优化下,使用标签,利用伪元素,例如:

.half:before { content: &#39;\2002&#39;; speak: none; }.full:before { content: &#39;\2003&#39;; speak: none; }

html代码:

<ul>
    <li class="li">姓<span class="full"></span><span class="full"></span>名:<input type="text" /></li>
    <li class="li">手<span class="half"></span>机<span class="half"></span>号:<input type="text" /></li>
    <li class="li">电子邮箱:<input type="text" /></li></ul>

css代码:

.half {
    *zoom: expression( this.runtimeStyle['zoom'] = '1', this.innerHTML = ' ');}
    .full {
    *zoom: expression( this.runtimeStyle['zoom'] = '1', this.innerHTML = ' ');}
    .half:before { content: &#39;\2002&#39;; speak: none; }.full:before { content: &#39;\2003&#39;; speak: none; }

上面用到了runtimeStyle这个对象属性,这个是IE专属的。

下面简单介绍下style、 currentStyle、 runtimeStyle以及getComputedStyle的区别,在IE下测试如下。

html代码:

<p id="tt" style="color:blue;">这里是来检测style,currentStyle,runtimeStyle的区别</p>

js代码:

var myp = document.getElementById("tt");
myp.runtimeStyle.color="black"; 
console.log(myp.currentStyle.color);  
//blackconsole.log(myp.runtimeStyle.color);  
//blackconsole.log(document.defaultView.getComputedStyle(myp, null).color); 
//rgb(0, 0, 0)console.log(myp.style.color);         
//blue

说明一下:

obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在

IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法 。

"DOM2-level style" enhances document.defaultView and provides the getComputedStyle() method. This method accepts two parameters: the element to obtain the calculated style and a pseudo-element string (such as ":after"). If pseudo-element information is not required, the second parameter can be null. The getComputerStyle() method returns a CSSStyleDeclaration object that contains all computed styles for the current element.

The syntax is: document.defaultView.getComputedStyle('element', 'pseudo-class'); IE9 and above support this writing method , IE8 and below are not supported.

To summarize:

Get the background color through document.defaultView.getComputedStyle(). Different browsers get different results and may return Convert all colors to RGB format and possibly color values.

The color value obtained by IE through the currentStyle method does not convert the color into RGB format.

The above is the detailed content of Introduction to the space symbols (nbsp; ensp; emsp;) in html and a detailed explanation of the method of Chinese alignment implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!