本文翻译自:How do CSS triangles work?

There're plenty of different CSS shapes over at CSS Tricks - Shapes of CSS and I'm particularly puzzled with a triangle: CSS技巧中有很多不同的CSS形状-CSS的形状,我尤其对三角形感到困惑:

 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid red; } 
 <div id="triangle-up"></div> 

How and why does it work? 它如何以及为什么起作用?


#1楼

参考:https://stackoom.com/question/Tg8S/CSS三角形如何工作


#2楼

Consider the below triangle 考虑下面的三角形

.triangle {border-bottom:15px solid #000;border-left:10px solid transparent;border-right:10px solid transparent;width:0;height:0;
}

This is what we are given: 这是我们得到的:

Why it came out in this shape? 为什么以这种形状出现? The below diagram explains the dimensions, note that 15px was used for the bottom border and 10px was used for left and right. 下图说明了尺寸,请注意,下边框使用15px,左右使用10px。

It's pretty easy to make a right-angle triangle also by removing the right border. 也可以通过删除右边框来制作直角三角形。


#3楼

Different approach. 不同的方法。 With linear gradient (for IE, only IE 10+). 使用线性渐变(对于IE,仅IE 10+)。 You can use any angle: 您可以使用任何角度:

 .triangle { margin: 50px auto; width: 100px; height: 100px; /* linear gradient */ background: -moz-linear-gradient(-45deg, rgba(255,0,0,0) 0%, rgba(255,0,0,0) 50%, rgba(255,0,0,1) 50%, rgba(255,0,0,1) 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,rgba(255,0,0,0)), color-stop(50%,rgba(255,0,0,0)), color-stop(50%,rgba(255,0,0,1)), color-stop(100%,rgba(255,0,0,1))); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(-45deg, rgba(255,0,0,0) 0%,rgba(255,0,0,0) 50%,rgba(255,0,0,1) 50%,rgba(255,0,0,1) 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(-45deg, rgba(255,0,0,0) 0%,rgba(255,0,0,0) 50%,rgba(255,0,0,1) 50%,rgba(255,0,0,1) 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(-45deg, rgba(255,0,0,0) 0%,rgba(255,0,0,0) 50%,rgba(255,0,0,1) 50%,rgba(255,0,0,1) 100%); /* IE10+ */ background: linear-gradient(135deg, rgba(255,0,0,0) 0%,rgba(255,0,0,0) 50%,rgba(255,0,0,1) 50%,rgba(255,0,0,1) 100%); /* W3C */; } 
 <div class="triangle"></div> 

Here is jsfiddle 这是jsfiddle


#4楼

Lets say we have the following div: 可以说我们有以下div:

<div id="triangle" />

Now Edit the CSS step-by-step, so you will get clear idea what is happening around 现在,逐步编辑CSS,这样您就可以清楚地了解正在发生的事情

STEP 1: JSfiddle Link: 第1 JSfiddle链接:

 #triangle {background: purple;width :150px;height:150PX;border-left: 50px solid black ;border-right: 50px solid black;border-bottom: 50px solid black;border-top: 50px solid black;}

This is a simple div. 这是一个简单的div。 With a very simple CSS. 使用非常简单的CSS。 So a layman can understand. 所以外行可以理解。 Div has dimensions 150 x 150 pixels with the border 50 pixels. Div的尺寸为150 x 150像素,边框为50像素。 The image is attached: 图像已附加:

STEP 2: JSfiddle Link: 第2 JSfiddle链接:

#triangle {background: purple;width :150px;height:150PX;border-left: 50px solid yellow ;border-right: 50px solid green;border-bottom: 50px solid red;border-top: 50px solid blue;
}

Now I just changed the border-color of all 4 sides. 现在,我只是更改了所有4个面的边框颜色。 The image is attached. 图像已附加。

STEP:3 JSfiddle Link: 步骤:3 JSfiddle链接:

#triangle {background: purple;width :0;height:0;border-left: 50px solid yellow ;border-right: 50px solid green;border-bottom: 50px solid red;border-top: 50px solid blue;
}

