CSS3学习教程,从入门到精通,CSS3 常用文本属性语法知识点及案例代码(6)

CSS3 常用文本属性语法知识点及案例代码

一、 文本颜色 (color)

  • 语法: color: ;
  • 取值:
    • 颜色名称: 例如 red, blue, green
    • 十六进制颜色值: 例如 #ff0000, #00ff00, #0000ff
    • RGB 颜色值: 例如 rgb(255, 0, 0), rgb(0, 255, 0), rgb(0, 0, 255)
    • RGBA 颜色值: 例如 rgba(255, 0, 0, 0.5), rgba(0, 255, 0, 0.5), rgba(0, 0, 255, 0.5) 等,其中最后一个参数代表透明度,范围 0-1
    • HSL 颜色值: 例如 hsl(0, 100%, 50%), hsl(120, 100%, 50%), hsl(240, 100%, 50%)
    • HSLA 颜色值: 例如 hsla(0, 100%, 50%, 0.5), hsla(120, 100%, 50%, 0.5), hsla(240, 100%, 50%, 0.5) 等,其中最后一个参数代表透明度,范围 0-1
  • 案例代码:
p {
  color: red; /* 使用颜色名称 */
}

h1 {
  color: #ff0000; /* 使用十六进制颜色值 */
}

h2 {
  color: rgb(255, 0, 0); /* 使用 RGB 颜色值 */
}

h3 {
  color: rgba(255, 0, 0, 0.5); /* 使用 RGBA 颜色值,50% 透明度 */
}

h4 {
  color: hsl(0, 100%, 50%); /* 使用 HSL 颜色值 */
}

h5 {
  color: hsla(0, 100%, 50%, 0.5); /* 使用 HSLA 颜色值,50% 透明度 */
}

二、 文本对齐 (text-align)

  • 语法: text-align: left | right | center | justify;
  • 取值:
    • left: 左对齐 (默认值)
    • right: 右对齐
    • center: 居中对齐
    • justify: 两端对齐
  • 案例代码:
p {
  text-align: left; /* 左对齐 */
}

h1 {
  text-align: right; /* 右对齐 */
}

h2 {
  text-align: center; /* 居中对齐 */
}

h3 {
  text-align: justify; /* 两端对齐 */
}

三、 文本装饰 (text-decoration)

  • 语法: text-decoration: none | underline | overline | line-through | blink;
  • 取值:
    • none: 无装饰 (默认值)
    • underline: 下划线
    • overline: 上划线
    • line-through: 删除线
    • blink: 闪烁 (已废弃,不建议使用)
  • 案例代码:
a {
  text-decoration: none; /* 去除链接默认下划线 */
}

h1 {
  text-decoration: underline; /* 下划线 */
}

h2 {
  text-decoration: overline; /* 上划线 */
}

h3 {
  text-decoration: line-through; /* 删除线 */
}

四、 文本缩进 (text-indent)

  • 语法: text-indent: | ;
  • 取值:
    • : 使用固定的长度值,例如 2em, 20px
    • : 使用相对于父元素宽度的百分比,例如 10%
  • 案例代码:
p {
  text-indent: 2em; /* 首行缩进 2em */
}

h1 {
  text-indent: 10%; /* 首行缩进父元素宽度的 10% */
}

五、 文本大小写 (text-transform)

  • 语法: text-transform: none | capitalize | uppercase | lowercase;
  • 取值:
    • none: 无转换 (默认值)
    • capitalize: 每个单词的首字母大写
    • uppercase: 所有字母大写
    • lowercase: 所有字母小写
  • 案例代码:
p {
  text-transform: none; /* 无转换 */
}

h1 {
  text-transform: capitalize; /* 每个单词的首字母大写 */
}

h2 {
  text-transform: uppercase; /* 所有字母大写 */
}

h3 {
  text-transform: lowercase; /* 所有字母小写 */
}

六、 文本间距 (letter-spacing, word-spacing)

  • letter-spacing: 设置字符间距
    • 语法: letter-spacing: | normal;
    • 取值:
      • : 使用固定的长度值,例如 2px, 0.5em
      • normal: 正常间距 (默认值)
  • word-spacing: 设置单词间距
    • 语法: word-spacing: | normal;
    • 取值:
      • : 使用固定的长度值,例如 5px, 1em
      • normal: 正常间距 (默认值)
  • 案例代码:
