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

Animate CSS every quarter of a minute

Is it possible to create an animation that runs infinitely every 15 seconds in the sequence 1, 2, 3, 4, 3, 2, 1 with different delays for each item using CSS? The animation delay seems to only work on the first iteration. Achieving this effect is easy wi ...

100% width with a pixel offset

In the past, I have achieved this by using padding. The concept is to have two div elements positioned side by side, where one has a width of 100% and the other has a fixed width of 50px. Here's a rough illustration: ------------------------------- ...

Mutual data exchange between the Parent and Child components

Creating a front end in Next.js, I have nested one component inside another and aim for them to share an array of Ids. The main component is a page where users can create collections containing their posts. It sends a post request with the collection_name ...

A guide on adding an onClick listener to a span element dynamically and then saving it to MongoDB

I have been attempting to add an onClick event within a span tag as shown below, with the goal of storing it in MongoDb. However, my event does not seem to be saving and is automatically removed. When I retrieve data from the database, it is not present. H ...

Creating a dynamic dropdown menu where the available options in one select box change based on the selection made in another

Looking at the Material-UI Stepper code, I have a requirement to create a select element with dynamic options based on the selected value in another select within the same React component. To achieve this, I wrote a switch function: function getGradeConte ...

The link tag cannot be used inside a division element

I am facing an issue with a button that has three different states represented by images. While the button looks great and functions well, it fails to perform its primary function - linking to another document. Upon clicking the button, no action is initi ...

Tips for aligning inputs vertically and stretching them horizontally with bootstrap flex only

For a practice exercise, I want to achieve vertical left alignment and horizontal stretching of input fields using only Bootstrap Flex (or CSS3 flexbox), without relying on Bootstrap Grid or CSS3 grid layout. Here is the code snippet (also available on co ...

Browser disregards backslashes in the content of pseudo elements

I have been struggling with a simple issue for quite some time now, and I seem to be out of ideas. The problem lies in the styling of a CSS pseudo-element before, which is defined as follows: #fPhone::before{ color: #78be20; content: "\e622" ...

"Learn how to easily upload files to Google Drive using a web app created with a combination of Google Apps Script, React JS, and CLASP

I am currently working on a web application using React JS, Parcel, and Google Apps Script. Everything is functioning correctly, and all entries are successfully saving to a Google Sheet. However, I have encountered an issue with the file upload input - in ...

JavaScript MP3 player

Could someone kindly point out where I went wrong? I am attempting to create an MP3 player using CSS, HTML, and JavaScript. Currently, the script only functions to start or stop the audio file. However, I keep encountering an error message: TypeError: docu ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...

Refreshing SQL Server data using an HTML form

In the table below: <table id="skuTable" role="grid"> <thead> <th class="skuRow">Order</th> <th>Fab. Date</th> <th class="skuRow">Norder</th> <th>Color</th> ...

Retrieve the screen width using a JavaScript function and apply it as a percentage

I find myself needing to adjust the size of table elements based on the width of the screen. While I am not well-versed in javascript or html, resolving this issue is crucial. Unfortunately, I did not create the original asp page and have limited ability t ...

Is it possible to have a never-ending slideshow with inline-blocks?

After spending countless hours trying to troubleshoot my code, I am now seeking help from the wonderful people of the internet! ;) It seems like only a small portion of my code is incorrect. What I'm attempting to do is move the leftmost object in the ...

The straightforward hyperlink to a specific section within a webpage is not functioning as expected

Having trouble with my HTML navigation in Angular, can't pinpoint the issue. When I click on a navigation button, the URL changes from http://localhost:8018/#/WebDev to http://localhost:8018/#WebDev Something off in my code? <head> < ...

Unleashing the power of Next.js SWR by effortlessly retrieving data from

Encountering a challenge with retrieving data from the cache. Within my dashboard component, data fetching is successful and accessible. const Dashboard = ({ code }) => { const { data, error, mutate } = useSWR(['/api/user', ...

Prevent page from reloading when Image Button is clicked

I attempted the following code: <asp:ImageButton ID="imgDropArrow" class="cssDropArrow" ImageUrl="~/Styles/Down.gif" OnClick="ImageDropArrow_Click" runat="server"/> Whenever I click on this image button, the page refreshes. What I'm looking ...

What is the best way to retrieve information in Next.js when there are changes made to the data, whether it be new

Could you share a solution for fetching data in Next.js when data is added, deleted, or edited? I tried using useEffect with state to trigger the function but it only works when data is added. It doesn't work for edit or delete operations. I have mult ...

Vertical alignment of text within a center位置

My current project involves creating a website layout similar to this: I am facing difficulty in centering the text within the box like this: This is my progress so far: @font-face { font-family: 'Trend Sans 004'; /*a name to be used lat ...

The location of the footer is not aligned correctly

Recently, I encountered an issue that has left me puzzled. The problem is that the footer on my webpage is appearing between the calendar button and the calendar itself. Here is a snippet from my calendar.html file: {% extends 'base.html' %} {% ...