Aligning the column to the right to ensure the text is centered horizontally on the screen

<div className={css.title} >
        <div className={css.row}>
            <div className={css.columnLeft}>
              <div className={css.header}>Images</div>
            </div>
            <div className={css.columnRight}>
              <strong>Image 1 of 2</strong>
            </div>
        </div>
        <Icon />
      </div>

css

.row {
  width: 100%;
  display: flex;
  align-items: center;
}

.columnLeft {
  flex: 1 0 25%;
  text-align: left;
}

.columnRight {
  flex: 1 0 75%;
}

https://i.stack.imgur.com/CSZ9P.png

Currently does

I would prefer it to be horizontally aligned more towards the center rather than the left.

Answer №1

If you want to ensure that "Image 1 of 2" (center column) stays in the middle of the screen, here's what you can do:

  • Set the left and right columns with flex: 1 1 50%
  • For the center column, use flex: 0 0 auto

By defining the left and right columns with a base of 50%, they will have equal weight regardless of content width differences. The center column will then adjust its size based on the available space between the left and right columns to keep everything centered.

If you want the center column to start wrapping at a specific width, add a max-width and text-align: center to it.

That's pretty much all there is to it!

<div className={css.title}>
  <div className={css.row}>
    <div className={css.columnLeft}>
      <div className={css.header}>Images</div>
    </div>
    <div className={css.columnCenter}>
      <strong>Image 1 of 2</strong>
    </div>
    <div className={css.columnRight}>
      <Icon />
    </div>
  </div>
</div>

CSS:

.row {
  display: flex;
}
.columnLeft,
.columnRight {
  flex: 1 1 50%;
}
.columnCenter {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
}
.columnRight {
  display: flex;
  flex-direction: row-reverse;
}

Try out the HTML demo below:

.row {
  display: flex;
  justify-content: center;
}

.columnLeft, .columnRight {
  flex: 0 1 50%;
  border: 1px solid red;
}

.columnCenter {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
}
.columnRight {
  display: flex;
  flex-direction: row-reverse;
}
.middle {
  width: 1px;
  height: 2rem;
  background-color: red;
}
<div class="row">
  <div class="columnLeft">
    <div class="header">More content on left side</div>
  </div>
  <div class="columnCenter">
    <strong>I am centered</strong>
  </div>
  <div class="columnRight">Icon</div>
</div>
<div class="row">
  <div class="middle" />
</div>


Additional Notes:

1 - I modified the class name of the central flexible element from 'columnRight' to 'columnCenter' for clarity. I also wrapped the <Icon /> in a .columnRight container since having the center element labeled as 'columnRight' seemed confusing.
2 - Remember that flex layouts adapt to different content sizes, so if one column has significantly more content than the others, the centering may not be perfect. To maintain center alignment no matter what, consider setting a max-width on the side columns.

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

React.js - "Encountered error: Unable to access 'map' property of undefined variable"

I've tried various methods I found through searching, but none of them seem to be working. Why is it still showing that map is undefined? import TextField from '@material-ui/core/TextField'; import Autocomplete from '@material-ui/lab/A ...

A method for consolidating multiple enum declarations in a single TypeScript file and exporting them under a single statement to avoid direct exposure of individual enums

I am looking to consolidate multiple enums in a single file and export them under one export statement. Then, when I import this unified file in another file, I should be able to access any specific enum as needed. My current setup involves having 2 separ ...

Vanishing Tooltip following an implementation of the backdrop-filter

I'm having an issue with the blur effect on my background image. Although it works well, my tooltips also end up being blurred behind the divs: https://i.stack.imgur.com/BMdh4.png Is there a way to ensure that my tooltips stay on top of the subseque ...

Remove the presence of a black square when selecting elements using CSS

Update: After some investigation, I discovered that the mysterious black square is actually a part of the scroll bar. By adding the following code snippet: select::-webkit-scrollbar { display: none; } The square no longer appears. Initially, my se ...

