如何使用 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