透明背景上图标和元素周围的部分边框

Partial Border around icons & elements on transparent background

如何在透明背景下创建可调整大小(甚至设置宽度)的图标和页面上其他元素周围的边框,如下所示?

------[element]------

目前最好的思路是

<div class="outer"> Border left / right
  outer:before - Border top/ bottom; width:10%
    Element
  outer:after- Border top/ bottom; width:10%
</div>

但是我如何合并包含整个中间部分的水平线以太侧

对于这条水平线,您可以使用带有 repeat-x 的背景图像 属性 并将位置居左。对于这些括号,您可以采用 :before 和 :after 伪 类,其中 DIV 元素绝对位于按钮元素的两侧,高度为 100%,宽度为 20px。这只是我首先想到的几个解决方案之一:)

老实说,我喜欢完整 CSS 而不使用任何类型的图像,我会在图标的左侧和右侧使用 2 个额外的 div 元素,作为 "brackets"

<div class="icon-container">
    <div class="icon-border icon-border-left"></div>
    <i class="fa fa-rocket"></i> <!-- or your central element -->
    <div class="icon-border icon-border-right"></div>
</div>

我会像这样以常规方式设置括号样式:

.icon-container .icon-border {
    border: 1px solid black;
    width: 25px;
    position: relative;
}

然后将一个伪元素应用到括号中以创建 "line",以非常长的宽度绝对定位。

.icon-container .icon-border:before {
    content: '';
    position: absolute;
    top: 50%;
    height: 1px;
    width: 2048px;
    background: black;
}

最后,应用所有例外情况将括号移近图标,给人一种它正在包裹起来的错觉,然后向左和向右赋予伪 类 以画一条线从括号末尾到屏幕末尾:

.icon-container .icon-border.icon-border-left {
    border-right-width: 0px;
    margin-right: -21px;
}
.icon-container .icon-border.icon-border-left:before {
    right: 100%;
}
.icon-container .icon-border.icon-border-right {
    border-left-width: 0px;
    margin-left: -21px;
}
.icon-container .icon-border.icon-border-right:before {
    left: 100%;
}

最后一件重要的事情,你的主图标容器必须有 overflow: hidden 否则你的线条将跨越整个屏幕,可能走出容器并走出你的 body 导致非常糟糕的水平滚动条。

演示http://codepen.io/luigimannoni/pen/epPBXJ

这应该会让您朝着正确的方向前进。

<div class="wrapper">
   <div class="left"></div>
       <button>Test</button>
   <div class="right"></div>
</div>

<style>
.wrapper {
  text-align: center;
  background: #ccc;
  padding: 20px;
}

button {
  vertical-align: middle;
  display: inline-block;
  position: relative;
  z-index: 2;
}

.left {
  position: relative;
  vertical-align: middle;
  height: 100px;
  box-sizing: border-box;
  padding-right: 30px;
  width: 400px;
  display: inline-block;
  margin: 0 -10px 0 0;
}

.left::before {
  position: absolute;
  border-top: 1px solid #333;
  top: 50%;
  z-index: 1;
  width: 100%;
  content: "";
  right: 30px;
}

.left::after {
  position: absolute;
  border-top: 1px solid #333;
  border-left: 1px solid #333;
  border-bottom: 1px solid #333;
  top: 0;
  z-index: 1;
  width: 30px;
  content: "";
  right: 0;
  height: 100%;
  box-sizing: border-box;
}

.right {
  position: relative;
  height: 100px;
  box-sizing: border-box;
  padding-right: 30px;
  width: 400px;
  display: inline-block;
  vertical-align: middle;
  margin: 0 0 0 -10px;
}

.right::before {
  position: absolute;
  border-top: 1px solid #333;
  top: 50%;
  z-index: 1;
  width: 100%;
  content: "";
  left: 30px;
}

.right::after {
  position: absolute;
  border-top: 1px solid #333;
  border-right: 1px solid #333         
  border-bottom: 1px solid #333;
  top: 0;
  z-index: 1;
  width: 30px;
  content: "";
  left: 0;
  height: 100%;
  box-sizing: border-box;
}
</style>

http://codepen.io/robotslater/pen/LpgxGe

让我添加另一种方式。我没有在 html 中使用任何额外的 div。只有一个包装器和一个跨度。

跨度得到class括号来绘制括号。我使用线性渐变作为边框。在这个例子中,我使用了一个简单的黑色-透明-黑色渐变,当然您也可以使用更复杂的渐变。

然后我在包装上使用两个伪元素 div 来绘制水平线。在这里你也可以得到比简单的黑色边框更花哨的东西。

body {
 background: linear-gradient(to right, #ffc, #ccf);
}
.bracketed{
 padding: 15px 20px;
 border: 1px solid;
 border-image: linear-gradient(to right, black 0%, black 29%, transparent 30%, transparent 70%, black 71%, black 100%);
 border-image-slice: 1;
}
.bordered {
 text-align: center;
 margin: 20px;
 display: flex;
}
.bordered::before, .bordered::after{
 content: "";
 flex: 1;
 align-self: center;
 border: 1px solid black;
 border-width: 1px 0px 0px;
 height: 0;
}
<div class="bordered"><span class="bracketed">A</span></div>

如果你想在图像上使用它,你在 html 中只需要

<div class="bordered"><img /></div>

codepen 上查看带有超赞字体图标的示例。