search
HomeWeb Front-endHTML TutorialLet's talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

Start this series and discuss some interesting CSS topics. Putting aside practicality, some topics are designed to broaden the ideas for solving problems. In addition, they involve some CSS details that are easily overlooked.

Compatibility is not taken into account when solving problems. The questions are wild and wild. Just say whatever comes to mind. If there are CSS properties that you feel are unfamiliar in the problem solving, go and study them quickly.

Keep updating, keep updating, keep updating, say important things three times.

Let’s talk about some interesting CSS topics (1)--How to implement the vertical bar on the left

Let’s talk about some interesting CSS topics (2) – Talking about the box model from the implementation of striped borders

Let’s talk about some interesting CSS topics (3) – How much do you know about stacking order and stack context

Let’s talk about some interesting CSS topics (4) – starting with reflection, let’s talk about CSS inheritance inherit

Let’s talk about some interesting CSS topics (5)--center a single line, center two lines, and omit more than two lines

All topics are summarized in my Github.

6. Fully compatible multi-column uniform layout problem

How to achieve the following multi-column uniform layout (the straight lines in the picture are not included to show the width of the container):

Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

Method 1: display:flex

CSS3 Flexible Box (Flexible Box or Flexbox) is a layout method that can still ensure that elements have more appropriate arrangement behavior when the page needs to adapt to different screen sizes and device types.

Of course, flex layout is good for mobile applications. If the PC needs to be fully compatible, the compatibility is not enough, so we will skip it here.

Method 2: Use pseudo elements and text-align:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

is defined as follows HTML style:

<div class="container">
    <div class="Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues">
        <i>1</i>
        <i>2</i>
        <i>3</i>
        <i>4</i>
        <i>5</i>
    </div>
</div>

We know that there is text-align:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues that can achieve the effect of aligning text on both ends.

text-align The CSS property defines how inline content (such as text) is aligned relative to its block parent element. text-align does not control the alignment of the block element itself, only the alignment of its inline content.

text-align:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues means the text is aligned to both sides.

At first I guessed it could be achieved using the following CSS:

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues{
  text-align: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues;
}

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues i{
  width:24px;
  line-height:24px;
  display:inline-block;
  text-align:center;
  border-radius:50%;
}

结果如下:

Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

 

Demo戳我

没有得到意料之中的结果,并没有实现所谓的两端对齐,查找原因,在 W3C 找到这样一段解释:

最后一个水平对齐属性是 Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues,它会带来自己的一些问题。CSS 中没有说明如何处理连字符,因为不同的语言有不同的连字符规则。规范没有尝试去调和这样一些很可能不完备的规则,而是干脆不提这个问题。

额,我看完上面一大段解释还是没明白上面意思,再继续查证,才找到原因:

虽然 text-align:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues 属性是全兼容的,但是要使用它实现两端对齐,需要注意在模块之间添加[空格/换行符/制表符]才能起作用。

也就是说每一个 1 间隙,至少需要有一个空格或者换行或者制表符才行。

好的,我们尝试一下更新一下 HTML 结构,采用同样的 CSS:

<div class="container">
    <div class="Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues">
        <i>1</i>

        <i>2</i>

        <i>3</i>

        <i>4</i>

        <i>5</i>

    </div>
</div>

尝试给每一块中间添加一个换行符,结果如下:

Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

Demo戳我

啊哦,还是不行啊。

再寻找原因,原来是出在最后一个元素上面,然后我找到了 text-align-last 这个属性,text-align-last属性规定如何对齐文本的最后一行,并且 text-align-last 属性只有在 text-align 属性设置为 Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues 时才起作用。

尝试给容器添加 text-align-last:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues{
  text-align: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues;
  text-align-last: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues; // 新增这一行
}

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues i{
  width:24px;
  line-height:24px;
  display:inline-block;
  text-align:center;
  border-radius:50%;
}

发现终于可以了,实现了Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues:

o_Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues2

Demo戳我

结束了?没有,查看一下 text-align-last 的兼容性:

Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

但是一看兼容性,惨不忍睹,只有 IE8+ 和 最新的 chrome 支持 text-align-last 属性,也就是说,如果你不是在使用 IE8+ 或者 最新版的 chrome 观看本文,上面 Demo 里的打开的 codePen 例子还是没有均匀分布。

上面说了要使用 text-align:Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues 实现多列布局,要配合 text-align-last ,但是它的兼容性又不好,真的没办法了么,其实还是有的,使用伪元素配合,不需要 text-align-last 属性。

我们给 class="Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues" 的 div 添加一个伪元素:

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues{
  text-align: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues;
}

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues i{
  width:24px;
  line-height:24px;
  display:inline-block;
  text-align:center;
  border-radius:50%;
}

.Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues:after {
  content: "";
  display: inline-block;
  position: relative;
  width: 100%;
}

Removed text-align-last: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues and added a pseudo element, the effect is as follows:

o_Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues2

Demo click me, any number of columns will be laid out evenly

By setting inline-block to the pseudo element :after and setting the width 100%, and matching the container's text-align: Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues, you can easily achieve a uniform layout of multiple columns. With a few more hack codes, it can be compatible with IE6+. The most important thing is that the code is not long and easy to understand.

The final realization of the problem is as shown in the beginning:

Lets talk about some interesting CSS issues (6) – fully compatible multi-column uniform layout issues

Demo click me, any number of columns will be laid out evenly

This method was first seen in this article. It was written into this series with the consent of the original blogger. It is worth reading:

  • Don’t think too much, it’s just that the two ends are aligned

All the topics are summarized in my Github and posted to the blog in the hope of getting more exchanges.

This is the end of this article. If you still have any questions or suggestions, you can communicate more. It is an original article. The writing style is limited and the knowledge is shallow. If there is anything wrong in the article, please let me know.

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
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.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor