question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the best practice for styling heading?

See original GitHub issue

Hi guys, been trying styled-components for a few days. Soooo great!

BTW, I have something to ask here, Is this kind code is a good approach?

// @flow

import { rem } from 'polished';
import styled, { css } from 'styled-components';

const baseStyles = css`
  color: ${props => props.theme.colors.black || '#222'};
  line-height: 1.2;
`;

const H1 = styled.h1`
  ${baseStyles}
  font-size: ${rem('33px')};
`;

const H2 = styled.h2`
  ${baseStyles};
  font-size: ${rem('28px')};
`;

const H3 = styled.h3`
  ${baseStyles};
  font-size: ${rem('23px')};
`;

const H4 = styled.h4`
  ${baseStyles};
  font-size: ${rem('19px')};
`;

const Heading = {
  H1,
  H2,
  H3,
  H4,
};

module.exports = Heading;

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
ezeikelcommented, Feb 27, 2019

Using as and a “level” prop to have one Component for h1-h5

import styled from 'styled-components';

const StyledHeading = styled.h1`
  font-family: var(--heading-font-family);
  font-style: normal;
  color: var(--color-black);
`;

const Heading = ({ level, ...rest }) => {
  return (
    <StyledHeading
      as={`h${level}`}
      {...rest}
    />
  )
};

Heading.defaultProps = {
  level: 1
};

export default Heading;
13reactions
nathan-kansucommented, Feb 16, 2018

Just in case anyone stumbles on here in future, ‘extendWith’ has been renamed to ‘withComponent’. https://www.styled-components.com/docs/api#withcomponent

So this:

const h1 = BaseHeader.extendWith('h1')`
font-size: ${rem(33)};
`

Is now this:

const h1 = BaseHeader.withComponent('h1').extend`
  font-size: ${rem(33)};
`
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Heading Tags Best Practices - Medium
Best Practices Are Simple: · Make sure you use H1 Heading · Always Nest headings by their level. · Keep most important heading...
Read more >
Headings - Web Best Practices | Western Kentucky University
Heading best practices for the WKU website. Information about ordering, heading styles, and secondary heading text.
Read more >
5 cool CSS header styles with cross-browser compatibility
Useful tips for styling header texts · Pick the right colors · Make it unique · Use the right fonts · Check compatibility....
Read more >
14 CSS Best Practices for Beginners - Kinsta
Today, we'll highlight 14 CSS best practices for beginners, but even experienced professionals should brush up on the basics sometimes.
Read more >
6 CSS Best Practices to Follow in 2021 - X-Team
6 CSS Best Practices to Follow in 2021 · Keep It Organized · Make It Understandable · Keep It Short · Use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found