Tips for effectively aligning CSS grids

I am trying to format my page with a title in h4, a sub-title in h5, and 3 divs arranged like the image shown here:

https://i.sstatic.net/29EJV.png

This is my current HTML code:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  column-gap: 50px;
  grid-template-columns: auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}
</style>
</head>
<body>

<div class="grid-container">
  <h4>Title 1</h4>
  <h5>SubTitle 1</h5>
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>  
</div>

</body>
</html>

However, I am facing an issue where my divs are shifted to the right and the titles are not aligned correctly as shown in this image: https://i.sstatic.net/E3lgP.png

Does anyone have any suggestions on how to fix this alignment issue?

Would combining Bootstrap 4 help align my titles and divs properly, or can it be achieved using only the grids?

Answer №1

If you want to achieve the desired layout without using flex, consider utilizing "grid-area" and "grid-area-template" as demonstrated in the code snippet below. Additionally, if you wish for a click on "2" to reveal a list, you can use hover effects combined with proper positioning to achieve this effect. Feel free to customize the code to suit your design needs.

<!DOCTYPE html>
<html>
<head>
<style>

    /* DEFINE GRID AREAS */
.item1 { grid-area: item1; }
.item2 { grid-area: item2; }
.item3 { grid-area: item3; }
.item4 { grid-area: item4; }
.item5 { grid-area: item5; }

.grid-container {
  display: grid;
  
  /* SPECIFY AREA LAYOUT */
    grid-template-areas:
        'item1 item1 item1 item1 item1 item1'
        'item2 item2 item2 item2 item2 item2'
        'item3 item3 item3 item3 item3 item3'
        'item4 item4 item4 item5 item5 item5';
  column-gap: 50px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
  margin-bottom: 35px;
}
.item4{
    position: relative;
}
.item4 .list{
    display: none;
    position: absolute;
    right: 250px;
    top: 0;
}

.item4 li{
    list-style: none;
    font-size: 20px;
    border: 2px solid black;
    padding: 3px 20px;
    margin-bottom: 2px;
    background-color: bisque;
}
.item4 li a{
    text-decoration: none;
}
.item4:hover > .list{
    display: block;
}
</style>
</head>
<body>

<div class="grid-container">
  <h4 class="item1">Title 1</h4>
  <h5 class="item2">SubTitle 1</h5>
  <div class="grid-item item3">1</div>
  <div class="grid-item item4">2
      <ul class="list">
          <li><a href="#"></a>Hello</li>
          <li><a href="#"></a>Hiii</li>
      </ul>
  </div>
  <div class="grid-item item5">3</div>  
</div>
</body>
</html>

Answer №2

To test the flexibility of the 2nd and 3rd items, you should detach the title and subtitle from the grid div for better separation. Please refer to the code snippet below and provide feedback.

$(".item2").click(function(){
  $(".popup").toggle();
});
.grid-container {
  display: grid;
  column-gap: 50px;
  grid-template-columns:auto;
  background-color: #2196F3;
  padding: 10px;
  grid-gap:10px;
  max-width:600px;

}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
  cursor:pointer;
  font-weight:bold;
}

.grid-item2 {
  display:flex;
  justify-content:space-evenly;
  width:100%;
  grid-gap:10px;
}

.grid-item2 > div {width:100%;}

.popup {
  height: auto;
  max-width: 250px;
  border: 1px solid gray;
  border-radius: 20px;
  display: none;
  position: relative;
  align-items: center;
  text-align: center;
  z-index: 1;
  margin-top:-60px;
  background-color:white;

 }
 
 .popup img {
  height:50px;
  width:50px;
  object-fit:cover;
  
  }

.fruit-mango {
    display:flex;
    align-items:center;
    justify-content:center;
    grid-gap:20px;
    }
<!DOCTYPE html>
<html>
<head>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<body>

<div>
  <h4>Title 1</h4>
  <h5>SubTitle 1</h5>
  <div class="grid-container">
  <div class="grid-item">1</div>
      <div class="grid-item2">
        <div class="grid-item item2">2</div>  
        <div class="grid-item">3</div>   
      </div>
</div>
<div class="popup" >
      <div class="fruit-mango">
        <img src="https://photos2.fotosearch.com/bthumb/CSP/CSP994/mango-clip-art__k16208308.jpg">
        <span >MANGO FR</span><br>
        </div>
        <hr>
        <div class="fruit-mango">
        <img src="https://photos2.fotosearch.com/bthumb/CSP/CSP994/mango-clip-art__k16208308.jpg">
        <span class="fruit">MANGO EN</span>
      </div>
</div>

</body>
</html>

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

Location of the webpage determined by the specific value

I need help with a specific request. I am looking to make some modifications to this form by adding two fields - one for "male" and one for "female" - each assigned to their respective URLs. For example, selecting the "female" option should redirect to " ...

Having trouble with setting the margin-top of a div?

