跟我一塊兒用CSS變量加grid畫一隻小企鵝吧!

今天在學習的時候發現本身竟然不知道css能夠使用變量來引用一個值。因此決定作一個小案例。恰好以前也瞭解到新出的grid佈局很好用,這裏我就結合一下兩個我不熟悉的知識點,給你們畫一隻可愛的小企鵝。歡迎你們指正錯誤。咱們共同進步吧!
1.grid實現」9宮格「-小企鵝是基於下圖的九宮格來展開的。

image.png

<!DOCTYPE html>
<html lang="en">
  <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <title>CSS變量加grid畫一隻企鵝</title>
 <style>
   .penguin {
     width: 300px;
     height: 300px;
     display: grid;
     grid-template-columns: 100px 100px 100px;//3列
     grid-template-rows: 100px 100px 100px; //3行
   }
   .item:nth-child(even) {
     background-color: darkkhaki;
   }
   .item:nth-child(odd) {
     background-color: darksalmon;
   }
 </style>
  </head>
  <body>
 <main class="penguin">
   <div class="item">1</div>
   <div class="item">2</div>
   <div class="item">3</div>
   <div class="item">4</div>
   <div class="item">5</div>
   <div class="item">6</div>
   <div class="item">7</div>
   <div class="item">8</div>
   <div class="item">9</div>
 </main>
  </body>
</html>

2.畫一隻小企鵝css

