Challenges arise when using CSS Grid to design a basic layout with rows and several centered divs

As a CSS Grid beginner, I am working on creating a simple layout for my personal blog using codepen.io

Visit my codepen.io project here

I aim to have multiple rows with full width, each containing various divs of different widths centered within the row. My goal is to have up to 10 divs arranged vertically in a single row. However, I'm facing challenges and it's not functioning as expected. Here's a basic image of what I'm trying to achieve (link provided below).

This is the HTML structure I'm using:

<div class="container">
  <div class="box menu">Menu items</div>
  <div class="box header">Header with custom inner box color</div>
  <div class="box posts">Blog posts go here...</div>
  <div class="box posts">
    <div class="post_small_width">Small width post</div>
    <div class="post_medium_width">Medium width post</div>
  </div>
  <div class="box pages">Pages section with specific width and center alignment</div>
  <div class="box footer">Site footer</div>
</div>

And this is the corresponding CSS code:

* {
    /* box-sizing: border-box; */
}

.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "menu"
    "header"
    "posts"
    "post_small_width"
    "post_medium_width"
    "pages"
    "footer"
  ;

  grid-template-rows: auto;
}

.menu {
  grid-area: menu;
  background-color: #444;
  padding: 10px;
}

.header {
  grid-area: header;
  background-color: pink;
  height: 100px;
  width: 600px;
  margin: auto;
}

.posts {
  margin:auto;
  background-color: yellow;
  grid-area: posts;
  padding-top: 10px;
  padding-bottom: 10px;
}

.pages {
  background-color: #ccc;
  grid-area: pages;
}

.post_small_width {
  background-color: red;
  grid-area: post_small_width;
}

.post_medium_width {
  background-color: red;
  grid-area: post_medium_width;
}

.footer {
  background-color: black;
  color: white;
  padding: 10px;
  grid-area: footer;
}

.box {

}

Check out the sample visual layout here.

Answer №1

Here is a solution that may help you achieve your desired outcome: https://codepen.io/binarytrance/pen/pWJPdN

It's essential to have a good understanding of DOM structure and responsiveness. Instead of assigning fixed width to parent containers, consider using max-width for better flexibility.

.parent-container {
   max-width: 900px;
   width: 100%;
   margin: auto;
 }

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

Enhancing your website with HTML5 Audio Player's Next and Previous functionalities

I've successfully created a playlist that is working, but I'm struggling to make the Next and Previous Buttons actually move forwards and backwards in the playlist. It seems like using the playNext function when next is clicked might work, but I ...

Discussing the use of local identification within a <BASE> tag in SVG specifically on the Firefox browser

Encountering an issue on a current Firefox browser running on Win7 while utilizing SVG (although the details may not be directly related to the problem): ` <head> <!-- base href="http://127.0.0.1/package/index.php" /--> < ...

Tips for enabling a keypad to restrict input field character limit

Encountering an issue with an input field that has a maximum length of 6 characters. Typing normally seems to work fine, but when using a custom keypad, the limit is not enforced and it allows for more than 6 characters to be entered. HTML: <div cl ...

Attempting to design a customized dropdown navigation feature using HTML

I am having some trouble creating a dropdown menu that functions properly. Here is the code I have written: <div> <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT A Subject:</H1> <select id ="dropDownId"> <!-- as ...

Creating a drop-down menu within an HTML table along with a D3 bar chart

How can I implement a drop-down menu allowing the user to choose a time interval for this HTML table and d3 bar chart? The time intervals needed are: Now, 24 hours, 48 hours, 72 hours, 1 week, and 1 month. I am relatively new to creating dynamic tables and ...

Custom Pug design without any CSS files added

I am having trouble with my .pug template in my express project as it is not including its stylesheets. I have referred to the pug documentation but still can't figure out the issue. Any help would be appreciated! FILE TREE: views `-- index.pug `-- ...

Endless Loading in Node.js on Google App Engine

I have deployed a standard nodejs app (free tier) using cloud shell, but when I try to access https://[ID].du.r.appspot.com/ , it keeps loading indefinitely. app.js: const express = require('express'); const path = require('path'); co ...

HTML elements generated dynamically do not possess any jQuery properties

I have implemented a draggable list of Div elements using jQuery. Here is the code: <div id="external-events"> <h4>List Of Staffs</h4> <div class="external-event" data-id="1">Name</div> //Draggab ...

Remove the ID, class, and other attributes from an HTML string using JavaScript for sanitization purposes

Can someone help me with sanitizing HTML text provided by a user? The HTML code in question is as follows: var htmlStr = `<p id="test" class="mydemo">TEhis is test</p> <pre class="css"> &lt;html> &lt;body cl ...

Guide on integrating React.js into an HTML development webpage

I can't seem to get this code to work properly. I have saved it as an .html file on my computer, but it's not displaying anything when I try to open it in Google Chrome. <DOCTYPE! html> <html> <head> <script src= ...

When hovering over the menu list, the background-color of the text area remains the same

I am facing an issue with my list menu. The hover effect changes the background color only in the box area, but not in the text area. I would like to figure out a way to make both areas change color on hover: Here is the code snippet: <div class="se ...

AngularJS Partial Views: Enhancing Your Website's User Experience

As a newcomer to the world of Angular, I am seeking guidance on a specific issue. I have encountered a one-to-many relationship scenario where one Category can have multiple Product items. The challenge lies in my layout page setup where I display various ...

Is it possible to rotate an image with a random angle when hovering in Angular?

I'm currently working on a photo gallery project and my goal is to have the images rotate when hovered over. However, I am experiencing difficulties in passing values from TypeScript into the CSS. HTML <div class="back"> <div cl ...

Animate div visibility with CSS transitions and Javascript

I'm currently working on incorporating an animation into a div that I am toggling between showing and hiding with a button click. While I have the desired animation, I am unsure of how to trigger it using JavaScript when the button is clicked. Can any ...

Preventing default form submission in jQuery: How to cancel it when a certain condition is met

I have a contact form where I validate the input values upon clicking on the submit button. If there is at least one empty input, I trigger an alert and prevent the form submission by using preventDefault. However, if all inputs are filled and submitted, t ...

Is there a way to inject C++ text into an input field on a webpage using QWebEngine?

I want to integrate a website with QWebEngine to manipulate input commands using Qt's event filters and more. The specific website I am working with requires a username/email and password, and I aim to manage the input of this text on my end. This inv ...

Is there a way to align these blocks in a single row?

Having trouble aligning these buttons horizontally. Any suggestions? I've been tinkering with this for a couple of hours now, so decided to seek help here. The desired effect should resemble the image below, but it's not quite there yet. Thank yo ...

Retrieve the identification number from the specific row chosen in the table

Having some issues with retrieving the correct ID from the selected table row in Datatable. The alert I'm using to check the output of the JS script always shows the same ID, regardless of how many rows are in the table list. Each row has a different ...

Is it possible to close the navigation menu by clicking on a link or outside of the navigation area?

Hey everyone, I've recently dived into the world of web design and encountered my first hurdle. I need your expertise to help me solve this problem. How can I modify my JavaScript to close the NAV element by clicking on one of the links or outside t ...

Troubleshooting: Issue with Bootstrap 4 navbar toggle button functionality

Is it possible to manually control the opening and hiding of the navbar when the toggle button is clicked in bootstrap 4? Currently, my navbar automatically opens and closes when clicking the toggle button. Any suggestions on how to modify this behavior wo ...