你值得收藏的66个实用的css开发技巧之布局技巧

AI 概述
备注Layout Skill使用 vw 定制 rem 自适应布局使用:nth-child()选择指定元素使用 writing-mode 排版竖文使用 text-align-last 对齐两端文本使用:not()去除无用属性使用 object-fit 规定图像尺寸使用 overflow-x 排版横向列表使用 text-overflow 控制文本溢出使用 transform 描绘 1px 边框使用 tran...
目录
文章目录隐藏
  1. 目录
  2. 备注
  3. Layout Skill
  4. 结束语

作为一枚前端开发人,写代码就像是我们在写作一样,同样的需要技巧。试想如果你的代码乱七八糟,没有头绪,还冗余,出现大量的无用代码,任何人看了都想对你有一种几天的感觉。所以一份良好的代码能让人耳目一新,让人容易理解,让人舒服自然,更容易让人维护,同时也让自己成就感满满(哈哈,这个才是重点)。因此,我整理下自己在前端开发中使用到的一些 CSS 开发技巧,希望能够帮助你写出耳目一新、容易理解、舒服自然的代码。全文共 66 个 css 开发技巧,因为文章有点长,为了减少大家阅读疲劳感,所以我打算将知识点分为五篇文章来实例讲解,当然,最重要的是能够学完之后对大家有用,应用到实战开发中。

目录

既然写文章有这么多的写作技巧,那么我也需要对 CSS 开发技巧整理一下,起个易记的名字。

  • Layout Skill:布局技巧
  • Behavior Skill:行为技巧
  • Color Skill:色彩技巧
  • Figure Skill:图形技巧
  • Component Skill:组件技巧

接下来我们先讲 Layout Skill:布局技巧

备注

  • 代码只作演示用途,不会详细说明语法
  • 兼容项点击链接即可查看当前属性的浏览器兼容数据,自行根据项目兼容需求考虑是否使用
  • 以下代码全部基于 CSS 进行书写,没有任何 JS 代码,没有特殊说明的情况下所有属性和方法都是 CSS 类型
  • 一部分技巧是自己探讨出来的,另一部分技巧是参考各位前端大神们的,都是一个互相学习的工程,大家一起进步

Layout Skill

使用 vw 定制 rem 自适应布局

要点:移动端使用 rem 布局需要通过 JS 设置不同屏幕宽高比的 font-size,结合 vw 单位和 calc()可脱离 JS 的控制

场景:rem 页面布局(不兼容低版本移动端系统)

兼容:vw、calc()

/* 基于 UI width=750px DPR=2 的页面 */
html {
    font-size: calc(100vw / 7.5);
}

使用:nth-child()选择指定元素

要点:通过:nth-child()筛选指定的元素设置样式

场景:表格着色、边界元素排版(首元素、尾元素、左右两边元素)

兼容::nth-child()

实例代码:
HTML

<div class="bruce flex-ct-x">
	<ul class="specified-scope">
		<li>10001</li>
		<li>10002</li>
		...
		<li>10009</li>
		<li>10010</li>
	</ul>
</div>

CSS

.specified-scope {
	width: 300px;
	li {
		padding: 0 20px;
		height: 40px;
		line-height: 40px;
		color: #fff;
		&:nth-child(odd) {
			background-color: $red;
		}
		&:nth-child(even) {
			background-color: $purple;
		}
		&:nth-child(n+6):nth-child(-n+10) {
			background-color: $green;
		}
	}
}

效果展示:
使用:nth-child()选择指定元素

使用 writing-mode 排版竖文

要点:通过 writing-mode 调整文本排版方向

场景:竖行文字、文言文、诗词

兼容:writing-mode

实例代码:

HTML

<div class="bruce flex-ct-x">
	<div class="vertical-text">
		<h3>情</h3>
		<p>我见犹怜,<br>爱不释手。<br>雅俗共赏,<br>君子好逑。</p>
	</div>
</div>

CSS

.vertical-text {
	writing-mode: vertical-rl;
	h3 {
		padding-left: 20px;
		font-weight: bold;
		font-size: 18px;
		color: $red;
	}
	p {
		line-height: 30px;
		color: $purple;
	}
}

效果展示:
使用 writing-mode 排版竖文

使用 text-align-last 对齐两端文本

要点:通过 text-align-last:justify 设置文本两端对齐

场景:未知字数中文对齐

兼容:text-align-last

实例代码:

HTML

<div class="bruce flex-ct-x">
	<ul class="justify-text">
		<li>账号</li>
		<li>密码</li>
		<li>电子邮件</li>
		<li>通讯地址</li>
	</ul>
</div>

CSS

.justify-text {
	li {
		margin-top: 5px;
		padding: 0 20px;
		width: 100px;
		height: 40px;
		background-color: $red;
		line-height: 40px;
		text-align-last: justify;
		color: #fff;
		&:first-child {
			margin-top: 0;
		}
	}
}

效果展示:
使用 text-align-last 对齐两端文本

使用:not()去除无用属性

要点:通过:not()排除指定元素不使用设置样式