<!DOCTYPE html>
<html lang="en">
  <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <title>CSS變量加grid畫一隻企鵝</title>
 <style>
   .penguin {
     width: 300px;
     height: 300px;
     background-color: aquamarine;
     display: grid;
     grid-template-columns: 65px 170px 65px;
     grid-template-rows: 100px 150px 50px;
     /* 聲明變量的時候,變量名前面要加兩根連詞線(--);
     var()函數用於讀取變量;
     函數還能夠使用第二個參數,表示變量的默認值。 
     eg:background: var(--penguin-skin, gray);*/
     /* 動態設置企業的膚色 */
     --penguin-skin: gray;
     --penguin-belly: white;
     --penguin-foot: orange;
     --penguin-cheek: pink;
   }

   /* 基數 */
   /* .item:nth-child(even) {
     background-color: darkkhaki;
   }*/
   /* 偶數 */
   /* .item:nth-child(odd) {
     background-color: darksalmon;
   } */
   /* 頭 */
   .head {
     background: var(--penguin-skin, gray);
     width: 80%;
     height: 100%;
     border-radius: 80% 80% 50% 50%;
     align-self: end;
     transform: translateY(20%);
     z-index: 3;
     position: relative;
   }
   .face {
     /* display: grid;
     grid-template-columns: 33% 33% 33%;
     justify-items: center;
     align-items: end; */
     width: 80%;
     height: 90%;
     /* border-radius: 50%; */
     position: relative;
     position: absolute;
     bottom: 0;
     left: 0;
     right: 0;
     top: 0;
     margin: 10% auto;
   }
   .face:before {
     display: block;
     content: "";
     width: 80%;
     height: 100%;
     border-radius: 70% 60% 50% 40%;
     transform: rotate(-45deg);
     background: var(--penguin-belly, white);
     position: absolute;
     left: -5%;
     top: 5%;
   }
   .face:after {
     display: block;
     content: "";
     width: 80%;
     height: 100%;
     border-radius: 60% 70% 40% 50%;
     transform: rotate(45deg);
     background: var(--penguin-belly, white);
     position: absolute;
     right: -5%;
     top: 5%;
   }
   .face-on {
     display: grid;
     grid-template-columns: 33% 33% 33%;
     justify-items: center;
     align-items: end;
     width: 80%;
     height: 80%;
     margin: 0 auto;
     position: absolute;
     left: 0;
     right: 0;
     bottom: 0;
   }
   .eye {
     background: black;
     width: 20px;
     height: 20px;
     border-radius: 50%;
   }
   .left-eye {
     margin-left: 50%;
   }
   .right-eye {
     margin-right: 50%;
   }

   /* 
     CSS 選擇器
     :before    p:before    在每一個 <p> 元素的內容以前插入內容。    
     :after    p:after        在每一個 <p> 元素的內容以後插入內容。 
     */
   .eye:after {
     display: block;
     content: " ";
     background-color: var(--penguin-belly, white);
     width: 8px;
     height: 8px;
     border-radius: 50%;
   }

   /* 臉頰 */
   .cheek {
     width: 60%;
     height: 30%;
     border-radius: 50%;
     background-color: var(--penguin-cheek, pink);
     align-self: start;
   }
   .mouth {
     width: 100%;
     height: 100%;
     display: block;
   }
   .mouth:before {
     display: block;
     content: " ";
     width: 60%;
     height: 20%;
     border-radius: 50%;
     background: var(--penguin-foot, rgb(139, 137, 137));
     margin: 0 auto;
   }
   .mouth:after {
     display: block;
     content: " ";
     width: 50%;
     height: 20%;
     border-radius: 50%;
     background: var(--penguin-foot, gray);
     margin: 0 auto;
   }
   /* 手 */
   .hand {
     width: 50%;
     height: 50%;
     background: var(--penguin-skin, gray);
     z-index: 0;
   }

   /* 
   設置多個不一樣的CSS變換(transform)屬性時,在屬性中間用空格隔開便可, 旋轉 縮放 扭曲 等同時執行多個效果!
   順序:是從後向前執行的!!!
   順序不一樣變換效果也會不一樣!!!
   eg: transform: rotate(45deg) translateX(10px);
   eg: transform: translateX(10px) rotate(45deg) ;
   位置會有很大的區別!
    */

   /* 左手 */
   .left-hand {
     border-radius: 30% 30% 120% 30%;
     transform: rotate(45deg) translate(90%, -26%);
     justify-self: end;
   }

   /* 右手 */
   .right-hand {
     border-radius: 30% 30% 30% 120%;
     transform: rotate(-45deg) translate(-90%, -26%);
   }

   /*肚子*/
   .center {
     margin: 0 auto;
   }

   .belly-border {
     width: 95%;
     height: 100%;
     background: var(--penguin-skin, gray);
     border-radius: 120%;
     z-index: 1;
   }

   .belly-in {
     width: 85%;
     height: 95%;
     background: var(--penguin-belly, white);
     border-radius: 120% 120% 100% 100%;
   }

   /* 腳 */
   .penguin-feet {
     position: relative;
   }

   .feet {
     display: inline-block;
     width: 30%;
     height: 50%;
     background: var(--penguin-foot, orange);
     border-radius: 50%;
     position: absolute;
     z-index: 0;
     top: -40%;
   }

   .left-feet {
     transform: rotate(160deg);
     left: 18%;
   }

   .right-feet {
     transform: rotate(-160deg);
     right: 18%;
   }
 </style>
  </head>

  <body>
 <img src="./penguin.png" style="width: 300px; height: 300px;" />
 <main class="penguin">
   <section class="item"></section>
   <section class="item head center">
     <div class="face"></div>
     <div class="face-on">
       <div class="eye left-eye"></div>
       <div></div>
       <div class="eye right-eye"></div>
       <div class="cheek left-cheek"></div>
       <div class="mouth"></div>
       <div class="cheek right-cheek"></div>
     </div>
   </section>
   <section class="item"></section>
   <section class="item hand left-hand"></section>
   <section class="item center belly-border">
     <div class="belly-in center"></div>
   </section>
   <section class="item hand right-hand"></section>
   <section class="item"></section>
   <section class="item penguin-feet">
     <section class="feet left-feet"></section>
     <section class="feet right-feet"></section>
   </section>
   <section class="item"></section>
 </main>
  </body>
</html>

3.預覽
image.pnghtml

推薦文章
1.grid佈局
http://www.ruanyifeng.com/blo...
2.css變量
http://www.ruanyifeng.com/blo...
3.css選擇器
https://www.w3school.com.cn/c...
4.flex佈局
http://www.ruanyifeng.com/blo...函數

相關文章
相關標籤/搜索