刚学完CSS基础语法却不知道从哪下手?这篇整理了最常用的8大CSS样式,5分钟让你从小白变实操达人!
p {
font-family: "Microsoft YaHei", sans-serif; /* 优先微软雅黑 */
font-size: 16px; /* 字号 */
font-weight: bold; /* 加粗 */
font-style: italic; /* 斜体 */
line-height: 1.5; /* 行高(无单位表示倍数) */
}
/* 简写写法(顺序不能乱!) */
h1 { font: italic 700 24px/1.2 "宋体"; }
避坑指南:
sans-serif
).text {
color: #333; /* 颜色 */
text-align: center; /* 居中/左/右 */
text-decoration: underline; /* 下划线/删除线 */
letter-spacing: 2px; /* 字间距 */
text-indent: 2em; /* 首行缩进(推荐em单位) */
}
实战场景:
text-overflow: ellipsis
实现文字超出显示"..."white-space: nowrap
禁止文本换行.box {
width: 100px; /* 宽度 */
height: 60px; /* 高度 */
max-width: 100%; /* 响应式必备 */
min-height: 50vh; /* 最小高度为视口50% */
}
⚡ 重要概念:
px
:固定像素%
:相对父元素vw/vh
:相对视口header {
background-color: #f5f5f5;
background-image: url("bg.jpg");
background-repeat: no-repeat; /* 禁止平铺 */
background-position: center; /* 图片定位 */
background-size: cover; /* 完全覆盖区域 */
}
/* 渐变色高级玩法 */
.gradient-bg {
background: linear-gradient(45deg, #ff9a9e, #fad0c4);
}
.btn {
cursor: pointer; /* 手型指针 */
transition: all 0.3s; /* 悬停动画 */
}
.btn:hover {
transform: scale(1.05); /* 悬浮放大效果 */
}
用户友好设计:
cursor: not-allowed
禁用状态cursor: zoom-in
放大镜效果/* 链接不同状态 */
a:link { color: blue; } /* 未访问 */
a:visited { color: purple; }/* 已访问 */
a:hover { text-decoration: none; }
/* 列表首元素特效 */
li:first-child {
font-weight: bold;
color: red;
}
.modal {
opacity: 0.8; /* 整体透明度 */
background: rgba(0,0,0,0.5); /* 仅背景透明 */
}
关键区别:
opacity
:影响元素+子元素rgba
:仅影响当前颜色 下一步建议:
下期预告:《CSS布局终极指南:Flex vs Grid全对比》