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

https://i.sstatic.net/qfcvF.jpg

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

Gaining a comprehensive understanding of media queries

Hello everyone, I need some help with media queries. I've been working on a website that is already responsive. However, I am required to write the code in a child theme rather than directly in the original files. When I attempt to add new media quer ...

Creating visually appealing tables in React.js

Having trouble with table rendering in React. The buttons in the table cell are not styled properly and do not center within their div. Additionally, the table border is cut off where there are buttons or empty headers. Need help figuring out what's g ...

Using setState within a promise in React does not function correctly, resulting in undefined state

The issue arises when retrieving links from photos stored in Firebase storage. The links are retrieved in an array (arrayurl) and everything seems to be working fine (console.log(arrayurl) displays the correct links). However, when I update the state wit ...

What is the best way to center text vertically in a <div> using CSS?

This is my first attempt at creating a chrome extension, and I am currently working on centering text. Right now, I am using text-align: centre; to align the text horizontally, but I am struggling to figure out how to align it vertically. As a result, my t ...

Arrangement of images in an array

Here's the scenario I'm facing. https://i.stack.imgur.com/FbAfw.jpg So, I have an array of images that I want to use to create a gallery with a specific layout. I've tried using grid and playing around with :nth-child(even) and :nth-child( ...

The issue arises in React when input elements fail to render correctly following a change in value, specifically when the keys remain identical

Click here to view the code sandbox showcasing the issue The code sandbox demonstrates two versions - a working one where Math.random() is used as the key, and a not working one where the index of the array is used as the key. When the array this.state.v ...

Can you explain the different types of dynamic page props available in a Next.js application?

Is there a specific type available in Next.js 14 that I can use to replace the "any" type in the TypeScript code snippet below, for my dynamic route? export default async function DetailProduct({ params }: any) { const { imageAddress, title, price } = ...

Grouping multiple buttons together in a ButtonGroup

Currently, while working with React 16.9.0 and Material-UI 4.4.2, there is a particular issue that I am facing. The requirement is to render a ButtonGroup containing Button elements which are generated from custom components. These custom components retur ...

Chrome's spinner fails to appear when performing a synchronous ajax request in IE9

While implementing an ajax request, I want my users to see a spinner for visual feedback. Surprisingly, the spinner works flawlessly on Firefox 13, but fails to function on Chrome and IE9 even though the ajax request takes approximately 1.2 seconds to comp ...

Maximizing the efficiency of enums in a React TypeScript application

In my React application, I have a boolean called 'isValid' set like this: const isValid = response.headers.get('Content-Type')?.includes('application/json'); To enhance it, I would like to introduce some enums: export enum Re ...

When should you use isRequired for PropType versus defaultProps in a React application?

I often find myself confused about when to use .isRequired versus .defaultProps={...}. I personally feel that I should avoid using isRequired, as it seems like creating a potential bug in the application. By using isRequired, it automatically removes the n ...

ReactJS encountered an error: _this3.onDismissID is not defined as a function

My goal is to retrieve the latest news related to a specific search term from a website, showcase them, and provide a dismiss button next to each news item for users to easily remove them if desired. Here's a snippet of the code I'm using: import ...

Firefox showing inconsistent jQuery positioning results

Here is a fiddle I created to demonstrate the issue at hand... https://jsfiddle.net/scottieslg/q78afsu8/10/ Running this fiddle in Chrome or Opera yields a value of 8. However, Firefox returns 9, and IE gives 8.5. How can I ensure consistency across al ...

Outlook for Windows automatically sets the default font for HTML emails to Times New Roman

My email design looks great in Mac Outlook, but Windows Outlook is changing the font to Times New Roman. Below is the code for the email template. Can someone help me figure out what I'm missing? <style style="-ms-text-size-adjust: 100%;&q ...

Seeking a solution for resizing the Facebook plugin comments (fb-comments) using Angular when the window is resized

Is it possible to dynamically resize a Facebook plugin comment based on the window or browser size? I want the fb-comment div to automatically adjust its size to match the parent element when the browser window is resized. <div id="socialDiv" class="c ...

Material UI: Display the avatar and text side by side within the initial column of the table

I am working on a project that includes an interface where I want to display the user's name next to their picture, but currently it is appearing above the picture. How can I fix this issue? Below is a file containing the table design with headers an ...

The <Redirect /> element is failing to direct to the intended component

I have configured the routes for child components within the parent component as displayed below: const AdminPage = props => ( <div> //some other code <Switch> //some other routes <Route exact ...

Placing a box in the center of its parent container using CSS positioning

Check out this code snippet : HTML <div class="app-cont"> <div class="app-head"> Additional Comments : </div> <div class="app-main"> balallalalalallalalalala <br/> jaslnflkasnlsnlk ...

How to ensure your content is always visible on Mobile Safari iOS 8 by making sure it stays

Although minimal-ui is no longer supported on iOS8, I've encountered an issue with some fixed content at the bottom of a webpage. The page height is set to 100vh to cover the entire viewport, but some of the content at the bottom is extending below t ...

Tips for adjusting the width of an HTML element that is set to position: fixed

<div class="ui-datatable-scrollable-view" style=" width: 100%; position: relative; "> <div class="ui-widget-header ui-datatable-scrollable-header" style="position:fixed;top:50px"> </div> </div> ...