What is the best way to adjust the height and width to perfectly align with the edge of the screen?

Struggling to create a grid here, but no matter what I do with the width and height in CSS, nothing changes.

I'm attempting to replicate this design https://i.sstatic.net/LLXqr.jpg and here's the code I've been experimenting with.

.container {
  background-color: #ccc;
  margin: 180px;
  padding: 100px;
  text-align: center;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 7px 7px;
  grid-template-areas: "A A A A" "B B B F" "C D E F";
  justify-content: center;
}

.A {
  grid-area: A;
  width: 100%;
  height: 20%;
}

.B {
  grid-area: B;
}

.C {
  grid-area: C;
}

.D {
  grid-area: D;
}

.E {
  grid-area: E;
}

.F {
  grid-area: F;
}
<body>
  <div class="container">
    <div class="A">A</div>
    <div class="B">B</div>
    <div class="C">C</div>
    <div class="D">D</div>
    <div class="E">E</div>
    <div class="F">F</div>
  </div>
</body>

No luck trying to adjust the width and height of class A. Even attempted to make it responsive, but feel lost in the confusion.

Answer №1

To achieve a full screen height container, utilize height: 100vh and ensure the body margin is set to 0 for the container to occupy the entire screen. Utilize fr units for grid rows and columns to fill the container effectively. Avoid specifying the height or width on grid children as they will automatically adjust within the allocated space. For a comprehensive guide on grid layouts, refer to Kevin Powell's instructional video here.

For responsiveness, implement a media query to switch from a 4 column, 3 row layout to a 3 column, 4 row layout. The use of grid-template-areas for this transition is recommended for optimal responsiveness. Best of luck!

* {
  box-sizing: border-box; /* Important to prevent borders affecting defined widths and heights */
}

body {
  margin: 0; /* Ensures content reaches screen edges */
}

.container {
  /* Add border around container as shown in the example */
  border: 3px solid #9a9a9a;
  border-radius: 0.5rem;

  /* Maintain consistent padding and gap for even spacing */
  padding: 0.5rem;
  gap: 0.5rem;

  /* Set up the grid */
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-template-areas: "A A A A" "B B B F" "C D E F";

  /* Set container height equal to viewport area using box-sizing property */
  height: 100vh;
}

.container > div {
  background-color: #d9d9d9;

  /* Display child elements as grids to easily center content */
  display: grid;
  place-items: center;
}

.A {
  grid-area: A;
}

.B {
  grid-area: B;
}

.C {
  grid-area: C;
}

.D {
  grid-area: D;
}

.E {
  grid-area: E;
}

.F {
  grid-area: F;
}

@media screen and (max-width: 600px) {
  .container {
    /* Transition to a 3 column, 4 row layout */
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 1fr 2fr 1fr 1fr;
    grid-template-areas: "A A A" "B B B" "C D E" "F F F";
  }

  .container > div {
    border-radius: 1rem;
  }
}
<div class="container">
  <div class="A">A</div>
  <div class="B">B</div>
  <div class="C">C</div>
  <div class="D">D</div>
  <div class="E">E</div>
  <div class="F">F</div>
</div>

Answer №2

To ensure the box-sizing property is set correctly and adjust the width and height values of element .A to fit the grid layout, follow these guidelines:

<style>
    .container {
        background-color: #ccc;
        margin: 180px;
        padding: 100px;
        text-align: center;
        display: grid; 
        grid-template-columns: 1fr 1fr 1fr 1fr; 
        grid-template-rows: 1fr 1fr 1fr; 
        gap: 7px 7px; 
        grid-template-areas: 
            "A A A A"
            "B B B F"
            "C D E F"; 
        justify-content: center; 
        box-sizing: border-box; /* it's important to include this line */
    }
    
    .A {
        grid-area: A;
        width: calc(25% - 7px); /* subtract the gap from the width */
         height: calc(33.33% - 7px); /* subtract the gap from the height */
        background-color: yellow; /* this line highlights the grid cell */
    }

     .B { grid-area: B; }
    .C { grid-area: C; }
    .D { grid-area: D; }
    .E { grid-area: E; }
    .F { grid-area: F; }
</style>

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

How to effectively manage multiple stylesheet links in React Native Expo development

Hello, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS/JS along with the basics of React. As a beginner developer, my current project involves creating a Study planner app for myself using React Native ...

Showing child elements within a div using AngularJS

I am eager to create a straightforward AngularJS website that will showcase an initially hidden HTML element along with all of its children. Below is the HTML structure snippet I plan to use: <div class="hiddenStuff"> <h3>Game Over</h3&g ...