p {
  letter-spacing: 2px; /* 字符间距 2px */
  word-spacing: 5px; /* 单词间距 5px */
}

h1 {
  letter-spacing: normal; /* 正常字符间距 */
  word-spacing: normal; /* 正常单词间距 */
}

七、 文本阴影 (text-shadow)

  • 语法: text-shadow: ;
  • 取值:
    • : 水平阴影的位置,允许负值
    • : 垂直阴影的位置,允许负值
    • : 模糊距离 (可选)
    • : 阴影颜色 (可选)
  • 案例代码:
h1 {
  text-shadow: 2px 2px 5px red; /* 水平阴影 2px,垂直阴影 2px,模糊距离 5px,阴影颜色红色 */
}

h2 {
  text-shadow: -2px -2px 5px blue; /* 水平阴影 -2px,垂直阴影 -2px,模糊距离 5px,阴影颜色蓝色 */
}

八、 文本溢出 (text-overflow)

  • 语法: text-overflow: clip | ellipsis | ;
  • 取值:
    • clip: 修剪文本 (默认值)
    • ellipsis: 显示省略号来代表被修剪的文本
    • : 使用给定的字符串来代表被修剪的文本 (例如 "...")
  • 案例代码:
p {
  width: 200px;
  white-space: nowrap; /* 强制文本在一行显示 */
  overflow: hidden; /* 隐藏溢出部分 */
  text-overflow: ellipsis; /* 显示省略号 */
}

九、 文本换行 (white-space)

  • 语法: white-space: normal | nowrap | pre | pre-wrap | pre-line;
  • 取值:
    • normal: 默认值,文本自动换行
    • nowrap: 强制文本在一行显示,不换行
    • pre: 保留空白符和换行符,类似于
       标签
    • pre-wrap: 保留空白符和换行符,但允许文本自动换行
    • pre-line: 合并空白符,但保留换行符,允许文本自动换行
  • 案例代码:
p {
  white-space: nowrap; /* 强制文本在一行显示 */
}

pre {
  white-space: pre; /* 保留空白符和换行符 */
}

十、 文本方向 (direction)

  • 语法: direction: ltr | rtl;
  • 取值:
    • ltr: 从左到右 (默认值)
    • rtl: 从右到左
  • 案例代码:
p {
  direction: ltr; /* 从左到右 */
}

h1 {
  direction: rtl; /* 从右到左 */
}

十一、 其他文本属性

  • font-family: 设置字体
  • font-size: 设置字体大小
  • font-weight: 设置字体粗细
  • font-style: 设置字体样式 (例如斜体)
  • line-height: 设置行高
  • text-align-last: 设置最后一行文本的对齐方式
  • unicode-bidi: 设置文本方向 (例如混合从左到右和从右到左的文本)

总结

以上是 CSS3 中常用的文本属性语法知识点及案例代码。掌握这些属性可以帮助你更好地控制网页中文本的显示效果。

二、案例代码

案例 1:基本文本样式

DOCTYPE html>
<html>
<head>
    <title>CSS3 文本属性案例title>
    <style>
        body {
            font-family: Arial, sans-serif; /* 设置整体字体 */
            line-height: 1.6; /* 设置整体行高 */
        }
        
        h1 {
            color: #2c3e50; /* 设置标题颜色 */
            text-align: center; /* 标题居中 */
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3); /* 添加阴影 */
        }
        
        p {
            font-size: 16px; /* 设置段落字体大小 */
            color: #34495e; /* 设置段落颜色 */
            margin: 20px 0; /* 设置段落上下外边距 */
            text-indent: 2em; /* 首行缩进 */
        }
        
        a {
            color: #3498db; /* 设置链接颜色 */
            text-decoration: none; /* 去掉下划线 */
        }
        
        a:hover {
            text-decoration: underline; /* 鼠标悬停时显示下划线 */
        }
        
        .important {
            font-weight: bold; /* 加粗重要文本 */
            color: #e74c3c; /* 改变重要文本颜色 */
        }
        
        .highlight {
            background-color: #f39c12; /* 设置高亮背景色 */
            padding: 5px; /* 添加内边距 */
            border-radius: 4px; /* 添加圆角 */
        }
        
        .container {
            max-width: 800px; /* 设置容器最大宽度 */
            margin: 0 auto; /* 水平居中 */
            padding: 20px; /* 添加内边距 */
        }
    style>
