styling a flex container with aligned text and buttons

I'm attempting to align text on the left and button controls on the right within a display: flex div.

<CustomFieldContainer>
  <StyledWellContainer>
    <FieldDetails key={id}>
      <H4 bold>{label}</H4>
      <StyledBody>({capitalizeFirst(type)}) {required && <StyledSpan>REQUIRED</StyledSpan>} </StyledBody>
    </FieldDetails>
    <DeleteAction {...props} />
  </StyledWellContainer>
</CustomFieldContainer>

This is what I have achieved so far.

Below is the CSS for all the ReactJS elements mentioned above.

const CustomFieldContainer = styled.div`
  display: flex;
  flex-direction: row;
`;


const FieldDetails = styled.div`
  display: flex;
  flex-direction: column;
  border: none;
`;

const DeleteButton = styled(Button)`
  flex-basis:33.33%;
  margin-right: 0;
  margin-left:auto;
  display:block;
`;

With this setup,

https://i.stack.imgur.com/mjfkF.png

The goal is simple, as shown below:

https://i.stack.imgur.com/Yk958.png

Any assistance would be greatly appreciated. The main idea is to have a container with texts on the left and controls on the right. Thank you.

Update:

const StyledWellContainer = styled(WellContainer)`
  margin-right: 1rem;
  width: 100%;
  padding-bottom: 2px;
  padding-top: 0;
`;


import styled from 'styled-components';

const WellContainer = styled.div`
  display: flex;
  flex-direction: column;
  padding: 1rem;
  border: ${({ theme, borderType }) => (borderType === 'attention' ? theme.color.border.negative : theme.color.border.light)};
  border-radius: 12px;

  & > * {
    padding-bottom: 1rem;
    margin-bottom: 1rem;
    border-bottom: ${({ theme, disableDivider }) => (disableDivider ? 'none' : theme.color.border.light)};
  }
  & > :last-child {
    padding-bottom: 0;
    margin-bottom: 0;
    border-bottom: none;
  }
`;

export default WellContainer;

Update after changes, advised by Adam.

https://i.stack.imgur.com/H7TfM.png

Answer №1

Your StyledWellContainer needs some adjustments to its styles:

  flex: 1 0;
  display: flex;
  flex-direction: row;
  align-items: flex-start;

Currently, the container is set as a column, causing the button to appear below instead of beside the content.

Changing it to a row layout will fix this issue. The flex: 1 0 property ensures it stretches to fit the parent, while align-items: flex-start; prevents the button's content from stretching in height.

Additionally, allow the button to maintain its base size without forcing it to grow:


const DeleteButton = styled(Button)`
  flex: 0 0 auto;
  margin-right: 0;
  margin-left: auto;
`;

Answer №2

TheStyledContainer should act as the flex container instead of OtherFieldContainer

const TheStyledContainer = styled.div`
  align-items: center;
  display: flex;
  flex-direction: row;
`;

const FieldInfo = styled.div`
  display: flex;
  flex: 2;
  flex-direction: column;
  height: 100%;
  border: none;
`;

const RemoveButton = styled(Button)`
  flex: 1;
  margin-right: 0;
  margin-left:auto;
  display:block;
`;

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

What is the most effective method for implementing a debounce effect on a field in React or React Native?

