css arrow with bottom border [duplicate]

℡╲_俬逩灬. 提交于 2019-12-12 22:04:40

问题


I need to create an arrow inside a div. I have created the arrow but need to get the visul effect with border as shown as below

Demo of the tried sample http://jsfiddle.net/rLZkf/4/

div {
  margin: 20px;
  height: 200px;
  width: 200px;
  background-color: #c1c1c1;
  border: #000 solid 2px;
  background-image: -webkit-linear-gradient(135deg, transparent 75%, #fff 75%), -webkit-linear-gradient(45deg, transparent 75%, #fff 75%);
  background-image: -moz-linear-gradient(135deg, transparent 75%, #fff 75%), -moz-linear-gradient(45deg, transparent 75%, #fff 75%);
  background-image: -ms-linear-gradient(135deg, transparent 75%, #fff 75%), -ms-linear-gradient(45deg, transparent 75%, #fff 75%);
  background-image: -o-linear-gradient(135deg, transparent 75%, #fff 75%), -o-linear-gradient(45deg, transparent 75%, #fff 75%);
  background-image: linear-gradient(135deg, transparent 75%, #fff 75%), linear-gradient(45deg, transparent 75%, #fff 75%);
  background-position: 30px 0, 0 0;
  background-repeat: no-repeat;
  background-size: 30px 30px;
}
<div></div>

回答1:


You can use CSS3 rotate property for this. Write like this:

div {
    margin:20px;
    height: 200px;
    width: 200px;
    background-color: #c1c1c1;
    border:#000 solid 2px;
    position:relative;
}
div:after{
    content:'';
    width:20px;
    height:20px;
    background:#fff;
    position:absolute;
    -moz-transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    transform:rotate(45deg);
    top:-11px;
    left:10px;
    border-right:#000 solid 2px;
    border-bottom:#000 solid 2px;
} 

Check this http://jsfiddle.net/rLZkf/6/

UPDATED

Check this http://jsfiddle.net/rLZkf/9/




回答2:


Personally, I quite like to use a different approach to the chosen answer here.

Using the principle explained on https://css-tricks.com/snippets/css/css-triangle/ for example, you can use two pseudo elements to create the effect you're after.

You will basically create two triangles using this border trick: one will be the same colour as your page background and the other will be the same as your container border and will act as a 'border' to the first triangle.

You then just offset the position of the two triangles so they're on top of your container border and that's it.

.container:after,
.container:before {
  border-left: 10px solid transparent;
  border-right: 10px solid transparent; 
  content: " ";
  left: 10px; 
  margin-top: -1px;
  position: absolute;
}

.container:before {
  border-top: 10px solid #000;
  top: 1px; 
}

.container:after {
  border-top: 10px solid #fff;
  top: 0px; 
}

Of course you can just wrap the whole thing in a mixin if you use Sass or similar and pass it the colours / arrow size.

See http://codepen.io/arnaudvalle/pen/EjgYrp for a quick example



来源:https://stackoverflow.com/questions/12634366/css-arrow-with-bottom-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!