css 绘制三角形_解释CSS形状:如何使用纯CSS绘制圆,三角形等

css 绘制三角形

Before we start. If you want more free content but in video format. Don't miss out on my Youtube channel where I publish weekly videos on FrontEnd coding. https://www.youtube.com/user/Weibenfalk----------Are you new to web development and CSS? Have you ever wondered how those nice shapes are made that you see all over the internet? Wonder no more. You've come to the right place.

在我们开始之前。 如果您想获得更多免费内容,但使用视频格式。 千万不要错过我的YouTube频道,在这里我每周发布有关FrontEnd编码的视频。 https://www.youtube.com/user/Weibenfalk ----------您是Web开发和CSS的新手吗? 您是否想过如何在互联网上看到那些漂亮的形状? 别再奇怪了。 您来对地方了。

Below I will explain the very basics of creating shapes with CSS. There's a lot to tell you about this topic! Therefore I will not cover all (far from all) tools and shapes but this should give you a basic idea of how shapes are created with CSS.

下面,我将解释使用CSS创建形状的基本知识。 关于这个话题, 有很多要告诉你的! 因此,我不会涵盖所有(绝大部分)工具和形状,但这应该使您基本了解如何使用CSS创建形状。

Some shapes require more "fix and tricks" than others. Creating shapes with CSS is usually a combination of using width, height, top, right, left, border, bottom, transform and pseudo-elements like :before and :after. We also have more modern CSS properties to create shapes with like shape-outside and clip-path. I'll write about them below also.

一些形状比其他形状需要更多的“技巧和窍门”。 使用CSS创建形状通常是使用宽度,高度,顶部,右侧,左侧,边框,底部,变换和伪元素(例如:before:after)的组合。 我们还具有更现代CSS属性,可使用诸如shape-outsideclip-path之类的形状来创建形状 我也会在下面写它们。

CSS形状-基本方法 (CSS Shapes - The basic way)

By using a few tricks in CSS we've always been able to create basic shapes like squares, circles, and triangles with regular CSS properties. Let's look at a few of them now.

通过CSS使用一些技巧,我们总是能够创造像正方形圆形并且与普通CSS属性的三角形基本形状。 现在让我们看看其中的一些。

正方形和矩形 (Squares and rectangles)

Squares and rectangles are probably the easiest shapes to achieve. By default, a div is always a square or a rectangle.

正方形和矩形可能是最容易实现的形状。 默认情况下,div始终是正方形或矩形。

You set the width and height as shown in the below code. Then it's just a matter of giving the element a background color. You can have whatever other properties you want on the element.

如下面的代码所示,设置宽度和高度。 然后,只需为元素提供背景色即可。 您可以在元素上具有任何其他想要的属性。

#square {background: lightblue;width: 100px;height: 100px;
}


(
Circles)

It's almost as easy to create a circle. To create a circle we can set the border-radius on the element. This will create curved corners on the element.

创建圆几乎一样容易。 要创建一个圆,我们可以在元素上设置边框半径。 这将在元素上创建弯曲的角。

If we set it to 50% it will create a circle. If you set a different width and height we will get an oval instead.

如果将其设置为50%,将创建一个圆。 如果您设置不同的宽度和高度,我们将得到一个椭圆形。

#circle {background: lightblue;border-radius: 50%;width: 100px;height: 100px;
}

三角形 (Triangles)

Triangles are a little trickier. We have to set the borders on the element to match a triangle. By setting the width and height to zero on the element, the actual width of the element is going to be the width of the border.

三角形有点棘手。 我们必须在元素上设置边框以匹配三角形。 通过将元素的宽度和高度设置为零,元素的实际宽度将成为边框的宽度。

Keep in mind that the border edges on an element are 45 degree diagonals to each other. That's why this method works to create a triangle. By setting one of the borders to a solid color and the other borders to transparent it will take the form of a triangle.

请记住,元素的边界边缘彼此成45度对角线。 这就是为什么此方法可以创建三角形的原因。 通过将其中一个边界设置为纯色,而将其他边界设置为透明,它将采用三角形的形式。

#triangle {width: 0;height: 0;border-left: 40px solid transparent;border-right: 40px solid transparent;border-bottom: 80px solid lightblue;
}

If you want to have a triangle/arrow pointing in another direction You can change the border values corresponding to what side you want to be visible. Or you can rotate the element with the transform property if you want to be really fancy.

如果要使三角形/箭头指向另一个方向,则可以更改与要显示的那一侧相对应的边框值。 或者,如果您真的想花哨的话,可以使用transform属性旋转元素。

#triangle {width: 0;height: 0;border-top: 40px solid transparent;border-right: 80px solid lightblue;border-bottom: 40px solid transparent;}

Alright – that's an intro to basic shapes with CSS. There are probably an endless amount of shapes you can think of to create. These are just the fundamentals, but with a little creativity and determination you can achieve a lot with just basic CSS properties.

好的-这是CSS基本形状的简介。 您可能会想出无数种形状。 这些只是基础知识,但是只要有一些创造力和决心,仅凭基本CSS属性就可以实现很多目标。

In some cases, with more advanced shapes, it's also a good idea to use the :after and :before pseudo selectors. This is out of scope of this article though as my intention is to cover the basics to get you going.

在某些情况下,对于更高级的形状,使用:after和:before伪选择器也是一个好主意。 这超出了本文的范围,因为我的意图是涵盖使您入门的基础知识。

坏处 (Disadvantage)

There is one big disadvantage with the above approach. For example, if you want your text to flow around and wrap your shape. A regular HTML div with background and borders to make up the shape won't allow that. The text will not adapt and flow around your shape. Instead it will flow around the div itself (which is a square or a rectangle).

上述方法有一个很大的缺点。 例如,如果您希望文本四处流动并包裹形状。 具有背景和边框以构成形状的常规HTML div不允许这样做。 文本将不会适应您的形状,并且不会围绕您的形状流动。 相反,它将围绕div本身(是正方形或矩形)流动。

Below is an illustration showing the triangle and how the text will flow.

下图显示了三角形以及文本如何流动。

Luckily we have some modern CSS properties to use instead.

幸运的是,我们可以使用一些现代CSS属性来代替。

CSS形状-另一种方法 (CSS Shapes - The other way)

Nowadays we have a property called shape-outside to use in CSS. This property lets you define a shape that the text will wrap/flow around.

如今,我们有一个名为shape-outside的属性可在CSS中使用。 此属性使您可以定义文本将环绕/环绕的形状。

Along with this property we have some basic shapes: inset()circle()ellipse()polygon()

除了这个属性,我们还有一些基本形状: inset() circle()ellipse()polygon()

Here's a tip: You can also use the clip-path property. You can create your shape with that in the same way, but it won't let the text wrap around your shape like shape-outside does.

提示 :您还可以使用clip-path属性。 您可以用相同的方式创建形状,但是不会像外部形状那样使文本环绕形状。

The element that we are going to apply the shape to with the shape-outside property to has to be floated. It also has to have a defined width and height. That's really important to know!

我们将要使用shape-outside属性将形状应用到的元素必须是浮动的。 它还必须具有定义的宽度和高度。 要知道,这真的很重要!

You can read more about why here. Below is also a text that I've taken from the provided link to developer.mozilla.org.

您可以在此处详细了解原因。 以下也是我从提供的developer.mozilla.org链接中获取的文本。

The shape-outside property is specified using the values from the list below, which define the float area for float elements. The float area determines the shape around which inline content (float elements) wrap.

使用以下列表中的值指定shape-outside属性,该值定义了float元素的浮动区域。 浮动区域确定内联内容(浮动元素)环绕的形状。

inset() (inset())

The inset() type can be used to create a rectangle/square with an optional offset for the wrapping text. It allows you to provide values on how much you want your wrapping text to overlap the shape.

inset()类型可用于创建一个矩形/正方形,并为包装文本提供可选的偏移量。 它使您可以提供所需的大小,以使换行文字与形状重叠。

You can specify the offset to be the same for all four directions like this: inset(20px). Or it can be individually set for each direction: inset(20px 5px 30px 10px).

您可以将所有四个方向的偏移量指定为相同的: inset(20px)。 或者可以为每个方向分别设置: inset(20px 5px 30px 10px)

You can use other units also to set the offset, for example, percent. The values correspond like this: inset(top right bottom left).

您还可以使用其他单位来设置偏移量,例如百分比。 这些值的对应关系如下: inset(右上左下)

Check out the below code example. I've specified the inset values to be 20px at the top, 5px to the right, 30px at the bottom and 10px to the left. If you want your text to go around your square instead you can just skip using inset() at all. Instead set the background on your div and specify the size as usual.

查看以下代码示例。 我已将插入值指定为顶部20px,右侧5px,底部30px和左侧10px。 如果您希望文本围绕正方形显示,则可以完全跳过inset()。 而是在div上设置背景并照常指定大小。

#square {float: left;width: 100px;height: 100px;shape-outside: inset(20px 5px 30px 10px);background: lightblue;}

It is also possible to give inset() a second value that specifies the border-radius of the inset. Like below:

还可以给inset()一个第二个值,该值指定插图的边界半径。 如下所示:

#square {float: left;width: 100px;height: 100px;shape-outside: inset(20px 5px 30px 10px round 50px);background: lightblue;}

圈() (circle())

In this one a circle is created using the shape-outside property. You also have to apply a clip-path with the corresponding property for the circle to show up.

在这个图中,使用shape-outside属性创建了一个圆。 您还必须应用具有相应属性的剪切路径 ,才能显示圆。

The clip-path property can take the same value as the shape-outside property so we can give it the standard circle() shape that we used for shape-outside. Also, note that I've applied a 20px margin on the element here to give the text some space.

clip-path属性可以使用与shape-outside属性相同的值,因此我们可以为其提供用于shape-outside的标准circle()形状。 另外,请注意,我在此处的元素上应用了20px的边距,为文本提供了一些空间。

#circle {float: left;width: 300px;height: 300px;margin: 20px;shape-outside: circle();clip-path: circle();background: lightblue;
}

In the above example, I don't specify the radius of the circle. This is because I want it to be as big as the div is (300px). If you want to specify a different size for the circle you can do that.

在上面的示例中,我没有指定圆的半径。 这是因为我希望它与div一样大(300px)。 如果您想为圆指定不同的大小,则可以这样做。

The circle() takes two values. The first value is the radius and the second value is the position. These values will specify the center of the circle.

circle()采用两个值。 第一个值是半径,第二个值是位置。 这些值将指定圆的中心。

In the below example I've set the radius to 50%. Then I have shifted the center of the circle by 30%. Note that the word "at" has to be used between the radius and position values.

在下面的示例中,我将半径设置为50%。 然后,我将圆心偏移了30%。 注意,在半径和位置值之间必须使用单词“ at”。

I've also specified another position value on the clip-path. This will clip the circle in half as I move the position to zero.

我还在剪切路径上指定了另一个位置值。 当我将位置移到零时,这会将圆圈切成两半。

#circle {float: left;width: 150px;height: 150px;margin: 20px;shape-outside: circle(50% at 30%);clip-path: circle(50% at 0%);background: lightblue;}

椭圆() (ellipse())

Ellipses work the same way as circles except that they create an oval. You can define both the X value and the Y value, like this: ellipse(25px  50px).

椭圆的工作方式与圆形相同,只不过它们创建了一个椭圆。 您可以定义X值和Y值,如下所示: ellipse(25px 50px)。

The same as a circle, it also takes a position value as the last value.

与圆形相同,它也将位置值作为最后一个值。

#ellipse {float: left;width: 150px;height: 150px;margin: 20px;shape-outside: ellipse(20% 50%);clip-path: ellipse(20% 50%);background: lightblue;}

多边形() (polygon())

A polygon is a shape with different vertices/coordinates defined. Below I create a "T" shape which is the first letter in my name. I start from the coordinates 0,0 and move from left to right to create the "T" shape.

多边形是定义了不同顶点/坐标的形状。 在下面创建一个“ T”形,这是我名字中的第一个字母。 我从0,0坐标开始,然后从左向右移动以创建“ T”形。

#polygon {float: left;width: 150px;height: 150px;margin: 0 20px;shape-outside: polygon(0 0,100% 0,100% 20%,60% 20%,60% 100%,40% 100%,40% 20%,0 20%);clip-path: polygon(0 0,100% 0,100% 20%,60% 20%,60% 100%,40% 100%,40% 20%,0 20%);background: lightblue;}

图片 (Images)

You can also use images with transparent backgrounds to create your shape. Like this round beautiful moon below.

您也可以使用具有透明背景的图像来创建形状。 就像下面这轮美丽的月亮。

This is a .png image with a transparent background.

这是具有透明背景的.png图像。

<img src="src/moon.png" id="moon" />
#moon {float: left;width: 150px;height: 150px;shape-outside: url("./src/moon.png");}

And that's it! Thank you for reading.

就是这样! 感谢您的阅读。

关于本文的作者 (About the author of this article)

My name is Thomas Weibenfalk and I'm a developer from Sweden. I regularly create free tutorials on my Youtube channel. There's also a few premium courses out there on React and Gatsby. Feel free to visit me on these links:

我叫Thomas Weibenfalk,是瑞典的一名开发商。 我定期在我的YouTube频道上创建免费教程。 在React和Gatsby上还有一些高级课程。 随时通过以下链接访问我:

Twitter — @weibenfalk,Weibenfalk on Youtube,Weibenfalk Courses Website.

Twitter — @ weibenfalk ,Weibenfalk在Youtube上 ,Weibenfalk 课程网站 。

翻译自: https://www.freecodecamp.org/news/css-shapes-explained-how-to-draw-a-circle-triangle-and-more-using-pure-css/

css 绘制三角形

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390530.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

密码学基本概念(一)

区块链兄弟社区&#xff0c;区块链技术专业问答先行者&#xff0c;中国区块链技术爱好者聚集地 作者&#xff1a;于中阳 来源&#xff1a;区块链兄弟 原文链接&#xff1a;http://www.blockchainbrother.com/article/72 著权归作者所有。商业转载请联系作者获得授权&#xff0c…

JAVA-初步认识-第十三章-多线程(验证同步函数的锁)

一. 至于同步函数用的是哪个锁&#xff0c;我们可以验证一下&#xff0c;借助原先卖票的例子 对于程序中的num&#xff0c;从100改为400&#xff0c;DOS的结果显示的始终都是0线程&#xff0c;票号最小都是1。 票号是没有问题的&#xff0c;因为同步了。 有人针对只出现0线程&a…

追求卓越追求完美规范学习_追求新的黄金比例

追求卓越追求完美规范学习The golden ratio is originally a mathematical term. But art, architecture, and design are inconceivable without this math. Everyone aspires to golden proportions as beautiful and unattainable perfection. By visualizing data, we chal…

leetcode 275. H 指数 II

给定一位研究者论文被引用次数的数组&#xff08;被引用次数是非负整数&#xff09;&#xff0c;数组已经按照 升序排列 。编写一个方法&#xff0c;计算出研究者的 h 指数。 h 指数的定义: “h 代表“高引用次数”&#xff08;high citations&#xff09;&#xff0c;一名科研…

Node js开发中的那些旮旮角角 第一部

#前戏 上一周是我到现公司来最忙碌的&#xff08;最有意思的&#xff09;一周了&#xff0c;为什么这么说呢&#xff1f;因为项目中需要提供服务端对用户病人信息的一个汇总并以email的形式分享信息的接口&#xff0c;在几天的时间里调研处理一套实施方案。我们服务端是Node.js…

文件2. 文件重命名

servlet对本机已存在的文件进行重命名。 .jsp界面 1 <form action"<%basePath %>fileAction" method"get" >2 <table>3 <tr>4 <td>输入文件路径</td>5 <td&…

js字符串slice_JavaScript子字符串示例-JS中的Slice,Substr和Substring方法

js字符串sliceIn daily programming, we often need to work with strings. Fortunately, there are many built-in methods in JavaScript that help us while working with arrays, strings and other data types. We can use these methods for various operations like sea…

leetcode 218. 天际线问题

城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度&#xff0c;请返回由这些建筑物形成的 天际线 。 每个建筑物的几何信息由数组 buildings 表示&#xff0c;其中三元组 buildings[i] [lefti, righti, heighti] 表示&#xff1a…

[Android Pro] 终极组件化框架项目方案详解

cp from : https://blog.csdn.net/pochenpiji159/article/details/78660844 前言 本文所讲的组件化案例是基于自己开源的组件化框架项目github上地址github.com/HelloChenJi…其中即时通讯(Chat)模块是单独的项目github上地址github.com/HelloChenJi… 1.什么是组件化&#xff…