Now I just changed the height & width of div from 150 pixels to zero. 现在,我只是将div的高度和宽度从150像素更改为零。 The image is attached 图像已附加

STEP 4: JSfiddle: 步骤4: JSfiddle:

#triangle {background: purple;width :0px;height:0px;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 50px solid red;border-top: 50px solid transparent;
}

Now I have made all the borders transparent apart from the bottom border. 现在,使底部边界以外的所有边界透明。 The image is attached below. 图像附在下面。

STEP 5: JSfiddle Link: 第5 JSfiddle链接:

#triangle {background: white;width :0px;height:0px;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 50px solid red;border-top: 50px solid transparent;
}

Now I just changed the background color to white. 现在,我只是将背景色更改为白色。 The image is attached. 图像已附加。

Hence we got the triangle we needed. 因此,我们得到了所需的三角形。


#5楼

Different approach: 不同的方法:

CSS3 triangles with transform rotate 具有转换功能的CSS3三角形

Triangular shape is pretty easy to make using this technique. 使用此技术很容易制作出三角形形状。 For people who prefer to see an animation explaining how this technique works here it is : 对于喜欢看动画解释此技术原理的人来说,它是:

  • Link to the ANIMATION : How to make a CSS3 triangle . 链接到动画: 如何制作CSS3三角形 。
  • And DEMO : CSS3 triangles made with transform rotate. 以及DEMO:用变换制作的CSS3三角形旋转。

Otherwise, here is detailed explanation in 4 acts (this is not a tragedy) of how to make an isosceles right-angled triangle with one element. 否则,这里将详细说明如何用一个元素制作等腰直角三角形的4个动作(这不是悲剧)。

  • Note 1 : for non isosceles triangles and fancy stuff, you can see step 4 . 注意1:对于非等腰三角形和奇特的东西,您可以参阅第4步
  • Note 2 : in the following snippets, the vendor prefixes aren't included. 注意2:在以下代码段中,不包括供应商前缀。 they are included in the codepen demos . 它们包含在codepen演示中
  • Note 3 : the HTML for the following explanation is always : <div class="tr"></div> 注3:用于以下说明的HTML始终为: <div class="tr"></div>

STEP 1 : Make a div 步骤1:进行div

Easy, just make sure that width = 1.41 x height . 容易,只需确保width = 1.41 x height You may use any techinque ( see here ) including the use of percentages and padding-bottom to maintain the aspect ratio and make a responsive triangle . 您可以使用任何技巧( 请参见此处 ),包括使用百分比和底部填充来保持长宽比并创建一个自适应三角形 。 In the following image, the div has a golden yellow border. 在下图中,div具有金黄色边框。

In that div, insert a pseudo element and give it 100% width and height of parent. 在该div中,插入一个伪元素,并为其提供100%的父级宽度和高度。 The pseudo element has a blue background in the following image. 下图中的伪元素具有蓝色背景。

At this point, we have this CSS : 至此,我们有了这个CSS

.tr {width: 30%;padding-bottom: 21.27%; /* = width / 1.41 */position: relative;
}.tr: before {content: '';position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: #0079C6;
}

STEP 2 : Let's rotate 步骤2:让我们旋转

First, most important : define a transform origin . 首先,最重要的是: 定义变换原点 The default origin is in the center of the pseudo element and we need it at the bottom left. 默认原点在伪元素的中心,我们需要它在左下角。 By adding this CSS to the pseudo element : 通过将此CSS添加到伪元素中:

transform-origin:0 100%; or transform-origin: left bottom; transform-origin: left bottom;

Now we can rotate the pseudo element 45 degrees clockwise with transform : rotate(45deg); 现在我们可以使用transform : rotate(45deg);将伪元素顺时针旋转45度transform : rotate(45deg);

At this point, we have this CSS : 至此,我们有了这个CSS

.tr {width: 30%;padding-bottom: 21.27%; /* = width / 1.41 */position: relative;
}.tr:before {content: '';position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: #0079C6;transform-origin: 0 100%;        transform: rotate(45deg);
}

STEP 3 : hide it 步骤3:隐藏