head>
<body>
    <div class="container">
        <h1>欢迎学习 CSS3 文本属性h1>
        <p>这是一个普通的段落文本,展示了基本的文本样式设置。p>
        <p class="important">这是一个重要的通知,使用了加粗和不同的颜色来突出显示。p>
        <p class="highlight">这是一个高亮显示的段落,使用了背景色和内边距来增强视觉效果。p>
        <p>访问 <a href="https://www.example.com">示例网站a> 获取更多信息。p>
    div>
body>
html>

案例 2:文本对齐与缩进

DOCTYPE html>
<html>
<head>
    <title>文本对齐与缩进案例title>
    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        
        .left-aligned {
            text-align: left; /* 左对齐 */
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .right-aligned {
            text-align: right; /* 右对齐 */
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .centered {
            text-align: center; /* 居中对齐 */
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .justified {
            text-align: justify; /* 两端对齐 */
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .indented {
            text-indent: 2em; /* 首行缩进 */
            border: 1px solid #ddd;
            padding: 15px;
        }
    style>
head>
<body>
    <div class="container">
        <h1>文本对齐与缩进示例h1>
        
        <div class="left-aligned">
            <h2>左对齐h2>
            <p>这是一个左对齐的段落文本,通常用于普通的正文内容。p>
        div>
        
        <div class="right-aligned">
            <h2>右对齐h2>
            <p>这是一个右对齐的段落文本,有时用于特殊的布局需求。p>
        div>
        
        <div class="centered">
            <h2>居中对齐h2>
            <p>这是一个居中对齐的段落文本,常用于标题或需要强调的内容。p>
        div>
        
        <div class="justified">
            <h2>两端对齐h2>
            <p>这是一个两端对齐的段落文本,使文本在视觉上更加整齐。p>
        div>
        
        <div class="indented">
            <h2>首行缩进h2>
            <p>这是一个首行缩进的段落文本,符合中文排版的习惯。p>
        div>
    div>
body>
html>

案例 3:文本装饰与阴影

DOCTYPE html>
<html>
<head>
    <title>文本装饰与阴影案例title>
    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        
        .no-decoration {
            text-decoration: none; /* 无装饰 */
            color: #3498db;
            display: inline-block;
            padding: 10px;
            border: 1px solid #3498db;
            margin-bottom: 20px;
        }
        
        .no-decoration:hover {
            text-decoration: underline; /* 鼠标悬停时显示下划线 */
            background-color: #f8f9fa;
        }
        
        .overline {
            text-decoration: overline; /* 上划线 */
            display: inline-block;
            margin: 10px 0;
        }
        
        .line-through {
            text-decoration: line-through; /* 删除线 */
            display: inline-block;
            margin: 10px 0;
        }
        
        .shadow {
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); /* 文本阴影 */
            font-size: 24px;
            font-weight: bold;
            margin: 20px 0;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 8px;
        }
        
        .multiple-lines {
            line-height: 1.8; /* 设置行高 */
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 8px;
        }
    style>
head>
<body>
    <div class="container">
        <h1>文本装饰与阴影示例h1>
        
        <a href="#" class="no-decoration">无装饰链接a>
        
        <p><span class="overline">上划线文本span>p>
        
        <p><span class="line-through">删除线文本span>p>
        
        <h2 class="shadow">带有阴影的标题h2>
        
        <div class="multiple-lines">
            <p>这是一个设置行高的多行文本,使文本在视觉上更加舒适。p>
            <p>第二段内容,展示了行高对阅读体验的影响。p>
        div>
    div>
body>
html>

案例 4:文本溢出处理

DOCTYPE html>
<html>
<head>
    <title>文本溢出处理案例title>
    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        
        .ellipsis {
            white-space: nowrap; /* 不换行 */
            overflow: hidden; /* 溢出部分隐藏 */
            text-overflow: ellipsis; /* 溢出部分显示省略号 */
            width: 300px; /* 设置固定宽度 */
            border: 1px solid #ddd;
            padding: 10px;
            margin-bottom: 20px;
        }
        
        .break-word {
            word-wrap: break-word; /* 长单词换行 */
            width: 300px; /* 设置固定宽度 */
            border: 1px solid #ddd;
            padding: 10px;
            margin-bottom: 20px;
        }
        
        .hyphenated {
            hyphens: auto; /* 自动插入连字符 */
            width: 300px; /* 设置固定宽度 */
            border: 1px solid #ddd;
            padding: 10px;
        }
    style>
head>
<body>
    <div class="container">
        <h1>文本溢出处理示例h1>
        
        <div class="ellipsis">
            <p>这是一个非常长的文本,用于演示当文本溢出容器时如何显示省略号。p>
        div>
        
        <div class="break-word">
            <p>这是一个包含非常长单词的文本,用于演示如何自动换行:supercalifragilisticexpialidociousp>
        div>
        
        <div class="hyphenated">
            <p>这是一个英文文本,用于演示如何自动插入连字符:Thequickbrownfoxjumpsoverthelazydogp>
        div>
    div>
body>
html>

以下是在开发中常用的CSS3文本属性的实际案例:

案例5:响应式导航栏

DOCTYPE html>
<html>
<head>
    <title>响应式导航栏title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
        }
        
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #333;
            padding: 1rem 2rem;
            color: white;
        }
        
        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            text-decoration: none;
        }
        
        .nav-links {
            display: flex;
            list-style: none;
        }
        
        .nav-links li {
            margin-left: 2rem;
        }
        
        .nav-links a {
            text-decoration: none;
            color: white;
            font-size: 1rem;
            transition: color 0.3s ease;
        }
        
        .nav-links a:hover {
            color: #4CAF50;
        }
        
        .search-box {
            display: flex;
            align-items: center;
        }
        
        .search-box input {
            padding: 0.5rem;
            border: none;
            border-radius: 4px 0 0 4px;
            outline: none;
        }
        
        .search-box button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 0 4px 4px 0;
            cursor: pointer;
        }
        
        @media (max-width: 768px) {
            .navbar {
                flex-direction: column;
                padding: 1rem;
            }
            
            .nav-links {
                margin-top: 1rem;
                flex-direction: column;
                width: 100%;
            }
            
            .nav-links li {
                margin: 0.5rem 0;
            }
            
            .search-box {
                margin-top: 1rem;
                width: 100%;
            }
            
            .search-box input {
                flex: 1;
                border-radius: 4px;
            }
            
            .search-box button {
                border-radius: 4px;
            }
        }
    style>
