2023-06-06 05:00:00

轮廓半径?

是否有任何方法在div元素的轮廓上获得圆角,类似于border-radius?


当前回答

我刚刚找到了一个很好的解决方案,在看了所有的回复后,我还没有看到它发布。所以,这就是我所做的:

我为类创建了一个CSS规则,并为该规则使用了一个伪类:focus。我设置outline: none来去掉Chrome默认使用的浅蓝色无边框半径的“outline”。然后,在同样的:focus伪类中,轮廓不再存在,我添加了半径和边界属性。导致以下结果

outline: none;
border-radius: 5px;
border: 2px solid maroon;

当用户通过选项卡选择元素时,有一个带有边界半径的栗色轮廓。

其他回答

There is the solution if you need only outline without border. It's not mine. I got if from Bootstrap css file. If you specify outline: 1px auto certain_color, you'll get thin outer line around div of certain color. In this case the specified width has no matter, even if you specify 10 px width, anyway it will be thin line. The key word in mentioned rule is "auto". If you need outline with rounded corners and certain width, you may add css rule on border with needed width and same color. It makes outline thicker.

我把轮廓设置为透明。

input[type=text] {
  outline: rgba(0, 0, 0, 0);
  border-radius: 10px;
}

input[type=text]:focus {    
  border-color: #0079ff;
}

Chrome 94.0 +。

I tested it in chrome 94.0 and it seems that the outline property honors the border-radius now.

.outline { outline: 2px solid red; } .border { border: 2px solid red; } .outline-10 { border-radius: 10px; } .border-2 { border-radius: 2px; } .outline-2 { border-radius: 2px; } .border-10 { border-radius: 10px; } .outline-50 { border-radius: 50%; } .border-50 { border-radius: 50%; } .circle { display: inline-block; width:50px; height: 50px; } <strong>Test this in chrome 94.0+</strong> <br/><br/> border-radius: 2px <span class="outline outline-2">outline</span> <span class="border border-2">border</span> <br/><br/> border-radius: 10px <span class="outline outline-10">outline</span> <span class="border border-10">border</span> <br/><br/> border-radius: 50% <span class="outline outline-50">outline</span> <span class="border border-50">border</span> <span class="outline circle outline-50">outline</span> <span class="border circle border-50">border</span>

Firefox 88+: border-radius

从2021年4月起,你就可以在Firefox上使用简单的CSS了:

.actual { 轮廓:实红色; border - radius: 10 px; } .expected { 边框:实红色; border - radius: 10 px; } 在Firefox 88+中, <span class="actual">this outline</span> 应该是这样的 <span class="expected">this border</span>

Firefox 86.0的当前行为:

Webkit:没有解决方案

使用outline-style: auto将告诉«用户代理渲染自定义outline样式»:参见[MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/outline-style(。

当您使用outline-style: auto时,基于webkit的浏览器将在边界上绘制轮廓。很难正确地设计它的风格。

.actual { 大纲:自动红色; border - radius: 10 px; } .expected { 边框:实红色; border - radius: 10 px; } 在WebKit浏览器(Chrome, Edge)中, <span class="actual">this outline</span> 应该看得很近 <span class="expected">this border</span>

Chrome 89.0的当前行为:

更多的信息

从Firefox 88(将于2021年4月20日发布)开始,outline将遵循边界半径的形状。 当前的-moz-outline-radius将变得多余并将被删除。

参见MDN关于-moz-outline-radius的条目:

从Firefox 88开始,标准的outline属性将遵循border-radius的形状,使-moz-outline-radius属性变得多余。因此,此属性将被删除。

我刚刚找到了一个很好的解决方案,在看了所有的回复后,我还没有看到它发布。所以,这就是我所做的:

我为类创建了一个CSS规则,并为该规则使用了一个伪类:focus。我设置outline: none来去掉Chrome默认使用的浅蓝色无边框半径的“outline”。然后,在同样的:focus伪类中,轮廓不再存在,我添加了半径和边界属性。导致以下结果

outline: none;
border-radius: 5px;
border: 2px solid maroon;

当用户通过选项卡选择元素时,有一个带有边界半径的栗色轮廓。