html - 使用 css 逻辑解释从 div 创建三 Angular 形?

标签 html css css-shapes

<分区>

我最近致力于创建一个元素,其中包含一些文本,但它必须是基于标志的形状。我用谷歌搜索并在下面找到了很好的 css 来实现它。他们基本上使用 css 从 div(指向左/右/顶部/底部)创建小三 Angular 形并将其附加到相邻的 div。

但是,我没有找到上述逻辑的任何答案,这让我对使用 CSS 从 div 创建三 Angular 形感到困惑。

我想知道那个 css 是如何工作的,特别是下面使用的 arrow-left 类。

HTML:

<div class='element'>
   <p>OFFER</p>
   <div class="arrow-left"></div>
</div>

CSS:

.element{
  background-color:#E47911;
  display:inline-block;
  position:relative;
  padding:2px 15px 2px 10px;
  color:white;
  line-height:18px;
}
p{
  font-size:10px;
  margin:0;
}
.arrow-left {
   width: 0; 
   height: 0; 
   border-top: 11px solid transparent;
   border-bottom: 11px solid transparent;
   border-right: 11px solid white;
   display:inline-block;
   position:absolute;
   top:0px;
   right:0px;
 }

这是代码笔链接:http://codepen.io/anon/pen/JGvBpN

非常感谢任何指点!谢谢。

最佳答案

考虑到边界交叉点是如何以一定 Angular 呈现的,它并没有像看起来那样创建三 Angular 形。

.arrow-left {
 width: 0; /* unnecessary, but does override any unexpected width  */
 height: 0; /* unnecessary, but does override any unexpected height */

 /* These set up the intersecting borders, set each one to a different color to see the borders forming the "triangle" e.g. red, green, blue  */
 border-top: 11px solid transparent;
 border-bottom: 11px solid transparent;
 border-right: 11px solid white;

 display:inline-block; /* unnecessary, you're taking the elemtn out of the document flow when you absolute potiion it and the div id a block level element anyway, but to correctly have a border you'll need this so it's a decent safeguard */

 /* this takes the element out of the document flow and positions it absolutely within the nearest positioning parent, in this case, the relatively positioned parent */
 position:absolute;

 /* this moves it to the top right edges of the positioning parent*/
 top:0px;
 right:0px;

这是 chrome 的开发工具中的框的插图,将其拉出位置以使正在发生的事情的效果更加明显:

enter image description here

这是恢复定位后的样子:

enter image description here

请注意,您认为透明的部分实际上是不透明的,因此这不会遮盖橙色结帐 block 的任何部分;如果底层页面的背景不是白色,那么很明显您在那里绘制了一个白色部分。

就其值(value)而言,可能值得研究使用图像或 SVG 和 CSS masking实际“切掉”按钮的一部分,但你需要检查 user agent support满足您的需求或尝试一些 work arounds .

关于html - 使用 css 逻辑解释从 div 创建三 Angular 形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068374/

相关文章:

javascript - 如何使用这个 JavaScript

html - 使用不同列的不同规则控制 html 表格列宽

javascript - 图像的插入边框半径

google-chrome - 更复杂的 Flex 布局

url - 为 CSS 行为构建 URL

css - 形状外和垂直对齐的文本

javascript - 从内容上方的重叠区域中删除三 Angular 形的阴影?

html - 为什么文本框中的 padding-left 会导致宽度大于 100%

javascript - 如何检测 HTML5 video 标签支持的视频格式?

html - JSoup 解析div 中的HTML 表格