To hide the unwanted parts of the pseudo element (everything that overflows the div with the yellow border) you just need to set overflow:hidden; 要隐藏伪元素的多余部分(所有溢出div并带有黄色边框的部分),您只需要设置overflow:hidden; on the container. 在容器上。 after removing the yellow border, you get... a TRIANGLE ! 删除黄色边框后,您会得到... 三角形 :

DEMO 演示

CSS : CSS:

.tr {width: 30%;padding-bottom: 21.27%; /* = width / 1.41 */position: relative;overflow: hidden;
}.tr:before {content: '';position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: #0079C6;transform-origin: 0 100%;transform: rotate(45deg);
}

STEP 4 : go further... 步骤4:进一步...

As shown in the demo , you can customize the triangles : 如演示中所示,您可以自定义三角形:

  1. Make them thinner or flatter by playing with skewX() . 通过玩skewX()使它们更薄或更平坦。
  2. Make them point left, right or any other direction by playing with the transform orign and rotation direction. 通过播放转换方向和旋转方向使它们指向左,右或任何其他方向。
  3. Make some reflexion with 3D transform property. 使用3D变换属性进行一些反射 。
  4. Give the triangle borders 给三角形边框
  5. Put an image inside the triangle 将图像放在三角形内
  6. Much more... Unleash the powers of CSS3 ! 还有更多...释放CSS3的强大功能

Why use this technique? 为什么要使用这种技术?

  1. Triangle can easily be responsive. 三角形可以轻松响应。
  2. You can make a triangle with border . 您可以制作一个带边框的三角形 。
  3. You can maintain the boundaries of the triangle. 您可以保持三角形的边界。 This means that you can trigger the hover state or click event only when the cursor is inside the triangle . 这意味着仅当光标位于三角形内部时,才可以触发悬停状态或单击事件。 This can become very handy in some situations like this one where each triangle can't overlay it's neighbours so each triangle has it's own hover state. 在像这样的情况下,这变得非常方便,在这种情况下,每个三角形都无法覆盖其邻居,因此每个三角形都有自己的悬停状态。
  4. You can make some fancy effects like reflections . 您可以制作一些奇特的效果,例如反射 。
  5. It will help you understand 2d and 3d transform properties. 它将帮助您了解2d和3d变换属性。

Why not use this technique? 为什么不使用这种技术?

  1. The main drawback is the browser compatibility , the 2d transform properties are supported by IE9+ and therefore you can't use this technique if you plan on supporting IE8. 主要缺点是浏览器兼容性 ,IE9 +支持2d转换属性,因此,如果计划支持IE8,则不能使用此技术。 See CanIuse for more info. 有关更多信息,请参见CanIuse 。 For some fancy effects using 3d transforms like the reflection browser support is IE10+ (see canIuse for more info). 对于某些使用3d变换的奇特效果,例如反射浏览器支持为IE10 +(有关更多信息,请参见canIuse )。
  2. You don't need anything responsive and a plain triangle is fine for you then you should go for the border technique explained here : better browser compatibility and easier to understand thanks to the amaizing posts here. 您不需要任何响应,并且普通三角形适合您,那么您应该采用此处说明的边框技术:更好的浏览器兼容性和易于理解的感谢(感谢此处引人入胜的帖子)。

#6楼

And now something completely different... 现在完全不同了...

Instead of using css tricks don't forget about solutions as simple as html entities: 除了使用CSS技巧外,别忘了像html实体这样简单的解决方案:

Result: 结果:

See: What are the HTML entities for up and down triangles? 请参阅: 上下三角形的HTML实体是什么?

