Styling with CSS in a React component

I am trying to display a row of buttons instead of columns on my webpage. I have used display:flex but it is still showing as a column. What I want is for each button to display the first character of its name underneath it, with all buttons lined up next to each other. Currently, the button is on the left and the name is on the right. Any help would be greatly appreciated.

RoomList.js

import React from 'react';
import './RoomList.css';
import PropTypes from 'prop-types';

const RoomList = ({ roomList }) => {
  return (
    <div>
      {roomList.map((room) => {
        const firstToChar = room.split('');
        const firstChar = firstToChar[0];
        return (
          <div>
            <li className="list">
              <div className="room-list">

                <button type="submit">{firstChar}</button>

                <div>{room}</div>

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

RoomList.propTypes = {
  roomList: PropTypes.func.isRequired,
};

export default RoomList;

RoomList.css

button {
  height: 40px;
  min-width: 40px;
  display: block;
  border-radius: 50%;
  margin-bottom: 5px;
}

.room-list {

}

.list {
  list-style-type: none;
  display: flex;
}

Answer №1

The issue lies in the HTML structure you are generating. Currently, you are creating a list for each room individually. What you actually need is to have one master list containing individual items for each room.

button {
  height: 40px;
  min-width: 40px;
  display: block;
  border-radius: 50%;
  margin-bottom: 5px;
}

.room-list {

}

.list {
  list-style-type: none;
  display: flex;
}
<div>
          <div>
            <li class="list">
              <div class="room-list">
                <button type="submit">K</button>
                <div>Kitchen</div>
              </div>
            </li>
          </div>
           <div>
            <li class="list">
              <div class="room-list">
                <button type="submit">B</button>
                <div>Bathroom</div>
              </div>
            </li>
          </div>

</div>

What you should aim for:

button {
  height: 40px;
  min-width: 40px;
  display: block;
  border-radius: 50%;
  margin-bottom: 5px;
}

.room-list {

}

.list {
  list-style-type: none;
  display: flex;
}
<div>
          <div>
            <li class="list">
              <div class="room-list">
                <button type="submit">K</button>
                <div>Kitchen</div>
              </div>
              <div class="room-list">
                <button type="submit">B</button>
                <div>Bathroom</div>
              </div>
            </li>
          </div>

</div>

In essence, what you truly need is:

const RoomList = ({ roomList }) => {
  return (
    <div>
          <div>
            <li className="list">
              {roomList.map((room) => {
                const firstToChar = room.split('');
                const firstChar = firstToChar[0];
                return (
                      <div className="room-list">

                        <button type="submit">{firstChar}</button>

                        <div>{room}</div>

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

Answer №2

It would be more appropriate to have the flex feature applied to the list of rooms

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

Creating basic animations using React and GSAP

I’ve been grappling with this issue for days and struggling to comprehend the various sources of documentation and examples available. I'm really hoping someone can assist me with a specific example that I’m currently working on, which might help ...

arranging texts in a vertical stack with wrapping

Looking to change the orientation of my horizontally wrapped list items from vertical to horizontal. Take a look at my code snippet: HTML <ul> <li>Long text goes here.</li> <li>Another Longer Text Goes Here</li> < ...

What is the best way to determine if the form has been submitted?

I am working on a React form and need to determine when the form has been successfully submitted in order to trigger another request in a separate form. const formRef = React.useRef<HTMLFormElement>(null); useEffect(() => { if (formRef &a ...

Combining the power of next.js, styled-components, and react-helmet for seamless website development

Having trouble using both react-helmet and styled-component in conjunction with next.js. The next.js examples demonstrate utilizing each package in separate repositories. Does anyone know how to resolve this issue? ...

The version of `create-react-app` you are currently using is 4.0.1, but the most recent release is 4.0.3

Even after running the command npm uninstall -g create-react-app followed by npm install -g create-react-app, I am still encountering the same error message and the create-react-app version remains unchanged. I attempted to use npm install -g <a href=" ...

Guidelines for integrating deep links into a ReactJS Ionic Android application

I have recently converted my ReactJS project into an android project following a tutorial. Now, I have successfully created an APK file and would like to configure the app to open when a specific URL is clicked on a mobile browser. Any guidance on how to a ...

Attempting to place the search bar within the navigation bar

Having trouble positioning a search bar in my nav bar, I'd like it to show on the right side without overlapping any other elements. Tried relative and absolute positioning with no success so far. Any assistance would be greatly appreciated, thank yo ...

The div background image in Vue.js is displaying a 404 not found error

I've encountered an issue while trying to set a background for a div. Strangely, when using the same relative path with an img tag everything works fine, but as soon as I try to achieve the same result with a div, I receive a 404 error. This img tag ...

The CSS for a VueJS compiled app seems to fail to apply properly unless manually copied and pasted into the browser's style editor

After compiling my vuejs app with npm run build, I noticed that the CSS does not display when viewing it in Firefox. Surprisingly, the styles do load in the network tab and appear under the style editor, but with "0 rules". However, everything displays fin ...

Is the original object cloned or passed by reference when passing it to a child React component through props?

When passing an object to a child component through the components props in React, does the object get cloned or does it simply pass a reference to the original object? For instance, in my App.js, I am importing a JSON object called ENTRY_DATA. Then, I pa ...

Typescript error points out that the property is not present on the specified type

Note! The issue has been somewhat resolved by using "theme: any" in the code below, but I am seeking a more effective solution. My front-end setup consists of React (v17.0.2) with material-ui (v5.0.0), and I keep encountering this error: The 'palet ...

Image broken on default bootstrap navbar

On my computer where I am developing a Ruby on Rails app, I have a relative link to an image. To customize the style, I am using Bootstrap and have used their code to source the image for the top left brand placement. You can view the Bootstrap link here. ...

To overcome the 401 (Unauthorized) error on specific requests, how can one bypass it when trying to access http://localhost:3000/user/auth

I am encountering a 401 (Unauthorized) error when trying to access certain requests that do not require user authentication. For instance, in an eCommerce store, all visitors should be able to view products without logging in, but sellers need to authentic ...

Adjust the vertical size and smoothly lower a text input field

When a user clicks on a textarea, I want it to smoothly change height to 60px and slide down in one fluid animation. Check out the JSFiddle example here: http://jsfiddle.net/MgcDU/6399/ HTML: <div class="container"> <div class="well"> ...

Ensuring that the carousel images maintain a consistent size when switching between images

Issue with Carousel I have been struggling with a carousel feature on my website. Despite following the code provided on the Bootstrap website, I am facing an issue where the height and width of the entire div change when loading the next image. I have tr ...

Gathering every import within a single file

Is it a good practice to centralize all import files in an allExports.js file and then call them whenever needed from there? My main goal is to have visibility and control over all files from one place... It seems pretty neat and makes the code clear, but ...

I'm experiencing a problem with my React application where errors are not being shown in the browser. Instead

Why is React only displaying an empty screen instead of showing errors like on my instructor's system? How can I resolve this issue? EDIT: While I can see the error in the console and IDE, I want to test error boundaries. When using error boundary a ...

Customize MUI 5 input label background color for your own component

Struggling with overriding the background color for a MUI 5 input label on focus in a specific component? I successfully changed it to blue globally in my _theme.js file, but now need to make it red for my KeywordSearchTextField in index.js following MUI ...

The React.js Mui Collapse component exclusively triggers animation during the transition while closing, without any animation when expanding

Whenever I implement the Collapse component from Mui library in my projects, I face an issue where the transition animation only works when closing the component (when in={false}). However, when opening the component, there is a delay before the animation ...

Tips for sending a property to a React function component

I am having an issue with dynamically customizing the title in my stack navigation bar using react-navigation. Whenever I try to pass a string to it, I receive the error message below: Functions are not valid as a React child. This may happen if you retur ...