8- 【JavaWeb】用HTML和CSS来创建一个简洁的登录界面

我们将使用HTML和CSS来创建一个简洁、现代的登录界面。

1. HTML结构

首先,我们需要一个基本的HTML结构来容纳登录表单。保存为 index.html 文件:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="login-box">
            <h2>登录</h2>
            <form>
                <div class="textbox">
                    <input type="text" placeholder="用户名" required>
                </div>
                <div class="textbox">
                    <input type="password" placeholder="密码" required>
                </div>
                <input type="submit" class="btn" value="登录">
            </form>
        </div>
    </div>
</body>
</html>

2. CSS样式

接下来,我们将编写CSS样式来美化登录界面。保存为 styles.css 文件:

css 复制代码
/* Reset some default styles */
/* Reset some default styles */
body, html {
    margin: 0;
    padding: 0;
    font-family: Arial, sans-serif;
    height: 100%;
}

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background: url('https://cdn.pixabay.com/photo/2024/08/02/09/01/barracuda-8939250_1280.jpg') no-repeat center center fixed;
    background-size: cover;
}

.login-box {
    width: 360px;
    padding: 40px;
    position: relative;
    background: rgba(255, 255, 255, 0.9); /* Slightly transparent background for better readability */
    border-radius: 10px;
    box-shadow: 0 15px 25px rgba(0, 0, 0, 0.2);
}

.login-box h2 {
    margin: 0 0 20px;
    color: #333;
    text-align: center;
}

.textbox {
    margin-bottom: 20px;
}

.textbox input {
    width: 100%;
    padding: 10px;
    background: #f9f9f9;
    border: 1px solid #ddd;
    border-radius: 5px;
    font-size: 16px;
}

.btn {
    width: 100%;
    background: #4facfe;
    border: none;
    padding: 15px;
    cursor: pointer;
    border-radius: 5px;
    color: #fff;
    font-size: 18px;
    transition: background 0.3s ease;
}

.btn:hover {
    background: #00f2fe;
}
  • 背景图片 : 使用 background 属性设置背景图片,no-repeat 确保图片不会重复,center center 将图片居中显示,fixed 确保背景图片在滚动时固定不动。
  • 背景大小 : background-size: cover; 确保背景图片覆盖整个容器,同时保持图片的纵横比。
  • 登录框背景 : 使用 rgba(255, 255, 255, 0.9) 为登录框设置一个略微透明的白色背景,以便在有背景图片的情况下保持文本的可读性。
  • login-box: 设置登录框的宽度、内边距、背景颜色、圆角和阴影,提升视觉效果。
  • textbox: 提供输入框的样式,包括内边距、背景色、边框和圆角。
  • btn: 设置按钮的样式,包括背景颜色、边框、内边距和过渡效果。

4. 运行和测试

  1. index.htmlstyles.css 文件保存在同一目录下。
  2. 打开 index.html 文件,应该能看到一个美观的登录界面。
    好的,我们可以将背景图片应用到登录界面中。你只需更新CSS样式以包含该背景图片。下面是更新后的CSS代码示例:
相关推荐
用户66006766853920 小时前
用HTML5 构建一个敲击乐钢琴
html
Qinana20 小时前
📚 论如何用代码谈一场不露脸的恋爱
前端·前端框架·html
www_stdio1 天前
HTML5 敲击乐:模块化开发打造交互式钢琴应用的实践指南
html
林恒smileZAZ1 天前
CSS3 超实用属性:pointer-events (可穿透图层的鼠标事件)
前端·计算机外设·css3
luckyPian1 天前
前端+AI:CSS3(二)
前端·css·css3
muyouking111 天前
Tailwind CSS 小白快速入门速查手册
前端·css·css3
昔人'1 天前
`list-style-type: decimal-leading-zero;`在有序列表`<ol></ol>` 中将零添加到一位数前面
前端·javascript·html
fruge2 天前
前端三驾马车(HTML/CSS/JS)核心概念深度解析
前端·css·html
znhy@1232 天前
CSS易忘属性
前端·css
软件技术NINI2 天前
html css js网页制作成品——HTML+CSS仙台有树电视剧网页设计(5页)附源码
javascript·css·html