CSS(Cascading Style Sheets)是一种样式表语言,用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档的外观和格式。CSS控制网页的布局、颜色、字体和其他视觉效果。
style
属性定义样式。<head>
部分使用<style>
标签定义样式。<link>
标签引入到HTML文档中。以下是一个简单的CSS模板代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Template</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<main>
<section>
<h2>About Us</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</section>
<section>
<h2>Our Services</h2>
<ul>
<li>Service 1</li>
<li>Service 2</li>
<li>Service 3</li>
</ul>
</section>
</main>
<footer>
<p>© 2023 My Website. All rights reserved.</p>
</footer>
</body>
</html>
对应的CSS文件styles.css
:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
text-align: center;
padding: 1em 0;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #f4f4f4;
text-align: center;
}
nav ul li {
display: inline;
margin-right: 10px;
}
nav ul li a {
display: inline-block;
padding: 10px 20px;
text-decoration: none;
color: #333;
}
nav ul li a:hover {
background-color: #333;
color: #fff;
}
main {
padding: 20px;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 1em 0;
position: fixed;
bottom: 0;
width: 100%;
}
通过以上示例代码和参考链接,你可以更好地理解和应用CSS来设计和美化网页。
领取专属 10元无门槛券
手把手带您无忧上云