【问题标题】:padding not working on border-ed div填充不适用于有边框的 div
【发布时间】:2016-01-04 00:36:56
【问题描述】:

我正在尝试在带有背景图像的 div 周围放置一个圆形边框。我希望背景图像尽可能大,在它和边框之间有 10 像素的填充。当用户滚动并移除边框时,此图像会转换(缩小)到 50 像素,因此它需要保留尽可能多的背景图像

css

.brand, .brand:visited, .brand:hover {
 display: block;
 position: relative;
 height: 100px; width: 100px;
 margin-top: 25px;
 background: url('img/logo.png') no-repeat center center;
 background-size: 100% auto;
 padding: 10px;
 border: 1px solid #fff;
 border-radius: 50%;
}

除了填充之外,一切正常。不知道如何修复它。另外,我不想剪掉背景图片

【问题讨论】:

    标签: html css


    【解决方案1】:

    元素的背景适用于任何填充...除非 background-clip 属性被更改。

    * {
      box-sizing: border-box;
    }
    .brand {
      margin: 25px auto;
      position: relative;
      height: 100px;
      width: 100px;
      background: url('http://lorempixel.com/image_output/city-q-c-200-200-7.jpg') no-repeat center center;
      background-size: 100% auto;
      padding: 10px;
      border: 1px solid red;
      border-radius: 50%;
      background-clip: content-box;
    }
    .brand:hover {
      padding: 5px;
    }
    <div class="brand"></div>

    替代方案:使用宽边框和框阴影作为外部“边框”。

    * {
      box-sizing: border-box;
    }
    .brand {
      margin: 25px auto;
      position: relative;
      height: 100px;
      width: 100px;
      background: url('http://lorempixel.com/image_output/city-q-c-200-200-7.jpg') no-repeat center center;
      background-size: 100% auto;
      box-shadow: 0px 0px 0px 1px red;
      border: 10px solid white;
      border-radius: 50%;
    }
    .brand:hover {
      border-width: 5px;
    }
    <div class="brand"></div>

    【讨论】:

    • 我不想剪掉图片
    • 很好的回答 Paulie,从这里开始,用户可以根据需要自定义它。
    • 添加了一个替代选项。
    • 看起来不错,谢谢,似乎是最简单的答案,但是透明边框会起作用,因为它可能会出现在图像上方,因此会显示粗边框颜色
    • 你可以试试,但我认为你可能会得到一个奇怪的结果。
    【解决方案2】:

    这是添加到 .brand 类的边框,其中 .brand 相对定位,其中 :after 伪类是绝对定位的。
    这是为了创建一个超出形状的criicle。

    .brand,
    .brand:visited,
    .brand:hover {
      position: relative;
      height: 100px;
      width: 100px;
      margin-top: 25px;
      margin-left: 25px;
      background: url('http://lorempixel.com/200/200') no-repeat center center;
      background-size: 100% auto;
      border-radius: 50%;
    }
    .brand:before {
      display: inline-block;
      position: absolute;
      left: -15px;
      top: -15px;
      content: "";
      width: 120px;
      height: 120px;
      border: 5px solid #fff;
      border-radius: 50%;
    }
    body {
      background-color: #222;
    }
    <div class="brand"></div>

    【讨论】:

    • 顺便说一句,您需要将//border: 5px solid #fff; 更改为/*border: 5px solid #fff;*/,因为您不能将// 与CSS 一起使用。
    • 这在技术上是正确的,任何“错误”的规则都会被忽略,所以添加 // 会破坏规则。
    【解决方案3】:

    据我所知,不可能有一个未剪辑的background: 考虑padding:

    所以...这有点 hacky,但你可以

    1. 声明box-sizing: border-box;
    2. border: 代替padding:;然后
    3. box-shadow: 代替border:

    请看下面的例子:

    /* Your original styles */
    .brand, .brand:visited, .brand:hover {
     display: block;
     position: relative;
     height: 100px; width: 100px;
     margin-top: 25px;
     background: url('img/logo.png') no-repeat center center;
     background-size: 100% auto;
     padding: 10px;
     border: 1px solid #fff;
     border-radius: 50%;
    }
    
    /* A few extra styles just to make the background and border visible */
    .brand, .brand:visited, .brand:hover {
     background-color: rgb(0,0,0);
     border: 1px solid rgb(255,0,0);
    }
    
    /* An alternative using border-box, and box-shadow */
    .brand2, .brand2:visited, .brand2:hover {
     display: block;
     position: relative;
     height: 122px; width: 122px;
     margin-top: 25px;
     background: url('img/logo.png') no-repeat center center;
     background-size: 100% auto;
     background-color: rgb(0,0,0);
     border: 10px solid #fff;
     border-radius: 50%;
     box-sizing: border-box;
     box-shadow:1px 1px rgb(255,0,0), 1px -1px rgb(255,0,0), -1px -1px rgb(255,0,0), -1px 1px rgb(255,0,0);
    }
    
    /* Lining everything up */
    div[class^='brand'] {
    float: left;
    margin-right:20px;
    }
    <div class="brand"></div>
    <div class="brand2"></div>

    【讨论】:

    • 是否会切断背景图像,我已将其设置为填充 div。所以我知道它忽略了填充。我只需要背景边缘和边框之间的空间
    • 上例中的圆的半径完全相同。第一个content-box圆是50pxcontent-radius + 10pxpadding + 1px边框(61px),第二个border-box圆是61px半径(包括50pxcontent-radius,@ 987654338@ 边框和1px 盒子阴影)。在这两种情况下,内容半径都是50px。因此,如果您的背景图像是100px x 100px,则不会切断任何内容(除了角落),不会。
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2021-09-17
    • 2014-05-28
    • 1970-01-01
    相关资源
    最近更新 更多