Is it possible to align a row so that it is centered based on one of the columns in the row?

Calling all experts in CSS and HTML: Let's say I have 3 columns in a row, each with a different size (refer to the image).

Now, picture this - I want to center the 2nd column right in the middle of my page.

If I simply use justify-content: center to center the content of the entire row, it would center the whole row as shown below.

But here's the challenge: how do I only center the 2nd column on the page, essentially basing the row's alignment on just one column while keeping them all in a row?

Is there a way to achieve this? Appreciate your insights.

Note: Please refrain from suggesting obvious solutions like "add margin-left to the row." The solution should be adaptable and responsive.

<!-- Code using bootstrap v15 classes -->

<div class="row w-100">
    <div class="col-2 purple-bg">
        <span>1st column</span>
    </div>

    <div class="col-4 green-bg">
        <span>2nd column</span>
        <b class="d-block">I want this column to be centered in the page</b>
    </div>

    <div class="col-6 red-bg">
        <span>3rd column</span>
    </div>
</div>

https://i.sstatic.net/M2HHx.jpg

Answer №1

Utilizing grids can be very effective in this scenario. By creating 3 columns and placing the left element in the first column, the right element in the third column, and the centered element in the middle column, you can then align the first column element to the right.

.container{
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    grid-template-rows: 1fr;
}

.col1{
    background-color: purple;
    width: 70px;
    grid-column: 1;
    justify-self: right;
}

.col2{
    background-color: green;
    width: 50px;
    grid-column: 2;
}

.col3{
    background-color: red;
    width: 90px;
    grid-column: 3;
}

.col{
    height: 20px;
    grid-row: 1;
}

/* Page setup */

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.vert{
    position: absolute;
    left: 50%;
    right: 50%;
    background-color: blue;
    height: 100vh;
    width: 1px;
    z-index: 10;
}

body{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;
}
<body>
    <div class="vert"></div>
    <div class="container">
        <div class="col col1"></div>
        <div class="col col2"></div>
        <div class="col col3"></div>
    </div>
</body>

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

Adjust the colors of two elements by clicking a button's onclick event

As stated in the title, I have a button in the books component. When this button is clicked, the color of a div within the books component and another div in the navbar component should change. - Below is the code for the books component : export class Bo ...

Dots are used to indicate overflow of title in React Material UI CardHeader

Is there a way to add ellipsis dots to the title in my Cardheader when it exceeds the parent's width (Card width)? Here is what I have attempted so far: card: { width: 275, display: "flex" }, overflowWithDots: { textOverflow: &apo ...

Applying Validators manually in Angular 2 beta 17

We are currently working on a legacy project that needs to be maintained until the final version with angular-final is deployed. Once we upgrade to the final version, I will be able to easily apply conditional Validators using: this.myForm.controls[&apos ...

Troubleshooting: Datepicker not appearing in Bootstrap

Here is the detailed markup for the datepicker component: <div class="form-group row"> <div class="col-xs-3 col-md-offset-9"> <label class="control-label">Pick Date</label> <div class="input-group date" id="dp3" data-d ...

Arranging divs within list items in a single row

My goal is to create a dropdown menu within an <li> element of <ul>. Here's what I have so far: HTML: <ul class="navigationTable"> <li>1</li> <li>2</li> <li><div id="comittee"> <div id= ...

Having trouble getting the CSS 3 Spin feature to work?

Using webkit for previewing in chrome. The spinning circles I created are not working properly (definitely not an HTML issue.) #loader-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1000; } #loader { display: inline-bl ...

Tips for accessing the @keyframes selector and extracting the value from it

In my CSS code, I have a shape element with an animation that spins infinitely for 50 seconds. #shape { -webkit-animation: spin 50s infinite linear; } @-webkit-keyframes spin { 0% { transform: rotateY(0); } 100% { transform: rotateY(-360deg ...

Unable to pass a parameter through an Angular http.get request

I've encountered an issue where I am attempting to pass the page number and page size values to a web API, but for some reason, no parameters are being passed. I have thoroughly debugged the application in VS Code, and verified that the pagingModel ob ...

The display of the AngularJS {{variable}} is not supported within a script tag

<script type="text/template"> <div class="abcd"> <div class="efg" data-price="{{::displayPrice}}"></div> </div> </script> I am facing an issue with a template that contains a div element with the da ...

Retrieve ag grid from TypeScript file

I am currently utilizing ag-grid-angular in my Angular application to showcase data. I have a button that is located outside the ag grid, and when it is clicked, I need to retrieve all row data from the grid. I am aware that there is an API available for a ...

The protractor-jasmine2-screenshot-reporter seems to be failing to generate the necessary screenshots within the designated folder

I have encountered an issue with my protractor.conf.js file and need some assistance. I have created the target/screenshots folder manually in the root of my angular-cli project, but when I run protractor conf.js, the protractor tests in the browser window ...

Is there a way to display shortened text only when hovering without altering the height of the list box?

Here is my script for a vue component : <template> ... <b-card-group deck v-for="row in formattedClubs"> <b-card v-for="club in row" img-src="http://placehold.it/130?text=No-image" img-alt="Img ...

Incorrect implementation of Bootstrap CSS in Jade template

Currently, I am leveraging Express to construct a website. However, there seems to be an issue with my jade template not displaying the bootstrap grid system accurately. Despite double-checking that my app path is correctly set through app.use(express.stat ...

Sequence of HTML elements arranged in a stack

Recently, I came across a useful jQuery tutorial called "jQuery for Absolute Beginners: Day 8". In this tutorial, there is an interesting code snippet that caught my attention: $(function() { $('.wrap').hover(function() { $(this).childre ...

Sending jQuery events from an aspx page to an ascx control

Yesterday, I began utilizing ascx and started breaking down my "dynamic" default.aspx page into smaller segments. In default.aspx, there is a significant amount of JavaScript/jquery code responsible for managing various events. After moving some html code ...

I possess legacy Angular 1 code in which I implemented div styles. How can I replicate the same functionality in Angular 11?

Recently, I have been revamping an older project and encountered a challenge in setting the style of an array where each point needs to have a unique position. The Divs are all identified by id = obj.id, and I must assign specific left and top values to ea ...

Having trouble locating the Xpath for the download link, it appears to be listed as a list item (li). I've attempted using the Contains function without success. Any assistance would be

Seeking the Xpath for the image below. The Download File link is on the right and the element DOM is on the left. enter image description here I attempted to find the Xpath by directly copying it and also using Contains text "Download File", but my scrip ...

Converting an empty response to JSON in Angular2's Http module

Hello, I am currently working with the following code snippet: save<T>(url: string, data:any, headers = null): Observable<T>{ return this.http .post(url, data, headers) .map(x =>{ return x.json() as T ...

What is the best way to access the inner html of every cell within a table row that I have selected?

I want to implement a feature on my website where users can click a "save" button on a specific row in a table and save the entire row's innerHtml onto another page as their favorite hiking trails. I've been trying to achieve this by adding clic ...

Bootstrap - the input group button is designed to align to the right side

Currently, I am in the process of constructing a navigation bar for a website that includes a search bar and a grouped button. Everything functions properly when viewed on a desktop, but as soon as I switch to mobile view and the collapsible menu is activa ...