每个开发人员都应该知道的 22 个 CSS 技巧和窍门
🚨🚨 注意:本文分享的所有技巧和窍门均来自 GitHub 代码库“css tips tricks”。这是一个专为开发者打造的专业 CSS 技巧和窍门合集。请查看该代码库,如果觉得有用,请点个星🌟
1. 文档布局
仅使用两行 CSS 即可创建响应式文档样式的布局。
.parent{
display: grid;
grid-template-columns: minmax(150px, 25%) 1fr;
}
2. 自定义游标
查看 github 存储库css 技巧以了解更多信息。
html{
cursor:url('no.png'), auto;
}
3.用图片填充文本
h1{
background-image: url('images/flower.jpg');
background-clip: text;
color: transparent;
background-color: white;
}
注意:使用此技术时务必指定background-color
,因为如果图像由于某种原因无法加载,这将用作后备值。
4. 为文本添加描边
text-stroke
使用属性为文本添加描边或轮廓,使文本更清晰可见。
/* 🎨 Apply a 5px wide crimson text stroke to h1 elements */
h1 {
-webkit-text-stroke: 5px crimson;
text-stroke: 5px crimson;
}
5.暂停伪类
:paused
在暂停状态下使用选择器来设置媒体元素的样式,同样:paused
我们也有:playing
。
/* 📢 currently, only supported in Safari */
video:paused {
opacity: 0.6;
}
6.强调文本
使用text-emphasis
属性将强调标记应用于文本元素。您可以指定任何字符串(包括表情符号)作为其值。
h1 {
text-emphasis: "⏰";
}
7. 首字下沉样式
避免不必要的跨度并使用伪元素来设置内容样式,同样first-letter
,我们也有first-line
伪元素。
h1::first-letter{
font-size: 2rem;
color:#ff8A00;
}
8. 变量的后备值
/* 🎨 crimson color will be applied as var(--black) is not defined */
:root {
--orange: orange;
--coral: coral;
}
h1 {
color: var(--black, crimson);
}
9.改变写作模式
您可以使用书写模式属性来指定文本在您的网站上的布局方式,即垂直或水平。
<h1>Cakes & Bakes</h1>
/* 💡 specifies the text layout direction to sideways-lr */
h1 {
writing-mode: sideways-lr;
}
10.彩虹动画
为元素创建连续循环的彩色动画,以吸引用户注意力。阅读CSS 技巧库,了解何时使用prefer-reduced-motion
媒体功能。
button{
animation: rainbow-animation 200ms linear infinite;
}
@keyframes rainbow-animation {
to{
filter: hue-rotate(0deg);
}
from{
filter: hue-rotate(360deg);
}
}
11. 掌握 Web 开发
订阅我们的YouTube 频道,提升您的 Web 开发技能。我们最近的一个视频系列讲解了如何创建以下开源作品集模板。
12. 悬停时缩放
/* 📷 Define the height and width of the image container & hide overflow */
.img-container {
height: 250px; width: 250px; overflow: hidden;
}
/* 🖼️ Make the image inside the container fill the container */
.img-container img {
height: 100%; width: 100%; object-fit: cover;
transition: transform 200m ease-in;
}
img:hover{
transform: scale(1.2);
}
13. 属性选择器
使用属性选择器根据属性选择 HTML 元素。
<a href="">HTML</a>
<a>CSS</a>
<a href="">JavaScript</a>
/* 🔗 targets all a elements that have a href attribute */
a[href] {
color: crimson;
}
14. 剪切元素
使用该clip-path
属性来创建有趣的视觉效果,例如将元素剪切为三角形或六边形等自定义形状。
div {
height: 150px;
width: 150px;
background-color: crimson;
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
15. 检测属性支持
使用 CSS@support rule
直接在 CSS 中检测 CSS 功能的支持情况。查看CSS 技巧库,了解更多关于功能查询的信息。
@supports (accent-color: #74992e) {
/* code that will run if the property is supported */
blockquote {
color: crimson;
}
}
16. CSS嵌套
CSS 工作组一直在研究如何在 CSS 中添加嵌套功能。有了嵌套功能,您将能够编写更直观、更有条理、更高效的 CSS。
<header class="header">
<p class="text">Lorem ipsum, dolor</p>
</header>
/* 🎉 You can try CSS nesting now in Safari Technology Preview*/
.header{
background-color: salmon;
.text{
font-size: 18px;
}
}
17. Clamp 函数
使用该clamp()
功能实现响应式和流畅的排版。
/* Syntax: clamp(minimum, preferred, maximum) */
h1{
font-size: clamp(2.25rem,6vw,4rem);
}
18. 样式可选字段
您可以使用伪类来设置没有必需属性的表单字段(如输入、选择和文本区域):optional
。
/* Selects all optional form fields on the page */
*:optional{
background-color: green;
}
19. 词间距属性
使用word-spacing
属性指定单词之间的空格的长度。
p {
word-spacing: 1.245rem;
}
20.创建渐变阴影
这就是您可以创建渐变阴影以获得独特的用户体验的方法。
:root{
--gradient: linear-gradient(to bottom right, crimson, coral);
}
div {
height: 200px;
width: 200px;
background-image: var(--gradient);
border-radius: 1rem;
position: relative;
}
div::after {
content: "";
position: absolute;
inset: 0;
background-image: var(--gradient);
border-radius: inherit;
filter: blur(25px) brightness(1.5);
transform: translateY(15%) scale(0.95);
z-index: -1;
}
21. 更改标题的侧面
使用该caption-side
属性将表格标题(表格标题)放置在表格的指定一侧。
22. 创建文本列
使用列属性为文本元素制作漂亮的列布局。
/* 🏛️ divide the content of the "p" element into 3 columns */
p{
column-count: 3;
column-gap: 4.45rem;
column-rule: 2px dotted crimson;
}
希望你喜欢这篇文章。请查看 GitHub 代码库“css tips tricks”,了解更多专业的 CSS 技巧和窍门。别忘了给代码库点个 Star⭐,这样可以帮助其他人找到这个代码库。
您可以通过关注我的GitHub 帐户来表示支持。谢谢,祝您编程愉快 ❤️
文章来源:https://dev.to/devsyedmohsin/22-useful-css-tips-and-tricks-every-developer-should-know-13c6