Arranging elements and buttons inside of components to create a cohesive design

My React application consists of two components: Open and Save. The Save component has a button labeled as saveButton. On the other hand, the Open component contains an openButton, as well as a stopButton and executeButton that are conditionally displayed based on the component's state. I would like to display the Open and Save components side by side in one row:

<div style={{display:'flex', flexDirection:'row'}}>
    <Save></Save>
    <Open></Open>
</div>

Initially, the state appears as follows:

When all four buttons are visible, the state is as shown below:

It is worth mentioning that the Execute and Stop buttons are part of the same Open component which also includes the Open button. How can I ensure that the Save/Open buttons are aligned to the left, while the Execute/Stop buttons are aligned to the right?

To provide further clarity, my desired button alignment is depicted below:

Save Open ------------all whitespace here--------------- Execute Stop

Update: Provided below is a simplified version of the code for these components:

// Save 

<Button>Save</Button>

//Open
this.state = { executeFlag: false, stopFlag: false}
render(){
    let stop;
    let execute;
    if(this.state.stopFlag) stopButton = <Button>Stop</Button>;
    if(this.state.executeFlag) executeButton = <Button>Execute</Button>;
    return(
        <div>
            <Button>Open</Button>{execute}{stop}
        </div>
    );
}

Answer №1

To enhance your CSS, consider adjusting the button widths to 50% each and setting the margin to zero. By utilizing flex, the buttons should appear on the same row as long as they are able to fit properly.

Answer №2

If you're looking to achieve that, it really hinges on the structure of your component rather than just the control buttons themselves. Below is a sample layout I typically employ using plain HTML and CSS for a similar scenario:

.container {
  display: flex;
}

.component-b {
  flex-grow: 1;
  display: flex;
  justify-content: space-between;
}

.sub-menu{
  display: flex;
  justofy-content: flex-end;
}

.button {
  background: #1E88E5;
  padding: 0.5rem 1rem;
  margin: 0.5rem;
}
<div class="container">
    <div class="component-a button">
      Save
    </div>
    <div class="component-b">
      <div class="button">
        Open
      </div>
      <div class="sub-menu">
        <div class="button">
          Execute
        </div>
        <div class="button">
          Stop
        </div>
      </div>
    </div>
</div>

Answer №3

To achieve this layout, you can wrap two components within a single div, and then wrap everything in another div using flex properties.

<div style={{
   display:'flex',
   justifyContent:'space-between',
   aligItems:'center'
    }}
>
<div style={{display:'flex', flexDirection:'row',justifyConetent:'space-around'}}>
    <Save></Save>
    <Open></Open>
</div>
<div style={{display:'flex', flexDirection:'row',justifyConetent:'space-around'}}>
    <Execute></Execute>
    <Stop></Stop>
</div>
</div>

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

Displaying text on an image when hovering over it

I have tried various solutions that I came across, but none of them have been successful so far. My goal is to display dynamic text over an image when the user hovers over it. Additionally, I would like to change the hover color. Currently, the text is alw ...

The issue of the footer kicking out of place in the HTML layout was successfully resolved after

