
页面渲染时,dom元素所采用的布局模型。可通过box sizing及进行设置
块级格式上下文,是一个独立的区域,让处于BFC内部的元素与外部的元素相互隔离,使内外元素定位不会发生相互影响
触发条件
规则
应用
.container {
overflow: hidden;
}
.left {
float: left;
width: 200px;
background-color: #f1f1f1;
padding: 20px;
}
.right {
overflow: hidden;
background-color: #e9e9e9;
padding: 20px;
}圣杯布局(三列自适应,左右定宽,中间自适应)
<body>
<div class="box">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
<style>
.box {
padding: 0 200px 0 200px;
}
.center, .left, .right {
height: 500px;
float: left;
position: relative;
}
.center {
width: 100%;
}
.left {
width: 200px;
left: -200px;
margin-left: -100%;
}
.right {
width: 200px;
right: -200px;
margin-right: -200px;
}
</style>元素提升为一个比较特殊的图层,在三维空间中(Z轴)高出普通元素一等
触发条件
层叠等级:层叠上下文在z轴上的排序
<style>
.box {
width: 400px;
height: 400px;
background: #fcc;
position: relative;
}
/* position + 负margin */
.horizontal-center-negmargin {
position: absolute;
width: 100px;
height: 50px;
left: 50%;
margin-left: -50px;
background: red;
}
/* position + transform */
.horizontal-center-marginauto {
position: absolute;
width: 100px;
height: 50px;
left: 0;
right: 0;
margin: auto;
background: red;
}
/* position + margin auto */
.horizontal-center-transform {
position: absolute;
left: 50%;
transform: translate(0, -50%);
background: red;
}
</style>
<div class="box">
<div class="horizontal-center">1111</div>
</div>.line {
display: -webkit-box;
overflow: hidden;
text-overflow: ellipsis;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
}
.line1 {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: stretch;
width: 300px; /* 或者你想要的任何宽度 */
}
.flex-item {
flex: 0 0 33.333%; /* 每个子项占据1/3的宽度 */
box-sizing: border-box;
padding: 10px; /* 根据需要添加内边距 */
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); /* 添加阴影以便更好的视觉效果 */
text-align: center;
}rem是相对根元素font-size的单位。为了简化编写rem单位,可以使用postcss插件进行转化
安装postcss插件
npm install postcss postcss-pxtorem autoprefixer -D创建postcss.config.js文件
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
'postcss-pxtorem': {
rootValue: 16, // 表示1rem等于多少px,这里设置为16px
propList: ['*', '!font-size'] // propList,表示需要转化哪些属性。这里设置为*,表示所有属性都需要转化, 同时!font-size表示不转化font-size属性
}
}
};设置根元素font-size
假设设计稿的宽度是1242px,那么根元素font-size的值为:屏幕宽度 * 16 / 1242
/* 1242下,1rem = 16px */
html {
font-size: calc(100vw * 16 / 1242);
}function setFontSize() {
// 获取屏幕宽度
var screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
// 根据屏幕宽度和设计稿的尺寸比例计算字体大小
var fontSize = screenWidth / 10; // 假设设计稿宽度为10rem
// 设置HTML元素的字体大小
document.documentElement.style.fontSize = fontSize + 'px';
}
setFontSize();
window.addEventListener('resize', setFontSize);使用 Tailwind CSS 可以轻松实现自适应布局,Tailwind CSS提供了一组响应式工具类,可以根据不同的屏幕尺寸应用不同的样式
主要思路
<div class="container mx-auto">
<div class="flex flex-col md:flex-row">
<div class="w-full md:w-1/2 lg:w-1/3 p-4">
<!-- 内容区域 1 -->
</div>
<div class="w-full md:w-1/2 lg:w-1/3 p-4">
<!-- 内容区域 2 -->
</div>
<div class="w-full lg:w-1/3 p-4">
<!-- 内容区域 3 -->
</div>
</div>
</div>伪类是用来为某些元素添加一些特殊的效果,例如当用户鼠标悬停在链接上时改变链接的颜色、当元素被点击时改变元素的背景色等。伪类通常用单冒号(:)来表示,例如:hover、:active等
伪元素则是用来在某些元素的前面或后面添加一些内容或样式,例如在元素前面添加一个小图标、在元素后面添加一些文本等。伪元素通常用双冒号(::)来表示,例如::before、::after等。
因此,伪类和伪元素的主要区别在于,伪类用于添加一些特殊的效果,而伪元素用于添加一些内容或样式。另外,伪类是基于元素的状态来匹配的,而伪元素则是基于元素的位置来创建的