Prevent the wrapping of table cells in CSS tables

I'm currently developing a board game in JavaScript, and I have stored the images for the board within a table structure. Initially, I used HTML <table> tags for this purpose. However, I later realized that since the table doesn't contain data per se, it would be more appropriate to utilize <div> elements to visually represent it.

<div id='board'>

  <!-- the table starts here -->
  <div class='mytable'>

    <!-- table has multiple rows -->
    <div class='myrow'>

      <!-- a row has multiple cells -->
      <div class='mycell'/>
      ...
    </div>
    ...
  </div>

</div>

To style these divs in a way similar to a traditional table, I've applied the following CSS properties.

.mytable{
  display:table;         
  width:auto;      
  border:0px;      
  border-spacing:0px;
  padding: 0px;
  border-collapse: collapse;
  table-layout:fixed;
}

.myrow{
  display:table-row;
  width:auto;
  height: auto;
  clear:both;
}

.mycell{
  float:left;/*fix for buggy browsers*/
  text-align: center;
  display:table-cell;         
  width:31px;
  height:31px;
  background: url(background.png) no-repeat;
}

Initially, everything appears to work perfectly. However, when the table size exceeds the screen dimensions, the cells start wrapping around to the next line. This typically occurs when resizing the browser window.

My desired outcome is for the cells to remain in their original positions and extend beyond the visible area of the web browser, or ideally placed within a fixed-size container with scroll bars appearing when needed.

