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

What could be causing my Rest API request to malfunction?

Currently, I am working on a Pokedex website as part of my practice to enhance my skills in using API Rest. However, I have encountered some issues with the functionality. When users first enter the site, the API is being called twice unnecessarily. Additi ...

Detection of TextField blur event in Material UI

Is there a way to detect when the user leaves or focuses away from a field in Material UI TextField? I am looking for two events - one for entering and one for leaving the field. I know that we can use onFocus to handle entering the field, but is there an ...

Ways to prevent a centered list from shifting to the right

Although this question has appeared before, I am facing an issue with a list displayed as an inline block for use as a navigation bar. My goal is to center the nav bar on the webpage and ensure it scales down appropriately. <ul class="nav_bar"> ...

Show HTML code inside a text box

I have a text box that I populate with data from a Json response like this: <div class="gadget-body" style="height:100px"> <label>{{ textData}}</label> </div> Now, the Json response contains HTML code with <p> and < ...

Is there a way to trigger a dialog to open upon clicking on a particular date?

Currently, I am working on developing a scheduling application. My goal is to create a feature where clicking on a specific date will open a dialog box. To accomplish this, I am utilizing the ShadCN Date-picker. calendar-picker.tsx "use client" ...

The function mustAsync onSuccess is not present in this type (typescript)

I currently have 2 mutations that are functioning well. However, I encountered an issue when attempting to implement onSuccess and invalidateQueries. The error message displayed is as follows: Property 'mutateAsync' does not exist on type '{ ...

What steps should I take to resolve the "StrictMode has found deprecated findDOMNode" error?

Whenever I trigger the button to open the drawer, my console displays the warning message '' findDOMNode is deprecated in StrictMode'' The container for the button component is called Sidenav import Sidenav from './Sidenav'; ...

Position the div to align with just one side of the table-cell

I am struggling with making the height of my code dependent on only the left column, while still allowing the right column to be scrollable if it contains more content than the left column. Despite my best efforts, I can't seem to figure out how to a ...

A fresh javascript HTML element does not adhere to the css guidelines

While attempting to dynamically add rows to a table using Javascript and jQuery, I encountered an issue. Here is my code: <script> $(document).ready(function(){ for (i=0; i<myvar.length; i++){ $("#items").after('<tr class="item- ...

Dealing with lag problems while feeding a massive dataset into the Autocomplete component of Material-UI

In my React project, I have integrated the Autocomplete component from Material-UI to enhance user experience. However, when attempting to pass a large dataset of 10,000 items to the Autocomplete component as a prop, there is a noticeable delay of approxim ...

How can I input text into separate tabs using a specific method?

I've been struggling with this issue for a while now and here's the code I have so far: <html> <head> <script src="http://mihaifrentiu.com/wp-content/themes/mf/js/jquery_1.7.1.min.js" type="text/javascript"></scr ...

Rendering a page for a missing resource

Within the App.js file, the routes component is currently only wrapping a portion of the website. However, I would like the NotFound component to be rendered for the entire page if an incorrect URL is entered. Can you please provide guidance on how this ...

Modify the color of a sub division's background using CSS when hovering over

As I work on this Fiddle, my goal is to change the background of each div section individually when moused over. However, currently, hovering over one section triggers the background change for all sections. Is there a way to ensure that only the specific ...

Customizing external elements with React's inline styling feature

Trying to use React to style elements generated by a third-party library has been a challenge. In the snippet below, I was able to achieve the desired styling using CSS, but now I am looking to accomplish the same using JavaScript. This way, users can defi ...

Trouble accessing Strapi CMS data on Next.js frontend using React.js

Currently, I am in the process of developing a website using Strapi as the CMS and Next.js(React) for the Frontend. The website features an image slider that includes an image, a headline, and a description. I am trying to retrieve these elements from my S ...

Unusual discovery: Mysterious object manifesting in my HTML on Chrome/Webkit (with visual evidence and interactive demonstration)

I'm at a loss with this peculiar artifact that has appeared in my HTML - take a look at this highly magnified screenshot: Highlighted in red is this bizarrely small line. I've scoured my code but can't seem to pinpoint its origin. I'v ...

Reducing the Distance Between Columns in Bootstrap 4

I am currently working on embedding two websites onto my own website. The idea is to showcase these websites side by side within a custom-sized iframe that adjusts responsively based on the viewport size. To achieve this, I am utilizing Bootstrap 4's ...

The CSS background image is not functioning

.menubar li { float: left; width: 115px; text-align: center; background-image: url(js/left_main_bg_image.gif); } Having issues with the background image not displaying properly in IE, Firefox, and Chrome. I've tried adding it to a tab ...

What is the best way to ensure uniform header height on cards or similar elements using flexbox?

Avoid using hacks Do not hard code the solution. The goal is to create a solution that works for dynamic content and responsive screen sizes, rather than just one specific case. The issue lies in the fact that the headers are not direct children of the e ...

When you add the measurements of two images to the top bar, you get the viewport size

Requirement 1: A top bar measuring 46px in height Reqirement 2: One photo positioned at the top and another at the bottom I have attempted the techniques mentioned on How to resize an image to fit in the browser window?. However, the images still appear ...