CSS3線性漸層實作4個圓環相連(程式碼實例)
本文目標:
1、掌握CSS3中線性漸變(linear-gradient)的實作方法
問題:
要求實現以下效果,使用純DIV CSS ,必須使用知識點 線性漸變(linear-gradient),且居中顯示
附加說明:
#1、一個彩色圓環的大小是400px *400px,裡面空白圓環大小為260px*260px
2、右邊圓環和左邊圓環的左移距離為120px
3、下邊圓環和上邊圓環的上移距離為20px
思路分析:
1、最外層是綠色邊框的div,裡麵包裹著4個圓環
2.div分上下兩部分,兩部分其實都是一樣的,只要實現了上面部分,下面部分其實是可以拷貝,只是margin-top要更改一下
3、每個圓環其實都是一樣,只是位置不同,所以只要把一個漸變圓環實現出來,其他所有的圓環都可以複製第一個圓環
現在來具體操作
1 、創建好index.html,寫好架構
架構思路:
1、外層容器我們就叫做.container容器,裡面分成.top,.bottom兩部分,每個部分裡麵包含兩個圓環div,
並行的兩個div圓環,因為要水平排列,所以肯定需要float,既然float,了,在容器的最後需要加上一個.clear的div,清除浮動,這樣可以讓容器依然包裹住兩個float的圓環div
2、每個圓環其實也是由兩個部分組成,一個外層彩色圓環和內層一個白色的小的圓環
根據分析得出以下架構代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>渐变圆环</title> </head> <body> <div class="container"> <div class="top"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> <div class="bottom"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> </div> </body> </html>
#2、接下來,寫樣式
1、創建css文件夾,方便管理所有的css文件,創建index.css,裡面的樣式怎麼寫呢,思路如下:
1、.container *
思路分析
##1 、為了設定容器裡的所有元素的公共樣式,我們可以將這些公共代碼寫入.container * 樣式內所以index.css中添加程式碼如下:.container *{ padding:0; margin: 0; }
.colorcircle{ width:400px; height:400px; border-radius: 400px; background-image:linear-gradient(to left, red, orange,yellow,green,blue,indigo,violet); border:1px solid transparent; float: left; }
.smallcircle{ width: 260px; height: 260px; border-radius: 260px; background-color: white; margin:70px auto; border:1px solid transparent; }
4、.clear樣式#想法分析:1、需要清除浮動,所以需要float:none,clear:both2、這種div裡面沒有內容,為了不影響佈局,需要設定width,height都為0#繼續index.css中加入程式碼如下:
.clear{ clear: both; float: none; width: 0; height: 0; }
.circle2{ margin-left:-120px; }
.bottom{ margin-top:-20px; }
.container{ border:1px solid green; width:684px; margin:0 auto; height: 784px; }
.container *{ padding:0; margin: 0; } .colorcircle{ width:400px; height:400px; border-radius: 400px; background-image:linear-gradient(to left, red, orange,yellow,green,blue,indigo,violet); border:1px solid transparent; float: left; } .smallcircle{ width: 260px; height: 260px; border-radius: 260px; background-color: white; margin:70px auto; border:1px solid transparent; } .clear{ clear: both; float: none; width: 0; height: 0; } .circle2{ margin-left:-120px; } .bottom{ margin-top:-20px; } .container{ border:1px solid green; width:684px; margin:0 auto; height: 784px; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>渐变圆环</title> <link rel="stylesheet" href="css/index.css" /> </head> <body> <div class="container"> <div class="top"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> <div class="bottom"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> </div> </body> </html>
運行結果如下:
发现下面部分.bottom的margin-top好像失效了,其实如果我们将.bottom的border设置成红色,可以看出,其实也是上移了20px,但是因为里面圆环是float的,且默认的postion为relative,所以圆环是无法再上移的,怎么办呢?这里提供两种解决方案:
1、我们将.bottom的postion设置为absoute
index.css中.bottom代码修改如下:
.bottom{ margin-top:-20px; position: absolute; }
我们再来运行效果:
我们发现效果实现了
还有一种方法就是
2、通过让.top,.bottom上下两个div都float:left,也可以解决,只不过这样在.container的最后需要添加.clear 块
index.html代码修改如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>渐变圆环</title> <link rel="stylesheet" href="css/index.css" /> </head> <body> <div class="container"> <div class="top"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> <div class="bottom"> <!-- 第一个圆环 --> <div class="colorcircle circle1"> <div class="smallcircle"> </div> </div> <!-- 第二个圆环 --> <div class="colorcircle circle2"> <div class="smallcircle"> </div> </div> <div class="clear"> </div> </div> <!--如果.top,.bottom都float了,需要加上此行--> <div class="clear"> </div> </div> </body> </html>
index.css代码如下:
.container *{ padding:0; margin: 0; } .colorcircle{ width:400px; height:400px; border-radius: 400px; background-image:linear-gradient(to left, red, orange,yellow,green,blue,indigo,violet); border:1px solid transparent; float: left; } .smallcircle{ width: 260px; height: 260px; border-radius: 260px; background-color: white; margin:70px auto; border:1px solid transparent; } .clear{ clear: both; float: none; width: 0; height: 0; } .circle2{ margin-left:-120px; } /* 解决上移问题 */ .bottom{ margin-top:-20px; float: left; } .top{ float: left; } /* end */ .container{ border:1px solid green; width:684px; margin:0 auto; height: 784px; }
运行结果为:
效果还是一样的
到此为止,我们就实现了全部的需求
总结:
1、学习了CSS3中线线渐变的语法如
linear-gradient(to left, red, orange,yellow,green,blue,indigo,violet);
其中方向left也可以是right,后面的颜色,可以2个或者3个都可以自定义
希望本文能给大家带来一定的帮助,谢谢!!!
以上是CSS3線性漸層實作4個圓環相連(程式碼實例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

純CSS3怎麼實現波浪效果?這篇文章就來跟大家介紹一下使用 SVG 和 CSS 動畫來製作波浪效果的方法,希望對大家有幫助!

兩種方法:1、利用display屬性,只要為元素加上「display:none;」樣式即可。 2.利用position和top屬性設定元素絕對定位來隱藏元素,只需為元素加上「position:absolute;top:-9999px;」樣式。

在css中,可以利用border-image屬性來實作花邊邊框。 border-image屬性可以使用圖片來建立邊框,即給邊框加上背景圖片,只需要將背景圖片指定為花邊樣式即可;語法「border-image: url(圖片路徑) 向內偏移值圖像邊界寬度outset 是否重複;」。

怎麼製作文字輪播與圖片輪播?大家第一想到的是利用js,其實利用純CSS也能實現文字輪播與圖片輪播,下面來看看實作方法,希望對大家有幫助!

實作方法:1、使用「:active」選擇器選取滑鼠點擊圖片的狀態;2、使用transform屬性和scale()函數實現圖片放大效果,語法「img:active {transform: scale(x軸放大倍率,y軸放大倍率);}」。

在css3中,可以利用「animation-timing-function」屬性來設定動畫旋轉速度,該屬性用於指定動畫將如何完成一個週期,設定動畫的速度曲線,語法為「元素{animation-timing-function:速度屬性值;}」。

css3中的動畫效果有變形;可以利用「animation:動畫屬性@keyframes ..{..{transform:變形屬性}}」實現變形動畫效果,animation屬性用於設定動畫樣式,transform屬性用於設定變形樣式。
