CSS画三角形引发的一些思考_html/css_WEB-ITnose
??今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎。很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的更深的思考。
??第一次遇到这个问题是在撸Bootstrap的一个demo ??Blog Template for Bootstrap,它的导航栏中用到了CSS来画三角形:
我们来看一看其中重点的那段CSS代码:
.blog-nav .active:after {
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
margin-left: -5px;
vertical-align: middle;
content: " ";
border-right: 5px solid transparent;
border-bottom: 5px solid;
border-left: 5px solid transparent;
}
??这只是一段简单的用CSS画出等腰直角三角形的实现案例,当时我在SF的一篇文章找到了答案:图解利用CSS实现三角形 - SegmentFault,通过这篇文章,我明白了原来border是一个梯形,当梯形的上底为0的极限情况时,梯形就成了一个三角形,画一个正方形,正方形的div为0时,隐藏三条border,剩下的可见的border便是所需的三角形。于是bootstrap的demo中的问题迎刃而解。
??然而,bootstrap的demo中的等边直角三角形是一个非常经典的情况,那么画任意三角形的时候怎么办呢?知乎的@Vkki用户给出了结论:
(上边的 width 控制了这个三角形上顶点离 div 边缘的距离是 10px)
下边的 width 控制了三角形的高(150px)
左右两边的 width 分别控制了三角形的底边长的两部分(加起来共 200px)
??记住结论固然重要,然而我又引发了好奇心,如果左右上下四边的width不相等的情况下,各个border又是什么样的的?
??于是我画了一个div,CSS代码如下:
div{
width:0;
height:0;
border-top:100px solid;
border-bottom:125px solid;
border-left:150px solid;
border-right:175px solid;
border-color:red green blue yellow;
}
??在浏览器中的效果图:
??结果和预想的有点不一样,但是结合上面的结论,已经非常好理解了:)
??其实在回答中,@王潇的答案也让我想到了很多,他利用CSS3中transform属性的shewX()方法以及rotate()方法还有活用skewX()方法画出了一般形状的三角形,和其他答案不一样的思路确实让我眼前一亮,想起了强大的CSS3,利用CSS3的新特性可以完成很多以前只能用js实现的效果,真棒!当然,付出的代价是兼容性。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.