I am in the process of creating a simple footer that will always stay at the bottom of the page. The content on the page will not exceed the screen size, eliminating the need for scrolling. (There are a total of 5 HTML pages, and I want the footer and head ...

Error: The React Native code is unable to access the property 'CameraType' because it is undefined

I'm experiencing issues with my app crashing on an Android device when trying to utilize the expo-camera for camera features. Here is a snippet of my JavaScript code: import React, { useState, useEffect, useRef } from 'react'; import { Text, ...

Can the getState() method be utilized within a reducer function?

I have encountered an issue with my reducers. The login reducer is functioning properly, but when I added a logout reducer, it stopped working. export const rootReducer = combineReducers({ login: loginReducer, logout: logoutReducer }); export c ...

Retrieve data from an HTML table with the power of jQuery

I have a table with the following structure. <form id="myform"> <table> <tr><th>TEST</th> <th>VALUES</th> <th>UNIT</th> <tr><td>Abc</td><td><input type="text" & ...

The div element is failing to occupy the entire page

I have encountered an issue where I created two small blocks, but they do not start from the screen edges. Instead, they take up space from the left, right, and top as well. Here is my code: HTML <body> <div id="header"></div> & ...

Swap out the content in a text input box with the text chosen from a suggested autocomplete option

I am working on a text input box with auto-complete options displayed below it. I want to enable users to navigate through the options using keyboard arrows and "select" one, causing it to change color. How can I update the text in the input box with the s ...

The presence of an undefined response in Next JS API requests is causing issues

Can anyone assist me with Next JS API responses? I have created a fetch request in api/proxy/page.js export default (req, res ) => { fetch('https://www.example.com') .then((response) => { console.log(response) ...

100% width is applied to the table cell within the div element

Below is the code I am working with: <div style='display: table'> <div style='height:200px; width:100%; text-align: center; display: table-cell; vertical-align: middle'>No result found</div> </div> I'm ...

CSS styling failing to meet desired expectations

Two different elements from different panels are using the same CSS styles, but one displays the desired text while the other does not. The CSS file in question is named grid.css. To see the issue firsthand on my live website, please visit: I am uncertai ...

How can we use the useState hook in React to dynamically generate state variables?

I'm currently working on a React app where input fields need to be stored in the state. While I can use the useState hook to easily store these values, the challenge I'm facing is that I don't know what fields are needed since they are retri ...

Navigating the terrain of multiple checkboxes in React and gathering all the checked boxes

I am currently developing a filter component that allows for the selection of multiple checkboxes. My goal is to toggle the state of the checkboxes, store the checked ones in an array, and remove any unchecked checkboxes from the array. Despite my attemp ...

Modify input value using jQuery upon moving to the next step

When you type into an input[text] and press the tab button, it automatically moves to the next input. If you fill out all the inputs and then want to re-fill them, the values will be highlighted in blue. However, I wanted to achieve this without having to ...

Angular is encountering a CORS issue with the "No Access-Control-Allow-Origin" error when trying to access a CSS file

Currently, in my Angular 5 application with a Web API back-end, my main goal is to reference a CSS file on a production server. I'm facing a cross-origin issue when trying to access the CSS file hosted on the IIS server website. Even though CORS is e ...

Experimenting with TypeScript asynchronous functions, the async callback was not triggered despite being called within the .then() method

After extensively searching for a solution, I have been unable to find one. The only possibility that comes to mind is that redux-mock-store does not support Promises. Below is the test method in question. import thunk from 'redux-thunk'; impor ...

Arrays in Javascript (correlating)

I'm having trouble figuring out what needs to be included in this code (I apologize that it's in German, it's an array corresponding to images for JavaScript.) function namenZuBildern(){ var bilder = new Array( "000227", "000228", " ...

Pagination in Material-UI Datagrid seems to be starting on the second page instead of the first when the page number is

I am encountering an issue with the pagination on my Material-UI Datagrid. When I make a request to an API with specified page numbers and limits (e.g. `page=${page}&limit=10`), I receive 10 rows of data along with any pagination information sent by La ...

I am unable to generate a vite application within WSL

My system is running node version 10.19.0 and npm version 6.14.4. Whenever I try to run create-vite@latest client, I encounter the following error: npx: installed 1 in 0.79s /home/victor/.npm/_npx/86414/lib/node_modules/create-vite/index.js:3 import &apos ...

Having trouble with changing the color of an element when the radio input is checked?

Can someone help me troubleshoot this HTML and CSS code? <label class="radio"> <input type="radio" name="plan" value="plan" required> <span> <h6>Plan</h6> <i class="pe-7s-graph1 ...

Hot reloading in React

Currently working on a React project that was initiated with create-react-app. However, I am experiencing inconsistencies with the hot reload feature. Sometimes it works for a brief period of time, then stops functioning, requiring me to restart the serv ...