Challenges regarding table layout in CSS and exploring additional concepts

In the code snippet provided below, the focus is on table display.

* {
  box-sizing: border-box;
}

.mother {
  width: 300px;
  margin: 2em auto;
  background: peachpuff;
  display: table;
}

.child {
  display: inline-table;
  width: 100px;
  height: 100px;
  background: cyan;
  border: 1px solid #7a7a7a;
}
<div class="mother">
  <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>

There seems to be an issue with the child boxes (.child) not being completely wrapped, which results in small white spaces. The question arises whether this is a result of using display: table or display: inline-table. If so, what display value(s) should be used to eliminate the whitespace?

Another concern pertains to changing the display value of .mother to table and that of .child to table-cell. In this scenario, the child elements adjust themselves regardless of the parent element's width. To ensure each row contains only 3 child elements for a perfect fit within a 300px wide parent element where each child is 100px wide, flex-wrap can provide a solution when used in conjunction with display: flex. What property should be utilized to address this issue?

Answer №1

To fix the issue, simply add font-size: 0 to the parent element.

* {
  box-sizing: border-box;
}

.mother {
  width: 300px;
  margin: 2em auto;
  background: peachpuff;
  display: table;
  font-size: 0;
}

.child {
  display: inline-table;
  width: 100px;
  height: 100px;
  background: cyan;
  border: 1px solid #7a7a7a;
}
<div class="mother">
  <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>

Answer №2

Absolutely, the reason lies in the utilization of display:inline-table for child elements. This causes them to act as distinct inline-level containers enveloped within a single anonymous 'cell' of the outer table.

Regrettably, CSS table display does not support row wrapping functionality. The sole method to reposition cells onto a new row involves employing additional wrappers with display:table-row. So, why wouldn't display:flex be suitable in this scenario?

Answer №3

* {
  box-sizing: border-box;
}

.parent {
  width: 300px;
  margin: 2em auto;
  background: peachpuff;
  display: table;
  font-size: 0;
}

.child {
  display: inline-table;
  width: 100px;
  height: 100px;
  background: cyan;
  border: 1px solid #7a7a7a;
  margin: -5px -2px;
}
<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>

While it may be a bit unconventional, this solution could potentially resolve your problem.

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

Organize objects neatly in a grid formation

I have a .test div with grid-template-columns: auto auto auto;. The column width is set to vary based on the element width, and I also used justify-content: start; to align the columns to the left. I chose to use auto instead of 1fr to prevent the columns ...

Is there a way to configure this footer ad in my blogger so that it only displays for visitors who are unique?

I am seeking a solution to customize this "footer ad code" so that it only appears for unique visitors to my blog and does not reappear if the visitor has previously closed it. In essence, once a visitor closes this footer ad on one page of my website, ...

When using the background-blend-mode property, transition-duration is not taken into account

Try hovering over the top left h1 element in this code example You'll notice that it smoothly changes the h1 element to a shade of blue in just 2 seconds. However, the transition doesn't affect the blend mode of the backgrounds. Even though the ...

The process of toggling a div to slide up and down explained

I'm attempting to create a slide toggle effect on a hidden DIV when a user hovers over specific link buttons. JavaScript: $(function () { // DOM ready shorthand var $content = $(".sliderText"); var $contentdiv = $(".sliderCo ...

Attempting to extract only the text content from a specific div using XPath

I am currently facing a challenge in writing a script to extract the title element from a poorly coded webpage. The developer who created this website did not use any classes, only div elements. Below is a snippet of the source code I am trying to scrape: ...

CSS: border-radius // background-color: white; rounded corners

Having recently delved into the world of HTML/CSS, I am currently working on a fun project to enhance my web development skills. In this project, I have created a search bar and successfully added round corners with border-radius. However, an issue arises ...

Leveraging HTML tables for input purposes

Just starting out with php, HTML, and mysql. Using HTML tables for the first time here. Created an HTML table filled with data from a MySQL table. Planning to use this table as a menu where users can click on a cell with a specific date. The clicked date ...

The functionality of the button in my script is limited to a single use

Below is a simple code snippet that I wrote: HTML & CSS: <!DOCTYPE html> <html> <head> <title> JavaScript Console </title> <style> button { text-align:center; ...

Troubleshooting: Magento checkout page keeps scrolling to the top

We are experiencing an issue where, during the one page checkout process, the next step is not automatically scrolling to the top of the page when it loads. After a user fills out all their billing information and clicks continue, the next step appears ha ...

Ways to retrieve the index of an element with a certain class

When I click on an item with a specific class, I want to retrieve its index within that class only. <div class="gallery"> <div class="gallery-item"></div> <div class="gallery-item"></div> <div class="gallery-item ...

Having a floating left <UL> list with floating right text boxes positioned below the UL list instead of being set next to it

I'm attempting to align an unordered list on the left and a group of textboxes on the right within a div tag. The problem I'm facing is that the text boxes are positioned below the list items instead of being adjacent to them. .PersonLI { flo ...

Ways to align antd Card at the middle of the Col component in a React application

I'm trying to position a card in the center of the screen using the code below: import React from "react"; import "./index.css"; import { Card, Col } from "antd"; const App = () => ( <Col style={{height: '50 ...

Selecting HTML5 data attributes using jQuery

There is a button on my page with multiple data attributes, and I am trying to hide it by selecting it based on its class and two specific data attributes. <a href='#' class='wishlist-icon' data-wish-name='product' data-wi ...

What is the Best Way to Create a Smooth Border-Bottom Transition?

I'm currently diving into the world of HTML5 and CSS. I want to replicate the layout from . Specifically, I am interested in adding a transition effect to the border bottom of certain elements such as "View Our Listings", "The Agency Daily", "Meet our ...

Unable to upload image to mySql database using PHP

I'm having trouble with this function, it doesn't seem to be working correctly for me. How do I create an active button that uploads data to my database? if(isset($_POST['submit'])){ //This code snippet shows how to upload an image to ...

Is it possible to detect inline elements that have been wrapped?

I am facing a challenge with displaying an indefinite amount of in-line elements. Depending on the width of the browser, some elements may shift to a new line. I am curious to know if it is possible to identify and isolate these rows of elements, or if the ...

Creating Unique Numbers for Every <a> Element

Can someone help me figure out how to create a form that generates a unique set of random numbers from the range (0,9) displayed in a group of button tags? I've written some javascript code but it's not quite working as intended. (function($) ...

Issue with the back-to-top button arises when smooth-scrolling feature is activated

This Back To Top Button code that I discovered online is quite effective on my website. // Defining a variable for the button element. const scrollToTopButton = document.getElementById('js-top'); // Creating a function to display our scroll-to- ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Error with Display of ASCII Character 62 in Superfish Menu

Currently, I have implemented the Superfish menu on my website. In order to indicate sub-menus within my menus, I have opted to utilize the character entity code &#62;, which historically has represented ">". Oddly enough, while viewing my website ...