{"id":22876675,"url":"https://github.com/ceteio/styled-components-rhythm","last_synced_at":"2025-08-14T05:16:41.104Z","repository":{"id":30967676,"uuid":"126269741","full_name":"ceteio/styled-components-rhythm","owner":"ceteio","description":"Vertical Rhythm and Font Baselines with Styled Components","archived":false,"fork":false,"pushed_at":"2021-11-10T04:55:52.000Z","size":93,"stargazers_count":80,"open_issues_count":4,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-06T21:27:33.519Z","etag":null,"topics":["bsaeline","css","css-in-js","font","styled-components","vertical-rhythm"],"latest_commit_sha":null,"homepage":"https://medium.com/cete/vertical-rhythm-and-font-baselines-with-styled-components-5b99b40050f0","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ceteio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-22T02:43:51.000Z","updated_at":"2024-05-30T07:32:31.000Z","dependencies_parsed_at":"2022-08-07T16:00:30.396Z","dependency_job_id":null,"html_url":"https://github.com/ceteio/styled-components-rhythm","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/ceteio/styled-components-rhythm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceteio%2Fstyled-components-rhythm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceteio%2Fstyled-components-rhythm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceteio%2Fstyled-components-rhythm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceteio%2Fstyled-components-rhythm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ceteio","download_url":"https://codeload.github.com/ceteio/styled-components-rhythm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceteio%2Fstyled-components-rhythm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270364971,"owners_count":24571423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bsaeline","css","css-in-js","font","styled-components","vertical-rhythm"],"created_at":"2024-12-13T15:27:10.929Z","updated_at":"2025-08-14T05:16:41.035Z","avatar_url":"https://github.com/ceteio.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./img/rhythm-12px.png\" height=\"386\"\u003e\n  \u003cp align=\"center\"\u003e\u003ci\u003e12px vertical rhythm with correctly aligned baselines\u003c/i\u003e\u003cp\u003e\n  \u003ch1 align=\"center\"\u003eStyled Components Rhythm\u003c/h1\u003e\n  \u003cp align=\"center\"\u003eBeautifully aligned type with Styled Components\u003cp\u003e\n\u003c/p\u003e\n\n## Installation\n\n```\nnpm i @ceteio/styled-components-rhythm\n```\nor, with yarn:\n\n```\nyarn add @ceteio/styled-components-rhythm\n```\n\n## Usage\n\n```javascript\nimport { ThemeProvider, injectGLobal }, styled from 'styled-components';\nimport styledComponentsRhythm from '@ceteio/styled-components-rhythm';\n\nconst rhythm = styledComponentsRhythm({\n  baseFontSize: 1, // 1rem. Browser default makes this 16px\n  baseLineHeight: 1.2, // unitless line-height, see: https://css-tricks.com/almanac/properties/l/line-height/#comment-1587658\n  rhythmHeight: 0.75, // rem units. With browser default, this is 16px * 0.75rem == 12px\n  capHeights: {\n    // Calculated with https://codepen.io/sebdesign/pen/EKmbGL?editors=0011\n    Lato: 0.72,\n  },\n  debug: true,\n});\n\ninjectGLobal`\n  /* Reset global margins and padding */\n  h1, p {\n    margin: 0;\n    padding: 0;\n  }\n\n  /* Using Lato font https://fonts.google.com/specimen/Lato */\n  @import url('https://fonts.googleapis.com/css?family=Lato')\n\n  ${rhythm.global()}\n`;\n\nconst H1 = styled.h1`\n  ${props =\u003e props.theme.setFontWithRhythm('Lato', 3)}\n  margin-top: ${props =\u003e props.theme.rhythmSizing(3)}rem;\n`;\n\nconst Paragraph = styled.p`\n  ${props =\u003e props.theme.setFontWithRhythm('Lato', 1)}\n  margin-top: ${props =\u003e props.theme.rhythmSizing(2)}rem;\n`;\n\nexport default () =\u003e (\n  \u003cThemeProvider theme={rhythm.theme}\u003e\n    \u003cH1\u003eHello world\u003c/H1\u003e\n    \u003cParagraph\u003eHow are you today?\u003c/Paragraph\u003e\n    \u003cParagraph\u003eFeeling quite \u003cem\u003ealigned\u003c/em\u003e!\u003c/Paragraph\u003e\n  \u003c/ThemeProvider\u003e\n);\n```\n\n## API\n\n### Creating Rhythm\n\nThe main export is a function which returns a rhythm object suitable for\nadding to a Styled Components `\u003cThemeProvider\u003e`:\n\n```javascript\nimport styledComponentsRhythm from '@ceteio/styled-components-rhythm';\n\nconst rhythm = styledComponentsRhythm(options);\n// { theme: ..., global: ... }\n```\n\n#### `options` (`Object`)\n\n- `baseFontSize` (`Number`): The `rem` font size of your root element (ie; the `\u003cbody\u003e`).\n- `rhythmHeight` (`Number`): The `rem` vertical grid size, to which text will have its baseline aligned. Works best when it divides evenly into (`baseFontSize` * `defaultLineHeight`).\n- `capHeights` (`Object`): Map of `font-family` font name -\u003e proportional height of capital letters. Heights can be calculated with [this tool](https://codepen.io/sebdesign/pen/EKmbGL?editors=0011).\n  For example:\n  ```javascript\n  {\n    Lato: 0.72,\n  }\n  ```\n- `defaultLineHeight` (`Number`): Default for `setFontWithRhythm()` below. Must be a unitless value, which will be [relative to the font size of an element](https://css-tricks.com/almanac/properties/l/line-height/#comment-1587658).\n- `debug` (`Boolean`): Will inject red horizontal lines to body for visually debugging alignments.\n- `defaultOutputType` (`String`): Set the output type of `setFontWithRhythm()` \u0026 `global()`. `'string'`: Return a css string . `'object'`: Return a css style object. Default: `'string'`.\n\n### Setting the theme\n\nThere are two pieces to the puzzle, both of which must be used to have effective\nvertical rhythm; `rhythm.theme`, and `rhythm.global`:\n\n```javascript\nimport styledComponentsRhythm from '@ceteio/styled-components-rhythm';\nimport { injectGlobal, ThemeProvider } from 'styled-components';\n\nconst rhythm = styledComponentsRhythm(options);\n\ninjectGlobal`${rhythm.global()}`;\nreturn \u003cThemeProvider theme={rhythm.theme}\u003e...\u003c/ThemeProvider\u003e;\n```\n\n#### `theme` (`Object`)\n\nPass this object to a Styled Components `ThemeProvider` as the [theme](https://www.styled-components.com/docs/advanced#theming):\n\n```javascript\nconst rhythm = styledComponentsRhythm(options);\n\nreturn \u003cThemeProvider theme={rhythm.theme}\u003e...\u003c/ThemeProvider\u003e;\n```\n\n#### `global([outputType]) =\u003e String`\n\nA string containing global CSS that needs to be applied. Best done using\nstyled-component's `injectGlobal`:\n\n```javascript\nconst rhythm = styledComponentsRhythm(options);\n\ninjectGlobal`${rhythm.global()}`;\n```\n\nor Emotion's `\u003cGlobal\u003e` component:\n\n```javascript\n\u003cGlobal styles={rhythm.global('object')} /\u003e\n```\n\n**Parameters**\n\n- `outputType` (`String`): `'string'`: Return a css string. `'object'`: Return a\n  css style object. Default: the value of `defaultOutputType`.\n\n### Using the theme values\n\nYou now have access to the following via the `theme` prop within a styled\ncomponent:\n\n#### `rhythmHeight`\n\nThe value as passed when creating the theme object.\n\n#### `setFontWithRhythm(fontName, fontSizeRem[, desiredLineHeight[, outputType]]) =\u003e String`\n\nThe main function which will generate the CSS necessary to correctly align the\nfont to a rhythm baseline.\n\nThis function makes 2 assumptions:\n\n1. All previous elements on the page are also correctly aligned to your vertical\n   rhythm.\n2. You will not manually set `padding-top` or `margin-bottom` on this element.\n\n**Parameters**\n\n- `fontName` (`String`): Should match the font name as you would declare it in the CSS\n  property `font-family`.\n- `fontSizeRem` (`Number`): A multiple of `baseFontSize`.\n- `desiredLineHeight` (`Number`): Will be rounded to the nearest rhythm line so you\n  don't have to worry.\n- `outputType` (`String`): `'string'`: Return a css string. `'object'`: Return a\n  css style object. Default: the value of `defaultOutputType`.\n\nThe output is the CSS string to add to the component:\n\n```javascript\nconst H1 = styled.h1`\n  ${props =\u003e props.theme.setFontWithRhythm('Lato', 3)}\n`;\n```\n\nOr as an object using the css prop (in both Styled Components \u0026 Emotion):\n\n```javascript\nconst H1 = props =\u003e (\n  \u003ch1 css={theme =\u003e theme.setFontWithRhythm('Lato', 3, 1, 'object')} /\u003e\n);\n```\n\n#### `rhythmSizing(multiple) =\u003e Number`\n\nA simple helper to calculate `multiple * rhythmHeight`.\n\nWorks great for setting margins or padding:\n\n```javascript\nconst H1 = styled.h1`\n  margin-top: ${props =\u003e props.theme.rhythmSizing(3)}rem;\n`;\n```\n\n## Related Projects\n\n- [`capsize`](https://seek-oss.github.io/capsize/) by Seek: _\"Using font metadata, text can now be sized according to the height of its capital letters while trimming the space above capital letters and below the baseline.\"_\n- [`basekick`](https://github.com/michaeltaranto/basekick) by [Michael Taranto](https://mobile.twitter.com/michaeltaranto) is another implementation of the same thing, targeted at LESS.\n- https://www.w3.org/TR/css-rhythm-1/ is a proposal to support vertical rhythm directly in CSS.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceteio%2Fstyled-components-rhythm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceteio%2Fstyled-components-rhythm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceteio%2Fstyled-components-rhythm/lists"}