Table of Contents
用 CSS 画一个完美的圆
border-radius 的工作原理
结论
Home Web Front-end HTML Tutorial Border-radius 50% vs 100%_html/css_WEB-ITnose

Border-radius 50% vs 100%_html/css_WEB-ITnose

Jun 24, 2016 am 11:25 AM

最近我在回顾曾经写过的代码。在过去写代码的时候,我似乎从未认真思考自己为什么要那么写,只是根据经验或者已有的代码写出来。或者是,我大概隐约知道那么写的原因,但是我从没有认真读过规范,所以并不了解具体的细节。所以我下定决心,尽可能多的问自己“为什么”,“为什么要这么写”。现在我发现这样频繁地询问自己,让我拥有了很多“灵光一闪”的瞬间,现在我就要把其中一个分享给你们。

用 CSS 画一个完美的圆

在 Web 上我们常常会看到很多用到圆形的地方,比如带背景色的或者图片是圆形的。

通常我们都是用 CSS 的 border-radius 属性实现圆形:先画一个方形,然后将它的 border-radius 设置成50%。但是为什么偏偏是50%呢?我从来没有思考过这个问题,只是单纯地认为把顶角的半径设置成方形的高度或者宽度的一半就可以得到一个圆形。

这是一个 150px x 150px 大小的方形,将它的四个角的半径都设置成 50%。根据 W3C border-radius 的规范定义,如果 border-radius 的值是百分比的话,就是相对于 border box 的宽度和高度的百分比。在我们的例子中,盒子的宽高都是 150px,所以 50% 对应的就是 75px。

border-radius 的工作原理

但是有时候我看到有的人会用 border-radius: 100%; 实现圆形的效果,在前段时间的一个项目中我想都没想就这么使用了,看起来和 50% 并没有什么区别。是什么原因呢?

在 Lea Verou 的演讲 The Humble Border Radius 中,她说到 W3C 对于重合曲线有这样的规范:如果两个相邻的角的半径和超过了对应的盒子的边的长度,那么浏览器要重新计算保证它们不会重合。

如果左上角的圆角半径被设置成了100%,那么圆角就会从这个方形左下角跨到右上角,相当于把圆角半径设置成为150px(也就是方形的大小)。如果同时把右上角的圆角半径也设置成为100%,则两个相邻圆角合起来就有200%。这种情况自然是不允许出现的,所以浏览器就会重新就算,匀出空间给右边的圆角,同时缩放两个圆角的半径直到它们可以刚好符合这个方形,所以半径就变成了50%。

同样的,浏览器会对其他的圆角应用相同的计算,计算的结果是每个圆角的半径变成了50%,所以我们看到了一个原型。即使将border-radius设置为150px,浏览器还是会按照75px画圆角,75px是浏览器所允许的这个方形能够拥有的最大的圆角半径。

结论

如果所有圆角的半径都被设置成了100%,浏览器会根据图形的实际情况做一些计算,保证圆角能够刚好适应图形。不过我不确定将所有图形的 border-radius 设置成100%对性能会不会有影响。

好了,现在我要去整理一下上周写的乱七八糟的 CSS 了…

原文链接:Border-radius 50% vs 100%

关注微博:前端外刊评论

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)

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.

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.

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

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.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles