A grid of N columns by 2 rows where the last row will be centered if it contains only one column

My goal is to create a dynamic N-by-2 grid that adjusts its display based on run-time factors, resulting in an unknown number of rows and columns at design time. The grid could have different configurations like:

1 2
3 4
 5
or
1 2
4 5 (missing 3)
or
1 3 (missing 2, 5)
 4  (last column)

The challenge is to ensure that the last element is always centered within the grid. Each div is set as .col-xs-6 to establish the grid as N-by-2.

How can I guarantee that the last column will be centered without knowing which one it is beforehand during the design phase? Since the position of the last column is uncertain, using offsets for specific columns is not feasible. Any suggestions are welcome!

Check out this link for reference.

If CSS or Bootstrap alone cannot accomplish this, please advise me accordingly.

Answer №1

To target the odd last element in your scenario, you can make use of the last-child property. This way, you ensure that only the odd last elements are centered, while the even last elements remain unaffected. Here is how you can implement it:

.col-xs-6
{
  display:inline-block;
  text-align:center;
}
.col-xs-6:nth-last-child(1):nth-child(odd) { /* Selects the last child with an odd element */
  display:inline-block;
  text-align:center;
  width:100%;
}

Check out the DEMO

Answer №2

JSfiddle Demo - Example 1

Keep in mind that this method does not involve using floats and instead utilizes display:inline-block.

CSS Styling

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.row {
    text-align: center;
    font-size:0;
}

.col-xs-6 {
    float:none;
    font-size:1rem;
    display: inline-block;
    width:50%;
    border:1px solid grey;
}

JSfiddle - Example 2

This approach involves traditional floats with a clearing mechanism for the last item.

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.row {
}

.col-xs-6 {

    border:1px solid grey;
}

.col-xs-6:nth-last-child(1):nth-child(odd) {
    clear:both;
    float:none;
    margin:0 auto;
}

NOTE: Please refer to Suresh's comment regarding the necessity of ensuring the last child is odd. Acknowledged.

Answer №3

Utilizing Bootstrap 5 framework:

<div class="row row-cols-2 justify-content-center">
  <div class="col">1</div>
  <div class="col">2</div>
  <div class="col">3</div>
  <div class="col">4</div>
  <div class="col">5</div>
</div>

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 seamlessly integrating an overlay into a different image

My current system is set up to overlay the image when you check the checkboxes. However, I'm facing an issue with positioning the image inside the computer screen so it's not right at the edge. Can anyone provide assistance with this? <html&g ...

How can I personalize the preview feature in a Bootstrap rich text editor?

Currently, I am utilizing Bootstrap's rich text editor by this method $('#editor').wysiwyg();. Utilizing AJAX, I am passing the HTML content created by .wysiwyg to save it in the database. Later on, I plan to retrieve the saved HTML data to ...

Wide container using Bootstrap

Currently, I am incorporating Bootstrap v5 alpha to develop a basic dashboard. According to Bootstrap guidelines, it is essential to utilize .container, .container-fluid, or .container-{size}, followed by .row and .col columns. However, I wish for the das ...

Responsive HTML grid with a distinct line separating each row

My challenge is to create a layout that features a grid of floating divs with horizontal lines under intermediate rows. The catch is that there should not be a line under the last row, and if there is only one row of elements, no line should be displayed. ...

Is it possible to rearrange the sequence of HTML DIV's exclusively through CSS?

I need to style a webpage with specific requirements that involve positioning various DIV elements without altering the HTML file. The Header DIV must be at the top of the page with 100% width, followed by the Navigation DIV (25% width) on the left side b ...

Spacing between posts using CSS and jQuery

Currently, my code is creating a random number of new articles within the body of my webpage. I have successfully implemented different colors for each article, but I am encountering an issue with padding. Being new to web development, I am attempting to s ...

HTML/CSS: What could be causing the iframe to not scroll properly?

I am facing an issue with my website that has 3 iframes, none of them seem to be working properly. In Firefox, the scroll bars appear but do not function, while in Chrome, the scroll bars do not appear at all. Surprisingly, they work fine in IE 6. This pr ...

Closing the Bootstrap Dropdown Menu

I have a dropdown menu in my bootstrap project. I am looking for a way to make the menu close when I click on one of the menu items, without refreshing the page. The links function like an ajax request model, ensuring that the page does not reload. <l ...

Is it possible to align Bootstrap buttons and input fields in a single row, and have the input field stretch to

I'm struggling to align Bootstrap buttons and input on the same line, with the input stretching horizontally while snugly fitting against the buttons. Here are my different attempts: https://i.sstatic.net/wuUsr.png In attempt #1: the button group an ...

A fading transparency box on the border

Looking for a box with varying opacity from left to right (See image for reference - the red marked box has decreasing opacity from right to left). Here is my attempt: .waterMark { background-color: #FFFFFF; float: right; opacity: 0.6; position: ...

I am struggling to make the horizontal scroll and fixed column features work in dataTables. The rendering seems to be inconsistent across different platforms. Can anyone help me figure out what I am doing incorrectly?

I've dedicated countless hours to unraveling this conundrum. I'm in urgent need of creating a table that mirrors the layout displayed on this webpage: https://datatables.net/extensions/fixedcolumns/ The desired functionality involves both verti ...

It appears that JavaScript has the capability to automatically remove event listeners from textareas

Greetings to all members of the community, I have developed a web application (see code below) that, when double-clicked, inserts a text area into the frame. Upon double-clicking the text area, it changes its color to green, and with another double-click ...

Achieve stacking one div on top of another without specifying a position and utilizing inline CSS only

Currently, I am working on creating a unique design for a letter that will be sent out to customers via my website. In my efforts to achieve this, I came across a simple design that caught my eye. To view the design inspiration, visit this link My goal i ...

Changing the display order of divs in Angular 2 when they are shown or hidden

I have a fixed length div called Content div that contains two other divs - div1 and div2. Both of these divs are larger in size compared to Content div, allowing the user to scroll within the container. The user will initially read the content in div1 and ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

Mastering the art of positioning images using CSS and HTML in email marketing

Currently in the midst of designing an email template for a new project. I have set up a table, with one row featuring some stripes and the next row showcasing a black bar to match the site's layout. My question is: Is there a way to incorporate an i ...

What are the methods for creating a diagonal line using GWT or the combination of HTML, CSS, and JavaScript?

I'm currently developing a web application that requires connecting elements with lines. I would prefer the lines to not be limited to just horizontal or vertical paths. Additionally, I need to be able to detect clicks on these lines. I've explor ...

Opting for css3 translate over using position: left for positioning elements

Why Using translate() for Element Movement is Superior to Pos: abs Top/left Read the full article Lisa Anderson I struggled with implementing a CSS3 translate animation, so I opted for a jQuery solution that achieves the same effect. Click on the X icon . ...

On a desktop view, the content is displayed in 4 columns, while on smaller

In my simple block, I am trying to arrange 4 items in a row on desktop and 2 items per row on smaller devices like tablets and mobile. The desired layout is shown below: https://i.sstatic.net/3i799.png To see a live demo, visit: jsfiddle Below is the HTM ...

Error: Uncaught TypeError - The function indexOf is not defined for e.target.className at the mouseup event in HTMLDocument (translator.js:433) within the angular

Upon clicking on an SVG to edit my data in a modal bootstrap, I encountered the following error: Uncaught TypeError: e.target.className.indexOf is not a function at HTMLDocument.mouseup (translator.js:433) This is my SVG code: <svg data-dismiss ...