css表格表单制作
CSS表格制作
使用CSS可以美化HTML表格,使其更符合设计需求。以下是一个基础表格样式示例:
<table class="styled-table">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>北京</td>
</tr>
<tr>
<td>李四</td>
<td>30</td>
<td>上海</td>
</tr>
</tbody>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
.styled-table tbody tr:hover {
background-color: #f1f1f1;
}
CSS表单制作
表单样式设计可以提升用户体验,以下是一个简单的表单样式示例:
<form class="styled-form">
<div class="form-group">
<label for="name">姓名</label>
<input type="text" id="name" placeholder="请输入姓名">
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" placeholder="请输入邮箱">
</div>
<div class="form-group">
<label for="message">留言</label>
<textarea id="message" placeholder="请输入留言"></textarea>
</div>
<button type="submit">提交</button>
</form>
.styled-form {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 20px;
}
.styled-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.styled-form input,
.styled-form textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.styled-form textarea {
height: 100px;
resize: vertical;
}
.styled-form button {
background-color: #009879;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.styled-form button:hover {
background-color: #007f63;
}
响应式表格设计
针对移动设备优化表格显示:
@media screen and (max-width: 600px) {
.styled-table {
border: 0;
}
.styled-table thead {
display: none;
}
.styled-table tr {
margin-bottom: 10px;
display: block;
border-bottom: 2px solid #ddd;
}
.styled-table td {
display: block;
text-align: right;
border-bottom: 1px dotted #ccc;
}
.styled-table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
表单验证样式
为表单添加验证状态提示:
.styled-form input:invalid {
border-color: #ff3860;
}
.styled-form input:valid {
border-color: #23d160;
}
.styled-form .error-message {
color: #ff3860;
font-size: 0.8em;
margin-top: 5px;
}






