如何使用 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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP