.container { width: 800px; margin: 0 auto; } .input { margin-bottom: 10px; } .result { display: flex; justify-content: space-around; } .success, .failure { width: 200px; height: 200px; display: flex; justify-content: center; align-items: center; font-size: 24px; font-weight: bold; } .success { background-color: lightgreen; } .failure { background-color: orange; }
时间: 2024-02-14 11:17:50 浏览: 23
这是一段CSS代码,用于设置网页中的样式。其中包括一个容器、一个输入框、一个结果展示区域,以及两个状态展示框(成功和失败)。容器的宽度为800像素,居中显示;输入框与下方元素的间距为10像素;结果展示区域中的元素水平分布,左右间距相等;成功状态展示框的背景颜色为浅绿色,失败状态展示框的背景颜色为橙色。
相关问题
.button { transition: background-color 0.3s ease-in-out; } .button:hover { background-color: #ff9900; } .container { width: 800px; margin: 0 auto; } .input { margin-bottom: 10px; } .result { display: flex; justify-content: space-around; } .success, .failure { width: 200px; height: 200px; display: flex; justify-content: center; align-items: center; font-size: 24px; font-weight: bold; } .success { background-color: lightgreen; } .failure { background-color: orange; }
这是CSS样式表中的一些样式定义,用于设置HTML元素的外观和布局等。
在上面的代码中,我们定义了一些类名,用于设置不同的样式:
1. .button:设置按钮的过渡效果,当鼠标悬浮在按钮上时,背景颜色会以0.3秒的时间从当前颜色渐变到#ff9900。
2. .button:hover:设置按钮在鼠标悬浮时的背景颜色为#ff9900。
3. .container:设置容器的宽度为800像素,并将其水平居中。
4. .input:设置表单输入框的下边距为10像素。
5. .result:将结果容器内的内容水平居中对齐。
6. .success和.failure:设置成功和失败的样式,宽高均为200像素,内容采用flex布局,居中对齐,字体大小为24像素,字体加粗。
7. .success:设置成功容器的背景颜色为lightgreen。
8. .failure:设置失败容器的背景颜色为orange。
通过定义这些样式,我们可以让HTML元素拥有更加美观、合理的外观和布局,从而提高用户体验。
加一个需求1. 实现模糊匹配:在输入框中输入字符后,将当前输入字符串与已添加的列表项进行模糊匹配,将匹配到的结果显示在输入框下方。如匹配不到任何列表项,列表显示空 2. 列表中匹配到的输入框的字符串需要染色为红色
以下是修改后的示例代码,实现了模糊匹配和染色功能:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TODO List</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>TODO List</h1>
<input type="text" id="new-task" placeholder="Add new task">
<div id="search-result"></div>
<ul id="task-list">
<!-- tasks will be added here -->
</ul>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-top: 0;
}
input[type="text"] {
display: block;
box-sizing: border-box;
width: 100%;
padding: 10px;
border: none;
border-bottom: 2px solid #ccc;
font-size: 16px;
margin-bottom: 20px;
}
#search-result {
margin-bottom: 20px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: flex;
align-items: center;
padding: 10px;
border-bottom: 1px solid #ccc;
}
li:hover {
background-color: #f4f4f4;
}
li span {
flex-grow: 1;
color: #333;
}
li span.match {
color: #f44336;
}
li button {
background-color: #f44336;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
margin-left: 10px;
}
```
JavaScript代码:
```
// 获取页面元素
var newTaskInput = document.getElementById("new-task");
var taskList = document.getElementById("task-list");
var searchResult = document.getElementById("search-result");
// 添加任务
function addTask() {
// 获取输入框内容
var taskName = newTaskInput.value;
// 创建任务元素
var taskItem = document.createElement("li");
taskItem.innerHTML = '<span>' + taskName + '</span><button>X</button>';
// 给删除按钮添加事件处理函数
taskItem.querySelector("button").addEventListener("click", function() {
taskItem.remove();
});
// 添加任务到任务列表
taskList.appendChild(taskItem);
// 清空输入框
newTaskInput.value = "";
}
// 模糊匹配
function searchTask() {
// 获取输入框内容
var keyword = newTaskInput.value.trim();
// 清空搜索结果
searchResult.innerHTML = "";
// 遍历任务列表
for (var i = 0; i < taskList.children.length; i++) {
var taskItem = taskList.children[i];
var taskName = taskItem.querySelector("span").textContent.trim();
// 如果任务名称包含关键字
if (taskName.indexOf(keyword) >= 0) {
// 创建匹配结果元素
var resultItem = document.createElement("div");
resultItem.textContent = taskName;
resultItem.classList.add("match");
// 添加匹配结果到搜索结果
searchResult.appendChild(resultItem);
}
}
}
// 给输入框添加事件处理函数
newTaskInput.addEventListener("input", searchTask);
// 给输入框添加回车键事件处理函数
newTaskInput.addEventListener("keypress", function(event) {
if (event.keyCode === 13) {
addTask();
}
});
```
这个示例代码实现了添加功能、删除功能、模糊匹配功能和染色功能。您可以按照需求进行修改和优化。