Centering Text and Image in React Horizontally

I'm attempting to achieve a layout similar to the one shown in the image below. Currently, my image is positioned at the top with the text below it. I would like to arrange it so that the text appears on the right side of the image.

Please take a look at this CodeSandbox link HERE

CODE

const drawer = (
    <div>
      <h2 className={classes.headerTitle}>Login</h2>
      <Divider />
      <div className={classes.headerIcon}>
        <AccountCircleIcon fontSize="large" />
      </div>
      <h5 className={classes.headerName}>Bake</h5>
      <p className={classes.headerRole}>User</p>
      <Divider />
    </div>
  );

Answer №1

When I needed to align my icon, name, and role in a row, simply adding display: "flex" to the children wasn't enough. Instead, I wrapped them in a parent div with the classes.wrapper that had properties like display: "flex", flexDirection:"row",

justifyContent: "center"
, and alignItems:"center". This allowed me to easily arrange these elements horizontally:

<div className="classes.wrapper">
  <div>This one is to the left</div>
  <div>This one is to the right</div>
</div>

However, keep in mind that if you add more divs under the one on the right, they will stack vertically because the flexDirection property only applies to direct children of the wrapper.

<div className="classes.wrapper">
  <div>This one is to the left</div>
  <div>
       <div>This one will appear above</div>
       <div>This one will be below</div>
  </div>
</div>

I also made some adjustments to the code. Here's an overview:

// Import necessary dependencies...
import React from "react";
// Add your custom styles using Material UI components...

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

  const drawer = (
    <>
      // Include content for the sidebar drawer...
      {drawer}
    </>
  );

  return (
    // Return the navigation component with responsive drawers...
  );
}

If you want to dive deeper into flexbox usage in CSS, I recommend checking out this comprehensive guide.

Answer №2

I have customized the layout by adjusting the text alignment and styling next to the icon. Additional styling can be applied:

import React from "react";
import { makeStyles } from "@material-ui/styles";
import Divider from "@material-ui/core/Divider";
import Drawer from "@material-ui/core/Drawer";
import Hidden from "@material-ui/core/Hidden";
import AccountCircleIcon from "@material-ui/icons/AccountCircle";
import "./styles.css";