Incorporating fontawesome icons ABOVE custom menu items in WordPress

In the process of customizing a menu in WordPress, I am looking to add fontawesome icons above each list item. In the past, I achieved this using the following code: <ul> <li> <a href="#"> <i class="fa fa-home"></i&g ...

How can the CORS issue be resolved when sending a form from a client to an AWS API gateway function?

Within my front end React application, I am looking to implement a contact form that will submit data to a Lambda function via an API Gateway with a custom domain attached. The frontend operates on the domain dev.example.com:3000, while the API Gateway is ...

"Successfully rendering the map on the initial load, but encountering an error of 'cannot map undefined'

Having trouble displaying data retrieved from an API. Initially, the movie titles are shown without any issues. However, upon refreshing the page, an error message stating "cannot map undefined" appears. Here is the code snippet: const [media, set ...

What advantage does React gain by utilizing the previous state to update state instead of the current state?

I'm seeking to grasp why it's better to utilize the previous state rather than the current state to update an element in React. Despite seeing this approach commonly used in code examples and tutorials, I haven't come across a satisfactory e ...

css - design your layout with floating div elements

I used to design layouts using tables, but now I'm trying it out with divs. However, I'm still struggling to get the layout right. It's not as straightforward as using tables. For instance, my code looks like this: var html_output = "< ...

Tips for choosing a row in Ag-grid when initiating a drag operation

My grid is set up with only 1 column. Currently, I am facing an issue where if row dragging is enabled, I cannot select rows unless I manually bind a key to set supressRowDrag=true. This should be possible and straightforward since the row node is alread ...

CSS/React JS: I wish for the input fields to maintain focus even after entering values

I'm working with a confirmation code that is made up of 6 digits. I've noticed that when I click on an input field, it becomes focused and underlined in blue. However, as soon as I start typing in the next input field, the blue underline disappe ...

Ensure the validity of a field in redux Form by checking it against another field

I am currently utilizing the redux-form library within my project, and I have a Component that contains multiple FieldArray elements. One of these FieldArray components generates a table structure similar to what is shown in the screenshot provided. In eac ...

Is it possible to change the style of an element when I hover over one of its children?

Encountered an issue while working with HTML and CSS: //HTML <div> <div class="sibling-hover">hover over me</div> </div> <div class="parent"> <div>should disappear</div> </div> ...

Enhancing Security Measures in a ReactJS Application on Heroku by Implementing Content Security

I recently encountered a frustrating issue with my CSP policy that I just can't seem to resolve. The setup involves a reactjs application running on Heroku within an expressjs environment. Here's a glimpse of my folder structure: ExpressApp ...

Error: props.addPost does not exist as a function

Help Needed with React Error: props.addPost is not a function. I am trying to add a new post in my app. Please assist me. import React from "react"; import classes from "./MyPosts.module.css"; import { Post } from "./Post/Post.jsx& ...

How to customize the color of radio buttons in JFXRadioButton using CSS

I am facing an issue with customizing the styling of JFXRadioButton component from this library : jfoenix.com I want to modify the color of the circle, but the default class is not working. Are there any suggestions or solutions available? (The colors me ...

Chrome Performance Profiling for React Apps

Currently, I am engaged in performance profiling using the Chrome developer tools on a single-page React application (version 15.6) that utilizes ag-grid-react. I've encountered a puzzling situation when trying to make sense of the profiler's out ...

Ways to have the right sidebar seamlessly blend in with the page content by hovering over it rather than occupying a separate area

While working on the design of my website, I encountered a challenge with the sidebar. I aim to have a sidebar that hovers from the right side to the left, displaying the full content when the cursor is placed over it, much like the one featured on . Altho ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

How to perfectly center an absolute positioned icon on a 767px screen

The icons positioned absolutely in the two columns must remain center aligned across all screen resolutions. The media query below attempted to align the icon on mobile devices, but it is inaccurate for any resolution. How can I correct the CSS? @media scr ...