CSS3 初始化代码(带注释)

/* 清除所有元素的内外边距 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box; /* 确保元素的宽度和高度包含边框和内边距 */
}

/* 设置 HTML 和 body 的默认样式 */
html, body {
    height: 100%; /* 使页面高度占满整个视口 */
    font-family: "Microsoft YaHei", Arial, sans-serif; /* 设置默认字体 */
    line-height: 1.5; /* 设置行高 */
    color: #333; /* 设置默认文字颜色 */
    background-color: #fff; /* 设置默认背景色 */
}

/* 清除列表的默认样式(如项目符号) */
ul, ol {
    list-style: none;
}

/* 清除链接默认下划线,并设置默认颜色 */
a {
    text-decoration: none;
    color: inherit; /* 继承父元素的颜色 */
}

/* 清除图片底部默认空白间隙 */
img {
    display: block; /* 避免图片下方出现空白 */
    max-width: 100%; /* 防止图片超出容器 */
    height: auto; /* 保持图片比例 */
}

/* 清除表单元素的默认样式 */
input, textarea, button, select {
    font-family: inherit; /* 继承父元素的字体 */
    outline: none; /* 去除聚焦时的轮廓线 */
    border: none; /* 去除默认边框 */
    background: none; /* 去除默认背景 */
}

/* 清除表格的默认间距 */
table {
    border-collapse: collapse; /* 合并边框 */
    border-spacing: 0; /* 去除单元格间距 */
}

/* 清除按钮的默认样式 */
button {
    cursor: pointer; /* 鼠标悬停时显示手型 */
}

关键点说明

  • box-sizing: border-box:确保元素的 widthheight 包含 paddingborder,避免布局计算错误。
  • max-width: 100%:防止图片超出容器宽度,同时保持比例。
  • list-style: none:去除 <ul><ol> 的默认项目符号。
  • border-collapse: collapse:合并表格边框,使表格更整洁。
Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