const headerStyles = {
  display: "flex",
  justifyContent: "center"
};

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex"
  },
  headerTitle: {
    ...headerStyles,
    fontSize: "1.3rem",
    cursor: "pointer"
  },
  headerIcon: {
    ...headerStyles,
    marginTop: "1rem"
  },
  headerName: {
    ...headerStyles,
    marginTop: "0.2rem"
  },
  headerRole: {
    ...headerStyles,
    marginTop: "-0.8rem",
    marginBottom: "1rem"
  },
  iconButtons: {
    marginLeft: "auto"
  },
  userName: {
    display: "flex",
    flexDirection: "row"
  }
}));

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

  const drawer = (
    <div>
      <h2 className={classes.headerTitle}>Login</h2>
      <Divider />
      <div className={classes.userName}>
        <div className={classes.headerIcon}>
          <AccountCircleIcon fontSize="large" />
        </div>
        <div>
          <h5 className={classes.headerName}>Bake</h5>
          <p className={classes.headerRole}>User</p>
        </div>
      </div>
      <Divider />
    </div>
  );

  return (
    <nav className={classes.drawer}>
      <Hidden lgUp implementation="css">
        <Drawer
          variant="temporary"
          anchor={"left"}
          classes={{
            paper: classes.drawerPaper
          }}
          ModalProps={{
            keepMounted: true
          }}
        >
          {drawer}
        </Drawer>
      </Hidden>
      <Hidden implementation="css">
        <Drawer
          classes={{
            paper: classes.drawerPaper
          }}
          variant="permanent"
          open
        >
          {drawer}
        </Drawer>
      </Hidden>
    </nav>
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for implementing two functions to run within the onClick event handler in React

I am looking to simultaneously execute two functions handleClose and saveData within the onClick method. The specific location where I want this execution to happen: <Button variant="contained" onClick={saveData}&g ...

Ways to prevent a specific class from using CSS styling

My HTML <body> <div id="finalparent"> <!--many parent divs here--> <div id="1stparent"> <div id="txt_blog_editor" class="box" style="width: 1097px;"> <div class="abc anotherclass"> </div> & ...

Setting up a serverless next.js react application on AWS Lambda resulted in receiving the message: {"error": "Server encountered an internal problem"}

Recently, I attempted to deploy a serverless React.js Next application on AWS Lambda. Despite successful deployment in Node.js and receiving an AWS CloudFormation status of UPDATE_COMPLETE, I encountered an issue when trying to access the endpoint link. A ...

Make the table width 100%, but we don't want the central cell to be stretched

My table contains a central image cell that spans two rows: <table width="100%" style="text-align:center"> <tr> <td>Cell 1</td> <td>Cell 2</td> <td rowspan="2"><img src="http://www.skrenta.com/ima ...

The material UI styled component is not appearing as expected

I'm having trouble getting the MUI styled() utility to apply styles to <MyComponent>Styled div</MyComponent> in my index.jsx file. Any ideas why? import Button from '@mui/material/Button' import Grid from '@mui/mater ...

Is there a way to stop Bootstrap from automatically bolding the font?

When testing this simple example without the bootstrap link, everything seems to be working correctly: Hovering over any word changes its color to red, and when hovering away it returns to black. But as soon as the bootstrap link is included, the text bec ...

What are some creative ways to incorporate images into websites outside of a ReactJS environment?

I'm currently working on a ReactJS application that is client-side only and intended for local use. One of the requirements is to save and load images using filepaths, including URLs and local file system paths. While I am able to store paths in loca ...

Changing the alignment of divs to center instead of the right side

I am currently working on a project that involves creating a tiled interface with responsive tiles. One issue I have encountered is that all the tiles are shifting to the left side of the div and not getting centered, no matter what I try. To see the pro ...

"Fixing the cubic-bezier for the exiting animation ends up causing issues with the entering

Trying to implement a collapsing list animation using React/Joy-UI. Below is the Transition element code snippet: <Transition nodeRef={nodeRef} in={browseOpen} timeout={1000}> {(state: string) => (<List aria-labelledby="nav-list-bro ...

What is the best way to use Immer to update Zustand state when incorporating objects that are added through a controlled form using React-Hook-

Having some trouble with integrating Zustand and Immer using React-Hook-Form. My goal is to capture a series of values from a form, store them in a list, and allow for the addition of new objects to that list. In this scenario, the user inputs data for a ...

Is there a way to spin these hexagons around?

Hey there, I need some assistance with a problem. I have to rotate these three hexagons slightly, about 15 degrees or so. The catch is, it needs to work in Internet Explorer only. I've been struggling with this all day and it's getting ...

Issue with webpack failing to detect css-loading module

My application functions properly when using yarn, however, I encounter an issue with loading the CSS when incorporating a benchmarking library called react-benchmark, which utilizes webpack. You can find a minimal example of the entire project here: https ...

I am having trouble with node.js express not recognizing the path to my CSS files

My objective is to load information onto an HTML page using Node.js and Express. The issue I am facing is that when I try to open the main page (which displays all the books from the database), everything works smoothly - the CSS and JS files are located ...

Drag a spinning cube using CSS and Jquery when the mouse is pressed

I'm attempting to develop a dynamic rotating cube that users can manipulate to their desired side by clicking and dragging on the cube. Currently, I have functionality where the cube moves as the mouse cursor moves across the screen, but this results ...

Utilize the power of React and Framer Motion to create a visually stunning fade

After creating a preloader that appears when the variable "loading" is set to true, I now want the loader to fade out. This is an overview of my files: On the home page with all the content: return ( <> {loading ? ( ...

Ever thought about harnessing the power of SassDoc to create detailed documentation for your sass

After experimenting with sassdoc for documenting our sass mixins and functions, I realized that there doesn't seem to be a straightforward way to generate documentation for our sass variables. Specifically, I am looking for a solution that can output ...

Shade the entire td column in different colors depending on the characteristics of th

I am working with a table and here is the code for it: <table> <thead> <tr> <th style="width: 40%; text-align: center; vertical-align: center;">Nr.<br> crt.</th> <th style="font-weight: bold; wi ...

Eliminate parameter from URL

Here is the URL I am working with: http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860 My goal is to redirect to this URL: http://my.site#/homepage To achieve this, I use the following code snippet: import { push } from 'react-router-redux& ...

Issues with styled components not properly rendering styles

These are the two components I'm working with: export const Icon = styled.svg` width: 8px; height: 8px; background: black; ` export const Dots = styled.div` border: 2px solid green !important; height: 30px; background: white; width: 1 ...

Customize the focus and selected background color of Material UI Select component

I am trying to customize the background color of a Select component and its MenuItems. Specifically, I want to remove or override the default background color of the focused Select component and selected MenuItem. The current background color of the selec ...