当前位置: 首页 > news >正文

CSS 样式用法大全

文章目录

    • 一、CSS 基础语法
    • 二、CSS 引入方式
    • 三、CSS 选择器大全
      • 常用伪类
      • 常用伪元素
    • 四、常用 CSS 属性
      • 1. 文本与字体
      • 2. 盒模型(Box Model)
        • 边框详细设置
      • 3. 背景
      • 4. 显示与可见性
      • 5. 定位(Positioning)
      • 6. 浮动与清除
    • 五、现代布局方式
      • 1. Flexbox(弹性布局)
      • 2. Grid(网格布局)
      • 3. 多列布局(Columns)
    • 六、响应式设计
      • 1. 媒体查询(Media Queries)
      • 2. 视口单位
      • 3. 相对单位
    • 七、过渡与动画
      • 1. 过渡(Transition)
    • 八、其他实用技巧
      • 1. 重置默认样式(CSS Reset)
      • 2. 清除浮动(Clearfix)
      • 3. 居中技巧
    • 九、CSS 最佳实践
    • 十、现代 CSS 特性(可选)

CSS(层叠样式表)是用于描述HTML或XML(包括如SVG或XHTML)文档外观和格式的样式语言。它控制网页的布局、颜色、字体、动画等视觉表现。

以下是一个全面的 CSS 样式用法大全 概览,涵盖常用语法、选择器、属性、布局方式及现代特性。

一、CSS 基础语法

选择器 {属性:;属性:;
}

示例:

p {color: red;font-size: 16px;
}

二、CSS 引入方式

  1. 内联样式(Inline)

    <p style="color: blue;">文本</p>
    
  2. 内部样式表(Internal)

    <head><style>p { color: green; }</style>
    </head>
    
  3. 外部样式表(External)

    <link rel="stylesheet" href="styles.css">
    

    文件 styles.css 内容:

    p { color: purple; }
    

三、CSS 选择器大全

选择器示例说明
元素选择器p { }选择所有 <p> 元素
类选择器.class { }选择 class=“class” 的元素
ID 选择器#id { }选择 id=“id” 的元素
后代选择器div p { }div 内的所有 p 元素
子选择器div > p { }div 的直接子元素 p
相邻兄弟h1 + p { }紧接在 h1 后的 p 元素
通用兄弟h1 ~ p { }h1 后面所有的同级 p 元素
属性选择器[type="text"]拥有 type=“text” 的元素
伪类a:hover { }鼠标悬停时的链接
伪元素::before, ::after在元素前后插入内容

常用伪类

  • :hover - 鼠标悬停
  • :focus - 获得焦点
  • :active - 被激活(点击)
  • :visited - 已访问链接
  • :first-child, :last-child
  • :nth-child(n) - 第 n 个子元素
  • :not() - 排除某个选择器

常用伪元素

  • ::before / ::after - 插入内容
  • ::first-line - 首行样式
  • ::first-letter - 首字母样式
  • ::selection - 选中文本样式

四、常用 CSS 属性

1. 文本与字体

color: #333;              /* 文字颜色 */
font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;         /* normal, bold, 100-900 */
font-style: italic;        /* 斜体 */
text-align: center;        /* left, right, justify */
text-decoration: none;     /* underline, overline, line-through */
text-transform: uppercase; /* 大写、小写、首字母大写 */
line-height: 1.5;          /* 行高 */
letter-spacing: 1px;       /* 字间距 */
word-spacing: 2px;         /* 词间距 */
white-space: nowrap;       /* 控制空白处理 */

2. 盒模型(Box Model)

width: 200px;
height: 100px;
padding: 10px;             /* 内边距 */
border: 1px solid #ccc;    /* 边框 */
margin: 20px;              /* 外边距 */
box-sizing: border-box;    /* 包含 padding 和 border 在 width/height 中 */
边框详细设置
border-width: 1px;
border-style: solid;
border-color: black;
/* 或简写 */
border: 2px dashed red;/* 圆角 */
border-radius: 8px;
border-top-left-radius: 10px;/* 阴影 */
box-shadow: 2px 2px 5px rgba(0,0,0,0.3);

3. 背景