head>
<body>
    <nav class="navbar">
        <a href="#" class="logo">Logoa>
        <ul class="nav-links">
            <li><a href="#">首页a>li>
            <li><a href="#">产品a>li>
            <li><a href="#">服务a>li>
            <li><a href="#">关于a>li>
            <li><a href="#">联系a>li>
        ul>
        <div class="search-box">
            <input type="text" placeholder="搜索...">
            <button type="submit">搜索button>
        div>
    nav>
body>
html>

注释:

  • 使用了 flexbox 布局来创建响应式的导航栏。
  • 设置了导航链接的字体大小、颜色和悬停效果。
  • 使用了 transition 来实现平滑的颜色过渡效果。
  • 在小屏幕上,导航栏会调整为垂直布局,以适应不同的设备尺寸。

案例6:格式化文章内容

DOCTYPE html>
<html>
<head>
    <title>格式化文章内容title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f8f9fa;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }
        
        .article {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 2rem;
        }
        
        .article h1 {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            color: #2c3e50;
            text-align: center;
        }
        
        .article p {
            margin-bottom: 1.5rem;
            font-size: 1.1rem;
            line-height: 1.8;
            text-align: justify;
        }
        
        .article p.lead {
            font-size: 1.2rem;
            font-weight: 500;
            color: #2c3e50;
            margin-bottom: 2rem;
        }
        
        .article ul {
            margin-left: 1.5rem;
            margin-bottom: 1.5rem;
        }
        
        .article ul li {
            margin-bottom: 0.5rem;
            font-size: 1.1rem;
        }
        
        .article blockquote {
            background-color: #f8f9fa;
            border-left: 4px solid #4CAF50;
            padding: 1rem 1.5rem;
            margin-left: 0;
            margin-right: 0;
            font-style: italic;
            font-size: 1.1rem;
        }
        
        .article a {
            color: #4CAF50;
            text-decoration: none;
        }
        
        .article a:hover {
            text-decoration: underline;
        }
        
        .article img {
            max-width: 100%;
            height: auto;
            margin-bottom: 1.5rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .article .caption {
            display: block;
            text-align: center;
            margin-top: 0.5rem;
            font-size: 0.9rem;
            color: #666;
        }
    style>
head>
<body>
    <div class="container">
        <article class="article">
            <h1>文章标题h1>
            <p class="lead">这是文章的引导段落,用于概述文章的主要内容和吸引读者的注意力。p>
            <p>这是文章的第一段内容。通过合理的排版和样式设置,使文章更易于阅读和理解。使用了合适的字体大小、行高和段落间距,确保文本的可读性。p>
            <p>这是文章的第二段内容。可以包含一些具体的细节和信息,帮助读者深入了解主题。同时,通过合理的文本样式,使内容更加吸引人。p>
            <ul>
                <li>列表项 1li>
                <li>列表项 2li>
                <li>列表项 3li>
            ul>
            <blockquote>
                这是一个引用块,用于突出显示重要的引用或引言。
            blockquote>
            <p>访问 <a href="https://www.example.com">示例网站a> 获取更多信息。p>
            <img src="https://via.placeholder.com/800x400" alt="示例图片">
            <span class="caption">示例图片的说明文字span>
        article>
    div>
body>
html>

注释:

  • 设置了整体的字体、行高和颜色,确保文章内容的可读性。
  • 使用了不同的字体大小和颜色来区分标题、段落和引用内容。
  • 为列表项、链接和图片添加了适当的样式,使内容更加丰富和吸引人。
  • 使用了阴影和圆角效果来增强视觉效果,使内容区域更加突出。

案例7:按钮样式设计

DOCTYPE html>
<html>
<head>
    <title>按钮样式设计title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        
        .button-container {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            padding: 2rem;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .button {
            padding: 0.8rem 1.5rem;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }
        
        .button-primary {
            background-color: #4CAF50;
            color: white;
        }
        
        .button-primary:hover {
            background-color: #45a049;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .button-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-success {
            background-color: #28a745;
            color: white;
        }
        
        .button-success:hover {
            background-color: #218838;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-danger {
            background-color: #dc3545;
            color: white;
        }
        
        .button-danger:hover {
            background-color: #c82333;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-warning {
            background-color: #ffc107;
            color: #212529;
        }
        
        .button-warning:hover {
            background-color: #e0a800;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-info {
            background-color: #17a2b8;
            color: white;
        }
        
        .button-info:hover {
            background-color: #138496;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-link {
            background-color: transparent;
            color: #007bff;
            padding: 0;
            border-radius: 0;
        }
        
        .button-link:hover {
            background-color: transparent;
            color: #0056b3;
            text-decoration: underline;
        }
        
        .button-outline {
            background-color: transparent;
            border: 2px solid #4CAF50;
            color: #4CAF50;
        }
        
        .button-outline:hover {
            background-color: #4CAF50;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        .button-disabled {
            background-color: #cccccc;
            color: #666666;
            cursor: not-allowed;
            opacity: 0.65;
        }
        
        .button-large {
            padding: 1rem 2rem;
            font-size: 1.25rem;
            border-radius: 6px;
        }
        
        .button-small {
            padding: 0.5rem 1rem;
            font-size: 0.875rem;
            border-radius: 3px;
        }
        
        .button-block {
            display: block;
            width: 100%;
            padding: 0.8rem;
        }
    style>
head>
<body>
    <div class="button-container">
        <h1>按钮样式示例h1>
        <a href="#" class="button button-primary">主要按钮a>
        <a href="#" class="button button-secondary">次要按钮a>
        <a href="#" class="button button-success">成功按钮a>
        <a href="#" class="button button-danger">危险按钮a>
        <a href="#" class="button button-warning">警告按钮a>
        <a href="#" class="button button-info">信息按钮a>
        <a href="#" class="button button-link">链接按钮a>
        <a href="#" class="button button-outline">轮廓按钮a>
        <a href="#" class="button button-primary button-large">大号按钮a>
        <a href="#" class="button button-primary button-small">小号按钮a>
        <a href="#" class="button button-primary button-block">块级按钮a>
        <a href="#" class="button button-disabled">禁用按钮a>
    div>
body>
html>

注释:

  • 设计了多种不同样式和功能的按钮,包括主要按钮、次要按钮、成功按钮、危险按钮等。
  • 使用了不同的背景颜色、文字颜色和悬停效果来区分不同类型的按钮。
  • 通过添加 hover 状态的样式,增强了用户体验和交互效果。
  • 提供了不同尺寸和布局方式的按钮,以适应不同的设计需求。

案例8:表单样式设计

DOCTYPE html>
<html>
<head>
    <title>表单样式设计title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        
        .form-container {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }
        
        .form-title {
            text-align: center;
            margin-bottom: 1.5rem;
            color: #2c3e50;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: #333;
        }
        
        .form-control {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }
        
        .form-control:focus {
            border-color: #4CAF50;
            outline: none;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
        }
        
        .form-control.invalid {
            border-color: #dc3545;
        }
        
        .form-control.valid {
            border-color: #28a745;
        }
        
        .form-text {
            font-size: 0.875rem;
            color: #666;
            margin-top: 0.25rem;
        }
        
        .form-checkbox-group {
            display: flex;
            align-items: center;
        }
        
        .form-checkbox {
            margin-right: 0.5rem;
        }
        
        .form-radio-group {
            display: flex;
            align-items: center;
        }
        
        .form-radio {
            margin-right: 0.5rem;
        }
        
        .form-select {
            appearance: none;
            background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23333' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 0.75rem center;
            background-size: 16px 16px;
        }
        
        .form-file {
            display: block;
            width: 100%;
            padding: 0.8rem;
            border: 1px dashed #ddd;
            border-radius: 4px;
            background-color: #f8f9fa;
            cursor: pointer;
        }
        
        .form-file::file-selector-button {
            display: none;
        }
        
        .form-submit {
            width: 100%;
            padding: 0.8rem;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        .form-submit:hover {
            background-color: #45a049;
        }
        
        .form-link {
            display: block;
            text-align: center;
            margin-top: 1.5rem;
            color: #007bff;
            text-decoration: none;
        }
        
        .form-link:hover {
            text-decoration: underline;
        }
    style>
head>
<body>
    <div class="form-container">
        <h2 class="form-title">注册表单h2>
        <form>
            <div class="form-group">
                <label for="username" class="form-label">用户名label>
                <input type="text" id="username" class="form-control" placeholder="请输入用户名">
                <small class="form-text">用户名长度至少为 4 个字符。small>
            div>
            <div class="form-group">
                <label for="email" class="form-label">电子邮箱label>
                <input type="email" id="email" class="form-control" placeholder="请输入电子邮箱">
                <small class="form-text">请输入有效的电子邮箱地址。small>
            div>
            <div class="form-group">
                <label for="password" class="form-label">密码label>
                <input type="password" id="password" class="form-control" placeholder="请输入密码">
                <small class="form-text">密码长度至少为 8 个字符。small>
            div>
            <div class="form-group">
                <label for="confirm-password" class="form-label">确认密码label>
                <input type="password" id="confirm-password" class="form-control" placeholder="请确认密码">
                <small class="form-text">请再次输入密码以确认。small>
            div>
            <div class="form-group">
                <label class="form-label">性别label>
                <div class="form-radio-group">
                    <input type="radio" id="male" name="gender" class="form-radio">
                    <label for="male" class="form-label">label>
                div>
                <div class="form-radio-group">
                    <input type="radio" id="female" name="gender" class="form-radio">
                    <label for="female" class="form-label">label>
                div>
            div>
            <div class="form-group">
                <label class="form-label">爱好label>
                <div class="form-checkbox-group">
                    <input type="checkbox" id="reading" name="hobbies" class="form-checkbox">
                    <label for="reading" class="form-label">阅读label>
                div>
                <div class="form-checkbox-group">
                    <input type="checkbox" id="sports" name="hobbies" class="form-checkbox">
                    <label for="sports" class="form-label">运动label>
                div>
                <div class="form-checkbox-group">
                    <input type="checkbox" id="music" name="hobbies" class="form-checkbox">
                    <label for="music" class="form-label">音乐label>
                div>
            div>
            <div class="form-group">
                <label for="country" class="form-label">国家label>
                <select id="country" class="form-control form-select">
                    <option value="">请选择国家option>
                    <option value="CN">中国option>
                    <option value="US">美国option>
                    <option value="JP">日本option>
                    <option value="UK">英国option>
                select>
            div>
            <div class="form-group">
                <label for="profile" class="form-label">个人简介label>
                <textarea id="profile" class="form-control" rows="4" placeholder="请输入个人简介">textarea>
            div>
            <div class="form-group">
                <label for="avatar" class="form-label">头像上传label>
                <input type="file" id="avatar" class="form-control form-file">
            div>
            <button type="submit" class="form-submit">注册button>
            <a href="#" class="form-link">已有账号?去登录a>
        form>
    div>
body>
html>

注释:

  • 设计了一个完整的注册表单,包含了各种常见的表单元素,如文本输入框、单选按钮、复选框、下拉选择框、文本域和文件上传等。
  • 为表单元素添加了适当的样式,包括标签、输入框、按钮等,使表单更加美观和易用。
  • 使用了 focus 状态的样式,为用户提供视觉反馈,增强用户体验。
  • 通过添加 hover 状态的样式,使按钮的交互效果更加友好。
  • 表单布局合理,元素之间的间距适中,整体视觉效果协调。

案例9:响应式卡片布局

DOCTYPE html>
<html>
<head>
    <title>响应式卡片布局title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            padding: 2rem;
            background-color: #f8f9fa;
        }
        
        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 1.5rem;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .card {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .card-image {
            width: 100%;
            height: 160px;
            background-color: #ddd;
        }
        
        .card-content {
            padding: 1.5rem;
        }
        
        .card-title {
            font-size: 1.25rem;
            margin-bottom: 0.75rem;
            color: #2c3e50;
        }
        
        .card-text {
            color: #666;
            margin-bottom: 1rem;
            line-height: 1.5;
        }
        
        .card-tag {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            background-color: #f1f1f1;
            border-radius: 20px;
            font-size: 0.75rem;
            margin-right: 0.5rem;
            margin-bottom: 0.5rem;
            color: #333;
        }
        
        .card-price {
            font-size: 1.5rem;
            font-weight: bold;
            color: #4CAF50;
            margin-top: 1rem;
        }
        
        .card-button {
            display: inline-block;
            padding: 0.5rem 1rem;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-top: 1rem;
            transition: background-color 0.3s ease;
        }
        
        .card-button:hover {
            background-color: #45a049;
        }
        
        @media (max-width: 768px) {
            .card-container {
                grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            }
            
            .card-image {
                height: 140px;
            }
            
            .card-title {
                font-size: 1.1rem;
            }
            
            .card-text {
                font-size: 0.9rem;
            }
            
            .card-price {
                font-size: 1.3rem;
            }
        }
    style>
head>
<body>
    <div class="card-container">
        <div class="card">
            <div class="card-image">div>
            <div class="card-content">
                <h3 class="card-title">产品名称h3>
                <p class="card-text">这是一款高品质的产品,具有出色的性能和精美的设计,适合各种场景使用。p>
                <span class="card-tag">标签 1span>
                <span class="card-tag">标签 2span>
                <span class="card-tag">标签 3span>
                <div class="card-price">¥199.00div>
                <a href="#" class="card-button">立即购买a>
            div>
        div>
        
        <div class="card">
            <div class="card-image">div>
            <div class="card-content">
                <h3 class="card-title">产品名称h3>
                <p class="card-text">这是一款高品质的产品,具有出色的性能和精美的设计,适合各种场景使用。p>
                <span class="card-tag">标签 1span>
                <span class="card-tag">标签 2span>
                <span class="card-tag">标签 3span>
                <div class="card-price">¥299.00div>
                <a href="#" class="card-button">立即购买a>
            div>
        div>
        
        <div class="card">
            <div class="card-image">div>
            <div class="card-content">
                <h3 class="card-title">产品名称h3>
                <p class="card-text">这是一款高品质的产品,具有出色的性能和精美的设计,适合各种场景使用。p>
                <span class="card-tag">标签 1span>
                <span class="card-tag">标签 2span>
                <span class="card-tag">标签 3span>
                <div class="card-price">¥399.00div>
                <a href="#" class="card-button">立即购买a>
            div>
        div>
    div>
body>
html>

注释:

  • 使用了 CSS Grid 布局来创建响应式的卡片布局,使卡片能够自适应不同的屏幕尺寸。
  • 为卡片添加了阴影和悬停效果,增强了视觉效果和用户体验。
  • 卡片内部的文本内容设置了适当的字体大小、颜色和行高,确保信息的可读性。
  • 使用了标签来对产品进行分类,方便用户快速筛选和浏览。
  • 卡片中的价格和按钮采用了突出的样式设计,吸引用户的注意力。

你可能感兴趣的:(前端开发,网页开发,编程语言如门,css3,学习,前端,css,html5,javascript,web开发)