I attempted setting a fixed width (e.g. width: 200px) for the parent node (div#board). Unfortunately, this approach only works if the set width is larger than the table (div.mytable), which may vary in size.

Here are some strategies I tried:

  • overflow: scroll was ineffective.
  • white-space: nowrap did not yield the desired result.
  • Experimenting with table-layout: fixed, yet no noticeable change occurred.
  • Considered using display: table-column instead of display: table-cell.

What could I possibly be overlooking?

Answer №1

One effective solution is to avoid using display: table and instead utilize white-space: nowrap for rows and display: inline-block for cells.

An issue with display: inline-block is that it may introduce around 4px of whitespace between elements if there is any in the markup. This can be resolved by placing all elements on a single line or by applying font-size: 0 to the board itself.

.board {
  font-size: 0;
}
.board__row {
  display: block;
  white-space: nowrap;
}
.board__cell {
  display: inline-block;
}
<div class="board">
  <div class="board__row">
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
  </div>
  <div class="board__row">
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
  </div>
  <div class="board__row">
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
    <div class="board__cell"><img src="http://www.placehold.it/31/f99/c66" /></div>
  </div>
</div>

Answer №2

The original code was inspired by a different thread found on Stack Overflow discussing how to create tables using only <div> tags and CSS. You can read more about it here: How to create table only using <div> tag and Css

It was mentioned that using float:left was necessary for browser support, but this led to unwanted wrapping issues. As a solution, the float should be removed.

Although removing the float prevents cells from wrapping, a new problem arises where cells resize and stretch images when there is not enough space. To prevent this, a min-width: 31px should be specified.

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

Having difficulty arranging the elements in a straight line

I am currently working on designing the footer for my website. My goal is to have 2 divs and one list displayed inline within this footer. However, I am facing an issue where using the CSS Property display:inline-block is not achieving the desired result. ...

Missing Home and Search icons on Jquery mobileNavigationBar

I am having an issue where the Home and search icons are not displaying properly in the output. Instead, they are showing up as hyperlinks. Can someone please help me with this? Here is the code I am using: <meta name="viewport" content="width=device ...

Angular - Implementing *ngFor for nested table rows

I am currently working with an object that has a specific data structure: - title - id - [artists] - { artist obj / document , - [albums] - { album obj / document }, - { - album obj / document ...

What is causing my link to be clickable beyond the designated linked text area?

https://i.sstatic.net/RZPaw.png Displayed above is the current image. By clicking anywhere inside the black border, you will be directed to the provided URL. However, I specifically want only the text "RANKINGS" to be clickable. Here is the HTML code: &l ...

Is there a way to adjust the transparency of an image without affecting any overlaid text while using Bootstrap's carousel feature?

I am currently working with Bootstrap v4 to build a carousel featuring an image with caption text. My goal is to have the image faded while leaving the text unaffected. Despite trying several non-Bootstrap solutions, I have been unsuccessful in achieving t ...

Effortless Page Navigation - Initial click may lead to incorrect scrolling position, but the subsequent link functions perfectly

While implementing the Smooth Page Scroll code from CSS Tricks, we encountered an issue where clicking on a navigation link for the first time only scrolls down to a point that is approximately 700px above the intended section. Subsequent clicks work perfe ...

Removing Inline Styles Using jQuery Scroll Event

Whenever I reach the scroll position of 1400 and then scroll back to the top, my chat elements (chat1, chat2, chat3, chat4) receive an "empty" inline style but only for a duration of 1 second. After that, the fadeIn effect occurs again every time I scroll ...

Why did the CSS rendering suddenly come to a halt?

I am facing a confusion with EJS. When I make a post request, EJS renders my CSS properly. However, when I try the following code snippet: exports.resetpassword = async (req, res) => { const {id, token} = req.params; const user = await LoginMo ...

Include an iframe with hidden overflow specifically for Safari browsers

I have a situation where I'm using a third-party iframe to display specific content. The iframe is enclosed within a div container that has border-radius and overflow: hidden properties applied. Strangely, the content inside the iframe doesn't se ...

What steps are involved in incorporating dynamic pagination in PHP using this particular snippet of code?

I am looking for a way to display a list of numbers from 1 to 1000 in a single line with the ability to scroll. However, I need to exclude both "Prev" and "1" from this list. How can I achieve this? Below is the code snippet that I currently have: echo ...

event fails to trigger on a fixed positioned element

Here's the code snippet I've been working with: if ($('body.page-service-map').length || $('body.page-contact').length) { $(document.body).append('<div class="black-overlay"></div>'); ...

Toggling a div updates the content of a different div

I am looking to make the content within <div class="tcw-content"> dynamically change when a different div is clicked. I have little experience with Ajax, so I'm wondering if there are alternative ways to accomplish this using JS/CSS. If anyone h ...

Combining several position-aligned classes into a single div instead of each having their own individual div

http://jsfiddle.net/58arV/ Trying to create a question component where users can choose the correct answer out of 4 options. I planned to use 4 input styles with a checkmark "radio" DIV on the right. When a user clicks on an option, it should display a c ...

Resolving CSS Conflict in Panels with Legacy Bootstrap

I have recently implemented Panels from Bootstrap 3 with the older version of Twitter-Bootstrap. My challenge lies in adding the well class to the panel body, as it results in excessive padding. This was not an issue when using BS3, leading me to believe ...

Concealing excess content in a vertical container with margins

I'm currently working on setting up a grid of thumbnails for my blog posts using Bootstrap 4. The columns have a size of col-md-6. Within each column, there is an anchor with a background image and a gradient overlay that transitions from black to tr ...

Issue with Dropdown Menu functionality while using multiple dropdown options

I am currently experimenting with dropdown menus from W3Schools and have encountered an issue when trying to implement two dropdown options. The code works fine for a single dropdown, but when I try to add a second one using GetElementsByClassName instead ...

Can this actually work? Overflow-y set to visible and overflow-x set to scroll/auto

We are facing a challenge within our team that we are struggling to resolve. Our Grid control has been customized by us. When the icon next to the column name is clicked, a pop-up div (referred to as divFilter) appears, allowing for filtering options to be ...

Tips for concealing overflow x on a responsive website

Could use some help here. Whenever I switch to responsive mode, the overflow x feature gets enabled and I'm at a loss on how to disable it. I've attempted using @media to disable overflow but unfortunately it's not working as expected. ...

Help Needed: Adding a Simple Element to jQuery Tabs Script

I recently came across a fantastic jQuery tabs script on a website called Tutorialzine. The link to the article can be found here. As I was implementing this script, I realized I needed to customize it by adding specific classes to certain tabs. Specifica ...

Trouble getting proper alignment displayed with JavaScript and CSS

If anyone has a solution for printing content in a div using JavaScript and CSS, please help. The main div with the id 'preview' contains content taken from a database using PHP and MySQL. However, the data on the print page appears vertically an ...