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

时间:2015-11-07 12:56:20

标签: css css3 icons

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

Element 1

Element 2 and 3

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

目前最好的思路

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

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

4 个答案:

答案 0 :(得分:3)

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

跨度将括号括起来绘制括号。我使用线性渐变作为边框。在这个例子中,我使用一个简单的黑色透明黑色渐变,但当然你可以使用更复杂的渐变。

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

&#13;
&#13;
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;
}
&#13;
<div class="bordered"><span class="bracketed">A</span></div>
&#13;
&#13;
&#13;

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

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

查看codepen上带有font-awesome图标的示例。

答案 1 :(得分:2)

老实说,我喜欢在不使用任何类型的图像的情况下使用完整的CSS,我会在图标的左侧和右侧使用2个额外的div元素,这些元素充当&#34;括号&#34;

<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;
}

然后将一个伪元素应用于括号以创建&#34;线&#34;,绝对定位宽度非常长。

.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,否则你的线条将跨越整个屏幕,可能会从容器中出来并走出你的身体,导致一个非常糟糕的水平滚动条。

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

答案 2 :(得分:0)

对于此水平线,您可以将background-image属性与repeat-x和left left center一起使用。对于这些括号,您可以使用:before和:在伪类之后,DIV元素绝对位于按钮元素的两侧,高度为100%,宽度为20px。这只是我首先想到的几种解决方案之一:)

答案 3 :(得分:0)

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

<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

相关问题