首頁 > web前端 > css教學 > 主體

分享12個實用的 CSS 小技巧(快來收藏)

青灯夜游
發布: 2022-03-07 19:39:17
轉載
2308 人瀏覽過

這篇文章跟大家分享12個有趣又實用的 CSS 小技巧,讓前端開發更輕鬆,快來收藏吧,希望對大家有幫助!

分享12個實用的 CSS 小技巧(快來收藏)

(建議學習:css影片教學

1. 打字效果

程式碼實作:

<div class="wrapper">
    <div class="typing-demo">
      有趣且实用的 CSS 小技巧
    </div>
</div>
登入後複製
.wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.typing-demo {
  width: 22ch;
  animation: typing 2s steps(22), blink .5s step-end infinite alternate;
  white-space: nowrap;
  overflow: hidden;
  border-right: 3px solid;
  font-family: monospace;
  font-size: 2em;
}

@keyframes typing {
  from {
    width: 0
  }
}
    
@keyframes blink {
  50% {
    border-color: transparent
  }
}
登入後複製

實現效果:

分享12個實用的 CSS 小技巧(快來收藏)

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 {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mr-2 {
  margin-right: 2em;
}

.mb-1 {
  margin-bottom: 1em;
}

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

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

.drop-shadow {
  filter: drop-shadow(2px 4px 8px #585858);
}
登入後複製

對比效果:

分享12個實用的 CSS 小技巧(快來收藏)

##3. 平滑滾動

無需JavaScript 即可實現平滑捲動,只需一行CSS:scroll-behavior: smooth;

<nav>
  Scroll to: 
  <a href="#sectionA" class="link bg-red">A</a>
  
  <a href="#sectionB" class="link bg-blue">B</a>
  
  <a href="#sectionC" class="link bg-green">C</a>
</nav>

<div class="wrapper">
  <div id="sectionA" class="section bg-red">A</div>
  
  <div id="sectionB" class="section bg-blue">B</div>
  
  <div id="sectionC" class="section bg-green">C</div>
</div>
登入後複製
html {
  scroll-behavior: smooth;
}

nav {
  position: fixed;
  left: calc(50vw - 115px);
  top: 0;
  width: 200px;
  text-align: center;
  padding: 15px;
  background: #fff;
  box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);
}

nav .link {
  padding: 5px;
  color: white;
}

.section {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 5em;
  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;
  height: 100vh;
  align-items: center;
  justify-content: center;
  background: #4776e6;
  background: linear-gradient(to right, #4776e6, #8e54e9);
  padding: 0 10px;
}

.tile {
    width: 200px;
    height: 200px;display: flex;
    align-items: center;
    justify-content: center;
    background-color: #de5448;
    margin-right: 10px;color: #fff;
    font-size: 1.4em;
    text-align: center;
  }

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

.tile-emoji-cursor {
  background-color: #4267b2;
  cursor: url("data:image/svg+xml;utf8,<svg xmlns=&#39;http://www.w3.org/2000/svg&#39;  width=&#39;40&#39; height=&#39;48&#39; viewport=&#39;0 0 100 100&#39; style=&#39;fill:black;font-size:24px;&#39;><text y=&#39;50%&#39;>?</text></svg>"), auto;
}
登入後複製

實作效果:

分享12個實用的 CSS 小技巧(快來收藏)

5. 截斷文字

一行文字溢位隱藏:

<div>
	白日依山尽,黄河入海流。欲穷千里目,更上一层楼。
</div>
登入後複製
div {
  width: 200px;
  background-color: #fff;
  padding: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
登入後複製

實作效果:

分享12個實用的 CSS 小技巧(快來收藏)

也可以使用「-webkit-line-clamp」屬性將文字截斷為特定的行數。文字會在截斷的地方會顯示省略號:

div {
  width: 200px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
登入後複製

實現效果:

5-分享12個實用的 CSS 小技巧(快來收藏)

#6. 自訂選取樣式

CSS 偽元素::selection,可以用來自訂使用者選取文件的高亮樣式。

<div class="wrapper">
  <div>
    <p>
     默认高亮
    </p>
    <p class="custom-highlighting">
      自定义高亮
    </p>
  </div>
</div>
登入後複製
.wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

p {
  font-size: 2rem;
  font-family: sans-serif;
}

.custom-highlighting::selection {
  background-color: #8e44ad;
  color: #fff;
}
登入後複製

實現效果:

分享12個實用的 CSS 小技巧(快來收藏)

6-分享12個實用的 CSS 小技巧(快來收藏)

#7. CSS 模態方塊

我們可以使用CSS中的:target 偽元素來建立一個模態框。

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

<div id="demo-modal" class="modal">
    <div class="modal__content">
        <h1>CSS Modal</h1>
        <p>hello world</p>
        <a href="#" class="modal__close">&times;</a>
    </div>
</div>
登入後複製
.wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to right, #834d9b, #d04ed6);
}

.wrapper a {
  display: inline-block;
  text-decoration: none;
  padding: 15px;
  background-color: #fff;
  border-radius: 3px;
  text-transform: uppercase;
  color: #585858;
  font-family: &#39;Roboto&#39;, sans-serif;
}

.modal {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(77, 77, 77, .7);
  transition: all .4s;
}

.modal:target {
  visibility: visible;
  opacity: 1;
}

.modal__content {
  border-radius: 4px;
  position: relative;
  width: 500px;
  max-width: 90%;
  background: #fff;
  padding: 1em 2em;
}

.modal__close {
  position: absolute;
  top: 10px;
  right: 10px;
  color: #585858;
  text-decoration: none;
}
登入後複製

實作效果:

分享12個實用的 CSS 小技巧(快來收藏)

8. 空元素樣式

可以使用:empty 選擇器來設定完全沒有子元素或文本的元素的樣式:

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

.box {
  display: inline-block;
  background: #999;
  border: 1px solid #585858;
  height: 200px;
  width: 200px;
  margin-right: 15px;
}

.box:empty {
  background: #fff;
}
登入後複製

實作效果:

分享12個實用的 CSS 小技巧(快來收藏)

#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 {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mr-1 {
  margin-right: 1em;
}

.tile {
  overflow: auto;
  display: inline-block;
  background-color: #ccc;
  height: 200px;
  width: 180px;
}

.tile-custom-scrollbar::-webkit-scrollbar {
  width: 12px;
  background-color: #eff1f5;
}

.tile-custom-scrollbar::-webkit-scrollbar-track{
  border-radius: 3px;
  background-color: transparent;
}

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

.tile-content {
  padding: 20px;
  height: 500px;
}
登入後複製

實作效果:

分享12個實用的 CSS 小技巧(快來收藏)

10. 動態工具提示

可以使用CSS 函數attr() 來建立動態的純CSS 工具提示。

<h1>
  HTML/CSS tooltip
</h1>
<p>
  Hover <span class="tooltip" data-tooltip="Tooltip Content">Here</span> to see the tooltip.
</p>
<p>
  You can also hover <span class="tooltip" data-tooltip="This is another Tooltip Content">here</span> to see another example.
</p>
登入後複製
.tooltip {
  position: relative;
  border-bottom: 1px dotted black;
}

.tooltip:before {
  content: attr(data-tooltip); 
  position: absolute;
  width: 100px;
  background-color: #062B45;
  color: #fff;
  text-align: center;
  padding: 10px;
  line-height: 1.2;
  border-radius: 6px;
  z-index: 1;
  opacity: 0;
  transition: opacity .6s;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  font-size: 0.75em;
  visibility: hidden;
}

.tooltip:after {
  content: "";
  position: absolute;
  bottom: 75%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  opacity: 0;
  transition: opacity .6s;
  border-color: #062B45 transparent transparent transparent;
  visibility: hidden;
}

.tooltip:hover:before, 
.tooltip:hover:after {
  opacity: 1;
  visibility: visible;
}
登入後複製

實現效果:

分享12個實用的 CSS 小技巧(快來收藏)

10-分享12個實用的 CSS 小技巧(快來收藏)

#11. 圓形漸層邊框

<div class="box gradient-border">
  炫酷渐变边框
</div>
登入後複製
.gradient-border {
  border: solid 5px transparent;
  border-radius: 10px;
  background-image: linear-gradient(white, white), 
    linear-gradient(315deg,#833ab4,#fd1d1d 50%,#fcb045);
  background-origin: border-box;
  background-clip: content-box, border-box;
}

.box {
  width: 350px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 100px auto;
}
登入後複製
# 實作效果:

1分享12個實用的 CSS 小技巧(快來收藏)

12. 灰階圖片

可以使用grayscale() 濾鏡功能將輸入影像轉換為灰階。

1分享12個實用的 CSS 小技巧(快來收藏)

(學習影片分享:

web前端入門教學

以上是分享12個實用的 CSS 小技巧(快來收藏)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
css
來源:juejin.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!