When I attempted to apply the margin-top property to a div's style, it didn't have any effect. https://i.stack.imgur.com/jyY2G.png Below is the code snippet: .side { margin-left: 65%; margin-top: 3%; margin-right: 3%; border: 1px sol ...

javascript conceal a div when clicked elsewhere

I am trying to implement two JavaScript functions: 1) Displaying a DIV (welcomeDiv) when clicking on a button; 2) Hiding the div by clicking outside of 'welcomeDiv' container. function showDiv() { document.getElementById('welcomeDi ...

RequireJS is timing out while loading the runtime configuration

I keep encountering a load timeout error with my run-time configuration, specifically with common.js. Although I have set the waitseconds value to 0 for files loaded from common.js, the loadTimeout issue persists for common.js itself. index.html <scr ...

Ways to position buttons at the bottom of a webpage

I am looking to position all buttons at the bottom of the page: https://i.sstatic.net/ikXW5.png This is the code snippet: https://codesandbox.io/s/angular-7-bootstrap-4-3pv0p?from-embed <div class="container-fluid py-2"> <div class="row"> ...

allowing users to scale content and adjust layouts based on device characteristics

When I inherited the maintenance of a website, I discovered that it was not optimized for mobile devices. The client requested that the site be made accessible on mobile devices, so I attempted to ensure that the mobile version mirrored the desktop version ...

Swapping one div for another, all the while incorporating a limited number of buttons within the webpage

I'm faced with a dilemma on my HTML page. In the middle of the content, there's a div that contains 4 links with images and text inside. What I'm looking to achieve is for each link to trigger a complete change in the div when clicked, with ...

Flickering Button Displayed After Fade-In Animation

After scouring the internet for a solution, I still can't seem to figure out why my code isn't working. Whenever an element fades in on Safari, it quickly flickers or flashes white, almost like a rapid refresh is happening. I've tried vario ...

Creating a custom HTML layout: A step-by-step guide

Struggling to create a unique layout in HTML and seeking assistance. Instead of words, I'll show you my current layout with images: While this works fine for now, if div1 becomes taller, the layout will look like this: My ultimate goal is to achiev ...

How I disabled scrollbars in my HTML/CSS while implementing the Progid Microsoft gradient

IMPORTANT: Please read the latest update at the end of the question! Check out this snippet from my HTML/CSS code: html { height: 100%; } body { height: 100%; margin: 0px; filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#6699 ...

Please choose only one selection from each of the two dropdown menus

I spent an entire day searching for the correct answer, but to no avail. The issue I am facing is with two dropdowns. What I need is that if a user selects an option from the first dropdown, the choice in the second dropdown should revert to default. Simi ...

Emphasize text within the input field

Is there a way to create an input textarea that can detect words from an array in this.state and highlight those words as the user types text? Here is what I'm aiming for: The list of words: https://i.sstatic.net/eOx7G.png The input textarea examp ...

The size of my image decreases only when I transfer it to the phone screen

I have an image displayed on my website that I want to shift to the right specifically for mobile devices. @media screen and (max-width: 450px) { .my-img { padding-left: 8em; } } Although the image does shift to the right, it ends up shrinking in ...

Whenever I create the code using javascript, padding seems to always show up

I have a basic stacked row layout that displays an upper box and lower box without any padding. <span id="route" class="small"> <div class="row" style="border-style:solid;"> <div class="col-md-12 col-12"> <p>UpperBox</p& ...

Mastering the technique of showcasing landscape using CSS3

I'm working on an HTML and CSS3 report with multiple columns. I am trying to print it in landscape orientation using HTML and CSS3. I attempted rotating the body and table, but only half of the table is visible while the other half is cut off or hidde ...

Ways to reduce the size of images in Bootstrap 4

Is there a way to adjust the height of the images to make them fit better on the page? Here is an example of what I'm trying to achieve: https://i.sstatic.net/BMlWl.jpg I attempted to use custom CSS to modify the height, but it ended up making the im ...

The margin of HTML is larger than that of the body

Currently, I am in the process of designing my own personal website using Bootstrap. While working with Bootstrap4 and making some modifications, I noticed that the HTML margin seems to be larger than the body. Despite trying to zero the margin of various ...

What is the best way to darken the background when an alert is displayed and disable the dimming effect when the alert is closed?

Here's the JavaScript snippet I'm using: reset.addEventListener("click", function(){ document.querySelector("#body").classList.add("darkenPage"); myReset(); alert("Reset Successful!!"); document.querySelector("#body").classList.re ...

Pause jQuery at the conclusion and head back to the beginning

As a beginner in the world of jQuery, I am eager to create a slider for my website. My goal is to design a slideshow that can loop infinitely with simplicity. Should I manually count the number of <li> elements or images, calculate their width, and ...

Oops! An error occurred while using the Google Maps API. Let's fix this hic

I recently encountered an issue on my website where the Google Map displayed suddenly showed an error message saying: "Oops! Something went wrong. This page didn't load Google Maps correctly. See the JavaScript console for technical details." Afte ...