Arrange a cluster of CSS table cells onto a fresh line

Currently, I am exploring CSS properties to create the visual appearance of a table using two levels of DOM elements. The highest-level element wraps around its child elements, which are styled to resemble a flat list. For example:

<div class="t">
    <div class="c">First row</div>
    <div class="c">2</div>
    <div class="c">3</div>
    <div class="c">4</div>
    <div class="c">5</div>
    <div class="c">Second row</div>
    <div class="c">7</div>
    <div class="c">8</div>
    <div class="c">9</div>
    <div class="c">0</div>
</div>

I am attempting to arrange this list into two rows, each containing five elements. My current CSS code is as follows:

.t {
  display: table;
  width: 100%;
}
.c {
  display: table-cell;
}
.c:nth-child(5n + 1):after {
  content: '-';
  display: table-row;
}

Unfortunately, this approach is not yielding the desired outcome.

Is there a method to maintain the two levels of nesting while achieving a table-like layout for the list?

Answer №1

When editing the HTML, consider structuring it like a complete table.

.t {
  display: table;
  width: 100%;
}
.r {
  display: table-row;
}
.c {
  display: table-cell;
}
<div class="t">
  <div class="r">
    <div class="c">First row</div>
    <div class="c">2</div>
    <div class="c">3</div>
    <div class="c">4</div>
    <div class="c">5</div>
  </div>
  <div class="r">
    <div class="c">Second row</div>
    <div class="c">7</div>
    <div class="c">8</div>
    <div class="c">9</div>
    <div class="c">0</div>
  </div>
</div>

Answer №2

If you want to control the behavior of elements, you have a few options. One option is to use the float property, but be aware that this may not work consistently across different browsers and the elements will not act like a traditional table.

Another approach is to utilize flexbox, although it still may not replicate the exact behavior of a standard table.

.t {
  display: table;
  width: 100%;
}
.c {
  display: table-cell;
  float: left;
  width: 20%;
}
<div class="t">
  <div class="c">First row</div>
  <div class="c">2</div>
  <div class="c">3</div>
  <div class="c">4</div>
  <div class="c">5</div>
  <div class="c">Second row</div>
  <div class="c">7</div>
  <div class="c">8</div>
  <div class="c">9</div>
  <div class="c">0</div>
</div>

Answer №3

In order to meet your desired outcome, I utilized the position:relative property along with display:table-cell.

HTML:

<div class="table-container">
  <div class="cell">First row</div>
  <div class="cell">2</div>
  <div class="cell">3</div>
  <div class="cell">4</div>
  <div class="cell">5</div>
  <div class="cell">Second row</div>
  <div class="cell">7</div>
  <div class="cell">8</div>
  <div class="cell">9</div>
  <div class="cell">0</div>
</div>

CSS:

.table-container {
  display: table;
  width: 100%;
}

.cell {
  display: table-cell;
  padding: 1%;
  border: 1px solid black;
  text-align: center;
  width: 10%;
}

.cell:nth-child(n+6) {
  display: table-cell;
  position: relative;
  left: -50%;
  top: 60px;
}

Codepen-http://codepen.io/nagasai/pen/mEOjLL

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

The functionality of onClick or onChange seems to be malfunctioning in the preline user interface select component

I recently integrated the preline UI library into my React project to enhance the design of my website. However, I encountered an issue where using the "select" element with the data-hs-select attribute as suggested by preline UI seemed to cause problems w ...

Challenges with resizing floating divs

As a beginner in Web Development, I am tasked with creating a web portfolio for an assignment. In my design layout, I have a navigation bar in a div floated left, and a content div floated right serving as an about me section containing paragraphs and lis ...

Position divs to be perfectly aligned and centered

I'm in the process of creating a webpage and facing challenges with aligning my divs properly, specifically the animated company logos. I want them to be positioned side by side rather than on top of each other, with space in between to fit the layou ...

Ensuring Equal Padding on Both Sides of a Div by Setting its Width

In search of a div that houses multiple inner divs with equal padding on the left and right edges. This is crucial for maintaining a fluid layout where the distance between the inner divs and outer div border remains consistent even when the window size ch ...

Updating Content in HTML Code Generated by JavaScript

My goal is to change the innerHTML of a div when it's clicked. I have an array of dynamically generated divs, and I want to target the specific table that the user clicks on. I attempted to set the IDs of the tables dynamically in my JavaScript code: ...

Django - tallying timer in action

Looking to add a timer that begins when accessing this template: {% block content %} <h3>C-Test</h3> <p>In the text below, some word endings are missing. The gap is about half the length of the word, so you need to fill in the rest ...

Troubleshooting Problems with CSS Before Selector and Margins

Currently, I am attempting to utilize the before selector in order to replicate some list item behavior for elements. Due to constraints where I cannot use list items, it is crucial that I find a way to make styles work effectively. If you would like to s ...

Error in Jquery click function not being triggered

I'm in the process of developing an eCommerce platform where users can choose options and have their shopping carts automatically updated using jQuery. Users will be presented with a few radio buttons to select from, and as they make their choice, th ...

Adjust the color of the label when the checkbox is marked, and make it red when the checkbox is left

Looking to create a customized checkbox due to challenges in styling the default HTML checkbox, I have attempted the following code. The checkbox functions properly when clicking on the label, but I am unable to change the border color of the label based o ...

Utilizing Jquery to toggle collapsed divs with a click event

Here is how my HTML appears: <h3><a href="#" title="story title">Story Title</a> <img class="expandstory" src="/images/plus.png" /></h3> <div class="description">Short description about the story</div> <h3 ...

Tips for accessing CSS properties on the img tag

I could use some assistance with CSS. I am in the process of creating a tree structure using many <ul> and <li> tags. The issue arises when I have multiple <li> elements with a specific class, each containing an <img> tag. How can ...

Is it possible to leverage Chrome for troubleshooting the reason behind a failure to load a Truetype font?

For my website, I am currently facing a problem with loading a ttf font. The error log only provides a generic message stating that the font failed to load, without any specific details regarding the cause. Interestingly, the font works perfectly fine on m ...

Optimizing jQuery Dialog for Different Window Sizes

I am currently developing a responsive website and facing a challenge. I need to implement a popup for window sizes smaller than 480px, but on desktop screens, the content should be visible without being inside the popup. I want to avoid duplicating the co ...

Encountering an issue with npm installation following a recent node version upgrade

Having trouble installing Sass on Node version v16.14.0. I keep receiving this error message: https://i.stack.imgur.com/6KNcF.png ...

What are the advantages of going the extra mile to ensure cross-browser compatibility?

It's fascinating how much effort is required to ensure web applications work seamlessly across all browsers. Despite global standards like those set by W3C, the development and testing process can be quite laborious. I'm curious why all browsers ...

What steps can I take to ensure my HTML5 mobile web app is optimized for compatibility across a variety of devices

I recently developed an HTML5 mobile web app and conducted tests on various devices including iPhones, iPads, Android phones, and tablets. What steps can I take to optimize the size and resolution of this app for seamless performance across all these dif ...

What is the number of steps jQuery animates in?

Exploring my creative side, I decided to create my own custom animate function. Struggling to achieve a seamless animation effect, unlike the smooth transitions produced by jQuery. I'm curious about the formula they utilize to determine the ideal num ...

SyntaxError: An invalid character was encountered (at file env.js, line 1, column 1)

This marks my debut question so kindly indulge me for a moment. I recently stumbled upon a guide that outlines how to dynamically alter environment variables in a React project without the need for re-building. You can find the guide here. The method work ...

What is the best way to retrieve the value of a submitted button in a jQuery form submission?

I am facing an issue with my HTML code. Here is the structure: <form method="post" id="IssueForm" action="wh_sir_insert.php"> <input name='id[]' type='checkbox' class="selectable" value='$col[8]' /> <inpu ...

Is it possible to utilize the AmMap API for developing an Android application?

I am currently in the process of creating a unique application with HTML, JS, and jQuery Mobile that will feature interactive maps. I have been searching the web for APIs to incorporate interactive maps into my project without using Google Maps APIs when I ...