【问题标题】:How to remove white pixels on the corners after border-radius如何在边框半径后去除角落上的白色像素
【发布时间】:2022-04-23 21:02:05
【问题描述】:

I've created a simple unordered list with rounded corners, when selecting a list item I want the background to become blue to show the active state.当我没有边界半径时,一切看起来都很棒。

但是,当我使用 4px 的边框半径对所有边缘进行圆角处理,并选择底部圆角的最后一个列表项 (#5) 时,蓝色活动状态现在在角上显示小的白色像素.我似乎无法弄清楚如何防止这种情况发生。谢谢你的帮助。 .

http://jsfiddle.net/8PN8Z/

HTML

<div class="left-dash">
  <div class="dash-stats-3">
        <ul class="dash-stats-3-ul">
            <li><span id="explore-themes">testing</span></li>
            <li>
                <a href="#" class="tab-link" data-remote="true">
                    <span class="ss-pika ss-icon">1</span><span class="theme"> test-1</span>
                </a>
            </li>
            <li>
                <a href=#"" class="tab-link" data-remote="true">
                    <span class="ss-pika ss-icon">2</span><span class="theme"> test-2</span>
                </a>
            </li>
            <li>
                <a href="" class="tab-link" data-remote="true">
                    <span class="ss-pika ss-icon">3</span><span class="theme"> test-3</span>
                </a>
            </li>
            <li>
                <a href="/theme/inspiring" class="tab-link" data-remote="true">
                    <span class="ss-pika ss-icon">4</span><span class="theme"> test-4</span>
                </a>
            </li>
            <li>
                <a href="#" class="tab-link" data-remote="true">
                    <span class="ss-pika ss-icon">5</span><span class="theme"> test-5</span>
                </a>
            </li>
        </ul>
    </div>
</div>

CSS

.left-dash {
    background: #444;
    height: 500px;
}
.left-dash div {
  -webkit-border-radius: 4px 4px 4px 4px;
  border-radius: 4px 4px 4px 4px;
}
.left-dash div ul li:first-child, .left-dash div ul li:first-child a {
  -webkit-border-radius: 4px 4px 0px 0px;
  border-radius: 4px 4px 0px 0px; 
}

.left-dash div ul li:last-child,
.left-dash div ul li:last-child a {
  -webkit-border-radius: 0px 0px 4px 4px;
  border-radius: 0px 0px 4px 4px; 
}



.dash-stats-3 {
    width: 220px;
    margin-bottom: 17px;
    background: #9f9f9f;
    -webkit-background-clip: padding-box; /* for transparent border with solid bg - Safari */
    background-clip: padding-box;/* for IE9+, Firefox 4+, Opera, Chrome */
    border-radius: 0px;
    border: 1px solid rgba( 0, 0, 0, 0.15);
    box-shadow: inset 0px 0px 2px 0px #FFFFFF,0px 1px 3px 0px rgba(0,0,0, 0.15);
    -webkit-box-shadow: inset 0px 0px 2px 0px #FFFFFF,0px 1px 3px 0px rgba(0,0,0, 0.15);
    -moz-box-shadow: inset 0px 0px 2px 0px #FFFFFF,0px 1px 3px 0px rgba(0,0,0, 0.15);
    -o-box-shadow: inset 0px 0px 2px 0px #FFFFFF,0px 1px 3px 0px rgba(0,0,0, 0.15);
    font-family:'Museo Sans W01 700' san-serif;
    font-size: 14px;
    color: #828282;
    text-shadow: 0px 1px 1px #FFFFFF;
    font-weight: normal !important;
}



.dash-stats-3 ul li a, .dash-stats-3 ul li:first-child {
    height: 41px;
    border-bottom: solid rgba( 0, 0, 0, 0.15) 1px;
    border-top: solid rgba(255, 255, 255, .65) 1px;
}
.left-dash div ul li:last-child, .left-dash div ul li:last-child a {
    border-bottom: none;
}

.dash-stats-3 ul li a {
    position:relative;
    line-height:43px;
    display:block;
}


.dash-stats-3 ul li a.active-tab {
    z-index:120;
    border-top: 1px solid #5b82a7;
    background: #4e81be; /* Old browsers */ 
}

JS

$(function(){
        $('a.tab-link').click(function(){
          $('a.tab-link').removeClass('active-tab')
          $(this).addClass('active-tab')
        });
      });

【问题讨论】:

  • 我认为你在 div 上应用了一些东西(也许是 box-shadow?),你应该应用到每个单独的 li 甚至链接上。父 div 不知道border-radius 被应用在它的一些孩子上,它应该适应它。
  • 您好,感谢您的帮助。我认为不可能将 box-shadow 应用于单个 li 或链接,这会使每个框都有自己的阴影而不是整个无序列表。我很困惑为什么如果所有内容的边界半径都相同,父 div 会导致问题?
  • 这段代码很粗糙。我建议把它降低一个档次,把问题提炼成简单的事情。我以前从未在 jsfiddle 中使用过 !important 。这些选择器很疯狂。我想你可能想多了。另外-您可能要注意,有时边界半径看起来很奇怪,例如 4px 等-平均屏幕无法流畅地渲染它,因为它太小而且像素太小了。在视网膜屏幕上检查它作为侧面测试。

标签: html css


【解决方案1】:

首先,给active-tab 一个半径。

.dash-stats-3 ul li a.active-tab {
    z-index:120;
    border-top: 1px solid #5b82a7;
    background: #4e81be; /* Old browsers */ 
        -webkit-border-radius: 0px 0px 2px 2px;
        border-radius: 0px 0px 2px 2px;  
}

确保它小于li 的半径,(2px)。

并删除默认状态下a 的半径。删除这个。

.left-dash div ul li:last-child a{
  -webkit-border-radius: 0px 0px 4px 4px;
  border-radius: 0px 0px 4px 4px; 
}

保留这个。

.left-dash div ul li:last-child{
  -webkit-border-radius: 0px 0px 4px 4px;
  border-radius: 0px 0px 4px 4px; 
}

检查一下:http://jsfiddle.net/FcXVY/

问题在于您为子元素提供了与父元素相同的边框半径。即使父元素没有边框,也会显示一些微小的像素,如果有边框,情况会更糟。看看这个:http://jsfiddle.net/UVTJQ/

【讨论】:

  • 感谢您的解决方案,我看到您完成此操作的方法是将活动选项卡上的边框半径更改为 2 像素而不是 4 像素。有没有办法让边界半径始终保持一致?
  • @trying_hal9000 我更新了我的答案,检查示例,并从父级移除边框,仍然会显示一些像素,解决这个问题的唯一方法是有一个绝对的孩子,那将当有top:0; left:0; right:0; bottom:0;时完全覆盖父级(需要测试)
  • @trying_hal9000 如果你可以牺牲&lt;li&gt; 中的&lt;a&gt;,而改用li,点击它,给它上色,把它的边框也染成蓝色,我想你会避免这个问题。
  • 您好,再次感谢您的帮助。我厌倦了只使用 li 的,问题仍然存在。我从来不知道父母和孩子边界半径的问题。我使用上面的方法一起进行了 hack,其中 javascript 还将父 div 的边框半径更改为 6px,以便活动选项卡的 4px 边框半径仍然小于父 jsfiddle.net/E2jk3。它似乎有效,但似乎更多的是 js 代码
  • @trying_hal9000 不错的技巧,不用担心使用 JS,我知道用纯 css 做事更好,但是 JS 无处不在,使用它并不丢人。父子边界半径的问题甚至不能用绝对子来解决(我测试过),当然如果尺寸很小,它不会被注意到,因为额外的像素只有 2 像素,我认为这是渲染问题,可能在浏览器和另一个浏览器之间有所不同。
【解决方案2】:

好的。我认为这可以简化事情。一探究竟。希望对你有帮助。

http://jsfiddle.net/sheriffderek/8WTam/

<ul class="test-list">
    <li><a href="?">Item thing</a></li>
    <li><a href="?">Item thing</a></li>
    <li><a href="?">Item thing</a></li>
    <li><a href="?">Item thing</a></li>
    <li><a href="?">Item thing</a></li>
    <li><a href="?">Item thing</a></li>
</ul>

CSS:

/* border box - google it. it rules*/ 
* { 
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box; 
}

.test-list {
    float: left;
    overflow: hidden;
    list-style: none;

    /* just to show the properly containing ul */  
    margin: 2em;
    background-color: rgba(0,0,0,.05);
    padding: 1em;
}

.test-list li {

}

.test-list a {
    display: block;
    float: left;
    clear: left;
    background-color: #f06;
    padding: 1em 2em;
    min-width: 12em;

    color: white;
    text-decoration: none;
    border: 3px solid rgba(0,0,0,1);
    border-bottom: 0;
}

.test-list a:hover {
    background-color: lightblue;
    color: black;
}

.test-list li:first-of-type a {
    -webkit-border-radius:.4em .4em 0 0;
    border-radius: .4em .4em 0 0;
}

.test-list li:last-of-type a {
    -webkit-border-radius: 0 0 .4em .4em;
    border-radius: 0 0 .4em .4em;
    border-bottom: 3px solid black;
}

【讨论】:

  • 或者你可能想要box-shadow: inset 0 0 0 .2em rgba(255,255,255,.5); 在那里......在a --- 我开始质疑我是否理解这个问题。
  • 感谢您的帮助,但这个解决方案并不能真正解决我的问题,如果我能够进行整个重新设计,我想我可以使用这个解决方案。仅供参考,因为您担心, !important 就在那里,因为我将项目中的代码复制并粘贴到 jsfiddle 中。
猜你喜欢
  • 1970-01-01
  • 2019-01-15
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多