Tips to position two shortcode elements next to each other horizontally

I have some code here and I am looking to align the two elements at the bottom, "php echo do_shortcode(wcas-search-form);" and "php do_action('hamzahshop_custom_min_cart');" on the same line horizontally. I am new to this and struggling to figure ...

Tips for customizing the back button in Ionic 3

<ion-header> <ion-navbar> <ion-buttons left> <button ion-button navPop icon-only> <ion-icon ios="ios-arrow-back" md="nbsons-arrow-back"></ion-icon> </button> </ion-buttons> <ion-title> ...

Type of element returned

Is there a way to retrieve the element type? I'm interested in applying the style of a class to HTML5 elements without using the class attribute. <!DOCTYPE> <html> <head> <title>My page</title> </head& ...

Various Mobile Devices Display Website in Unique Ways

I've been having trouble getting my website to display consistently across different phones and could use some assistance. You can view the site at www.fpphotos.store. I have set overflow-x:auto; for the table containing the images, with CSS styles de ...

Why am I only able to send form data using the GET method and not the POST method?

I'm currently delving into the realm of html and php, but I seem to have hit a snag. When utilizing the GET method in my html form, parameters are successfully sent to my php file. However, when attempting the same with the POST method, no parameters ...

When using Vue with CSS3, placing an absolute positioned element within a relative wrapper can cause issues with maintaining the

Just starting out with Vue and diving into the world of CSS3! I'm currently working on a component that you can check out here: https://codesandbox.io/s/yjp674ppxj In essence, I have a ul element with relative positioning, followed by a series of di ...

Creating a dynamic dashed line animation using SVG

Imagine a line growing in the same pattern as this fiddle. svg #path { stroke-dasharray: 19; stroke-dashoffset: 19; animation: dash 4s linear 0s forwards; } @keyframes dash { from { stroke-dashoffset: 19; } to { ...

Struggling to animate the selector of a nested div in Jquery?

Despite trying numerous variations, I am struggling to identify the correct selector of a nested div. The goal is to animate this particular div, but since no animations are taking place, I suspect that I have not selected the right element. Here is the a ...

Can a small white pop-up be triggered by clicking a button?

While exploring the website , I noticed that clicking on Availability Zones opens a small window with text on the right side of the page. Is it possible to achieve a similar feature on a leaflet map without using JavaScript? This functionality would be tri ...

The AJAX success function is operational, yet the file does not execute

As a newcomer to StackOverflow and web development, I am pressed for time with this assignment, so please bear with me if I struggle with understanding web development terminologies. My current challenge involves calling another php file through ajax in my ...

What could be causing the malfunction of the Carousel Slide feature in my code?

I have encountered an issue while trying to implement the Bootstrap code below. It seems to only display the first slide and does not progress to the next one. I am unsure of what might be causing this problem. Can anyone help me troubleshoot? <secti ...

What is the best way to apply styling to a kendo-grid-column?

Utilizing the kendo-grid in my project serves multiple purposes. Within one of the cells, I aim to incorporate an input TextBox like so: <kendo-grid-column field="value" title="{{l('Value')}}" width="200"></kendo-grid-column> It is ...

What is the process for attaching a right ribbon label onto a card in Fomantic UI components?

I am completely new to Fomantic (and web development in general) and I'm attempting to create a website featuring a card with a ribbon label on the right side. I saw similar designs in the documentation where they used a segment instead. However, when ...

Tips for displaying an HTML page using JavaScript

In my project, I am working with an .html file (File X) that needs to immediately open another .html file (File Y) based on a certain value. Could someone provide guidance on the best way to achieve this using JavaScript? Additionally, I would like the pa ...

Can user authentication be achieved without relying on a database or PHP?

Exploring a way to add user authentication (login/password) in HTML5 without relying on server language or database. Is this feasible, or am I diving into the realm of madness? Thank you. ...

Unable to get CSS to function properly on website

I am having trouble getting my text to format properly on my web page. I have defined rules in my style sheet to center an object on the screen, but it's not working as expected. Here is the code for my web page: body { text-align: center; } h1 { ...

Choosing identical elements within comparable sub-divisions

How can I use JQuery to select an array of elements that are in separate sub divs? For example: <div class="panelWrap"> <div id="dvStage1" class="milestoneWrap" runat="server"> <div class="grid_1 ...

Is Consistency Possible with CSS Transition Direction?

Take a look at this example: https://codepen.io/jon424/pen/XWzGNLe In the provided code snippet, there is a button that can toggle the visibility of an image. When clicked, the image disappears from bottom to top and reappears when clicked again. The use ...