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

Multer not running when file is uploaded in Node.js with Express using FormData

I have been working on uploading a video file to a local folder using form, multer, express, and nodejs. It seems that the video file successfully gets uploaded to the local folder each time I use the form. However, there is an issue with the code inside ...

Missing Component when Nested within Route in React Router v6

While incorporating the ChoosePlayer component within a Route using React Router v6, <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/players"> <Route element ...

"Using the d-flex class with Bootstrap 4 tables does not allow them to expand to

Since the latest version of Bootstrap 4, it has become more challenging to apply classes like col-md-2 directly to the tr or td elements of a table. A possible solution is to use class="d-flex" in the parent element: <tr class="d-flex"> However, w ...

What is the best way to ensure the header spans the entire width of a webpage using the display: flex; property and flex-direction: column styling within the body element

Click here for the CSS code snippet Hello everyone, this is my very first query on Stack Overflow. I know it might be a simple question but it's my first time posting here so kindly bear with me. Below is the CSS code snippet provided: *{ ...

What steps do I need to take to create a custom image for a website?

I'm looking for a way to create a website image using just code, without the need for an actual image file or image tag. Is there a method to do this? Would I use CSS, Javascript, or HTML5 for this task? If using JavaScript to dynamically generate the ...

Maximizing the potential of typescript generics in Reactjs functional components

I have a component within my react project that looks like this: import "./styles.css"; type InputType = "input" | "textarea"; interface ContainerProps { name: string; placeholder: string; as: InputType; } const Conta ...

Animation fails to initiate when the object enters the viewport

I attempted to inject some enchantment into my project by implementing code from a tutorial found on this CodePen. However, I encountered an issue where the code only functions properly within that specific CodePen environment. Even after copying the same ...

Utilizing Angular's ng-repeat with varying directives for each iteration

I am trying to utilize ng-repeat within Angular to iterate through multiple <li> elements with a directive embedded. My goal is to have the first two items in the ng-repeat display differently in terms of styling and content compared to the remaining ...

Struggling to publish a React application on GitHub pages, only to find that it keeps redirecting to the README file

Despite my best efforts and following tutorials, I am struggling to deploy my React App to GitHub pages using the gh-pages package. I have taken all the necessary steps such as: Installing the gh-pages package. Adding a homepage to the package.json in the ...

Streamlined Infinite Scrolling Data Visualization Using SVG Implemented in HTML and CSS

I have developed a dynamic graph that continuously updates with new data points being plotted. Right now, I am utilizing requestAnimationFrame() to render the element positions 30 times per second, but performance can be sluggish with numerous SVG element ...

Ways to display scroll bar when hovering the mouse pointer?

When visiting the website YouTube, you may notice that their sidebar scrollbar becomes visible as soon as your mouse moves over it. I've been attempting to achieve this effect on my own div, but currently, the scrollbar only appears once I start scrol ...

Pair objects that possess a specific attribute

I have a HTML snippet that I want to modify by adding the CSS class HideEdit to hide specific columns. <th class="rgHeader" style="text-align: left;" scope="col" _events="[object Object]" control="[object Object]" UniqueName="Edit"> This particular ...

Utilizing i18n's useTranslation and Redux's connect Higher Order Components to export components efficiently

My application has been utilizing Redux for quite some time, with the component exports structured like this: export default connect(mapStateToProps, mapDispatchToProps)(MyComponent); Now, I am integrating an i18n translation library and would like to use ...

Utilizing JQuery to create a dynamic background image slider that cycles through images extracted from a selection of

Case Study: Implementing a background-image slider with JQuery listening for window resizing. Depending on the screen size, a specific array is set as the image file paths source. Encountering a 6-second delay issue where the initial image does not displ ...

The child component is not displaying the default value being passed from the parent in the

Currently, I am in the process of developing a form using Reactjs that relies on defaultValues provided by the parent component. The issue arises when the parent component sets the values' states through an axios post and then passes them down to the ...

What is the correct way to initialize a variable that will store the output of setInterval?

While examining a question, I came across someone's solution that proposed updating the code below. In the comments section, it was suggested: Instead of setting this.tm to undefined, we should set it to 0. Since it's a time interval, it shoul ...

Optimal approach for handling onChange within useEffect

My child component is performing a fetch operation within a useEffect hook and then calling an onChange handler that is passed down from the parent. The issue is that the child component gets re-rendered three times, triggering the fetch operation three ti ...

Executing a function from another module (File) within a React JS application

I am currently utilizing the Material UI v1.0.0-beta.33 in my project, which includes an App Bar component. import React from "react"; import PropTypes from "prop-types"; import { withStyles } from "material-ui/styles"; import AppBar from "material-ui/App ...

Get rid of the rollover effect on the <a> tag when hovering over an image

I have a snippet of code that is fully functional. <div class="user-pic round-pic"> <a href="<?php echo $userLoggedIn; ?>"><img src="<?php echo $user['profile_pic']; ?>"></a> </div> The issue lies in i ...

Screen remains blank as the React webpage fails to load

Hello, I am having issues with my React page not showing up. Can someone please review my code to see if there are any errors? Here is the edited program: index.html <!doctype html> <html lang="en"> <head> <meta charset ...