html - 使用单独数字(无单位值)的 CSS3 border-radius 不起作用

标签 html css

MDN指出您可以使用 1-4 之间的数字作为长度属性,但它没有任何效果。

这有什么原因吗?

p {
  font-size: 0.85em;
  background-color: red;
  border-radius: 4;
}
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,'
  thought Alice 'without pictures or conversations?'</p>

结果:https://jsfiddle.net/6L31f9d6/

最佳答案

Note: The points indicated by Thieu Nguyen are valid. I am adding a separate answer because I think your problem is due to the interpretation (wrong) of the formal syntax and because I'm not comfortable forcing my content into another answer.

MDN page for border-radius 没有任何地方说明我们可以使用 1-4 之间的数字作为长度值。事实上,CSS 中的所有长度值都必须后跟一个单位,唯一的异常(exception)是 0。

以下是 CSS Spec for Length Units 的摘录:(强调是我的)

The format of a length value is an optional sign character ('+' or '-', with '+' being the default) immediately followed by a number (with or without a decimal point) immediately followed by a unit identifier (a two-letter abbreviation). After a '0' number, the unit identifier is optional.


基于your comment ,我感觉你混淆了 {1,4}在正式语法中是无单位值。

[ <length> | <percentage> ]{1,4} [ / [ <length> | <percentage> ]{1,4} ]?

但它们不是无单位的值。它就像正则表达式中的,其中 [a]{x,y}表示 a 的最少出现 x 次最多出现 y 次。因此,以下是应如何解释正式语法:

  • [ <length> | <percentage> ] - 表示长度或百分比可以是值。
  • {1,4} - 表示前一个实体必须至少出现一次,最多出现四次。也就是说,该值应至少为一个长度或百分比,最大为四个长度或百分比。
  • [ / [ <length> | <percentage> ]{1,4} ]? - 与上面类似,但末尾的问号表示该位是可选。即/其后面的长度或百分比值是可选的。如果提供,它们将用作垂直边框半径,如果没有,它们将与水平半径相同。

关于html - 使用单独数字(无单位值)的 CSS3 border-radius 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35930208/

相关文章:

javascript - 单击另一个切换 div 时将图标切换回 "i"元素到原始位置

html - CSS 渐变功能似乎使用了过多的 CPU 功率,我做错了什么吗?

html - 垂直对齐文本区域

html - 使用 CSS 从右到左 div 并在 5 秒后隐藏

jquery - 通过 jquery 合并 @media 规则

php - 拉推特头像

html - 有人可以向我解释这个 flexbox css 问题吗?

html - 使用 Bootstrap 添加边框以输入

html - 使位置固定不可滚动

html - css 中的 <div> 是否可以留边距?