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

Ideas for displaying or hiding columns, organizing rows, and keeping headers fixed in a vast data table using jQuery

My data table is massive, filled with an abundance of information. Firstly, I am looking to implement show/hide columns functionality. However, as the number of columns exceeds 10-12, the performance significantly decreases. To address this issue, I have ...

Utilizing CSS3 Pseudo Elements for Styling a Graph

I'm struggling to achieve the desired look of my ancestor chart using an unordered list format. Initially, I borrowed code from CSS3 Family Tree, but found it to be more like an organizational chart rather than a family tree. For testing purposes, he ...

"Comparison: Utilizing HTML5 Video Player with IE8 Embed Fallback versus Implementing Fixed

This dilemma has me at my wit's end. In an HTML5 Video element, I've included an mp4, ogg, and an embedded mp4 fallback. However, on the embed fallback in IE8, all my attempts to position the fixed element (#fixed) above it with z-indexing have f ...

How can React TypeScript bind an array to routes effectively?

In my project, I am using the standard VisualStudio 2017 ASP.NET Core 2.0 React Template. There is a class Home included in the template: import { RouteComponentProps } from 'react-router'; export class Home extends React.Component<Rout ...

React Native: A guide to triggering a modal or action sheet when a button tab is clicked using Wix React Native navigation

Is it possible to trigger a modal or actionsheet by clicking on a specific bottom tab in a tab-based application using wix react native v2 navigation? These are the current packages and versions I am working with: react-native : "0.59.8" react : "16.8. ...

Adaptive scrolling backdrop

As a complete novice in HTML/CSS, I am attempting to create my portfolio. My approach is to start with the most basic elements, beginning with the background. Since my main page is a large scrollable one, I want the background to fit the screen size of the ...

Difficulty encountered in a Cast when using Router.put with mongoDB in a MERN application

Currently, I am trying to implement the router.put method for updating the Boolean value (isOn) in a toggle button. However, before making any changes, I decided to test it out and now I am encountering an issue. const express = require("express"); const ...

Can a custom CSS be linked directly to the angular-cli.json file?

I'm trying to incorporate custom CSS into my angular-cli.json file, but I haven't been able to find a successful method. I attempted to use the following code: "styles": [ "styles.css", "http://site.test/mycss.css" ], However, it does ...

Eliminate any additional spacing within the pre/code tags

I am currently utilizing prism.js for code highlighting. I have encountered an issue where there are unnecessary white spaces at the top and bottom of my output. You can view a live example here. <pre> <code class="language-css"> &lt ...

Troubleshooting notifications encountered while detaching hooks from a class component - Custom Navigation Bar based on User Roles

To create a header using Material UI, Redux, and React with a dropdown menu that displays only sections accessible to the user, I have modified my code. However, I am encountering errors different from what I initially had when using class component hooks. ...

Customize default zoom settings with Cascading Style Sheets (CSS)

body{ margin-bottom: 10%; margin-left: 10%; width:80%; color: #264653; position: relative; } #photo{ border-radius: 70%; position: absolute; left: 25%; top: 50px; } .left1{ margin-top: 10%; background-color: #264653; width : 30%; ...

A guide on exporting a constant in a React functional component

import { StarIcon } from '@heroicons/react/solid' import NumberFormat from 'react-number-format'; import Image from 'next/image' import { useDispatch, useSelector } from 'react-redux'; import { removeFromBasket, sele ...

Using the built-in API to populate default values in MUI Text field

I created a form using material UI and I want the default values to come from an API. The goal is to have an Edit screen where users can update details and save them. However, I am struggling to make it work. To fetch the data, I use axios.get request: ...

Is there a copyright concern regarding CSS?

There are countless websites that spark my creativity. If I am inspired by a particular design, CSS, or JavaScript on one of these sites, am I allowed to copy it to my local folder and use it? For instance, let's say I come across a website called xy ...

Having trouble with the scrollbar appearance after updating Chrome?

I've encountered an issue with my code after the latest Chrome update. Is there a way to implement cross-browser styling for scrollbars? // Looking for Scrollbar Styling Solution const scrollbarStyle = { scrollbarColor: `${themeLayers.scrollBar[0 ...

Is there a way to identify and count duplicate data-items within an array, and then showcase this information using a react view?

If we have an array like this: [{id: 1, name: "chocolate bar"}, {id:2, name: "Gummy worms"}, {id:3, name:"chocolate bar"}] Is there a way to show on the dom that we have "2 chocolate bars and 1 Gummy Worms"? ...

The media query for mobile is not functioning properly

Hello, I have a page with MVC that includes CSS for iPad and mobile devices. The media query for iPad (@media only screen and (min-device-width: 768px) and (max-device-width: 1024px)) works perfectly, but I am struggling to get the media query for mobile ...

The <hr> element creating a line beneath a connected div

Currently, I am in the process of designing a website with three main divs all on one page. To visually separate these divs, I have included a subtle horizontal line. The horizontal line between the first and second div looks correct as intended. However ...

Is there a way to center the text vertically within an anchor tag?

How do I align text vertically within an anchor tag? Here's a visual representation of the issue: The text "Publisher Search," "Add a New Publisher," and "Edit a Publisher" should be aligned vertically in the "bar." This is the coding structure: & ...

How can one effectively style text to appear italic using CSS and HTML?

What is the proper method to italicize text? I have come across these four different approaches: <i>Italic Text</i> <em>Italic Text</em> <span class="italic">Italic Text</span> <span class="footnote">Italic Tex ...