CSS核心知识详解:选择器与属性实战指南

一、CSS入门:从选择元素开始

1. 基础选择器

选择器类型示例使用场景优先级
元素选择器p修改所有段落样式1
类选择器.nav-item复用样式(最常用)10
ID选择器#header唯一元素样式100
后代选择器div p嵌套元素定位累加
伪类选择器a:hover交互状态样式10
通用选择器*全局样式重置0

示例代码

/* 元素选择器 */
h1 {
    color: blue;
}

/* 类选择器 */
.active {
    font-weight: bold;
}

/* ID选择器 */
#main-content {
    padding: 20px;
}

/* 后代选择器 */
nav a {
    text-decoration: none;
}

/* 伪类选择器 */
button:hover {
    background-color: #f0f0f0;
}

二、文本样式:让文字会说话

1. 字体控制

p {
    font-family: "Helvetica Neue", Arial, sans-serif; /* 字体栈 */
    font-size: 16px;       /* 推荐使用rem单位 */
    font-weight: 400;      /* 100-900,400=normal */
    line-height: 1.6;      /* 行高建议用无单位值 */
    color: #333;           /* 十六进制颜色值 */
    text-align: center;    /* 对齐方式 */
    text-decoration: underline; /* 下划线/删除线 */
}

2. 字体单位对比

单位特点使用场景
px固定像素值精确控制
em相对于父元素字体大小相对单位
rem相对于根元素字体大小推荐使用
%相对于父元素字体大小百分比控制
vw/vh视口宽高百分比响应式设计

三、盒子模型:网页布局的基石

1. 盒子模型详解

.box {
    width: 200px;         /* 内容区宽度 */
    height: 150px;
    padding: 20px;        /* 内边距 */
    border: 2px solid #000; /* 边框 */
    margin: 30px;         /* 外边距 */
    box-sizing: border-box; /* 推荐使用!保持元素总尺寸 */
}

2. 常见问题解决方案

  • margin塌陷:父元素添加overflow: hidden或使用Flex布局

  • 边框重叠:使用box-shadow替代边框

  • 高度塌陷:使用clearfix技巧


四、定位与浮动:元素位置控制

1. 定位系统

定位方式特点示例代码
static(默认)正常文档流position: static;
relative相对自身定位position: relative; top: 10px;
absolute相对于最近定位父元素position: absolute; left: 0;
fixed相对于视口固定position: fixed; top: 0;
sticky混合定位(新版特性)position: sticky; top: 20px;

2. 浮动布局

.left {
    float: left;
    width: 200px;
}

.right {
    float: right;
    width: 200px;
}

.clearfix::after {
    content: '';
    display: block;
    clear: both;
}

五、显示与隐藏:元素可见性控制

1. 常用属性

属性特点使用场景
display控制显示方式none完全隐藏
visibility控制可见性hidden保留空间
opacity控制透明度0-1之间取值
overflow内容溢出处理hidden隐藏溢出内容

2. 示例代码

.hidden {
    display: none;      /* 完全隐藏,不占空间 */
}

.invisible {
    visibility: hidden; /* 隐藏但保留空间 */
}

.transparent {
    opacity: 0.5;       /* 半透明效果 */
}

.scrollable {
    overflow: auto;     /* 自动显示滚动条 */
}

六、实战案例:个人简介页面美化

HTML结构

<div class="profile">
    <img src="avatar.jpg" alt="头像">
    <h1>张三</h1>
    <p>前端开发工程师</p>
    <ul class="skills">
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
    </ul>
</div>

CSS样式

.profile {
    width: 300px;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid #ddd;
    text-align: center;
}

.profile img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    margin-bottom: 15px;
}

.skills {
    list-style: none;
    padding: 0;
}

.skills li {
    display: inline-block;
    margin: 5px;
    padding: 5px 10px;
    background: #f0f0f0;
    border-radius: 15px;
}

七、常见问题解答

Q1:如何实现垂直居中?

/* 方案1:Flex布局 */
.container {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 方案2:Grid布局 */
.container {
    display: grid;
    place-items: center;
}

/* 方案3:绝对定位 */
.container {
    position: relative;
}
.item {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Q2:如何实现响应式布局?

  • 使用媒体查询@media

  • 使用相对单位(rem、%)

  • 结合Flex/Gird的自动换行特性

Q3:如何清除浮动?

.clearfix::after {
    content: '';
    display: block;
    clear: both;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值