search
HomeWeb Front-endHTML Tutorialcss实现两端对齐~

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

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

方法一、是在司徒正美的博客上看到的—— 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>

这种方法试了试,发现还是有些问题,是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
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
How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.