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.pswp.cn/news/390530.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390530.shtml
英文地址,請注明出處:http://en.pswp.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…