Difficulty arising from a CSS positioning issue with a 'menu' element

Having a div named "NormalInventory" with 32 children, each measuring 85px x 85px except for one which is larger at 175px x 175px, positioned relatively and displayed inline-block creating a CSS "list". Due to the size discrepancy, there are gaps in between the smaller boxes. How can these empty spaces be filled?

Here's a screenshot of the current layout:

https://i.sstatic.net/odn5G.png

And here's an edited version using paint to show how it should look:

https://i.sstatic.net/dSjfd.png

HTML structure:

<div class="NormalInventory">
    <div class="children">
        ...
    </div>
</div>
.NormalInventory {
    position: absolute;
    height: 100%;
    width: 100%;

    .InventoryItemPlaceholder {
        position: relative;
        display: inline-block;
        background-color: #070707E6;
        left: -5px;
        margin-left: 5px;
    }
}

Answer №1

To achieve the desired effect, CSS Grid can be utilized instead of using inline-block for elements. Simply add a display: grid to the parent element and set columns to repeat(4, 1fr), which creates 4 equal columns with 1 fraction each. The Grid system will automatically manage the rows.

For the larger element, apply span 2 to both grid-column and grid-row.

.App {
  border: 1px solid black;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  max-width: fit-content;
}

.box {
  background: black;
  height: 85px;
  width: 85px;
}

.big {
  height: 175px;
  width: 175px;
  grid-column: span 2;
  grid-row: span 2;
}
<div class="App">
  <div class="box"></div>
  <div class="box big"></div>
  <div class="box"></div>
</div>

Answer №2

    .parent{
      display: flex;
      width: 355px;
      justify-content: space-between;
      flex-wrap: wrap;
      position: relative;
    }

    .child{
    width: 85px;
    height: 85px;
    background: black;
    margin-bottom: 5px;
    }

    .child--big{
    width: 175px;
    height: 175px;
    position: absolute;
    top: 90px;
    left: 90px;
    background: black;
    }
    <div class = "parent">
      <div class = "child"></div>  
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"></div> 
      <div class = "child"--make bigger"></div> 
    </div>

Answer №3

Consider using a grid for a simpler solution:

.container {  
  display: grid;
  grid-template-columns: 85px 85px 85px 85px;
  grid-template-rows: 85px 85px 85px 85px 85px 85px 85px 85px;
  gap: 5px 5px;
  grid-auto-flow: row;
  grid-template-areas:
    "a1 b1 c1 d1"
    "a2 bigBlock bigBlock d2"
    "a3 bigBlock bigBlock d3"
    "a4 b4 c4 d4"
    "a5 b5 c5 d5"
    "a6 b6 c6 d6"
    "a7 b7 c7 d7"
    "a8 b8 c8 d8";
}
.bigBlock { grid-area: bigBlock; }
.a1 { grid-area: a1; }
.a2 { grid-area: a2; }
.a3 { grid-area: a3; }
.a4 { grid-area: a4; }
.a5 { grid-area: a5; }
.a6 { grid-area: a6; }
.a7 { grid-area: a7; }
.a8 { grid-area: a8; }
.b1 { grid-area: b1; }
c1 { grid-area: c1; }
.d1 { grid-area: d1; }
d2 { grid-area: d2; }
d3 { grid-area: d3; }
b4 { grid-area: b4; }
b5 { grid-area: b5; }
b6 { grid-area: b6; }
b7 { grid-area: b7; }
b8 { grid-area: b8; }
c4 { grid-area: c4; }
c5 { grid-area: c5; }
c6 { grid-area: c6; }
c7 { grid-area: c7; }
c8 { grid-area: c8; }
d4 { grid-area: d4; }
d5 { grid-area: d5; }
d6 { grid-area: d6; }
d7 { grid-area: d7; }
d8 { grid-area: d8; }

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

Struggling to properly align a div on top of an image

view image description herei need assistance with centering the div over the background image. I attempted to use relative positioning for the container and absolute positioning for the elements, but it did not yield the desired result. The code snippet be ...

On mobile devices, the Next.JS application (featuring Chakra UI) does not stretch to full width

I'm stumped on this one... Maybe there's something simple I'm overlooking, but I'm hoping for some help from you guys. When viewed on mobile devices, the entire HTML element takes up around 80% of the screen width. Oddly enough, resiz ...

The CSS styles within the GridView are not appearing on the page after an AJAX update

