react 中css樣式_React中CSS
標簽: css vue react javascript html
react 中css樣式
Using React you have various ways to add styling to your components.
使用React,您可以通過多種方式向組件添加樣式。
使用類和CSS (Using classes and CSS)
The first and most simple is to use classes, and use a normal CSS file to target those classes:
首先也是最簡單的方法是使用類,并使用普通CSS文件將這些類作為目標:
const Button = () => {
return <button className="button">A button</button>
}
.button {
background-color: yellow;
}
You can import the stylesheet using an import statement, like this:
您可以使用import語句導入樣式表,如下所示:
import './style.css'
and Webpack will take care of adding the CSS property to the bundle.
Webpack將負責將CSS屬性添加到捆綁包中。
使用樣式屬性 (Using the style attribute)
A second method is to use the style
attribute attached to a JSX element. Using this approach you don’t need a separate CSS file.
第二種方法是使用附加到JSX元素的style
屬性。 使用這種方法,您不需要單獨CSS文件。
const Button = () => {
return <button style={{ backgroundColor: 'yellow' }}>A button</button>
}
CSS is defined in a slightly different way now. First, notice the double curly brackets: it’s because style
accepts an object. We pass in a JavaScript object, which is defined in curly braces. We could also do this:
CSS的定義現在略有不同。 首先,請注意兩個大括號:這是因為style
接受一個對象。 我們傳入一個JavaScript對象,該對象以花括號定義。 我們也可以這樣做:
const buttonStyle = { backgroundColor: 'yellow' }
const Button = () => {
return <button style={buttonStyle}>A button</button>
}
When using create-react-app
, those styles are autoprefixed by default thanks to its use of Autoprefixer.
使用create-react-app
,由于使用了Autoprefixer ,這些樣式在默認情況下會自動前綴 。
Also, the style now is camelCased instead of using dashes. Every time a CSS property has a dash, remove it and start the next word capitalized.
此外,現在樣式為駝峰式,而不是使用破折號。 每次CSS屬性帶有破折號時,請將其刪除,然后將下一個單詞大寫。
Styles have the benefit of being local to the component, and they cannot leak to other components in other parts of the app, something that using classes and an external CSS file can’t provide.
樣式的優點是對于組件來說是本地的,并且不會泄漏到應用程序其他部分的其他組件,這是使用類和外部CSS文件無法提供的。
使用CSS模塊 (Using CSS Modules)
CSS Modules seem to be a perfect spot in the middle: you use classes, but CSS is scoped to the component, which means that any styling you add cannot be applied to other components without your permission. And yet your styles are defined in a separate CSS file, which is easier to maintain than CSS in JavaScript (and you can use your good old CSS property names).
CSS模塊似乎是中間的一個完美選擇:您使用類,但是CSS限于組件的范圍,這意味著您添加的任何樣式都不能在未經您許可的情況下應用于其他組件。 但是,您的樣式是在單獨CSS文件中定義的,它比JavaScript中CSS易于維護(并且您可以使用舊CSS屬性名)。
Start by creating a CSS file that ends with .module.css
, for example Button.module.css
. A great choice is to give it the same name as the component you are going to style
首先創建一個以.module.css
結尾CSS文件,例如Button.module.css
。 最好的選擇是給它與要樣式化的組件相同的名稱
Add your CSS here, then import it inside the component file you want to style:
在此處添加CSS,然后將其導入要設置樣式的組件文件中:
import style from './Button.module.css'
now you can use it in your JSX:
現在您可以在JSX中使用它:
const Button = () => {
return <button className={style.content}>A button</button>
}
That’s it! In the resulting markup, React will generate a specific, unique class for each rendered component, and assign the CSS to that class, so that the CSS is not affecting other markup.
而已! 在生成的標記中,React將為每個渲染的組件生成一個特定的唯一類,并將CSS分配給該類,以使CSS不會影響其他標記。
react 中css樣式
智能推薦
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...
Linux C系統編程-線程互斥鎖(四)
互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...
統計學習方法 - 樸素貝葉斯
引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...
猜你喜歡
styled-components —— React 中的 CSS 最佳實踐
https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...
19.vue中封裝echarts組件
19.vue中封裝echarts組件 1.效果圖 2.echarts組件 3.使用組件 按照組件格式整理好數據格式 傳入組件 home.vue 4.接口返回數據格式...
【一只蒟蒻的刷題歷程】【藍橋杯】歷屆試題 九宮重排 (八數碼問題:BFS+集合set)
資源限制 時間限制:1.0s 內存限制:256.0MB 問題描述 如下面第一個圖的九宮格中,放著 1~8 的數字卡片,還有一個格子空著。與空格子相鄰的格子中的卡片可以移動到空格中。經過若干次移動,可以形成第二個圖所示的局面。 我們把第一個圖的局面記為:12345678. 把第二個圖的局面記為:123.46758 顯然是按從上到下,從左到右的順序記錄數字,空格記為句點。 本題目的任務是已知九宮的初態...