如何写一个vue指令directive

举个例子 &#xff1a;clickoutside.js const clickoutsideContext clickoutsideContext;export default {/*param el 指令所绑定的元素param binding {Object} param vnode vue编译生成的虚拟节点*/bind (el, binding, vnode) {const documentHandler function(e) {console.…

安装angular cli_Angular 9适用于初学者—如何使用Angular CLI安装第一个应用程序

安装angular cliAngular is one of the most popular JavaScript frameworks created and developed by Google. In the last couple of years, ReactJS has gained a lot of interest and has become the most popular modern JS library in the industry. But this doesn’t …

leetcode 1818. 绝对差值和

给你两个正整数数组 nums1 和 nums2 &#xff0c;数组的长度都是 n 。 数组 nums1 和 nums2 的 绝对差值和 定义为所有 |nums1[i] - nums2[i]|&#xff08;0 < i < n&#xff09;的 总和&#xff08;下标从 0 开始&#xff09;。 你可以选用 nums1 中的 任意一个 元素来…

【转载】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因

初学STM32&#xff0c;在RealView MDK 环境中使用STM32固件库建立工程时&#xff0c;初学者可能会遇到编译不通过的问题。出现如下警告或错误提示&#xff1a; warning: #223-D: function "assert_param" declared implicitly;assert_param(IS_GPIO_ALL_PERIPH(GPIOx…

下岗职工_下岗后我如何获得多位软件工程师的面试

下岗职工“Opportunities to find our deeper powers come when life seems most challenging.” -Joseph Campbell“当生活似乎最具挑战性时&#xff0c;就有机会找到我们更深层的力量。” 约瑟夫坎贝尔 I was recently laid off for the first time in my life. I realized t…

1846. 减小和重新排列数组后的最大元素

给你一个正整数数组 arr 。请你对 arr 执行一些操作&#xff08;也可以不进行任何操作&#xff09;&#xff0c;使得数组满足以下条件&#xff1a; arr 中 第一个 元素必须为 1 。任意相邻两个元素的差的绝对值 小于等于 1 &#xff0c;也就是说&#xff0c;对于任意的 1 <…

bashdb常用命令

一、列出代码和查询代码类&#xff1a; l 列出当前行以下的10行- 列出正在执行的代码行的前面10行. 回到正在执行的代码行w 列出正在执行的代码行前后的代码/pat/ 向后搜索pat&#xff1f;pat&#xff1f;向前搜索pat二、Debug控制类&#xff1a; h 帮助help 命令 得到…

podcast播客资源_为什么播客是我的新维基百科-完美的非正式学习资源

podcast播客资源In this article, I’ll explain why podcasts replaced a lot of my Wikipedia usage for informal learning. I’ll also talk about how I listen to 5 hours of podcasts every day.在本文中&#xff0c;我将解释为什么播客代替了我的许多Wikipedia用于非正…

剑指 Offer 53 - I. 在排序数组中查找数字 I(二分法)

统计一个数字在排序数组中出现的次数。 示例 1: 输入: nums [5,7,7,8,8,10], target 8 输出: 2 示例 2: 输入: nums [5,7,7,8,8,10], target 6 输出: 0 限制&#xff1a; 0 < 数组长度 < 50000 解题思路 先用二分法查找出其中一个目标元素再向目标元素两边查找…

MVC与三层架构区别

我们平时总是将三层架构与MVC混为一谈&#xff0c;殊不知它俩并不是一个概念。下面我来为大家揭晓我所知道的一些真相。 首先&#xff0c;它俩根本不是一个概念。 三层架构是一个分层式的软件体系架构设计&#xff0c;它可适用于任何一个项目。 MVC是一个设计模式&#xff0c;它…

tensorflow 实现逻辑回归——原以为TensorFlow不擅长做线性回归或者逻辑回归,原来是这么简单哇!...

实现的是预测 低 出生 体重 的 概率。尼克麦克卢尔&#xff08;Nick McClure&#xff09;. TensorFlow机器学习实战指南 (智能系统与技术丛书) (Kindle 位置 1060-1061). Kindle 版本. # Logistic Regression #---------------------------------- # # This function shows ho…