搜尋
首頁web前端css教學國家自然科學基金委員會

昂CSS

oncss 是一個 CSS-in-JS 函式庫,為開發人員提供強大的 css 功能來設計 Web 應用程式的樣式。它支援現代樣式技術,包括嵌套選擇器、響應式設計和動態關鍵幀,同時提供與 React 等 JavaScript 框架的無縫整合。


安裝

透過 npm 安裝 oncss 套件:

npm install oncss

在專案中導入css函數:

import css from 'oncss';

核心概念:css函數

css 函數是 oncss 的核心,旨在動態產生 CSS 並將其註入到您的應用程式中。它支援:

  • CSS 屬性:使用標準 CSS 屬性和值。
  • 巢狀選擇器:使用 & 將樣式套用於子元素或狀態。
  • 媒體查詢:使用@media規則實作響應式設計。
  • 關鍵影格:使用@keyframes 建立動畫。
  • 全域樣式:使用@global全域應用樣式。
  • 自訂斷點:定義可重複使用的斷點以提高反應能力。
  • 其他 At 規則:利用其他 at 規則,例如 @container、@layer 和 @supports。

基本範例

const buttonStyles = css({
  backgroundColor: 'blue',
  color: 'white',
  padding: '10px 20px',
  borderRadius: '5px',
  '&:hover': {
    backgroundColor: 'darkblue',
  },
  '@media (min-width: 768px)': {
    padding: '15px 30px',
  },
});

console.log(buttonStyles);

配置選項

可以透過選項物件自訂CSS功能:

可用屬性

Property Type Description
classPrefix string Adds a prefix to generated class names.
breakpoints object Custom breakpoints for responsive designs.
aliases object Custom shorthand properties for CSS rules.
injectStyle boolean Controls whether styles are auto-injected.
skipProps function Filters out unwanted properties.
getValue function Transforms property values dynamically.
getProps function Customizes specific property handling.

帶選項的範例

npm install oncss

使用斷點

您可以使用樣式中定義的斷點來建立響應式設計:

import css from 'oncss';

反應集成

oncss 與 React 無縫整合。只需將產生的類別名稱傳遞給您的元件即可。

反應範例

const buttonStyles = css({
  backgroundColor: 'blue',
  color: 'white',
  padding: '10px 20px',
  borderRadius: '5px',
  '&:hover': {
    backgroundColor: 'darkblue',
  },
  '@media (min-width: 768px)': {
    padding: '15px 30px',
  },
});

console.log(buttonStyles);

進階功能

嵌套選擇器

將樣式套用於子元素或偽類:

const styles = css({
  fontSize: 16,
  padding: 10,
}, {
  classPrefix: 'myprefix',
  breakpoints: {
    sm: 480,
    md: 768,
    lg: 1024,
  },
});

媒體查詢

輕鬆加入響應式樣式:

const responsiveStyles = css({
  fontSize: 14,
  padding: {
    sm: 12,
    lg: 24
  },

}, {
  breakpoints: {
    sm: 480,
    md: 768,
    lg: 1024,
  },
});

關鍵影格

定義並使用動畫:

import React from 'react';
import css from 'oncss';

const buttonStyle = css({
  backgroundColor: 'green',
  color: 'white',
  padding: '10px 20px',
  borderRadius: '8px',
  '&:hover': {
    backgroundColor: 'darkgreen',
  },
});

function Button() {
  return <button classname="{buttonStyle.toString()}">Click Me</button>;
}

export default Button;

全球風格

輕鬆套用全域樣式:

const cardStyles = css({
  padding: '20px',
  border: '1px solid #ccc',
  '& h1': {
    fontSize: '24px',
    margin: 0,
  },
  '&:hover': {
    boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
  },
});

支援的 At 規則

oncss 支援各種 CSS at 規則,以增強您的樣式設計能力。以下是受支援的 at 規則清單及說明:

At-Rule Description
@media Used for applying styles based on media queries for responsive design.
@keyframes Defines animations that can be applied to elements.
@global Applies styles globally across the entire application.
@container Used for container queries to apply styles based on container size.
@layer Defines style layers to control the order of style application.
@supports Applies styles based on the support of specific CSS features in the browser.

伺服器端樣式

oncss 透過利用 CSSFactory 儲存和管理產生的 CSS 樣式來支援伺服器端渲染 (SSR)。這允許您提取樣式並將其註入到伺服器渲染的 HTML 中。

反應範例

以下是如何使用 oncss 透過 React 進行伺服器端渲染的範例:

npm install oncss

格式化CSS值

formatCSSValue 是一個實用函數,用於格式化 CSS 值,並在必要時添加 px 等單位。

import css from 'oncss';

TypeScript 集成

oncss 提供完整的 TypeScript 支持,讓您可以定義 CSS 屬性和選項的類型。

定義 CSS 屬性

您可以使用 CSSProps 類型定義 CSS 屬性的類型:

const buttonStyles = css({
  backgroundColor: 'blue',
  color: 'white',
  padding: '10px 20px',
  borderRadius: '5px',
  '&:hover': {
    backgroundColor: 'darkblue',
  },
  '@media (min-width: 768px)': {
    padding: '15px 30px',
  },
});

console.log(buttonStyles);

將選項與類型一起使用

您也可以為選項物件定義類型:

const styles = css({
  fontSize: 16,
  padding: 10,
}, {
  classPrefix: 'myprefix',
  breakpoints: {
    sm: 480,
    md: 768,
    lg: 1024,
  },
});

結論

oncss 簡化了現代 Web 應用程式的樣式。其強大的功能集,從響應式設計到關鍵影格動畫,使其成為開發人員的寶貴工具。

作者

國家自然科學基金委員會 納克斯魯爾·艾哈邁德
國家自然科學基金委員會 Naxrul Ahmed
GitHub Profile
npm Profile
Open Source Projects
GitHub 簡介 npm 簡介

開源專案

表>

⚡️ 哪裡可以找到我國家自然科學基金委員會 國家自然科學基金委員會國家自然科學基金委員會 國家自然科學基金委員會 國家自然科學基金委員會

以上是國家自然科學基金委員會的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
CSS動畫:很難創建它們嗎?CSS動畫:很難創建它們嗎?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@KeyFrames CSS:最常用的技巧@KeyFrames CSS:最常用的技巧May 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSS計數器:自動編號的綜合指南CSS計數器:自動編號的綜合指南May 07, 2025 pm 03:45 PM

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用捲軸驅動動畫的現代滾動陰影使用捲軸驅動動畫的現代滾動陰影May 07, 2025 am 10:34 AM

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。

重新訪問圖像圖重新訪問圖像圖May 07, 2025 am 09:40 AM

讓我們快速進修。圖像地圖一直返回到HTML 3.2,首先是服務器端地圖,然後使用映射和區域元素通過圖像上的單擊區域定義了可單擊區域。

DEV狀態:每個開發人員的調查DEV狀態:每個開發人員的調查May 07, 2025 am 09:30 AM

開發委員會調查現已開始參與,並且與以前的調查不同,它涵蓋了除法:職業,工作場所,以及健康,愛好等。 

什麼是CSS網格?什麼是CSS網格?Apr 30, 2025 pm 03:21 PM

CSS網格是創建複雜,響應式Web佈局的強大工具。它簡化了設計,提高可訪問性並提供了比舊方法更多的控制權。

什麼是CSS Flexbox?什麼是CSS Flexbox?Apr 30, 2025 pm 03:20 PM

文章討論了CSS FlexBox,這是一種佈局方法,用於有效地對齊和分佈響應設計中的空間。它說明了FlexBox用法,將其與CSS網格進行了比較,並詳細瀏覽了瀏覽器支持。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。