Ways to position two buttons in each corner with flexbox in a react js application

I'm fairly new to React JS and have just started building a simple React app. I'm trying to place two buttons on each side using CSS flex, but I'm having trouble achieving it. Can anyone help point out where I might be going wrong?

      import React, { Component } from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import Button from "@material-ui/core/Button";
import IconButton from "@material-ui/core/IconButton";
import MenuIcon from "@material-ui/icons/Menu";



const styles = {
  root: {
    display:'flex',
    justifyContent: 'space-between',
    width:'100%'
  },

};

class Header extends Component {
  render() {
    return (
    <div className={styles.root}>
   <button>news</button>
   <button >login</button>
</div>
    );
  }
}

export default Header;

The two icons, News and Login, should be in two corners of the AppBar.

https://i.sstatic.net/h3Oif.png

Answer №1

Update

Instead of using the className attribute, you should opt for the style attribute in your React code. The purpose is to apply styles, not a class.

The following is an example of how the React code should look:

class Header extends React.Component {

  render() {
    const styles = {
      root: {
        display:'flex',
        justifyContent: 'space-between',
        width:'100%'
      }    
    }

    return (
      <div style={styles.root}>
        <button>news</button>
        <button >login</button>
      </div>
    );
  }
}

ReactDOM.render(<Header />, document.querySelector("#app"))

You can find the working Fiddle here.


Your flex container is not properly positioned in the DOM. To make it work, the display: flex assignment should be moved down to <Toolbar>, which is the direct parent of <Button> and <Typography>. In order to align two buttons in opposite corners using flex, you can utilize justify-content: space-between.

.container {
  display: flex;
  justify-content: space-between;
}
<div class="container">
   <button>a button</button>
   <button>a button</button>
</div>

Check out the jsFiddle here

Answer №2

To achieve this layout, you can utilize the CSS property justify-content: space-between. Check out an informative guide on this topic at css-tricks by visiting: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

It's important to note that in order for this to work effectively, the flex items need to be the immediate children of the flex container (the element with display: flex applied), like this:

<div className="toolbar flex-container">
   <button className="child">news</button>
   <button className="child">login</button>
</div>

Answer №3

Have you given this a shot?

display: 'flex',
align-items: 'center',

I'm not entirely familiar with React's syntax.

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

disabling past dates in Bootstrap calendar

How can I disable past dates in Bootstrap easily? <script type="text/javascript"> $(function(){ $('.datepicker').datepicker(); }); </script> Date: <input type="text" class="datepicker"></input> I have added the ...

TSX is throwing an error: "No matching overload found for this call."

Just starting out with React and TypeScript here! I tried passing the propTypes into a styled-component and ran into this error message: Oh no, there's an issue with the overloads. Overload 1 of 2 seems to be missing some properties. Overload 2 of ...

Managing the URLs of single page applications

Typically in a Single Page App (SPA), there is usually one main page that contains a side navigation menu with various anchor tags. These anchor tag URLs are managed by the angular/react/sammy js router, and the content of the main section is updated based ...

Centered Bootstrap 4 modal with responsive width

Struggling to create a BootStrap 4 Modal that is centered in the screen and adjusts its width based on content? You're not alone. Despite trying different approaches like the container fluid and CSS methods, achieving a variable width modal is proving ...

Adjust camera focus towards object and utilize the lookAt() method (React three fiber)

Seeking a smooth transition for camera.position and camera.lookAt as I switch between "zoomed out" and "zoomed in" views of individual objects randomly placed. The positioning aspect is working smoothly, but lerping the lookAt() function seems to be causi ...

Error: The property value supplied for the calc() function is invalid

<App> includes both <Header> and <Content> components. I am attempting to calculate the height of <Content>, which is equal to the height of <App> minus the height of the header. // App.js import { useStyles } from "./Ap ...

Bring your text to life with animated movement using HTML5 and/or CSS3

My goal is to have a snippet of text, like "Lorem Ipsum..." slide horizontally into a colored DIV quickly, then smoothly come to a slow stop, continue moving slowly, speed up again, and exit the DIV - all while staying within the boundaries of the DIV. I ...

When text exceeds multiple lines, display an ellipsis to indicate overflow and wrap only at word boundaries

Here is an example snippet of my code: <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> and I am using the following jQuery code: $(&apos ...

Tips for designing a flexible structure with 2 columns, each containing an additional 2 columns within

Is it possible to create a 4-column responsive layout? | column left | column right | column left| column right| I would like to achieve a single row layout on larger devices as shown below | column left | column right | column left| column right| an ...

The issue I am facing with this self-closing TITLE tag causing disruption to my webpage

I am encountering a problem with my basic webpage. <html> <head> <title/> </head> <body> <h1>hello</h1> </body> </html> When viewed in both Chrome and Firefox, the webpage ...

Utilizing multiple criteria to filter through a nested array

I am faced with an array structured as follows const treeObj = [ { id: 1, name: 'Section One', items: [ { id: 1, name: 'Section One Item One' }, { id: 2, name: 'Section One Item Two' }, ...

"Although all error messages are functional in Postman, they are not appearing correctly on the frontend client

I am currently developing a website using React and Node. While testing my register and login functionality in Postman, I encountered an issue where the error message for login (e.g., user doesn't exist, username or password incorrect) is not displayi ...

Changing the color of a div's text based on its content with CSS

I am facing a challenge in styling the text inside a “div” element using a Javascript function. Initially, when the page loads, these “divs” contain no value. The values of the "divs" will change between "Open" and "Closed" twice a day when the Jav ...

Error in jQuery when trying to access the src attribute of an image: "undefined src

Just started delving into jQuery. I'm trying to grab the image source and showcase it in a fixed division like a pop-up, along with the image title. However, I'm encountering difficulties in fetching the image source. My attempt with the .attr() ...

How to align an unordered list horizontally in HTML without knowing the number of items

I'm currently developing a web page that needs to display an unknown number of items using the ul/li HTML tag. Here are my requirements: The list should utilize as much horizontal space as possible The list must be horizontally centered, even if lin ...

Attempting to transfer information from a JSON file to a Netflix-inspired platform

Images I am currently working on integrating data from my json file export const products = [ { name: 'iPhone 11', image: '/assets/images (7).jpeg', time: '1 hour 14mins', age: '+16&apo ...

The `useMap()` function consistently returns the value of `{ current: undefined }`

I've been working on a project with react-map-gl. The map itself is rendering fine, but I'm running into an issue with using useMap(). Whenever I try to use it, I always get { current: undefined } in the logs. Any suggestions or solutions? In my ...

Parsing JSON data in a CodeIgniter PHP environment from React using AJAX

Currently, my React application is running on localhost:3000 while my CodeIgniter API is running on localhost:8000. I am attempting to send an AJAX post request and pass data in JSON format but I am unsure of how to receive it in my controller. Here is wh ...

Does Virtual DOM speed up or slow down applications?

After reading through the Svelte JS documentation, I noticed that one of its advantages is the absence of a Virtual DOM, making apps built with this framework faster and lighter. However, conflicting information suggests that having a Virtual DOM can act ...

One-way communication between two clients using Socket.io

While working on a basic socket.io application using React and Express, I've encountered an issue where two clients are facing difficulties in sending data to each other. For instance: Player 1 connects to the server followed by Player 2. Player 1 ...