Currently, I am working on implementing a debounce effect in my useEffect. The issue is that the function inside it gets called as soon as there is a change in the input value. useEffect(() => { if (inputValue.length > 0) { fn(); ...

The integration of a Bootstrap modal within the side bar's rendering

Struggling with a modal appearing inside my side div and can't find any solutions in the bootstrap5 documentation or online forums. https://i.stack.imgur.com/gHsBi.png I just want the modal to display centered on the page with the background fade ef ...

Utilizing the handleChange method to manage multiple input fields

It is important to me that the function handleChange only updates the input value that is being changed. Specifically, if I modify the 'firstName' input, I want only that state to be updated, and the same applies to the 'lastName' input ...

ReactJS Reactstrap input dropdown fails to display the chosen value

Using reactstrap in conjunction with React JS, I am attempting to figure out how to bind the input dropdown list. The optionID can range from 1 to 5 when the data is incoming. My expectation is that upon bringing up the UI, the dropdown should always displ ...

What is the best way to upload custom fonts in a next.js application during its production phase?

Alright, so here's a question that might seem simple at first glance, but I've already combed through the next.js documentation and can't seem to find a solution. In my next.js project, I'm facing an issue that seems to be either a bug ...

Utilizing Color Theme Created in App.js's createMuiTheme Function within Child Components in React

I'm just getting started with React. I successfully set the primary color for my React application using MuiThemeProvider. Here's how: https://i.stack.imgur.com/VCzOC.png Every time the app loads, the primary color is randomly chosen. Now, I wa ...

Is it necessary to save every async call response in the redux store?

How should async calls be handled in react-redux? I understand that each component can make API calls independently and store data locally, but what is the purpose of the redux store then? Is it only for storing shared data between components? What are t ...

How to position multiple CSS background images effectively?

Greetings all! I'm seeking advice on how to add a background image to the right of the description and left of the first post column. Any tips on proper placement? Appreciate any help :) ...

Passing props from pages to components in NextJS: A guide

My nextjs-application has a unique folder structure: components -- layouts --- header.tsx --- index.tsx pages -- index.tsx -- [slug].tsx In the [slug].tsx file, I utilize graphql to fetch data and set the props accordingly: export default ...

Sass: Setting a maximum width relative to the parent element's width

I have a resizable container with two buttons inside, one of which has dynamic text. Within my scss file, I am aiming to implement a condition where if the width of the container is less than 200, then the max width of the dynamic button should be 135px, ...

What are some tips for managing the hover effect using CSS3?

Here's a demonstration of my current setup. When you hover over the black box, a transition occurs and reveals my tooltip. However, I only want the tooltip to appear when hovering over the black box itself. Currently, the transition also triggers if y ...

Creating an arrow dialog box in Material UI: A step-by-step guide

I have successfully created an arrow box (div) using custom CSS and HTML. However, I am facing difficulty in implementing the same design in Material UI with React JS. The code below demonstrates how to achieve this using custom CSS. My question is: How ...

The image is being loaded onto the canvas and is being resized

Currently, I am facing an issue with loading images into a canvas element as the image seems to be scaled in some way. To provide some context, I have multiple canvases on my webpage and I want to load images into them. Since the dimensions of the canvas ...

Using ES6 syntax, ignite the React function

Below is the code snippet provided: class Seismo extends Component { constructor(props) { super(props); this.state = { news: "" } this.updateNews = this.updateNews.bind(this) } updateNews = () => { console.log('te ...

Require assistance with arranging font-awesome icons in a stacked formation

After upgrading to the latest version of font-awesome, I encountered some stacking issues with my icons. In the previous version, everything was working perfectly. <li><span class="icon-stack stacked"><i class="icon-circle icon-stack-base" ...

Executing an Ajax form submission in React.js

Currently in the process of developing my initial application using React.js and aiming to send a contact form via email with Ajax. I've been referencing a solution from this source: . Unfortunately, only parts of the full component file are available ...

What are the recommended margins for various alphabets?

When displaying text, some alphabets take up more space than others. So how can I adjust the white space between elements '#re1' and '#re2' automatically in the scenario described below? In this code snippet, what is the best way to ad ...

Unable to include query parameters in the nextjs dynamic route

I have a file called [slug].js. What I am attempting to do is add a query parameter to this dynamic route. Here's the code: await router.replace({ pathname: `${router.pathname}`, query: { coupon }, }, undefined, ...

I am looking to enhance the readability of my asynchronous function calls

At the moment, I am handling my Promises by calling an async function and then chaining it with .then(). But I feel like there could be a more readable approach. This is how it currently works: const fetchData = async() => { const response = await ax ...

Having trouble with your React/TypeScript/Redux/Thunk action not dispatching and the state remaining unchanged?

Currently, I am facing an issue while attempting to send a GET request to an API using React-Redux & TypeScript. The goal is to dispatch an action upon clicking a button (onClick event), make the request, update the state via the reducer, and finally log t ...