Ali*_*mii 4 css html5 border css3 css-shapes
我正在尝试使用具有向下箭头的底部边框来设置div .div将在其中包含图像,并且不应具有顶部,右侧或左侧边框.向下箭头的填充应与div或透明相同.
我已经能够使用以下代码使其大部分工作:
.hero {
position: relative;
background-color: yellow;
height: 320px !important;
width: 100% !important;
border-bottom: 1px solid red;
}
.hero:after {
content: '';
position: absolute;
top: 100%;
left: 50%;
margin-left: -50px;
width: 0;
height: 0;
border-top: solid 50px #e15915;
border-left: solid 50px transparent;
border-right: solid 50px transparent;
}Run Code Online (Sandbox Code Playgroud)
<div class="hero"></div>Run Code Online (Sandbox Code Playgroud)
看到这个小提琴:http://jsfiddle.net/alisamii/tjep3h8t/
无论我试图做什么来"镂空"箭头要么导致无边框div(所以它有一个黄色的填充但任何一侧没有边框)或在一个围绕整个div的边界.
有帮助吗?
使用CSS3有两种方法可以实现这一点.一种是使用skewX伪元素,而另一种是使用rotate伪元素.这两种方法都具有响应性.
这个方法改编自web-tiki在这个帖子中的答案.它基本上使用两个伪元素(容器的宽度约为50%),它们在相反的方向上倾斜并适当地定位以达到箭头形状.形状具有边框,其中background设置为透明意味着伪元素将产生底部边框+向下箭头效果.此示例中的箭头填充始终是透明的(或正文颜色).
body {
background: rgb(245, 242, 242);
}
.bordered {
position: relative;
height: 200px;
width: 200px;
margin: 10px;
line-height: 200px;
}
.bordered:after,
.bordered:before {
position: absolute;
content: ' ';
height: 8px;
width: 50%;
bottom: 0px;
}
.bordered:before {
left: 0px;
border-top: 1px solid gray;
border-right: 1px solid gray;
transform-origin: left bottom;
transform: skewX(45deg);
}
.bordered:after {
right: 0px;
border-top: 1px solid gray;
border-left: 1px solid gray;
transform-origin: right bottom;
transform: skewX(-45deg);
}
.bordered img {
width: 150px;
padding: 25px;
vertical-align: middle;
}
/* Just for demo */
.bordered {
transition: all 1s;
text-align: center;
}
.bordered:hover {
height: 250px;
width: 250px;
line-height: 250px;
}Run Code Online (Sandbox Code Playgroud)
<!-- library included only to avoid browser prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="bordered">
<img src="http://i.imgur.com/0Xqum3A.png" />
</div>Run Code Online (Sandbox Code Playgroud)

下面的方法将产生一个透明的三角形,以便通过三角形切割看到页面的背景.
body {
background: rgb(245, 242, 242);
}
.bordered {
position: relative;
height: 200px;
width: 200px;
margin: 10px;
background: rgb(200, 200, 200);
background-clip: content-box;
line-height: 200px;
overflow: hidden;
}
.bordered.top {
padding-top: 8px;
}
.bordered.bottom {
padding-bottom: 8px;
}
.bordered:after,
.bordered:before {
position: absolute;
content: ' ';
height: 8px;
width: 50%;
background: inherit;
}
.bordered.top:after,
.bordered.top:before {
top: 0px;
}
.bordered.bottom:after,
.bordered.bottom:before {
bottom: 0px;
}
.bordered:before {
left: 0px;
border-right: 1px solid gray;
}
.bordered.top:before {
border-top: 1px solid gray;
transform-origin: left bottom;
transform: skewX(45deg);
}
.bordered.bottom:before {
border-bottom: 1px solid gray;
transform-origin: left top;
transform: skewX(-45deg);
}
.bordered:after {
right: 0px;
border-left: 1px solid gray;
}
.bordered.top:after {
border-top: 1px solid gray;
transform-origin: right bottom;
transform: skewX(-45deg);
}
.bordered.bottom:after {
border-bottom: 1px solid gray;
transform-origin: right top;
transform: skewX(45deg);
}
.bordered img {
width: 150px;
padding: 25px;
vertical-align: middle;
}
/* Just for demo */
div{
display: inline-block;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="bordered top">
<img src="http://i.imgur.com/0Xqum3A.png" />
</div>
<div class="bordered bottom">
<img src="http://i.imgur.com/0Xqum3A.png" />
</div>Run Code Online (Sandbox Code Playgroud)
此方法使用旋转的伪元素(旋转45度)来实现向下箭头效果,然后将其置于底部border的顶部div.在该方法中,background伪元素的值被设置为与div包含它的颜色相同的颜色.(注意:在此示例中,图像div具有不同的填充颜色.)
body {
background: lightgray;
}
.colored {
height: 200px;
width: 200px;
position: relative;
margin: 10px;
line-height: 200px;
}
.colored img {
vertical-align: middle;
width: 150px;
padding: 25px;
}
.colored {
background: yellow;
border-bottom: 1px solid gray;
}
.colored:after {
height: 10px;
width: 10px;
position: absolute;
content: '';
background: yellow;
border: 1px solid gray;
border-top-width: 0px;
border-left-width: 0px;
transform: rotate(45deg);
bottom: -6px;
left: calc(50% - 4px);
}
/* Just for demo */
.colored{
text-align: center;
transition: all 1s;
}
.colored:hover{
height: 250px;
width: 250px;
line-height: 250px;
}Run Code Online (Sandbox Code Playgroud)
<!-- library included only to avoid prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="colored">
<img src="http://i.imgur.com/0Xqum3A.png" />
</div>Run Code Online (Sandbox Code Playgroud)

一个简单的解决方案,其中箭头的背景将是透明的,允许您在更改背景时使用它:
HTML:
<div class="line-separator">
<div class="side-line"> </div>
<div class="triangle"> </div>
<div class="side-line"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.side-line {
display: inline-block;
border-top: 1px solid black;
width: 20%;
}
.triangle {
display: inline-block;
height: 7px;
width: 7px;
transform: rotate(45deg);
transform-origin: center center;
border-top: 1px solid black;
border-left: 1px solid black;
margin: 0 -3px -3px;
}
Run Code Online (Sandbox Code Playgroud)
现场演示: http: //jsfiddle.net/85saaphw/