Arranging content within columns according to their column position

As I design a grid with three columns, each grid box contains a div with a maximum width of 280px. Sometimes the grid columns exceed this width. To achieve my desired layout, I aim to align the content in the grid boxes so that the left column aligns to the left, the central column aligns to the center, and the right column aligns to the right. For reference, please see the image below:

https://i.stack.imgur.com/6K0dI.png

My current approach involves using the CSS rule justify-items: justify on the grid container element. However, the current result is displayed below:

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

Seeking guidance on what adjustments can be made in CSS to achieve the layout depicted in my desired diagram.

Answer №1

To achieve this layout, you can utilize CSS Grid and target specific items within grid-columns, adjusting their alignment with the justify-self property.

A unique scenario arises when dealing with a row that contains only one item.

Check out this code snippet on JSFiddle

.container {
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(3, 1fr);
}

.item {
  width: 100px;
  height: 300px;
  background: pink;
}

.item:nth-of-type(3n) {
  justify-self: end;
}

.item:nth-of-type(3n + 2) {
  justify-self: center;
}

.item:nth-of-type(3n + 1):last-of-type {
  grid-column: 2;
  justify-self: center;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Answer №2

To achieve the desired layout, you can set the width of your item to 33% and then align the content of each column to the left, center, or right using the nth-child selector.

This can be accomplished using methods such as inline-block or flex, with the example below demonstrating the use of Flexbox.

Below is a sample code snippet:

.flex {
  display: flex;
  flex-wrap: wrap;
}

.flex .item {
  width: calc((100% / 3) - 10px);
  border: 1px dotted black;
  box-sizing: border-box;
  margin: 5px;
  display: flex;
}

.flex .item:nth-child(3n+2) div {
  margin: 0 auto;                  /* center align every 3rd, starting from item 2  */
}

.flex .item:nth-child(3n+3) div {
  margin-left: auto;               /* right align every 3rd, starting from item 3  */
}

.flex .item div {
  max-width: 280px;
  padding: 20px;
  background: lightgray;
}
<div class="flex">
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
</div>


If you need to support browsers that do not fully support Flexbox or have buggy behavior, an alternative method using inline-block and float can be used.

.parent::after {
  content: '';
  display: block;
  clear: both;
}

.parent .item {
  float: left;
  width: calc((100% / 3) - 10px);
  border: 1px dotted black;
  box-sizing: border-box;
  margin: 5px;
}

.parent .item:nth-child(3n+2) {
  text-align: center;              /*  center align every 3rd, starting from item 2  */
}

.parent .item:nth-child(3n+3) {
  text-align: right;               /*  right align every 3rd, starting from item 3  */
}

.parent .item div {
  display: inline-block;
  max-width: 280px;
  padding: 20px;
  background: lightgray;
  text-align: left;
}
<div class="parent">
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
  <div class="item">
    <div>Some content that can can be a maximum of 280px wide</div>
  </div>
</div>

Answer №3

Utilizing Flexbox is the way to go for creating these types of layouts effortlessly.

Make sure to include justify-content:space-between.

Check out this live demo on Fiddle

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

Design a Hover Mega Menu - click outside to close

Although there are similar topics on stackoverflow, the code I have is different from them. I am trying to achieve the following: If I click on the search box (the white square on the site), my search box should open If I open the search box and then cl ...

Sending values from buttons to different Django templates/views

Initially, the user provides a CSV file. (Template File:) <form method="post" action="{% url 'rowcol' %}" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file" accept=".csv"> <button type="sub ...

Trouble with Selecting an un-ordered list menu item in Selenium Java's side bar navigation

Exploring a different approach to selecting a menu item in the sidebar navigation within an iframe. After switching to the iframe, I am attempting to choose a specific item from the side menu to display its respective navigational items for selection. Thes ...

Unable to align the row in the center

I'm having trouble centering a row with 4 columns. It seems off when I look at the picture below, specifically at the vertical border under the "most read" blue square. html file: <div class="block-section"> <div class="sm-section-wrapper" ...

What is the best way to position a search icon on the right side using CSS?

I am trying to figure out how to display a search icon image on the right side using CSS, but so far I have been unsuccessful. Here is the code that I have: The CSS code: .search { background: url(../images/icons/search.png) no-repeat; display:in ...

Updating image URL for grouped objects with Fabric JS

Check out this link :http://jsfiddle.net/mishragaurav31/ang58fcL/#base I created two groups of objects; one consisting of a circle and text, and the other with two images. When I try to change attributes for group 1, it works fine. However, when attempt ...

Issue with DIV height in Internet Explorer occurs only when application is accessed using server name

Encountering a unique issue specific to IE (confirmed in v8 and v9). When utilizing jquery to determine the height of a div: $('#sys_MainPage').height() In Chrome, Firefox, and when accessing using the IP address, this code returns around 26 ...

Issue: The component factory for GoogleCardLayout2 cannot be located

Recently I've been working on an Ionic 3 with Angular 6 template app where I encountered an issue while trying to redirect the user to another page upon click. The error message that keeps popping up is: Uncaught (in promise): Error: No component fac ...

Tips for integrating a unique font into your PhoneGap project

I've been attempting to incorporate a custom font into my PhoneGap application, trying out two different methods I came across online. Unfortunately, neither of them seem to be effective when using PhoneGap 3 and Xcode 5. First method involves using ...

Building an HTML Form for Requests

Having some trouble creating an HTML form, particularly with resizing the Fieldset and legend elements. As a novice programmer, I am practicing building HTML forms. My goal is to have the form adjust its size based on the window without displacing the cont ...

Are there any other CSS methods available to address the limited support for flex-grow in Safari?

I'm currently developing an interactive "accordion" component that can accommodate multiple child elements. Each child item closes to the height of its header, but expands evenly with other open siblings when activated. If this explanation is unclear, ...

sticky placement changes when option is chosen

I'm experimenting with the CSS style called position: sticky and I've encountered an issue. Everything works perfectly until the select element is activated, causing the page to scroll back to the original position of the sticky element. <div ...

The code is malfunctioning on this site (yet functions perfectly on a different website)

So I have a question that may seem silly, but it's worth asking. I'm attempting to create a sticky div element that stays in place when you scroll past a certain point on the page. This is the script I have: <script type="text/javascript"> ...

Thymeleaf: Expression parsing error

I am working on a Thymeleaf template that includes pagination functionality. <ul class="results_perpage" > <li th:if="${previous != null}"><a th:href="javascript:movePage(`${previous}`);" class="results_menu" th:text="PREVIOUS">< ...

Add a 'tag a' directly following 'img'

Check out this code snippet: <a href="#" class="list-group-item"> <span class="badge"><?=$st ?></span> <img class="small_profile_pic" src="<?=$pic ?>" /> aaa<a ...

Top methods for integrating custom divs into your WordPress site using PHP

As someone who is new to wordpress, I have a solid understanding of php, html, css and javascript. I am interested in integrating a custom font resizer into my wordpress site similar to the example shown below: https://i.stack.imgur.com/FwoIJ.jpg What w ...

How to create nested nav menus that are optimized for touch-screen devices?

When it comes to creating responsive websites, one challenge I frequently encounter is designing a multi-level nav menu that functions effectively for touch devices. Many plugins and methods exist, but most fall short because they do not allow a second-l ...

Is there a way to create a navigation menu that highlights as we scroll down the page?

Check out this example of what I am looking for. https://i.stack.imgur.com/fdzvZ.png If you scroll, you will notice that the left menu highlights. Sub-menus will extend when the corresponding menu is highlighted and collapse when other menus are highlig ...

Get rid of the .php extension in the URL completely

Lately, I've been experimenting a lot with the .php extension. I successfully used mod_rewrite (via .htaccess) to redirect from www.example.com/example.php to www.exmaple.com/example. Everything is running smoothly. However, I noticed that even though ...

What is the best way to get rid of a connect-flash notification?

I'm having trouble removing the message (with the username displayed) after logging out by pressing the logout button. Every time I try to press the logout button, it just refreshes the page without any action. I want to stay on the same page and not ...