Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Create a Horizontal Divider with Text In between

I need to create a React component that is a Horizontal Divider with a content like text In between. All the resources I have online is unable to help me get this done. I tried a material-ui divider by creating a Divider component and placing my text in-between like the example below:

<Divider>Or</Divider>

But I get the error:

hr is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.

I need to achieve this in the image below:

Display a horizontal Divider

Any help will be appreciated.

These are my codes below:

 import React from 'react';
 import { makeStyles } from '@material-ui/core/styles';
 import List from '@material-ui/core/List';
 import Divider from '@material-ui/core/Divider';

 const useStyles = makeStyles((theme) => ({
   root: {
   width: '100%',
   maxWidth: 360,
   backgroundColor: theme.palette.background.paper,
 },
 }));

 export default function ListDividers() {
 const classes = useStyles();

 return (
 <List component="nav" className={classes.root} aria-label="mailbox 
 folders">

  <Divider>Or</Divider>

  </List>
  );
 }
like image 877
Joe Avatar asked May 11 '20 13:05

Joe


People also ask

How do you add a horizontal line in React?

Use the <hr /> tag and set the style prop on it. Set the height of the line and optionally set backGroundColor and color .

How do you use hr tag in React?

To draw a red horizontal line in React, we can add an hr element that has a height and background color. We create the ColoredLine component that takes the color prop. And we set the style prop to an object with the color , backgroundColor and the height properties. backgroundColor makes the line set to color .

What is import {} In React?

Introduction. Importing and exporting in React JS will help us write modular code, i.e., splitting code into multiple files. Importing allows using contents from another file, whereas exporting makes the file contents eligible for importing.


3 Answers

Using Material UI.

import React from "react";
import { makeStyles } from "@material-ui/core";

const useStyles = makeStyles(theme => ({
  container: {
    display: "flex",
    alignItems: "center"
  },
  border: {
    borderBottom: "2px solid lightgray",
    width: "100%"
  },
  content: {
    paddingTop: theme.spacing(0.5),
    paddingBottom: theme.spacing(0.5),
    paddingRight: theme.spacing(2),
    paddingLeft: theme.spacing(2),
    fontWeight: 500,
    fontSize: 22,
    color: "lightgray"
  }
}));

const DividerWithText = ({ children }) => {
 const classes = useStyles();
 return (
  <div className={classes.container}>
    <div className={classes.border} />
    <span className={classes.content}>{children}</span>
    <div className={classes.border} />
  </div>
 );
};
export default DividerWithText;

You can import and use it like the below

<DividerWithText>Or</DividerWithText>

Result material ui divider with text at center

like image 61
DiaMaBo Avatar answered Sep 25 '22 19:09

DiaMaBo


Here a custom example of what you could do : repro on stackblitz

import React, { Component } from "react";
import { render } from "react-dom";
import Hello from "./Hello";
import "./style.css";

const App = () => {
  return <Divider>Or</Divider>;
};

const Divider = ({ children }) => {
  return (
    <div className="container">
      <div className="border" />
      <span className="content">
        {children}
      </span>
      <div className="border" />
    </div>
  );
};

render(<App />, document.getElementById("root"));

And the CSS:

.container{
  display: flex;
  align-items: center;
}

.border{
  border-bottom: 1px solid black;
  width: 100%;
}

.content {
  padding: 0 10px 0 10px;
}
like image 27
Quentin Grisel Avatar answered Sep 25 '22 19:09

Quentin Grisel


Update 29/03/2022

That's now possible with Material UI 🔥

https://mui.com/components/dividers/#dividers-with-text


You may want different spacing sometime

<Divider spacing={1}>Hello World</Divider>

Or

<Divider spacing={2}>Hello World</Divider>

For a configurable spacing here a Github Gist

Or a playground in codesandbox if you prefer

enter image description here

like image 23
Mamadou Ouologuem Avatar answered Sep 24 '22 19:09

Mamadou Ouologuem