Changing button alignment using CSS to create a new line

Currently, I am working on a React project using Material-UI where I am trying to create a numpad. The code snippet below showcases part of my implementation:

    // some parts are omitted for conciseness
    const keys = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0];

    return (
        <div>
            {keys.map(key => {
                return (
                    <KeyPadButton key={`button-${key}`}
                         value={key}
                         styles={[4, 1, 0].includes(key) ? styles.keypadButtonStyles : {}}

                    />
                )
            })}
        </div>
    );

const styles = {
    keypadButtonStyles: {
        clear: 'left',
        display: 'table',
    },
}

My goal is to arrange the numbers in the numpad as follows:

7, 8, 9 4, 5, 6 1, 2, 3 0

Specifically, I want 4, 1, 0 to be on a new line. Although I've tried using CSS properties such as clear: left and display: table, they move the numbers onto separate lines without any wrapping arrangement. I aim to have each number like 5, 6 next to its preceding digit, maintaining an organized layout. Unfortunately, altering the style to display: block disrupts the default Material-UI styles. Do you have any suggestions to help me achieve the desired layout?

Answer №1

Utilizing flex-wrap: wrap; in Flexbox should be compatible with all child elements, including RaisedButton.

Displayed below is a working snippet (with simplified JS, focusing on CSS):

const keys = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0];

htmlKeys = keys.map(key => `<div class=key>${key}</div>`);

document.querySelector(".keypad").innerHTML = htmlKeys.join("");
.keypad {
  width: 12em;
  display: flex;
  flex-wrap: wrap;
  background: #333;
}

.key {
  width: 3em;
  background: #444;
  color: #eee;
  text-align: center;
  line-height: 3em;
  border-radius: 0.25em;
  margin: 0.25em;
}
<div class=keypad />

An alternative approach using display: inline-block (may clash with Material UI styling):

const keys = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0];

htmlKeys = keys.map(key => `<div class=key>${key}</div>`);

document.querySelector(".keypad").innerHTML = htmlKeys.join("");
.keypad {
  width: 12em;
  background: #333;
}

.key {
  width: 3em;
  background: #444;
  color: #eee;
  text-align: center;
  line-height: 3em;
  border-radius: 0.25em;
  margin: 0.25em;
  display: inline-block;
}
<div class=keypad />

An additional option could be to utilize CSS grid, but it's suggested that flexbox is adequate in this scenario (and has better browser support).

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

The navigation bar and text subtly shift when displayed on various devices or when the window size is adjusted

Hello, I am brand new to web development and have encountered an issue on my website. Whenever the window is resized or viewed on a different screen, the navigation bar (which consists of rectangles and triangles) and text elements start moving around and ...

Error: Attempting to access a property called 'sign' on an undefined value

I encountered an issue while signing my transaction where I received an error message stating sendTransaction needs signer. Even though both message (encrypted using keccak256) and signer have values, I am unsure why there is a problem when executing the w ...

Discover the process of dynamically importing JavaScript libraries, modules, and non-component elements within a Next.js

Lately, I have been utilizing Next.js and mastering its dynamic import feature for importing components with named exports. However, I recently encountered a particular npm package that functions only on the client-side (requires window) and has a substant ...

Error: Attempting to access the 'state' property of an undefined variable within the bootstrap framework

While attempting to update the state value, I encountered an error stating TypeError: Cannot read property 'state' of undefined. This error occurs when I enter something in a field and then click submit. As a newcomer to React, I realize this may ...

Chrome causing the vanishing act of floated iframes

I encountered an unexpected issue on my website recently. For the past three years, everything was working smoothly until I updated Chrome to version 60. Now, whenever I try to load iframes from Archive.org, they momentarily appear and then vanish instantl ...

Issue with loading function in FullCalendar React integration

Currently experimenting with FullCalendar v5 for React and exploring the Resource Timeline view feature. Facing an issue with the "loading" function - the idea is to monitor the state of FullCalendar and, if it's in a loading state, display a Spinner ...

Building an object in React using JavaScript: A step-by-step guide to including all necessary fields

I am working on a Sign-up page where I console log the input values from each field when the 'Sign Up' button is clicked. However, I want to combine these individual values into one object in the console. If anyone can provide assistance with thi ...

Achieving smooth client-side routing functionality with Shopify app-bridge-react

Struggling with implementing an embedded React Shopify app using create-react-app? I am having difficulty understanding how to make the embedded routing work smoothly with navigation links in Shopify. The official docs for ClientRouter component in app-bri ...

Unidentified object type detected - Material UI

This snippet of code was taken directly from the React Material UI website <Select id="selectedSubusecases" multiple value={stepsState.campaignOverviewStep.selectedSubUsecases} ...

error 404 when sending a xhr request in node and react js

I am currently developing a basic login page in React that needs to connect to a database through an AJAX call to a Node.js file. Here is the Node.js code I have implemented: var express=require('express'); var app=express(); var db=require(&ap ...

What is the best way to position a DIV class on top of another DIV class in a specific location?

I have successfully implemented a simplistic bar chart using CSS and HTML. The chart comprises two DIV classes: one for the background bar and another for the front bar. The heights of these bars are dynamically set based on percentage values passed from P ...

Allow only specific inner divs to dictate the width of the outer div

I'm working with the following HTML structure: <div id="container"> <div id="block1"> <img src="someimage/path" /> </div> <div id="block2"> Some Text </div> <div id="block3"&g ...

jquery technique for toggling a single button

My goal is to use jQuery to toggle a button rather than the typical paragraph toggling. I want the button to appear and disappear when clicked, while also increasing the "score" upon each click and decreasing it when the button reappears. Can anyone guide ...

React Router shifting upwards instead of altering properties

I'm currently working on creating a dropdown menu in a navbar using semantic-ui-react. You can take a look at the progress in this image The dropdown contains 3 items - "Apples", "Mangoes", and "Bananas". When any of these items are clicked, the cont ...

Switch the hover effect to be activated by clicking

Recently, I discovered an incredible plugin created by the talented developer janko. It has been a fantastic addition to my project. The only issue I've encountered is that I do not want the default hover style. Is there a way to transform it into a ...

Reorganize containers without relying on bootstrap styling

Is there a method to rearrange the order of two divs without using the bootstrap library? <div class='parent'> <div class='child-1'>Abc..</div> <div class='child-2'>Xyz..</div> </div> I ...

Issues are arising with Bootstrap's functionality within the Vite React app

I am new to working with react and vite. I am currently developing a vite react application that requires the use of bootstrap. I followed the instructions provided on the official Bootstrap website here. However, not all Bootstrap functionalities are work ...

Next.js API caching is a crucial feature for optimizing performance and

Currently, I am in the process of developing an application using Next.js that contains over 100,000 pages with content that changes on a daily basis. To optimize performance, I have opted to utilize server-side rendering (SSR) and getServerSideProps metho ...

Employing debounce in conjunction with React hooks leads to an infinite number of requests

Seeking a solution to efficiently fetch data from the server based on changes in the search input without triggering a new request for every character entered? Utilizing debounce from the use-debounce package found at https://github.com/xnimorz/use-debounc ...

Leveraging Next.js for credential validation along with secured paths using next-auth

Currently in the process of developing a web application using NextJs (13.1.1), I have been provided with an external backend that requires a connection via username/password to obtain an access token, refresh token, and its expiration time in the response ...