One div is experiencing trouble with the `align-items: center` property, while the other divs are functioning

Presenting my React component:

<div className='sidebar'>
        <div className="sidebarItem">
            <span className="sidebarTitle">About Me</span>
            <img src="https://wallpaperaccess.com/full/1129022.jpg" alt="" />
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magnam sit 
               accusantium perferendis earum. Numquam libero vero, fuga, omnis.</p>
        </div>

        <div className="sidebarItem">
            <span className="sidebarTitle">Categories</span>
            <ul className="sidebarList">
                <li className="sidebarListItem">Life</li>
                <li className="sidebarListItem">Music</li>
                <li className="sidebarListItem">Movies</li>
                <li className="sidebarListItem">Tech</li>
            </ul>
        </div>

        <div className="sidebarItem">
            <span className="sidebarTitle">Follow Us</span>
            <div className="sidebarSocial">
                <i className="sidebarIcon fa-brands fa-facebook-f"></i>
                <i className="sidebarIcon fa-brands fa-instagram"></i>
                <i className="sidebarIcon fa-brands fa-twitter"></i>
            </div>
        </div>
    </div>
  

Including my CSS styling:

.sidebar{
    flex: 3;
    margin: 20px;
    padding-bottom: 30px;
    background-color: hwb(0 96% 1%);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}


.sidebarTitle{
    
    align-self: center;
    margin: 10px;
    padding: 5px;
    width: 80%;
    border-top: 1px solid #a7a4a4;
    border-bottom: 1px solid #a7a4a4;
    font-family: 'Rubik', sans-serif;
    font-size: 12px;
    color: #222;
    font-weight: 600;
    line-height: 20px;
    text-align: center;
    display: flex;
}

.sidebar img{
    margin-top: 15px;
    width: 300px;
    height: 400px;
}

However, the 'align-items: center' property in the sidebar class is not working for the first sidebar item. Here's how it looks:

https://i.sstatic.net/cruzn.png

I want all the divs to be centered, but the first one remains off-center. Any insights on what I might be missing here?

Answer №1