In the context of a gridview, I have implemented a CSS-based inline graph. Initially, everything functions as expected until a filter is applied and the gridview undergoes an update. At that point, the CSS styling ceases to apply within the grid. Has anyon ...

Sending the servlet response back to the original JSP page

Is there a way to display the message "File uploaded successfully" on the upload.jsp page? The page retrieves session attributes, checks the user's department, and acknowledges users by displaying the name of their department. When users upload files ...

The presence of text is leading to CSS placement troubles

I operate a website dedicated to comic content and am looking to incorporate text (retrieved from the database field "description") beneath each comic thumbnail. Here are my two inquiries: 1) I'm facing an issue where adding text below a comic caus ...

CSS - Incorporate the value of the counter() function as a property value

Is it possible to increment the margin-top of an element based on its index using a counter()? For example: div { margin-top: calc(counter(myCounter) * 10px); } I couldn't find any information on whether this is achievable or not. Since counters ...

The image filter plugin is functioning properly in one project, however, it is not working in

After successfully using a plugin from w3schools to filter elements in one project, I encountered an issue when attempting to implement it in another project. Here is the link to the problematic code pen: https://codepen.io/zakero/pen/mZYBPz If anyone ca ...

Center align the text inside a table cell of a span that is floated to the right and has a varying

Struggling to align a span vertically in the middle with a different font size than its table cell. The goal is to have the following layout: | | | Measurement (font-size: 100%) unit (font- ...

The text on the asp:Button displays incorrectly in IE8+

The content inside the button slightly shifts or moves downward by a few pixels. <div> <asp:Button runat="server" ID="Button1" CssClass="ButtonClass" Text="" runat="server" /> </div> ascx.cs: Button1.Text = LocalizationResourceManager. ...

Verifying CSS updates with Capybara detection

I'm in the process of developing a web application that dynamically changes its CSS styles based on user input. Everything is working smoothly, but I'm facing an issue with my automated tests not confirming whether the CSS updates are actually ta ...

Discovering the element's class within an each loop

I am currently working on a dynamic menu system that changes the style of the active page item in the menu. My approach involves utilizing separate body classes for each page and then iterating through the li elements in the menu to find a match with the b ...

The title for beforeShowDay in bootstrap-datepicker is not functioning as expected

When it comes to inserting text below each day in a datepicker, I attempted to use the beforeShowDay function. According to the documentation, the return value of beforeShowDay should include the following properties: - enabled (Boolean) - classes (Strin ...

How can I align an image and text alongside another image using HTML and CSS?

Having some trouble positioning an Image and Text next to another Image. Check out an example here: I attempted using floats, but it doesn't seem to be working. Here is the code I have: .left {float: left;} .right{float: right;} <div class="left ...

Close the ionicPopup by tapping anywhere

Currently, I have implemented an ionicPopup that automatically closes after a certain time limit. However, I am wondering if there is a way to configure it to close with a single or double tap anywhere on the screen instead. While I am aware that setting a ...

Generating a hyperlink to a specific user ID within a data table

I'm facing an issue with the formatting of my simple table when trying to navigate to user.id. Is there a better approach to this or should I consider moving LinkToUser? All styling has been removed to avoid any conflicts. import styled from 'st ...

Is it appropriate to include JavaScript and CSS in views?

In my ASP.NET MVC project, I have set up a _Layout, a controller, and several views. Certain pieces of code are clearly global in nature, such as the CSS included in the _Layout file and other styles that are consistent throughout the site. However, when ...

Is there a way to create an effect where the color of a floating action button changes when you hover over it, similar to a filter?

I'm new to using the <Fab /> element and I'm struggling to figure out how to implement a hover effect that will change the button's color. Here's what I have so far: JavaScript: <Fab variant="extended" className=&quo ...

Floating left and right positions for a fixed CSS div

My navbar is currently set to float right and 330px left with absolute positioning. Everything is working fine in this setup, but I want it to be fixed. The problem arises when trying to convert it to a fixed position. I've tried various methods but n ...

Error in Layout of Navigation Panel and Tabbed Pages

Working on a school project, I encountered a challenge. I found two useful solutions from the W3 website - a sticky navigation bar and the ability to include tabs on a single page for a cleaner presentation of information. However, when trying to implement ...

Unable to modify element attributes in css with tag selectors

I recently started my journey in the field of Web Development with Angela Yu's Web Development bootcamp. Currently, I am utilizing bootstrap v5, html5, and CSS3. While working on a project, I encountered an issue where I wasn't able to modify th ...