Best Practices and Tips for Layout Design Using HTML and CSS Grid

As I begin my journey into front-end development, I am currently immersing myself in studying html, css, and vanilla javascript.

While things are going well so far, I find myself struggling with understanding the organization of design layouts in general. Responsive design has also become a challenge for me as I struggle to define a default layout correctly.

What are the best practices when it comes to this?

When using CSS GRID, should I stick to creating basic areas like header, nav, main, footer? Or should I delve deeper and create nested grids within div/section elements?

For example:

Should I define in CSS:

#main-grid{
  display:grid;
  grid-template-areas:
    "nav nav"
    "head head"
    "bio bio"
    "picture1 article1"
    "article2 article2"
    "picture2 picture2"
    "article3 picture3"
  grid-template-columns: auto auto;

or would it be better to simply declare a main content area (span 1) with CSS GRID and insert div elements inside in the HTML (using flexbox, float, adjusting width and height, etc.)?

#main-grid{
          display:grid;
          grid-template-areas:
            "nav"
            "head"
            "main-content";
          grid-template-columns: auto;
    }

and #main-content in HTML:

<div>picture1 + article1 </div>
    <div>article2 + picture2</div>
    <div>article3 + picture3</div>

I understand that approaches can vary based on individual preferences and project requirements, but I believe there must be some best practices or default logic to follow. Any insights would be greatly appreciated!

Thank you!

Answer №1

Personally, I have reservations about

#main-grid{
  display:grid;
  grid-template-areas:
    "nav nav"
    "head head"
    "bio bio"
    "picture1 article1"
    "article2 article2"
    "picture2 picture2"
    "article3 picture3"
  grid-template-columns: auto auto;
}

the concept behind this approach. What would happen if there were a large number of articles and pictures involved?

A more effective strategy could involve utilizing a combination of grid for overall layout management, alongside flexbox/grid to handle the individual articles and images.

To achieve a responsive design, one example method could be:

body {
  margin: 0;
  padding: 0;
}

* {
  box-sizing: border-box;
}

.mySite {
  display: grid;
  grid-template-columns: 1fr .5fr;
  grid-template-areas: 
                  "header header"
                  "main   aside"
                  "footer footer";
}

header {
  grid-area: header;
  height: 40px;
  background: red;
  display: flex;
  align-items: center;
  justify-content: center;
}

main {
  grid-area: main;
  display: grid;
  grid-gap: 25px;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  background: blue;
  padding: 25px;
}

article {
  background: aliceblue;
  max-width: 200px;
  height: 100px;
  width: 100%;
}

aside {
  grid-area: aside;
  background: green;
  display: flex;
  align-items: center;
  justify-content: center;
}

footer {
  grid-area: footer;
  height: 150px;
  background: yellow;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="mySite">
  <header>
    <p>HEADER</p>
  </header>
  <main>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
  </main>
  <aside>
    <p>SIDEBAR</p>
  </aside>
  <footer>
    <p>FOOTER</p>
  </footer>
</div>

The flexible nature of the grid system can automatically adjust the layout based on these lines:

display: grid;
grid-gap: 25px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));

To further customize your layout responsively, utilize media queries. For instance, to stack everything vertically below 767px screen width, you could incorporate rules like this:

@media only screen and (max-width: 767px) {
  .mySite {
    grid-template-columns: 1fr;
    grid-template-areas: 
                      "header"
                      "main"
                      "aside"
                      "footer";
  }
}

The CSS grid empowers you to dynamically adapt the layout within the parent container itself.

In summary, employ CSS Grid for managing structured sections like headers, sidebars, footers, etc., while Flexbox aids in organizing content within those divisions. For instance, arranging elements within an article block by placing the date on the left and author on the right using:

display: flex;
justify-content: space-between;

Essentially, view flexbox as a tool with one-dimensional capabilities, allowing for alignment along a single axis, whereas CSS grid offers a two-dimensional setup where precise placement in both rows and columns is achievable.

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

Determining if a URL links to an image when the file extension is not informative

I am currently working on building an AJAX call to retrieve data from an API with a messy data structure. The challenge I'm facing is that the array returned by each AJAX call can contain up to 30 elements, some of which have image URLs without a file ...

Establishing space between table rows

I need to create a specific margin between two rows in a table. In my css code, I have the following snippet: background-color: #72c2dd; margin-top: 25px; The background-color is being applied correctly, but the margin-top property is not working as int ...

Turn off the Antd Datepicker tooltip