CSS三角形如何工作?相关推荐

  1. 【夯实基础--CSS】=> 高级技巧(雪碧图/滑动门/CSS三角形/字体图标(iconfont)等)

    CSS高级技巧 CSS用户界面样式 所谓的界面样式, 就是更改一些用户操作样式, 比如 更改用户的鼠标样式, 表单轮廓等.但是比如滚动条的样式改动受到了很多浏览器的抵制,因此我们就放弃了. 防止表单域 ...

  2. css三角形之美与品优购项目

    CSS三角形之美 div{width: 0;height: 0;line-height:0;font-size: 0;border-top: 10px solid red;border-right:1 ...

  3. html li 怎么合并,HTML—CSS-case(合并boder / 滑动门 / 去除li边框 /css三角形原理)...

    1.实现多个div盒子横排列,鼠标点上去后显示border,相连的border合并. :利用position:relative的层级比标准流高(后来居上原则),与margin-left:-1px: 若 ...

  4. 六、pink老师学习笔记——CSS三角形之美 margin负值之美文字围绕浮动元素行内块元素布局的巧妙运用

    7. 拓展@ 7.1 margin负值之美 1). 负边距+定位:水平垂直居中 咱们前面讲过, 一个绝对定位的盒子, 利用 父级盒子的 50%, 然后 往左(上) 走 自己宽度的一半 ,可以实现盒子水 ...

  5. 前端读者 | CSS三角形和饼图

    @羯瑞 三角形 .triangle{width:0;height:0;border-width:50px;border-style:solid;border-color:red blue green ...

  6. 前端怎么画三角形_WEB前端-CSS三角形绘制方法

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

  7. CSS第六天-高级技巧(元素隐藏、精灵图、CSS用户界面样式、margi负值作用、CSS三角形)

    ps 今天这是html和css初级部分的最后一份文章了,下次发的就是HTML5和CSS3的一些基础知识.越学到后面,感觉越有意思.到后面可以自己去尝试做一些比较炫酷的动画了,还是蛮期待的. CSS高级 ...

  8. html三角形图标,纯css三角形和css三角箭头实例代码

    css箭头实例demo 纯css三角形和css三角箭头,可以随意放大缩小,可以随意颜色,这要比图片实现的三角箭头或者三角形状要灵活的多. 本文的Demo和源代码已放到GitHub,如果您觉得本篇内容不 ...

  9. css三角形 增涨,CSS3实现三角形不断放大效果

    一.CSS3三角形不断放大特效 11.1 图片预览 11.2 index.html代码 CSS3三角形不断放大特效 11.3 style.css代码 html { height: 100%; } bo ...

最新文章

  1. 硬盘驱动器是如何工作的?
  2. sudo: unable to resolve host ubuntu提示的解决
  3. DL之DNN之BP:神经网络算法简介之BP算法/GD算法之不需要额外任何文字,只需要八张图讲清楚BP类神经网络的工作原理
  4. iBATIS.NET 学习笔记(五)
  5. ps怎么对比原图快捷键_PS学习之旅:ps如何制作满天星,让你夜晚的天空图片更美...
  6. XML与HTML的作用不同
  7. 618 前夕,不谈促销,京东云带你聊聊技术……
  8. Android 百度地图开发(三)--- 实现比例尺功能和替换自带的缩放组件
  9. MyBatis的一级缓存、二级缓存演示以及讲解,序列化异常的处理
  10. 极致物业管理软件的系统特点
  11. java实现手机扫描二维码后网站跳转新页面
  12. 【Python抢票神器】火车票枪票软件到底靠谱吗?实测—终极攻略。
  13. https证书格式转换(cer转bks)
  14. 一键获取谷歌网盘(Google Drive)真实直链下载地址
  15. 有什么蓝牙耳机好用又不贵?适合学生党的平价蓝牙耳机推荐
  16. pygame 窗口标题和图标设置
  17. java暗黑破坏神,《暗黑破坏神2》1.10 雇用兵详细介绍
  18. 51nod题解 1006 最长公共子序列LCS
  19. 地雷復 (易經大意 韓長庚)
  20. mysql的sql查询IN里面有重复的值,怎么不去重查询,这是一条思路

热门文章

  1. 8086寄存器组织结构
  2. Android String.xml 批量翻译工具 | Android string.xml 各国语言转换
  3. Android中shape属性详解
  4. xUtils简介及其使用方法
  5. Android之安装常见的一些解决方法
  6. UITableView 顶部空白总结
  7. swift_039(Swift中的KVC的使用方法)
  8. webpack中loader加载器(打包非js模块)
  9. TP5.0 PHPExcel 数据表格导出导入(引)
  10. 深入理解java虚拟机(7)---线程安全  锁优化