Text that is not aligned in the middle and has a colored background

After setting up a flexbox container with some flex-items, I encountered an issue:

When clicking on either "Overview" or "Alerts", the white background border is not displayed. To highlight the selected item, a class called .selected is triggered which adds a white background color. However, when selecting an item, the white border does not span the entire width and the text inside the background is not centered.

Here is the code snippet:

<div className="container">
    <div className="flex-item item-1 item">John Smith</div>
    <div className="flex-item item-2 item">Male, 26 years old</div>
    <div className='flex-item item-3 item' onClick={setSelectedItem} style={style} > Overview</div>
    <div className='flex-item item-4 item' onClick={setSelectedItem} style={style}>Alerts</div>
</div>

And here is the CSS code:

.container {
    border: 2px solid #57c0e8;
    background-color: #57c0e8;
    margin-top: 5%;
    float: left;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
    color:white;
    display: flex;
    flex-direction: column;
    height:40rem;
    width:15rem;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: flex-start;
    font-size:0.9rem;
}

.flex-item{
    margin-top: 2rem;
}

.selected{
    background-color: white;
    color: #57c0e8;
    border-radius: 50px 0 0 50px;
    box-shadow: 2px 2px 5px lightgray;
    width: 14rem;
    height: 5%;
    text-align: left;
}

Answer №1

If my understanding is correct, you are aiming to center the text vertically in the white section that reads "Overview". To achieve this, I suggest applying display:flex and align-items:center to your .selected element. It appears that the width of a container may be causing an issue as well since it seems to be fixed. Without seeing a codepen example, it's not definitive, but consider removing the fixed width and adding padding-left to all items except for "Overview". Please provide feedback on the outcome after making these adjustments.

I would greatly appreciate it if you could share a codepen link with me.

Answer №2

When targeting the element with the class of .selected, you should eliminate the border-radius property and instead assign a width of 100%.

Answer №3

If you want to vertically center text inside an element, simply include align-items: center in the CSS for the class .flex-item:

.flex-item {
    display: flex;
    align-items: center; 
}

To make it take up the full width, just change width: 100% on the .selected class.

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

socket.io allows for the reception of emit events from namespace that are dynamically created

Seeking help on how to send messages from my react client to express server using socket.io. This is the simplified Client code: import { useRef, useEffect } from 'react'; import io from "socket.io-client"; const Generator = () => ...

Adjust the Color of MUI Accordion Based on User Input

I'm currently working on developing an Accordion component where the text in the Accordion summary changes based on user input in the field. It's a pretty straightforward process and it's already up and running smoothly. This is a snippet o ...

Ways to make two blocks of text appear side by side and adapt to different screen sizes

Check out our website imageI'm facing a challenge in making these two text elements appear next to each other rather than stacking on top of each other. Additionally, I need them to adapt responsively to changes in screen size. When 'Icontext&ap ...

Ways to showcase the outcome of a spin after each rotation

I am currently working on a spinning code snippet that allows users to spin and potentially win different amounts. The code includes functionality to prevent more than 3 spins, set random rotation values, and animate the spinning effect. However, I now wa ...

Is it possible to export all types/interfaces from .d.ts files within multiple folders using index.ts in a React TypeScript project?

In my current React project, I am managing multiple configuration folders: -config -api/ |index.ts |types.d.ts -routes/ |index.ts |types.d.ts ... For example, in the api/index.ts file, I can import necessary types using import {SomeTyp ...

Encountering an issue with importing an enum: An error is triggered stating 'Variable implicitly has type 'any' in certain areas where its type remains undetermined.'

When I define simple enums in the same file, everything works fine. However, exporting/importing them causes numerous compilation errors related to types. It seems like the issue only arises when defining enums in a separate file, pointing towards a proble ...

What is the best way to adjust the position of a dropdown menu in Material UI?

I am currently working on a Gatsby application and I'm trying to achieve a dropdown menu effect when the Menu icon is clicked, with the menu appearing right under the header and expanding to 100% width. Below is the code snippet that I have been using ...

Easily Organize Your Data with Kendo React Grid: Rearrange Columns and Preserve Your Custom Layout

Currently, I am working on implementing a Kendo React grid with the option set to reorderable={true} for allowing column reordering. However, I am facing difficulty in determining how to save the new order of columns. Which event should I use to detect whe ...

Tips for creating 2 fetch methods for a contact API and a Message API in ReactJS!

I am just starting to learn reactjs and have been able to successfully map data from a JSON API. My goal now is to ensure that when a number is saved in the contact list, it should also display the name in the message list. To achieve this, I plan to creat ...

Ensuring nested JSON key existence before rendering in React

When retrieving data from MarvelAPI, there is no certainty that the key being accessed will be available. Therefore, I am implementing if statements to check each key before accessing them. The current code below appears quite messy. Is there a more effic ...

Enhancing CSS with Additional Properties

As a web developer, I recently had the opportunity to explore the new LOGIN PAGE preview of GMAIL and was very impressed with the Sign In button's UI. After examining the Page's CSS, I discovered some interesting properties, such as: **backgroun ...

Use JQuery to constantly monitor for any changes in HTML Div

I am dealing with a browser-based chat application where separate div elements are generated for each chat conversation. An external module oversees the entire chat process, and I do not have access to its underlying code. It is important to note that the ...

Can you give me some insights about what an Action Creator is?

function createRefDoneAction(widgetsArray: widget[]): WidgetAction { return { type: actionTypes.REFRESH_WIDGET_DONE, widgets: widgetsArray }; } Could you please clarify the necessity of having two sets of parameters (e.g. 'wid ...

When the clearOnBlur setting is set to false, Material UI Autocomplete will not

I recently encountered an issue in my project while using Material UI's Autocomplete feature. Despite setting the clearOnBlur property to false, the input field keeps getting cleared after losing focus. I need assistance in resolving this problem, an ...

What is the best method for transferring information from a web application or server to a React Native application?

As someone who is new to React Native and mobile development, I have recently become part of a project that involves creating a React Native app that can receive notifications when a user interacts with the corresponding web app. For example, if there is ...

Navigating the Basics: Understanding the Four Quadrant Selection Grid in a Material UI & React Pop-Up Form

Sorry if these questions seem silly. I'm diving into React & Material-UI without a mentor to guide me, relying on documentation and tutorials. Is there a better place for quick beginner questions? Maybe a chat forum or Slack group? Not sure if Stack i ...

Guide to integrating react-phone-number-input into material-ui TextField

Would it be possible for me to use a Material UI TextField component as the inputComponent prop for the PhoneInput component from react-phone-number-input? I am facing an issue where I am unable to apply the ref successfully. Even though I can see the Mat ...

Tips on how to apply CSS styles to a specific "div" card only?

I'm working on a project that involves using multiple templates, but I've run into issues with conflicting CSS links from different templates. I want to ensure that the CSS link from one template only affects a specific HTML card without impactin ...

Utilizing Next.js named imports without server-side rendering

I am looking to update this code snippet to use next.js dynamic imports without server-side rendering (SSR) import { widget } from "./charting_library/charting_library"; Here is what I have tried so far const widget = dynamic(() => import(&qu ...

Utilize Java to launch the HTML file in a web browser

I have a specialized need for my HTML files to be displayed on a platform that is browser-free. The solution that immediately comes to mind for me in this scenario is utilizing applet. Therefore, I am interested in having my HTML file (including CSS and JS ...