background-color: #f0f0f0;
background-image: url('bg.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;     /* contain, 100% 100% */
background-attachment: fixed; /* 背景固定 *//* 简写 */
background: #fff url(bg.png) no-repeat center center / cover;

4. 显示与可见性

display: block;            /* block, inline, inline-block, none */
visibility: hidden;        /* visible, hidden (保留空间) */
opacity: 0.8;              /* 透明度 0-1 */
overflow: hidden;          /* visible, scroll, auto */

5. 定位(Positioning)

position: static;          /* 默认 */
position: relative;        /* 相对定位 */
position: absolute;        /* 绝对定位(相对于最近的定位祖先) */
position: fixed;           /* 固定定位(相对于视口) */
position: sticky;          /* 粘性定位 */top: 10px;
right: 10px;
bottom: 10px;
left: 10px;z-index: 10;               /* 层叠顺序,仅对定位元素有效 */

6. 浮动与清除

float: left;               /* left, right, none */
clear: both;               /* both, left, right, none */

⚠️ 注意:浮动主要用于早期布局,现多被 Flex/Grid 替代。

五、现代布局方式

1. Flexbox(弹性布局)

.container {display: flex;flex-direction: row;           /* row, column, row-reverse, column-reverse */justify-content: center;       /* 主轴对齐 */align-items: center;           /* 交叉轴对齐 */flex-wrap: wrap;               /* 是否换行 */
}.item {flex: 1;                       /* 缩写:flex-grow, flex-shrink, flex-basis */flex-basis: 100px;
}

2. Grid(网格布局)

.container {display: grid;grid-template-columns: 1fr 2fr; /* 列宽 */grid-template-rows: 100px 1fr;  /* 行高 */gap: 10px;                      /* 网格间距 */grid-template-areas:"header header""nav main""footer footer";
}.header { grid-area: header; }
.nav { grid-area: nav; }

3. 多列布局(Columns)

.container {column-count: 3;column-gap: 20px;column-rule: 1px solid #ddd;
}

六、响应式设计

1. 媒体查询(Media Queries)

@media (max-width: 768px) {.container {flex-direction: column;}
}@media (min-width: 1024px) {.sidebar {width: 300px;}
}

2. 视口单位

width: 100vw;   /* 视口宽度的 100% */
height: 100vh;  /* 视口高度的 100% */
font-size: 4vmin; /* 取 vw 和 vh 中较小者 */

3. 相对单位

  • em:相对于父元素字体大小
  • rem:相对于根元素(html)字体大小
  • %:相对于父元素
  • ch:相对于字符 ‘0’ 的宽度
  • ex:相对于小写字母 x 的高度

七、过渡与动画

1. 过渡(Transition)

.button {background: blue;transition: all 0.3s ease;
}.button:hover {background: red;transform: scale(1.1);
}### 2. 动画(Animation)
```css
@keyframes fadeIn {from { opacity: 0; }to { opacity: 1; }
}.element {animation: fadeIn 2s ease-in-out infinite alternate;
}

八、其他实用技巧

1. 重置默认样式(CSS Reset)

* {margin: 0;padding: 0;box-sizing: border-box;
}

2. 清除浮动(Clearfix)

.clearfix::after {content: "";display: table;clear: both;
}

3. 居中技巧

  • 水平居中:margin: 0 auto;(块级元素)
  • 水平垂直居中:
    .center {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);
    }
    
    或使用 Flex:
    .container {display: flex;justify-content: center;align-items: center;height: 100vh;
    }
    

九、CSS 最佳实践

  1. 语义化命名:使用 BEM、SMACSS 等命名规范
  2. 避免过度嵌套:减少选择器复杂度
  3. 使用变量(CSS Custom Properties)
    :root {--primary-color: #007bff;
    }
    .btn {background: var(--primary-color);
    }
    
  4. 模块化与组件化:便于维护
  5. 性能优化:避免频繁重排重绘,合理使用 transformopacity

十、现代 CSS 特性(可选)

  • CSS Houdini:自定义属性与渲染
  • Container Queries:基于容器尺寸的响应式
  • Scroll Snap:滚动吸附
  • Backdrop Filter:背景模糊
  • Color Functionscolor-mix(), oklch() 等(现代浏览器支持)
http://www.dtcms.com/a/523998.html

相关文章:

  • Chrome旧版本下载
  • 浙江省建设网站首页html网站源代码
  • 厦门行业网站建设怎样建立自己的销售网站
  • 网站建设丿选择金手指排名15企业网站的制作公司
  • 结合MAML算法元强化学习
  • 重组蛋白表达的几种类型介绍
  • STM32之TM1638数码管及键盘驱动
  • Windows 10 安装 Docker Desktop
  • 数据的存储
  • GJOI 10.20/10.22 题解
  • Linux:权限(完结)|权限管理|修改权限chmod chown charp|文件类型|拓展
  • (一)仓库创建与配置 - .git 目录的结构与作用
  • Office 2010 64位 补丁 officesp2010-kb2687455 安装步骤详解(附安装包)
  • 建免费网站建设银行网站能不能注销卡
  • springboot中的怎么用JUnit进行测试的?
  • LeetCode:695. 岛屿的最大面积
  • 传奇手游可以使用云手机挂机搬砖吗
  • 2025 OSCAR丨与创新者同频!Apache RocketMQ 邀您共赴开源之约
  • Dify配置本地部署的音频识别模型
  • C# .NET Core中Chart图表绘制与PDF导出
  • 相机拍照的图片怎么做网站呀国内互联网公司排名
  • 微信怎么建设自己网站在单机安装wordpress
  • 实验-Vlan基础
  • Windows CMD 常用命令:7 大核心模块速查指南(附实战场景)
  • OCR国内外证件识别接口调用指南-身份证文字识别
  • 使用acme.sh创建自己的第一个https证书
  • Galera Cluster部署
  • 【Flink实战】升级HDFS,对Flink SQL(On Yarn模式)的功能兼容性验证
  • LangChain 表达式语言:SQL 数据库查询链
  • 通辽网站网站建设网站卖东西怎么做