场景:符号分割文字、边界元素排版(首元素、尾元素、左右两边元素)

兼容::not()

实例代码:

HTML

<div class="bruce flex-ct-x">
	<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>

CSS

.cleared-attr {
	li {
		height: 40px;
		line-height: 40px;
	}
	span {
		display: inline-block;
		color: $purple;
	}
	.first-line span:not(:last-child)::after {
		content: ",";
	}
	.second-line span:not(:nth-child(-n+3)) {
		display: none;
	}
}

效果展示:
使用:not()去除无用属性

使用 object-fit 规定图像尺寸

要点:通过 object-fit 使图像脱离 background-size 的约束,使用来标记图像背景尺寸

场景:图片尺寸自适应

兼容:object-fit

实例代码:

<div class="bruce flex-ct-y">
	<ul class="image-size">
		<li>
			<h3>Cover</h3>
			<img src="/static/codepen/ab-1.jpg" class="cover">
		</li>
		<li>
			<h3>Contain</h3>
			<img src="/static/codepen/ab-1.jpg" class="contain">
		</li>
		<li>
			<h3>Fill</h3>
			<img src="/static/codepen/ab-2.jpg" class="fill">
		</li>
		<li>
			<h3>ScaleDown</h3>
			<img src="/static/codepen/ab-2.jpg" class="scale-down">
		</li>
	</ul>
</div>

CSS

h1 {
	line-height: 50px;
	font-weight: bold;
	font-size: 30px;
	color: $red;
}
.image-size {
	display: flex;
	justify-content: space-between;
	width: 1000px;
	height: 300px;
	li {
		width: 200px;
	}
	h3 {
		height: 40px;
		line-height: 40px;
		text-align: center;
		font-weight: bold;
		font-size: 16px;
	}
	img {
		width: 100%;
		height: 260px;
		background-color: $green;
		&.cover {
			object-fit: cover;
		}
		&.contain {
			object-fit: contain;
		}
		&.fill {
			object-fit: fill;
		}
		&.scale-down {
			object-fit: scale-down;
		}
	}
}

效果展示:
使用 object-fit 规定图像尺寸

使用 overflow-x 排版横向列表

要点:通过 flexbox 或 inline-block 的形式横向排列元素,对父元素设置 overflow-x:auto 横向滚动查看

场景:横向滚动列表、元素过多但位置有限的导航栏

兼容:overflow-x

实例代码:

<div class="bruce flex-ct-y">
	<div class="horizontal-list flex">
		<ul>
			<li>Alibaba</li>
			<li>Tencent</li>
			<li>Baidu</li>
			<li>Jingdong</li>
			<li>Ant</li>
			<li>Netease</li>
			<li>Meituan</li>
			<li>ByteDance</li>
			<li>360</li>
			<li>Sina</li>
		</ul>
	</div>
	<div class="horizontal-list inline">
		<ul>
			<li>Alibaba</li>
			<li>Tencent</li>
			<li>Baidu</li>
			<li>Jingdong</li>
			<li>Ant</li>
			<li>Netease</li>
			<li>Meituan</li>
			<li>ByteDance</li>
			<li>360</li>
			<li>Sina</li>
		</ul>
	</div>
</div>

CSS

.horizontal-list {
	overflow: hidden;
	width: 300px;
	height: 100px;
	ul {
		overflow-x: scroll;
		cursor: pointer;
		&::-webkit-scrollbar {
			height: 10px;
		}
		&::-webkit-scrollbar-track {
			background-color: #f0f0f0;
		}
		&::-webkit-scrollbar-thumb {
			border-radius: 5px;
			background-color: $red;
		}
	}
	li {
		overflow: hidden;
		margin-left: 10px;
		height: 90px;
		background-color: $purple;
		line-height: 90px;
		text-align: center;
		font-size: 16px;
		color: #fff;
		&:first-child {
			margin-left: 0;
		}
	}
}
.flex {
	ul {
		display: flex;
		flex-wrap: nowrap;
		justify-content: space-between;
	}
	li {
		flex-shrink: 0;
		flex-basis: 90px;
	}
}
.inline {
	margin-top: 10px;
	height: 102px;
	ul {
		overflow-y: hidden;
		white-space: nowrap;
	}
	li {
		display: inline-block;
		width: 90px;
	}
}

效果展示:
使用 overflow-x 排版横向列表

使用 text-overflow 控制文本溢出

要点:通过 text-overflow:ellipsis 对溢出的文本在末端添加…

场景:单行文字溢出、多行文字溢出

兼容:text-overflow、line-clamp、box-orient

实例代码:

HTML

<div class="bruce flex-ct-y">
	<p class="single-line sl-ellipsis">CSS 非常有趣和搞怪,可以做一些 JS 也能做的事情</p>
	<p class="multiple-line ml-ellipsis">层叠样式表(CSS)是一种用来表现 HTML(标准通用标记语言的一个应用)或 XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS 不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。</p>
	<p class="multiple-line mls-ellipsis">层叠样式表(CSS)是一种用来表现 HTML(标准通用标记语言的一个应用)或 XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS 不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。</p>
</div>

CSS

p[class*=-line] {
	line-height: 30px;
	font-size: 20px;
}
.single-line {
	width: 200px;
}
.multiple-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-line-clamp: 3;
	/* autoprefixer: off */
	-webkit-box-orient: vertical;
	/* autoprefixer: on */
}
.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: "...";
	}
}

效果展示:
使用 text-overflow 控制文本溢出

使用 transform 描绘 1px 边框

要点:分辨率比较低的屏幕下显示 1px 的边框会显得模糊,通过::before 或::after 和 transform 模拟细腻的 1px 边框

场景:容器 1px 边框

兼容:transform

实例代码:

HTML

<div class="bruce flex-ct-y">
	<div class="onepx-border normal">1px</div>
	<div class="onepx-border thin">0.5px</div>
</div>

CSS

.onepx-border {
	margin-top: 10px;
	width: 200px;
	height: 80px;
	cursor: pointer;
	line-height: 80px;
	text-align: center;
	font-weight: bold;
	font-size: 50px;
	color: $red;
	&:first-child {
		margin-top: 0;
	}
}
.normal {
	border: 1px solid $red;
}
.thin {
	position: relative;
	&::after {
		position: absolute;
		left: 0;
		top: 0;
		border: 1px solid $red;
		width: 200%;
		height: 200%;
		content: "";
		transform: scale(.5);
		transform-origin: left top;
	}
}

使用 transform 描绘 1px 边框

使用 transform 翻转内容

要点:通过 transform:scale3d()对内容进行翻转(水平翻转、垂直翻转、倒序翻转)

场景:内容翻转

兼容:transform

实例代码:

HTML

<div class="bruce flex-ct-x">
	<ul class="horizontal-flip">
		<li>正常文本</li>
		<li class="x-axis">水平翻转</li>
		<li class="y-axis">垂直翻转</li>
		<li class="reverse">倒序翻转</li>
	</ul>
</div>

CSS

.horizontal-flip {
	li {
		position: relative;
		margin-top: 10px;
		width: 121px;
		height: 51px;
		line-height: 51px;
		text-align: center;
		font-weight: bold;
		font-size: 30px;
		color: $red;
		&::before,
		&::after {
			position: absolute;
			background-color: $purple;
			content: "";
		}
		&:first-child {
			margin-top: 0;
		}
	}
}
.x-axis {
	transform: scale3d(1, -1, 1);
	&::after {
		left: 0;
		top: 25px;
		width: 100%;
		height: 1px;
	}
}
.y-axis {
	transform: scale3d(-1, 1, 1);
	&::after {
		left: 60px;
		top: 0;
		width: 1px;
		height: 100%;
	}
}
.reverse {
	transform: scale3d(-1, -1, 1);
	&::before {
		left: 0;
		top: 25px;
		width: 100%;
		height: 1px;
	}
	&::after {
		left: 60px;
		top: 0;
		width: 1px;
		height: 100%;
	}
}

效果展示:
使用 transform 翻转内容

使用 letter-spacing 排版倒序文本

要点:通过 letter-spacing 设置负值字体间距将文本倒序

场景:文言文、诗词

兼容:letter-spacing

实例代码:

HTML

<div class="bruce flex-ct-x">
	<div class="reverse-text">恭喜发财</div>
</div>

CSS

.reverse-text {
	font-weight: bold;
	font-size: 50px;
	color: $red;
	letter-spacing: -100px; // letter-spacing 最少是 font-size 的 2 倍
}

效果展示:
使用 letter-spacing 排版倒序文本

使用 margin-left 排版左重右轻列表

要点:使用 flexbox 横向布局时,最后一个元素通过 margin-left:auto 实现向右对齐

场景:右侧带图标的导航栏

兼容:margin

实例代码:

HTML

<div class="bruce flex-ct-x">
	<ul class="left-list">
		<li>Alibaba</li>
		<li>Tencent</li>
		<li>Baidu</li>
		<li>Jingdong</li>
		<li>Ant</li>
		<li>Netease</li>
	</ul>
</div>

CSS

.left-list {
	display: flex;
	align-items: center;
	padding: 0 10px;
	width: 600px;
	height: 60px;
	background-color: $green;
	li {
		padding: 0 10px;
		height: 40px;
		background-color: $orange;
		line-height: 40px;
		font-size: 16px;
		color: #fff;
		& + li {
			margin-left: 10px;
		}
		&:last-child {
			margin-left: auto;
		}
	}
}

效果展示:
使用 margin-left 排版左重右轻列表

结束语

以上就是今天码云笔记为大家整理的你值得收藏的 66 个实用的 css 开发技巧第一篇,希望对你有帮助,接下来为大家整理css 开发技巧之行为技巧第二篇未完待续。。。

以上关于你值得收藏的66个实用的css开发技巧之布局技巧的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。

「点点赞赏,手留余香」

16

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » 你值得收藏的66个实用的css开发技巧之布局技巧

发表回复