Achieve perfect centering for content and left align text with CSS Grid

It seems like there should be a simple solution to this issue, but I'm having trouble finding it. I'd like to replicate the layout shown in the image linked below:

https://i.sstatic.net/5GsWL.png

The challenge is to center the item and quantity rows below the details title, while keeping the text inside them left-aligned. Here's my current attempt at achieving this:

.grid-container {
  background: gray;
  display: inline-grid;
  justify-items: center;
  align-content: center;
  grid-template-columns: 1fr 1fr;
  padding: 10px;
}

.details {
  font-weight: bold;
  grid-column: span 2;
}

.info {
  text-align: left;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="./style.css" />
    <title>Document</title>
  </head>
  <body>
    <div class="grid-container">
      <div class="details">Details</div>
      <div class="info">Item</div>
      <div class="info">Beans</div>
      <div class="info">Quantity</div>
      <div class="info">41</div>
    </div>
  </body>
</html>

While the content is centered correctly, the text remains unjustified. Is there a way to achieve this using CSS Grid?

Thanks for any assistance provided.

Answer №1

To adjust the alignment, simply delete the line justify-items: center (which centers each item's content within the item) and add justify-self: center to the .details class.

Give it a try:

.grid-container {
  background: gray;
  display: inline-grid;
  align-content: center;
  grid-template-columns: 1fr 1fr;
  padding: 10px;
}

.details {
  font-weight: bold;
  grid-column: span 2;
  justify-self: center;
}
<div class="grid-container">
  <div class="details">Details</div>
  <div class="info">Item</div>
  <div class="info">Beans</div>
  <div class="info">Quantity</div>
  <div class="info">41</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

There seems to be a JSON syntax error on the website for tracking Covid-

Hi everyone, I'm currently working on a Covid-19 tracker website and facing an issue that says: 'SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data' ; Being new to web development, I am unsure about what&apo ...

Slider buttons are not appearing in Time Picker

I recently added a Time Picker to my project, but I'm experiencing an issue where the slider buttons for selecting time are not showing up. Could you please refer to this image: Checking the console, there don't seem to be any errors present. ...

Incorrect calculation of offsetWidth occurs in presence of special characters within text

I'm currently trying to determine the width of a specific text in my scenario. Below is a simple example of my code: var text = "Country <textinbrackets> and some following text"; var textObj = document.createElement('text'); $(textOb ...

Tips for utilizing iemoji on your Edge browser

Here is a snippet of code I'm working with: <option> &#x1F1E9;&#x1F1EA; Deutsch</option> or <option> &#127465;&#127466; Deutsch</option> When I view this on Firefox, it appears as shown in the image below: h ...

RegEx for Filtering Out HTML Entities Without Escaping

Is there a way to prevent the use of unescaped ampersands in a specific input field? I've been struggling to create a RegEx that blocks "&" unless it's followed by "amp;" or just avoid the usage of "& " (with a space). I attempted to mod ...

Incrementing the image name by 1 each time a new image is added to the directory

Currently honing my PHP skills and in the learning process. Developed a simple script that enables users to upload images to a directory on the server. I aim to append a number at the end of each image name to prevent duplication. Here is my attempt at i ...

Tips on adjusting font color without activating the :visited state

Currently, I have set up the following HTML and CSS: <div id = "navi"> <ul> <li><a id = "topLinks" class = "currentPage" href = "index.html"> Home </a></li> <li><a id ...

Ways to dynamically modify the CSS attributes of a webpage during the page load event

I'm looking to incorporate the background:rgba(226, 93, 141, 0.76) attribute into the body:after psuedo-element once my page has finished loading. Below is my HTML and CSS code: <style runat="server" id="myStyle" > body:after{ position:fix ...

`The LiipImagineBundle HTML encoding error`

Utilizing LiipImagineBundle, I efficiently resize my images after uploading. However, I encountered an error specifically when trying to upload certain JPEG files due to HTML encoding issues. The resizing process works fine with JPEG files from my camera, ...

Utilizing the onchange event handler to dynamically alter the webpage upon input

I am attempting to dynamically change my HTML page based on certain input. However, I have encountered issues with JavaScript event handlers not functioning as expected in achieving this. Below is the code snippet I used: var nam = document.getElementById( ...

Conceal social media icons when hovering over specific images similar to the feature on the website medium

I am facing an issue with fading out social links that appear over medium and large images on a webpage. I want the social links to fade out when they are over the images and fade back in once they move off the images. The number of medium and large images ...

Can the :not pseudo selector be utilized in conjunction with the adjacent sibling combinator?

I am attempting to achieve the following - when the table footer is rendered, I want to remove the border radius from the table cells within the last row inside the table body. I am unsure if the code below will do the trick: table tbody:not(+ tfoot) tr td ...

Changing the position of navigation items within a Bootstrap4 navbar - what's the trick?

I'm facing a challenge with aligning my navigation items in the center of my Bootstrap navbar. I don't want them all the way to the outside or completely centered, but rather have the left elements aligned to the center-left and right elements to ...

Eliminating classes with jQuery through the use of logical operators

I have been trying out some experiments and I got curious if there is a way to eliminate a class from a div in all divs except the specified one: $(function() { $('#tab-2').click(function() { ...

How can JavaScript Regular Expressions be used for Form Validation?

For my registration form, I am including fields such as userid, name, email, password, confirm password, and affiliation. Using regular expressions in JavaScript, I am validating these fields. I am attempting to display any validation errors within the for ...

Border added to Bootstrap layout with 5 columns

Trying to figure out the best way to create a 5 column layout in Bootstrap with borders and spacing. https://i.sstatic.net/CG5MX.jpg A custom class has been created to accommodate the 5 column grid setup: .col-xs-15, .col-sm-15, .col-md-15, .col-lg-15 { ...

What is the best way to center content without aligning text in the center using Bootstrap?

When using Bootstrap, I have divided my main page into two grids to enable content to stack vertically in mobile view. However, I am struggling to center the text horizontally and vertically within the grid without aligning the text to the center using the ...

Creating dynamic div shapes in HTML is a fun and creative way

Is it possible to create a div with a unique shape? I know that by default, divs are rectangular and you can achieve some shapes using the border-radius property, but what I really want is a semi-leaf shaped element. Something like this: The image might n ...

What is the best way to create a JQuery function that fills a div based on the selected option from an HTML select

Check out this simple JavaScript function below. <html> <head> <script> $(document).ready(function() { $.get('getImage.php', function(data) { $('#imageSelector').html("<select>" + d ...

Is there a way to update components in Angular without affecting the current URL?

I want to update a component without changing the URL. For example, I have a component called register. When I visit my website at www.myweb.com, I would like to register by clicking on sign up. How can I display the register component without altering the ...