✨
Help-a-Family Manual
  • Help a Family Docs
  • Info for Charities
  • Info for API Users
  • Tutorial
  • Contentful
  • Environment Variables
    • Lighthouse CI
  • Troubleshooting
  • Contributing to Help-a-Family
    • Fork and Clone the Repo
    • MHF Javascript/React Style Guide
    • MHF CSS Guide
    • Contributor Power Tips
Powered by GitBook
On this page
  • CSS in JS
  • Styled JSX
  • Writing CSS
  • Scoping CSS

Was this helpful?

  1. Contributing to Help-a-Family

MHF CSS Guide

PreviousMHF Javascript/React Style GuideNextContributor Power Tips

Last updated 3 years ago

Was this helpful?

CSS in JS

  • Avoid the inline style attribute.

// DO NOT DO THIS
<button className="btn" style={{margin: '10px'}} />
  • Avoid css modules unless you are working on a whole page.

// DO NOT DO THIS... most of the time
<button className={styles.btn} />
  • Use the styled jsx library and follow .

// CORRECT!
<button className="btn" />
<style jsx>{`.btn {...}`}</style>

Styled JSX

Styled JSX is a small library built into Next.js. It's built and maintained by Vercel and therefor integrates nicely. There's a small learning curve to using it. Here are some basics.

Using the library

Implementing a styled jsx element is simple. You simply insert this block:

<style jsx>{``}</style>

into your React Component in your return area, like so.

const MyComponent = () => {
    return (
    <div>
        <input />
        <buton />
        <style jsx>{``}</style>
    </div>
}

You don't have to import the library, you can just use it out of the box.

Writing CSS

const MyComponent = () => {
    return (
    <div className="my-wrapper">
        <input className="my-input" />
        <buton className="my-btn" />
        <style jsx>{`
            .my-wrapper {
                height: 100vh;
            }
            .my-input {
                padding: 10px;
            }
            .my-btn {
                background: salmon;
            }
        `}</style>
    </div>
}

Scoping CSS

Styled JSX is scoped, so you can be more free with your class naming, just make sure it stays scoped in the case of sub components or a wrapper component.

<div id="container">
  <div className="my-class">
    // MyComponent is considered scoped to this container
    <MyComponent />
  </div>

  <styled jsx>`
  .my-class {
    height: 100vh;
  }
  `</styled>
</div>

Note the back ticks. Styled JSX uses something called template strings. Similar to what you would see in a GraphQL call if you've ever used that. So similarly, there's a handy to help see your CSS in the style JSX block with color and linting enabled.

So now we can simply insert class names and in the styled JSX element we write our CSS the way we would in any CSS file. Again, the does wonders here and I would highly recommend you use it.

these conventions
VScode extension
VScode extension