如何使用 CSS 將表單新增到全寬影像中?
我們可以在網頁上輕鬆新增表單。同樣,也可以使用 CSS 將表單新增到影像中。在本教程中,我們將使用 CSS 將表單新增到全寬影像中。讓我們看看如何操作。
設定網頁樣式
首先,設定網頁的高度、邊距和填充。我們還使用 `font-family` 屬性設定了字體系列。
body, html {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
設定所有元素的樣式
我們使用了 `box-sizing` 屬性將填充和邊框包含在元素的總寬度和高度中。`border-box` 使 `width` 和 `height` 屬性包含內容、填充和邊框。
* {
box-sizing: border-box;
}
設定影像和表單的容器
我們有一個 `div` 和一個包含在其中的表單。表單包括電子郵件、密碼和地址的輸入元素。每個表單欄位的標籤和佔位符也已設定。我們還使用 `
<div class="backgroundImage">
<form class="formContainer">
<h1>Register Here</h1>
<label for="eMail">Email</label>
<input type="text" placeholder="Enter your Email ID" name="eMail" required>
<label for="pass">Password</label>
<input type="password" placeholder="Enter your Password" name="pass" required>
<label for="Address">Address</label>
<input type="text" placeholder="Enter your Address" name="Address" required>
<button class="btn-login">Login</button>
</form>
</div>
設定全寬背景影像
類名為 `backgroundImage` 的 `div` 設定了一個全寬背景影像。使用 `height` 屬性將高度設定為 100%。使用 `background-image` 屬性設定背景影像。`background-size` 用於設定背景影像的大小。`cover` 值表示將背景影像調整大小以覆蓋整個容器。
.backgroundImage {
height: 100%;
background-image: url("https://images.pexels.com/photos/1424246/pexels-photo-1424246.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1000");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
將表單設定為影像
表單設定為具有 `absolute` 位置以適應影像。`right` 屬性也已設定,以使表單保持在左側。它影響定位元素的水平位置。表單寬度使用 `max-width` 屬性設定。它設定表單的最大寬度。
.formContainer {
position: absolute;
right: 40%;
max-width: 400px;
margin: 20px;
padding: 16px;
background-color: white;
}
示例
以下是使用 CSS 將表單新增到全寬影像中的程式碼:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body, html {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
* {
box-sizing: border-box;
}
.backgroundImage {
height: 100%;
background-image: url("https://images.pexels.com/photos/1424246/pexels-photo-1424246.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1000");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
.formContainer {
position: absolute;
right: 40%;
max-width: 400px;
margin: 20px;
padding: 16px;
background-color: white;
}
label{
font-size: 20px;
font-weight: bolder;
}
input[type=text], input[type=password] {
width: 100%;
font-size: 18px;
padding: 15px;
margin: 5px 0 22px 0;
border: none;
background: #e3ff95;
}
input[type=text]:focus, input[type=password]:focus {
background-color: #ddd;
outline: none;
}
.btn-login {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
border: none;
cursor: pointer;
width: 100%;
font-size: 20px;
}
</style>
</head>
<body>
<div class="backgroundImage">
<form class="formContainer">
<h1>Register Here</h1>
<label for="eMail">Email</label>
<input type="text" placeholder="Enter your Email ID" name="eMail" required>
<label for="pass">Password</label>
<input type="password" placeholder="Enter your Password" name="pass" required>
<label for="Address">Address</label>
<input type="text" placeholder="Enter your Address" name="Address" required>
<button class="btn-login">Login</button>
</form>
</div>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP