上一章 下一章

第七章:表单和交互

HTML标签:

  • <form>

    表单容器,用于收集用户输入

  • <input>

    输入框,type属性决定类型

  • <button>

    按钮元素

CSS属性:

  • border-radius

    设置表单元素圆角

  • box-shadow

    添加阴影效果

  • :focus

    输入框焦点状态样式

综合示例:

HTML:

<form class="contact-form">
    <div class="form-group">
        <label for="name">姓名:</label>
        <input type="text" id="name" required>
    </div>
    <div class="form-group">
        <label for="email">邮箱:</label>
        <input type="email" id="email" required>
    </div>
    <button type="submit">提交</button>
</form>

CSS:

.contact-form {
    max-width: 400px;
    margin: 0 auto;
    padding: 20px;
}

.form-group {
    margin-bottom: 15px;
}

input {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
}

input:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37,99,235,0.2);
}

button {
    width: 100%;
    padding: 10px;
    background: #2563eb;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

HTML编辑器

CSS编辑器

预览结果