有趣且实用的 CSS 小技巧

IQ前端

共 6631字,需浏览 14分钟

 · 2022-02-19

今天来分享一些有趣且实用的 CSS 小技巧!

1. 打字效果

代码实现:

<div class="wrapper">
    <div class="typing-demo">
      有趣且实用的 CSS 小技巧
    div>
div>
.wrapper {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.typing-demo {
  width22ch;
  animation: typing 2s steps(22), blink .5s step-end infinite alternate;
  white-space: nowrap;
  overflow: hidden;
  border-right3px solid;
  font-family: monospace;
  font-size2em;
}

@keyframes typing {
  from {
    width0
  }
}
    
@keyframes blink {
  50% {
    border-color: transparent
  }
}

实现效果:

2. 设置阴影

当使用透明图像时,可以使用 drop-shadow() 函数在图像上创建阴影,而不是使用 box shadow 属性在元素的整个框后面创建矩形阴影:

<div class="wrapper">
  <div class="mr-2">
    <div class="mb-1 text-center">
      box-shadow
    div>
    
    <img class="box-shadow" src="https://markodenic.com/man_working.png" alt="Image with box-shadow">
  div>
    
  <div>
    <div class="mb-1 text-center">
      drop-shadow
    div>
    
    <img class="drop-shadow" src="https://markodenic.com/man_working.png" alt="Image with drop-shadow">
  div>
div>
.wrapper {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mr-2 {
  margin-right2em;
}

.mb-1 {
  margin-bottom1em;
}

.text-center {
  text-align: center;
}

.box-shadow {
  box-shadow2px 4px 8px #585858;
}

.drop-shadow {
  filterdrop-shadow(2px 4px 8px #585858);
}

对比效果:

3. 平滑滚动

无需 JavaScript 即可实现平滑滚动,只需一行 CSS:scroll-behavior: smooth;

<nav>
  Scroll to: 
  <a href="#sectionA" class="link bg-red">Aa>
  
  <a href="#sectionB" class="link bg-blue">Ba>
  
  <a href="#sectionC" class="link bg-green">Ca>
nav>

<div class="wrapper">
  <div id="sectionA" class="section bg-red">Adiv>
  
  <div id="sectionB" class="section bg-blue">Bdiv>
  
  <div id="sectionC" class="section bg-green">Cdiv>
div>
html {
  scroll-behavior: smooth;
}

nav {
  position: fixed;
  leftcalc(50vw - 115px);
  top0;
  width200px;
  text-align: center;
  padding15px;
  background#fff;
  box-shadow0 2px 5px 1px rgba(0000.2);
}

nav .link {
  padding5px;
  color: white;
}

.section {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color#fff;
  font-size5em;
  text-shadow:
    0px 2px 0px #b2a98f,
    0px 4px 3px rgba(0,0,0,0.15),
    0px 8px 1px rgba(0,0,0,0.1);
}

.bg-red {
  background#de5448;
}

.bg-blue {
  background#4267b2;
}

.bg-green {
  background#4CAF50;
}

实现效果:

4. 自定义光标

可以使用自定义图像,甚至表情符号来作为光标。

<div class="wrapper">
  <div class="tile">
    Default
  div>
  
  <div class="tile tile-image-cursor">
    Image
  div>
  
  <div class="tile tile-emoji-cursor">
    Emoji
  div>
div>
.wrapper {
  display: flex;
  height100vh;
  align-items: center;
  justify-content: center;
  background#4776e6;
  backgroundlinear-gradient(to right, #4776e6, #8e54e9);
  padding0 10px;
}

.tile {
   width200px;
   height200px;display: flex;
   align-items: center;
   justify-content: center;
   background-color#de5448;
   margin-right10px;color#fff;
   font-size1.4em;
   text-align: center;
  }

.tile-image-cursor {
   background-color#1da1f2;
   cursorurl(https://picsum.photos/20/20), auto;
}

.tile-emoji-cursor {
   background-color#4267b2;
   cursorurl("data:image/svg+xml;utf8,🚀"), auto;
}

实现效果:

5. 截断文本

一行文本溢出隐藏:

<div>
 白日依山尽,黄河入海流。欲穷千里目,更上一层楼。
div>
div {
  width200px;
  background-color#fff;
  padding15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

实现效果:

还可以使用“-webkit-line-clamp”属性将文本截断为特定的行数。文本将在截断的地方会显示省略号:

div {
  width200px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp2;
  overflow: hidden;
}

实现效果:

6. 自定义选中样式

CSS 伪元素::selection,可以用来自定义用户选中文档的高亮样式。

<div class="wrapper">
  <div>
    <p>
     默认高亮
    p>
    <p class="custom-highlighting">
      自定义高亮
    p>
  div>
div>
.wrapper {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

p {
  font-size2rem;
  font-family: sans-serif;
}

.custom-highlighting::selection {
  background-color#8e44ad;
  color#fff;
}

实现效果:

7. CSS 模态框

我们可以使用 CSS 中的 :target 伪元素来创建一个模态框。

<div class="wrapper">
    <a href="#demo-modal">Open Modala>
div>

<div id="demo-modal" class="modal">
    <div class="modal__content">
        <h1>CSS Modalh1>
        <p>hello worldp>
        <a href="#" class="modal__close">×a>
    div>
div>
.wrapper {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  backgroundlinear-gradient(to right, #834d9b, #d04ed6);
}

.wrapper a {
  display: inline-block;
  text-decoration: none;
  padding15px;
  background-color#fff;
  border-radius3px;
  text-transform: uppercase;
  color#585858;
  font-family'Roboto', sans-serif;
}

.modal {
  visibility: hidden;
  opacity0;
  position: absolute;
  top0;
  right0;
  bottom0;
  left0;
  display: flex;
  align-items: center;
  justify-content: center;
  backgroundrgba(777777, .7);
  transition: all .4s;
}

.modal:target {
  visibility: visible;
  opacity1;
}

.modal__content {
  border-radius4px;
  position: relative;
  width500px;
  max-width90%;
  background#fff;
  padding1em 2em;
}

.modal__close {
  position: absolute;
  top10px;
  right10px;
  color#585858;
  text-decoration: none;
}

实现效果:

8. 空元素样式

可以使用 :empty 选择器来设置完全没有子元素或文本的元素的样式:

<div class="wrapper">
  <div class="box">div>
  <div class="box">白日依山尽,黄河入海流。欲穷千里目,更上一层楼。div>
div>
.wrapper {
  height100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  display: inline-block;
  background#999;
  border1px solid #585858;
  height200px;
  width200px;
  margin-right15px;
}

.box:empty {
  background#fff;
}

实现效果:

9. 创建自定义滚动条

<div class="wrapper">
    <div>
      <div class="tile mr-1">
        <div class="tile-content">
          默认滚动条
        div>
      div>
      
      <div class="tile tile-custom-scrollbar">
        <div class="tile-content">
          自定义滚动条
        div>
      div>
    div>
div>
.wrapper {
  height100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mr-1 {
  margin-right1em;
}

.tile {
  overflow: auto;
  display: inline-block;
  background-color#ccc;
  height200px;
  width180px;
}

.tile-custom-scrollbar::-webkit-scrollbar {
  width12px;
  background-color#eff1f5;
}

.tile-custom-scrollbar::-webkit-scrollbar-track{
  border-radius3px;
  background-color: transparent;
}

.tile-custom-scrollbar::-webkit-scrollbar-thumb{
  border-radius:5px;
  background-color:#515769;
  border:2px solid #eff1f5
}

.tile-content {
  padding20px;
  height500px;
}

实现效果:

10. 动态工具提示

可以使用 CSS 函数 attr() 来创建动态的纯 CSS 工具提示 。

<h1>
  HTML/CSS tooltip
h1>
<p>
  Hover <span class="tooltip" data-tooltip="Tooltip Content">Herespan> to see the tooltip.
p>
<p>
  You can also hover <span class="tooltip" data-tooltip="This is another Tooltip Content">herespan> to see another example.
p>
.tooltip {
  position: relative;
  border-bottom1px dotted black;
}

.tooltip:before {
  contentattr(data-tooltip); 
  position: absolute;
  width100px;
  background-color#062B45;
  color#fff;
  text-align: center;
  padding10px;
  line-height1.2;
  border-radius6px;
  z-index1;
  opacity0;
  transition: opacity .6s;
  bottom125%;
  left50%;
  margin-left: -60px;
  font-size0.75em;
  visibility: hidden;
}

.tooltip:after {
  content"";
  position: absolute;
  bottom75%;
  left50%;
  margin-left: -5px;
  border-width5px;
  border-style: solid;
  opacity0;
  transition: opacity .6s;
  border-color#062B45 transparent transparent transparent;
  visibility: hidden;
}

.tooltip:hover:before
.tooltip:hover:after {
  opacity1;
  visibility: visible;
}

实现效果:

11. 圆形渐变边框

<div class="box gradient-border">
  炫酷渐变边框
div>
.gradient-border {
  border: solid 5px transparent;
  border-radius10px;
  background-imagelinear-gradient(white, white), 
    linear-gradient(315deg,#833ab4,#fd1d1d 50%,#fcb045);
  background-origin: border-box;
  background-clip: content-box, border-box;
}

.box {
  width350px;
  height100px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin100px auto;
}

实现效果:

12. 灰度图片

可以使用 grayscale() 过滤器功能将输入图像转换为灰度。

往期推荐

前端周刊 | 时隔8年,Chrome 发布全新 Logo; Vue 3 正式成为默认版本; Vite 2.8 发布

在 CSS 中隐藏元素的 10 种方法

Visual Studio Code 常用快捷键大全

实用 JavaScript 调试技巧

助力春招,盘点 2022 年最值得去的互联网公司!

结语

「关注公众号IQ前端,一个专注于CSS/JS开发技巧的前端公众号,更多前端小干货等着你喔」



浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报