Center

Centres its content horizontally and vertically.

import { ... } from '@corndev/ui'

No results yet

HTML

No JavaScript required — these classes are the whole component.

<div class="corn-center" data-fill>Centred</div>

Classes

Class
.corn-center

Attributes

AttributeValues
data-inlinepresent or absent
data-fillpresent or absent

Vanilla JS

Optional. createCenter only adds and removes the classes and attributes above, so the HTML is equivalent. Reach for it when you need to change options at runtime.

import { createCenter } from '@corndev/ui';
import '@corndev/ui/theme';

const el = document.querySelector('#target');
const instance = createCenter(el, { /* options */ });

Options

OptionTypeDefaultDescription
inlinebooleanfalseCentre inside a line of text or a table cell rather than as a block.
fillbooleanfalseFill the parent, which is what “centre this on the page” usually means.

React

Center — from @corndev/ui-react.

Props

PropTypeDefaultDescription
inlinebooleanfalseCentre inside a line of text or a table cell rather than as a block.
fillbooleanfalseFill the parent — usually what “centre this on the page” means.
asstring|React.ComponentType'div'Element or component to render as.
classNamestringundefinedMerged with the Corn UI classes.
childrenReact.ReactNodeundefined
import { Center } from '@corndev/ui-react';
import '@corndev/ui/theme';

<Center fill>
  <Spinner />
</Center>

fill makes it fill its parent, which is what “centre this on the page” almost always means — without it the box is only as tall as its content, and centring vertically inside it does nothing visible. That is the one thing people get wrong here.

inline centres inside a line of text or a table cell instead.

Vanilla

<div class="corn-center" data-fill>…</div>
import { createCenter } from '@corndev/ui';

createCenter(document.querySelector('#empty-state'), { fill: true });