Home Web Front-end HTML Tutorial css实现两端对齐~

css实现两端对齐~

Jun 16, 2016 am 08:39 AM

今天做表单时遇到让上下两个字段对齐的情况,手机号码、用户名。

然后今天在网上找了找相关方法,发现确实是没有什么好的方法解决,特别是当需要兼容的时候。找到了两个我觉得相对还不错的方法:

方法一、是在司徒正美的博客上看到的—— text-align, text-justify;

需要设置 text-align为justify就行了,text-justify的情况复杂些。IE的取值如下:

  • auto :允许浏览器用户代理确定使用的两端对齐法则
  • inter-word :通过增加字之间的空格对齐文本。该行为是对齐所有文本行最快的方法。它的两端对齐行为对段落的最后一行无效
  • newspaper : 通过增加或减少字或字母之间的空格对齐文本。是用于拉丁文字母表两端对齐的最精确格式
  • distribute :处理空格很像newspaper,适用于东亚文档。尤其是泰国
  • distribute-all-lines :两端对齐行的方式与distribute相同,也同样不包含两段对齐段落的最后一行。适用于表意字文档
  • inter-ideograph : 为表意字文本提供完全两端对齐。他增加或减少表意字和词间的空格

但它最早是作为IE的私有实现,像text-overflow, overflow-x等,在FF很晚才实现,换言之有严格的兼容性问题。并且FF,chrome需要手动在汉字间插入空白或软换行标签才生效,在chrome遇到的阻力就更大了。

<span style="color: #800000;">.test1 </span>{<span style="color: #ff0000;">
      text-align</span>:<span style="color: #0000ff;">justify</span>;<span style="color: #ff0000;">
      text-justify</span>:<span style="color: #0000ff;">distribute-all-lines</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">ie6-8</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
      text-align-last</span>:<span style="color: #0000ff;">justify</span>;<span style="color: #008000;">/*</span><span style="color: #008000;"> ie9</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
      -moz-text-align-last</span>:<span style="color: #0000ff;">justify</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">ff</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
      -webkit-text-align-last</span>:<span style="color: #0000ff;">justify</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">chrome 20+</span><span style="color: #008000;">*/</span>
  }<span style="color: #800000;">
  @media screen and (-webkit-min-device-pixel-ratio:0)</span>{<span style="color: #008000;">/*</span><span style="color: #008000;"> chrome</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
      .test1</span>:<span style="color: #0000ff;">after{
          content:"."</span>;<span style="color: #ff0000;">
          display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
          width</span>:<span style="color: #0000ff;">100%</span>;<span style="color: #ff0000;">
          overflow</span>:<span style="color: #0000ff;">hidden</span>;<span style="color: #ff0000;">
          height</span>:<span style="color: #0000ff;">0</span>;
      }<span style="color: #800000;">
  }</span>
Copy after login

这种方法试了试,发现还是有些问题,是test1需要时块元素,如果是航标签的话,需要设置display:block;

原文章:http://www.cnblogs.com/rubylouvre/archive/2012/11/28/2792504.html

 

方法二、这个是无意中看到的,之前也想过但从来没这么做过,没想到真有人做了。

就是用table方法,按做表格的方式来做,这样文字肯定会平均分开。(O(∩_∩)O哈哈~别鄙视我!)

也可以类似的设置display:table属性:

   table层设置display:table,td层设置display:table-cell。

当然原理是一样的,这么做需要每个字一个外框分割开来,方法确实不错,但是感觉挺BT得。

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 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 Design The Future of HTML: Evolution and Trends in Web Design Apr 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.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 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.

See all articles