如何使用 CSS 建立響應式結賬表單?


在網頁上,結賬功能通常出現在電子商務網站上。這是一個網頁,顯示您購買產品的全部詳細資訊。包括產品詳情、需支付的總金額、需輸入的卡資訊、賬單地址等。還包含一個按鈕來繼續購買流程。讓我們看看如何建立一個響應式結賬表單。

設定彈性佈局

要排列表單欄位,可以使用display屬性並將其設定為flex -

.Fields {
   display: flex;
   flex-wrap: wrap;
   padding: 20px;
   justify-content: space-around;
}

放置表單容器

這裡,購物車詳情可見。這顯示了使用者正在購買多少產品以及計算出的總金額 -

<div class="formContainer">
   <h4>
      Cart <span class="price" style="color:black"><b>2</b></span>
   </h4>
   <p>
      <a style="text-decoration: none;" href="#">Product 1</a>
      <span class="price">$10</span>
   </p>
   <p>
      <a style="text-decoration: none;" href="#">Product 2</a>
      <span class="price">$10</span>
   </p>
   <p>
      Total <span class="price" style="color:black"><b>$20</b></span>
   </p>
</div>

我們這樣設定容器的樣式 -

.formContainer {
   margin: 10px;
   background-color: #efffc9;
   padding: 5px 20px 15px 20px;
   border: 1px solid rgb(191, 246, 250);
   border-radius: 3px;
}

賬單地址

在結賬頁面,使用者需要新增賬單地址。包括使用者姓名、電子郵件和地址 -

<div>
   <h3>Billing Address</h3>
   <label for="fname">Full Name</label>
   <input type="text" id="fname" name="firstname" />
   <label for="email"> Email</label>
   <input type="text" id="email" name="email" />
   <label for="adr"> Address</label>
   <input type="text" id="adr" name="address" />
</div>

支付詳情

這裡,我們建立了信用卡支付的欄位。包括持卡人姓名、信用卡號碼、有效期和CVV -

<div>
   <h3>Payment</h3>
   <label for="cname">Name on Card</label>
   <input type="text" id="cname" name="cardname" />
   <label for="ccnum">Credit card number</label>
   <input type="text" id="ccnum" name="cardnumber" />
   <div class="Fields">
      <div>
         <label for="expyear">Exp Year</label>
         <input type="text" id="expyear" name="expyear" />
      </div>
      <div>
         <label for="cvv">CVV</label>
         <input type="text" id="cvv" name="cvv" />
      </div>
   </div>
</div>

結賬按鈕

最後放置一個按鈕來繼續購買流程。使用<button>元素 -

<input  type="submit" value="Continue to checkout" class="checkout" />

按鈕的樣式如下。使用cursor屬性將游標設定為指標,使其看起來像一個可點選的連結 -

.checkout {
   background-color: #4caf50;
   color: white;
   padding: 12px;
   margin: 10px 0;
   border: none;
   width: 100%;
   border-radius: 3px;
   cursor: pointer;
   font-size: 17px;
}

設定響應式

當網頁瀏覽器設定為小於 800px 時,響應式功能生效。flex direction 設定為 column reverse,即彈性專案垂直顯示為列,但順序相反 -

@media (max-width: 800px) {
   .Fields {
      flex-direction: column-reverse;
   }
}

示例

以下是使用 CSS 建立響應式結賬表單的程式碼 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: Arial;
         font-size: 17px;
         padding: 8px;
      }
      * {
         box-sizing: border-box;
      }
      .Fields {
         display: flex;
         flex-wrap: wrap;
         padding: 20px;
         justify-content: space-around;
      }
      .Fields div {
         margin-right: 10px;
      }
      label {
         margin: 15px;
      }
      .formContainer {
         margin: 10px;
         background-color: #efffc9;
         padding: 5px 20px 15px 20px;
         border: 1px solid rgb(191, 246, 250);
         border-radius: 3px;
      }
      input[type="text"] {
         display: inline-block;
         width: 100%;
         margin-bottom: 20px;
         padding: 12px;
         border: 1px solid #ccc;
         border-radius: 3px;
      }
      label {
         margin-left: 20px;
         display: block;
      }
      .icon-formContainer {
         margin-bottom: 20px;
         padding: 7px 0;
         font-size: 24px;
      }
      .checkout {
         background-color: #4caf50;
         color: white;
         padding: 12px;
         margin: 10px 0;
         border: none;
         width: 100%;
         border-radius: 3px;
         cursor: pointer;
         font-size: 17px;
      }
      .checkout:hover {
         background-color: #45a049;
      }
      a {
         color: black;
      }
      span.price {
         float: right;
         color: grey;
      }
      @media (max-width: 800px) {
         .Fields {
            flex-direction: column-reverse;
         }
      }
   </style>
</head>
<body>
      <h1 style="text-align: center;">Responsive Checkout Form</h1>
      <div class="Fields">
         <div>
            <div class="formContainer">
               <form>
                  <div class="Fields">
                     <div>
                        <h3>Billing Address</h3>
                        <label for="fname">Full Name</label>
                        <input type="text" id="fname" name="firstname" />
                        <label for="email"> Email</label>
                        <input type="text" id="email" name="email" />
                        <label for="adr"> Address</label>
                        <input type="text" id="adr" name="address" />
                     </div>
                     <div>
                        <h3>Payment</h3>
                        <label for="cname">Name on Card</label>
                        <input type="text" id="cname" name="cardname" />
                        <label for="ccnum">Credit card number</label>
                        <input type="text" id="ccnum" name="cardnumber" />
                        <div class="Fields">
                           <div>
                              <label for="expyear">Exp Year</label>
                              <input type="text" id="expyear" name="expyear" />
                           </div>
                           <div>
                              <label for="cvv">CVV</label>
                              <input type="text" id="cvv" name="cvv" />
                           </div>
                        </div>
                     </div>
                  </div>
                  <input
                     type="submit"
                     value="Continue to checkout"
                     class="checkout"
                  />
               </form>
            </div>
         </div>
         <div>
            <div class="formContainer">
               <h4>
                  Cart <span class="price" style="color:black"><b>2</b></span>
               </h4>
               <p>
                  <a style="text-decoration: none;" href="#">Product 1</a>
                  <span class="price">$10</span>
               </p>
               <p>
                  <a style="text-decoration: none;" href="#">Product 2</a>
                  <span class="price">$10</span>
               </p>
               <p>
                  Total <span class="price" style="color:black"><b>$20</b></span>
               </p>
            </div>
         </div>
      </div>
   </body>
</html>

更新於: 2023年12月8日

782 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告