I'm using Antd Datepicker in my website. We get an array of numbers representing disabled dates from an external api and show only the dates in the datepicker(no month, year etc). The date selection is for a monthly subscription and the user just sele ...

Trouble accessing onclick function

My dataSend AJAX function is not being called when I use the onclick event. I have checked in the Inspector of my browser and confirmed that the click handler is attached to it. However, when I set a breakpoint in the function using the Debugger, it never ...

Steps to add a label below a text input field

Within a form, I have two text boxes and I would like to add labels underneath each of them for better clarification. To illustrate my idea, here is a simple diagram: (input1)<--space-->(input2) <label1><--space--><label2> Is ther ...

Switch the placement of two DIV elements by their corresponding IDs

I am attempting to adjust the position of two divs using jQuery, but I seem to be a bit confused as they are already swapping positions. However, when they do swap, it results in a duplication of the field. I have tried using both insertBefore and insertA ...

Modify the css based on the user's input

<html lang="en"> <head> <link rel="stylesheet" href="style.css" /> <li id="visa"> <section class="credit-card visa gr-visa"> <div class="logo">visa</div> <form> <h2>Payment ...

Tips on saving this information in a MySQL database

I'm currently developing a php script that collects and saves links in mysql. However, I've encountered an issue where the script sometimes captures multiple links for the same download, as shown below: http://www.fileserve.com/file/XXXXXX http: ...

"Achieving Alignment: Placing a Div Element Inside an H

I am in the process of building a portfolio for freecodecamp, but I am having trouble with centering the button and row div on the page. The row div is necessary because I will be adding more buttons later on, but for now I just need the FCC button. Below ...

Ways to switch out event listener when button is deactivated

I find myself in a situation where I need to switch all unauthorized controls on my UI from a hidden state to a disabled state. Additionally, I want to add a tooltip with unauthorized text and have the control display this text when clicked. While I am ab ...

Hiding a div using the Bootstrap `.hidden-xs` class may not work as

I have been working on a website and I want to hide a specific section for users viewing it on small screens. I tried using Bootstrap's .hidden-xs class, but when I tested it using Chrome's mobile emulator set to "iPhone 5", the div did not hide ...

Is it possible to use JavaScript to toggle mute and unmute functions on an iPad?

Seeking assistance with managing audio on an iPad using JavaScript (mute/unmute). Any suggestions or advice would be greatly appreciated. ...

The image appears uniquely sized and positioned on every screen it is viewed on

After reviewing the previously answered topics on the site, I couldn't seem to make the necessary adjustments. Despite setting the size and location in the css file, the large image remains centered and fitting for the screen. The "imaged1" class seem ...

Unique Pie Chart Designs

As I attempt to create a dynamic arc-shaped pie graph using CSS, representing 100% of the data, I find myself facing challenges. Despite extensive research and Google searches, I have yet to come across a suitable solution. The following reference appears ...

Choose and Send pictures through a Form with JavaScript

Currently, I am exploring different methods for allowing users to submit a form with the images they have selected. My idea is to present users with a set of images from which they can choose multiple options. Potential Solution 1: One approach could invo ...

Conceal specific image(s) on mobile devices

Currently, I am facing an issue on my website where I need to hide the last two out of six user photos when viewed on mobile or smaller screens. I believe using an @media code is the solution, but I'm unsure about the specific code needed to achieve t ...

How can you remove the border when an input is in focus on Chrome or Microsoft Edge?

I need to style an input field in a way that removes the black borders/frame that appear around it when clicked on in Chrome and MS Edge. Strangely, Firefox does not display this frame/border. How can I achieve this consistent styling across all browsers? ...

The function Event.preventDefault seems ineffective when attempting to block input of CJK (Korean) characters in a v-text-field of type

Currently, I am tackling an issue in a Vue project where I need to fix a small bug. However, the solution seems quite challenging. I have managed to make the v-text-field accept only numerical input, which is functioning well. <v-text-field type=" ...

Is it possible to run both the frontend and backend on the same port when using vanilla JavaScript for the frontend and Node.js for the backend?

Is it possible to run frontend and backend on the same port in Rest APIs if using vanilla JS for the frontend and Node.js for the backend? I've come across information on how to do this with React, but nothing specific to vanilla JS. Any insights on t ...

Multiple keyup events being triggered repeatedly

Currently, I am developing an Angular 4 application. Within my component's HTML, there is a textbox where users can input text. As soon as the user starts typing, I want to trigger an API call to retrieve some data. The current issue I am facing is t ...