CSS开发技巧——布局技巧
-
使用vw定制rem自适应布局
移动端使用rem布局需要通过JS设置不同屏幕宽高比的font-size,结合vw单位和calc()可脱离JS的控制
-
场景: rem页面布局不兼容低版本移动端系统
-
// 基于 UI宽度为750px DPR=2的页面
-
html { fongt-size:calc(100vw / 7.5) }
-
-
使用:nth-child()选择指定元素
通过 :th-child()筛选指定的元素设置样式
-
场景:表格着色、边界元素排版(首元素、尾元素、左右两边元素)
-
<div class="bruce flex-ct-x" data-title="使用:nth-child()选择指定元素"> <ul class="specified-scope"> <li>10001</li> <li>10002</li> <li>10003</li> <li>10004</li> <li>10005</li> <li>10006</li> <li>10007</li> <li>10008</li> <li>10009</li> <li>10010</li> </ul> </div> <style> .specified-scope { width: 300px; li { padding: 0 20px; height: 40px; line-height: 40px; color: #fff; &:nth-child(odd) { background-color: #f66; } &:nth-child(even) { background-color: #66f; } &:nth-child(n+6):nth-child(-n+7) { background-color: #3c9; } } } </style>
-
-
使用writing-mode排版竖文
通过writing-mode调整文本排版方向
-
场景:竖行文字、文言文、诗词
-
<div class="bruce flex-ct-x" data-title="使用writing-mode排版竖向文本"> <div class="vertical-text"> <p>忽故人心上过,<br>回首山河已是秋。<br>两处相思同淋雪,<br>此生也算共白头。</p> </div> </div> <style> .vertical-text { writing-mode: vertical-rl; h3 { padding-left: 20px; font-weight: bold; font-size: 18px; color: #f66; } p { line-height: 30px; color: #66f; } } </style>
-
-
使用text-align-last对齐两端文本
通过text-align-last:justify设置文本两端对齐
-
场景:未知字数中文对齐
-
<div class="bruce flex-ct-x" data-title="使用text-align-last对齐两端文本"> <ul class="justify-text"> <li>账号</li> <li>密码</li> <li>电子邮件</li> </ul> </div> <style> .justify-text { li { padding: 0 20px; width: 100px; height: 40px; background-color: #f66; line-height: 40px; text-align-last: justify; color: #fff; & + li { margin-top: 5px; } } } </style>
-
-
使用:not()去除无用属性
通过:not()排除指定元素不使用设置样式
-
场景:符号分割文字、边界元素排版(首元素、尾元素、左右两边元素)
-
<div class="bruce flex-ct-x" data-title="使用:not()去除无用属性"> <ul class="cleared-attr"> <li class="first-line"> <span>A</span> <span>B</span> <span>C</span> <span>D</span> <span>E</span> </li> <li class="second-line"> <span>A</span> <span>B</span> <span>C</span> <span>D</span> <span>E</span> </li> </ul> </div> <style> .cleared-attr { li { height: 40px; line-height: 40px; } span { display: inline-block; color: #66f; } .first-line span:not(:last-child)::after { content: ","; } .second-line span:not(:nth-child(-n+3)) { display: none; } } </style>
-
-
使用object-fit规定图像尺寸
通过object-fit使图像脱离background-size的约束,使用来标记图像背景尺寸
- 场景:图片尺寸自适应
-
使用overflow-x排版横向列表
通过flexbox或inline-nlock的形式横向排列元素,对父元素设置overflow-x:auto横向滚动查看
-
场景:横向滚动列表、元素过多但位置有限的导航栏
-
<div class="bruce flex-ct-y" data-title="使用overflow-x排版横向列表"> <div class="horizontal-list flex"> <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> <li>E</li> <li>F</li> </ul> </div> <div class="horizontal-list inline"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> </div> <style> .horizontal-list { overflow: hidden; width: 300px; height: 100px; & + .horizontal-list { margin-top: 10px; } ul { overflow-x: auto; cursor: pointer; &::-webkit-scrollbar { height: 10px; } &::-webkit-scrollbar-track { background-color: #f0f0f0; } &::-webkit-scrollbar-thumb { border-radius: 5px; background-color: #f66; } } li { overflow: hidden; height: 90px; background-color: #66f; line-height: 90px; text-align: center; font-size: 16px; color: #fff; & + li { margin-left: 10px; } } &.flex { ul { display: flex; flex-wrap: nowrap; justify-content: space-between; } li { flex-shrink: 0; flex-basis: 90px; } } &.inline { height: 102px; ul { overflow-y: hidden; white-space: nowrap; } li { display: inline-block; width: 90px; } } } </style>
-
-
使用text-overflow控制文本溢出
通过text-overflow:ellipsis对溢出的文本在末端添加
...
-
场景:单行文字溢出、多行文字溢出
-
<div class="bruce flex-ct-y" data-title="使用text-overflow控制溢出文本"> <p class="ellipsis-text s-line sl-ellipsis">CSS非常有趣和搞怪,可以做一些JS也能做的事情</p> <p class="ellipsis-text m-line ml-ellipsis">层叠样式表(CSS)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。</p> <p class="ellipsis-text m-line mls-ellipsis">CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。</p> </div> <style> .ellipsis-text { line-height: 30px; font-size: 20px; &.s-line { width: 400px; } &.m-line { margin-top: 10px; width: 400px; text-align: justify; } &.sl-ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } &.ml-ellipsis { display: -webkit-box; overflow: hidden; text-overflow: ellipsis; -webkit-box-orient: vertical; -webkit-line-clamp: 3; } &.mls-ellipsis { overflow: hidden; position: relative; max-height: 90px; &::after { position: absolute; right: 0; bottom: 0; padding-left: 40px; background: linear-gradient(to right, transparent, #fff 50%); content: "..."; } } } </style>
-
-
使用transform描绘1px边框
分辨率比较低的屏幕下显示1px的边框回显得模糊,通过::before或::after和transform模拟细腻的1px边框。
-
场景:容器1px边框
-
<div class="bruce flex-ct-y" data-title="使用transform描绘像素边框"> <div class="onepx-border normal">1px</div> <div class="onepx-border thin">0.5px</div> </div> <style> .onepx-border { width: 200px; height: 80px; cursor: pointer; line-height: 80px; text-align: center; font-weight: bold; font-size: 50px; color: #f66; & + .onepx-border { margin-top: 10px; } &.normal { border: 1px solid #f66; } &.thin { position: relative; &::after { position: absolute; left: 0; top: 0; border: 1px solid #f66; width: 200%; height: 200%; content: ""; transform: scale(.5); transform-origin: left top; } } } </style>
-
-
使用transform翻转内容
通过transform:scale3d()对内容进行翻转(水平翻转、垂直翻转、倒序翻转)
-
<div class="bruce flex-ct-x" data-title="使用transform翻转内容"> <ul class="flip-content"> <li>正常文本</li> <li class="x-axis">水平翻转</li> <li class="y-axis">垂直翻转</li> <li class="reverse">倒序翻转</li> </ul> </div> <style> .flip-content { li { position: relative; width: 121px; height: 51px; line-height: 51px; text-align: center; font-weight: bold; font-size: 30px; color: #f66; &::before, &::after { position: absolute; background-color: #66f; content: ""; } & + li { margin-top: 10px; } &.x-axis { transform: scale(1, -1); &::after { left: 0; top: 25px; width: 100%; height: 1px; } } &.y-axis { transform: scale(-1, 1); &::after { left: 60px; top: 0; width: 1px; height: 100%; } } &.reverse { transform: scale(-1, -1); &::before { left: 0; top: 25px; width: 100%; height: 1px; } &::after { left: 60px; top: 0; width: 1px; height: 100%; } } } } </style>
-
-
使用letter-spacing排版倒序文章
通过letter-spacing 设置负值字体间距将文本倒序
-
<div class="bruce flex-ct-x" data-title="使用letter-spacing排版倒序文本"> <div class="reverse-text">平安喜乐</div> </div> <style> .reverse-text { padding-left: 100px; // 与letter-spacing一致 font-weight: bold; font-size: 50px; color: #f66; letter-spacing: -100px; // letter-spacing最少是font-size的2倍 } </style>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SBU7glwi-1626147337410)(C:%5CUsers%5Cmeryliangyu%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210712163540267.png)]
-
-
使用margin-left排版左重右轻列表
使用flexbox横向布局时,最后一元素通过margin-left:auto实现向右对齐
-
场景:右侧带图标的导航栏
-
<div class="bruce flex-ct-y" data-title="使用margin排版凸显布局"> <ul class="highlight-list left"> <li>A</li> <li>B</li> <li>C</li> <li>D</li> <li>E</li> <li>F</li> </ul> <ul class="highlight-list right"> <li>A</li> <li>B</li> <li>C</li> <li>D</li> <li>E</li> <li>F</li> </ul> </div> <style> .highlight-list { display: flex; align-items: center; padding: 0 10px; width: 600px; height: 60px; background-color: #3c9; & + .highlight-list { margin-top: 10px; } li { padding: 0 10px; height: 40px; background-color: #3c9; line-height: 40px; font-size: 16px; color: #fff; } &.left li { & + li { margin-left: 10px; } &:last-child { margin-left: auto; } } &.right li { & + li { margin-left: 10px; } &:first-child { margin-right: auto; } } } </style>
-