<code>
.side-nav {
      flex: 3;
      margin: 20px;
      padding-bottom: 30px;
      background-color: hwb(0 96% 1%);
      border-radius: 10px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    
    .side-nav-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .side-nav-title {
      margin: 10px;
      padding: 5px;
      width: 80%;
      border-top: 1px solid #a7a4a4;
      border-bottom: 1px solid #a7a4a4;
      font-family: "Rubik", sans-serif;
      font-size: 12px;
      color: #222;
      font-weight: 600;
      line-height: 20px;
      text-align: center;
    }
    
    .side-nav img {
      margin-top: 15px;
      width: 300px;
      height: 400px;
    }
    </code>

Answer №2

Your sidebar class is set to center all children, but you also need to center the child contents by modifying the following CSS properties:

.sidebar {
  flex: 3;
  margin: 20px;
  padding-bottom: 30px;
  background-color: hwb(0 96% 1%);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.sidebarTitle {
  align-self: center;
  margin: 10px;
  padding: 5px;
  width: 80%;
  border-top: 1px solid #a7a4a4;
  border-bottom: 1px solid #a7a4a4;
  font-family: 'Rubik', sans-serif;
  font-size: 12px;
  color: #222;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
}

.sidebarItem {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.sidebar img {
  margin-top: 15px;
  width: 300px;
  height: 400px;
}

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

Tips for eliminating inline CSS usage in React

Is it possible to avoid using inline CSS in React when styling an element like this? const dimensionStyles = { width: 10, height: 20 }; <div className="point-class" style={{ width: dimensionStyles.width + "px", height: ...

Ways to receive alerts when a marker enters a polygon

Looking for a way to receive notifications if my marker enters any of the polygons on the map. Having trouble implementing it correctly, here is my current code: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com ...

Is it possible to apply a delay function to JQuery's AjaxStart/Stop events?

As I was working on my single-page app, I thought about adding a spinner and gray background to provide feedback to users while processes are loading. I discovered JQuery's AjaxStart() and AjaxStop() functions, which allow me to display the spinner du ...

``Maybe there is a way to fix the issue of jQuery not functioning properly

I am currently working on integrating jquery with Reactjs to add a class on click event. The functionality works fine when the page is refreshed, but it stops working if I navigate to the page after clicking on any menu item without refreshing. How can I ...

Is it possible to set data using a React Hooks' setter before the component is rendered?

Instead of making a real API call, I am exporting a JS object named Products to this file to use as mock data during the development and testing phase. My goal is to assign the state of the function to this object, but modified with mapping. The current st ...

Is there a way to achieve a flex layout without having to modify the HTML structure?

I am working on achieving a specific layout using CSS: https://i.stack.imgur.com/xg7ZA.png This is the HTML structure I have and it cannot be altered, along with what I have managed to add with CSS. It's almost there but missing something: .conta ...

Cannot assign type void to 'Intrinsic Attributes & Dispatch<SetStateAction<>>'

Just starting out with typescript and ran into this error: Error :Type '{ setTasks: void; }' is not assignable to type 'IntrinsicAttributes & Dispatch<SetStateAction<IStudy[]>>'. Property 'setTasks' does not e ...

Creating a dynamic effect with CSS: animating a gradient background on hover

I'm currently working on styling my menu items with a background gradient that changes upon hover using the following CSS: #sidebar ul li a:hover { background-image: linear-gradient(bottom, rgb(68,68,68) 5%, rgb(51,51,51) 100%); background-im ...

React Component failing to display properly in Bootstrap tab

Why are my tabs and tabpanels showing up correctly, but the furniture components are not being rendered? Is there a fundamental issue with how I am trying to implement this? <Tabs defaultActiveKey={Object.keys(this.state.fTypes)[1]} transition={fal ...

Menu Toggle with Bootstrap 3

I have implemented two Bootstrap 3 navigation menus on a single page. One menu works fine when the toggle button is activated for responsiveness, but the other one doesn't work as expected. How can I resolve this issue? Below are the code snippets fo ...

What is the best way to condense all nested elements within a parent element?

I have a collapse element that contains some nested collapse elements. I am trying to make sure that when the parent collapses, the nested item also collapses. Although it appears to collapse when closed, it does not stay collapsed when I reopen the paren ...

In React (Next.js), the act of replacing a file is performed instead of adding a file

I kindly request a review of my code prior to making any changes. const test = () => { const [files, setFiles] = useState ([]); //I believe I need to modify the following statement. const handleFile = (e) => { const newFiles = [] for (let i= ...

Steps to create inactive HTML with a loading GIF displayed

When the sign-up button is clicked, a modal pops up. After inputting data, upon hitting the "sign up" button, I would like the actual popup's HTML content to become inactive and display a loading GIF icon in the center until an AJAX response is receiv ...

Using Private and Protected Methods in Typescript with React: Best Practices

Currently, I am engrossed in developing a React application utilizing Typescript. In cases where a component needs to offer functionality through a reference, I typically incorporate a public method (public focus() : void {...}). However, I find myself pon ...

Is it possible to set the state of my datepicker to a previous value only if the new one is determined to be invalid?

I am currently facing an issue with a library I'm utilizing, which has the potential to generate incorrect values that may cause my page to crash. To prevent this, I want to ensure that only valid values (the result of useDateRangePickerState) are app ...

Ensuring VSCode cooperates with React syntax: A guide

Even after installing all the recommended extensions, I am still facing issues with VSCode not recognizing any React syntax. How can I get VSCode to properly handle React (js / jsx) syntax? https://i.sstatic.net/H2RKO.png ...

Choose a procedure to reset to the original setting

I'm attempting to achieve something that seems straightforward, but I'm having trouble finding a solution. I have a <select> tag with 5 <option> elements. All I want to do is, when I click a button (which has a function inside of it), ...

Adjust the background color of the header as you scroll

Seeking assistance in adjusting the background color of my header upon scrolling. This is my current implementation: header.component.ts export class HeaderComponent { ngOnInit(): void { const header = document.querySelector('.header'); ...

Is it possible for me to add a div that consistently hovers above other divs on the page

Beginner in the world of CSS. Currently, I am developing a Safari extension where clicking its toolbar button will display a 'dialog banner' (comprising text and a few buttons) on the web page. This banner, in the form of a div, is dynamically a ...

When working on styling a different Styled Component, how should one define the type of props required?

I'm currently working on a NextJS project using styled components and typescript. I have customized a div element like this: export const ClippedOverlay = styled( ( props: React.DetailedHTMLProps< React.HTMLAttributes<HTMLDivElement& ...