Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8938

Gatsb:CSSファイルの置き場所とインポート(gatsby-browser.js)

$
0
0

Gatsb:CSSファイルの置き場所とインポート(gatsby-browser.js)

自分で作成したCSSの作り方は以下の通り。React特有の書き方…。

// index.jsimportReactfrom"react"import{Link}from"gatsby"importLayoutfrom"../components/layout"importImagefrom"../components/image"importSEOfrom"../components/seo"import{Container,Row,Col,Badge,Accordion,Card,Button}from'react-bootstrap'consth1Size={     // 自分でつけた任意の名前fontSize:'1.25rem',// font-sizeとするところfontSizeとする。React特有の書き方lineHeight:1.75// line-heightとするところlineHeight}consth2Size={marginTop:'1.25rem',fontSize:'1.1rem',color:'#999999',lineHeight:1.75}constfontSmallCrimson={color:'crimson',fontSize:'small'}constfontSmall={fontSize:'small',}constfontCrimson={color:'crimson',}constfontGray={color:'#777777',}constlineSpace={marginTop:20,}constIndexPage=()=>(<Layout><SEOtitle="ギャツビー"/><Containerfluid="md"><h1style={h1Size}>ギャツビー</h1>・・・以下略・・・

ファイル分割したい

デフォルトで存在するcomponents/layout.cssをいじってもいいのだが…別のファイルに自作CSSを分けたいと思った。

  1. まずsrc/stylesフォルダを作成。
  2. stylesフォルダの下にglobal.cssを作成。
  3. global.cssに分けたいCSSを記述

styles/global.css

h1Size{font-size:1.25rem;line-height:1.75;}h2Size{margin-top:1.25rem;font-size:1.1rem;color:#999999;line-height:1.75;}fontSmallCrimson{color:crimson;font-size:small;}fontSmall{font-size:small;}fontCrimson{color:crimson;}fontGray{color:#777777;}divLineSpace{margin-top:20;}
  1. gatsby-browser.jsに以下のように記述
import"./src/styles/global.css"

以上。
index.jsなどの表示コンポーネントにimport文はいらない。



参考:
Adding global styles without a layout component


Viewing all articles
Browse latest Browse all 8938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>