Do you think it's harmful to utilize "display: table;" in structuring a layout with two columns?

Attempting to create a 2 column layout has proven to be quite the challenge in CSS. Although tables are not recommended for layouts, I have resorted to using this CSS approach with the use of display: table.

div.container {
  width: 600px; height: 300px; margin: auto;
  display: table; table-layout: fixed;
  }

ul {
  white-space: nowrap; overflow: hidden;
  display: table-cell;
  width: 40%;
  }

div.inner {
  display: table-cell;
  width: auto;
  }

Implementing this structure:

<div class="container">

  <ul>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    </ul>

  <div class="inner">
    <p>Hello world</p>
    </div>

  </div>

This method works well, but I can't help ponder whether I am strictly following the guideline against using tables. There doesn't seem to be any positioning markup directly in the HTML, so it should be acceptable. However, uncertainty lingers about the "correct" approach.

Using CSS float is not an option, as I need the columns to adjust based on available space.

Seeking guidance from the community, please assist me in overcoming my existential dread surrounding these pseudo-tables.

Answer №1

There are a couple of key reasons why using tables is not recommended:

  1. Tables do not provide semantic markup.
  2. Using tables can result in tag soup, with too many tags cluttering the code.

While your approach does not necessarily conflict with these principles, it's worth testing the code in IE7 and IE6 as there may be inconsistencies.

Remember, these guidelines are just that - guidelines. It's important to understand when to use different techniques based on the task at hand. Sometimes, a table may be the most suitable option, while other times it may not be.

Answer №2

Examples like this are what the display: table-cell; property was specifically created for. By incorporating the formatting in the stylesheet instead of the markup, you can rest assured that everything is under control. So take a deep breath and relax!

Answer №3

Imagine a world where every layout could effortlessly be achieved using pure css, no tables or makeshift solutions required. While this may be the ideal scenario, sometimes exceptions arise and we must make do with what is currently available. Remember, the ultimate goal is to provide a seamless experience for your users, rather than strictly adhering to a specific design principle.

Answer №4

The issue with utilizing table tags for content that is not tabular in HTML is that it enforces formatting on what should simply be data.

By managing your table styling with CSS, your HTML remains semantically correct. Personally, I believe as long as your HTML is semantically accurate, how you enhance its appearance using CSS is entirely up to you, and I don't think your existential concerns are warranted.

As a side note, I have a strong suspicion that IE 6 does not support display:table-cell, so depending on your target audience, you may need to seek alternative solutions.

Answer №5

Utilizing the display:table property works well when considering semantics. The popular notion that "tables are bad" stems from the idea that HTML markup should accurately represent the content it contains. If the content is not tabular data, then it shouldn't be placed in a table.

When you're simply adjusting how the markup appears (within a table structure), using display:table is absolutely appropriate.

It's important to note, however, that this may not be compatible with all web browsers. Specifically, IE6 and IE7 (and IE8 in "compatibility mode") may struggle to render this properly.

To learn more about different display types and browser compatibility, check out the following resource: http://www.example.com/css/display.html

Answer №6

Have you ever considered using display:table for creating auto-width columns?

I prefer avoiding css float because I need the columns to adjust their size based on available space.

Another approach you could use is utilizing flex properties. Let's delve into that:

In CSS3, a new set of properties was introduced under the umbrella term 'flex' which allows for flexible divs without relying on tables or display: table. This is particularly useful for users with smaller screens such as mini laptops, tablets, or large smartphones like the Galaxy series. Here are the key properties:

  • Parent.
    • The main container where all child elements reside. For example, imagine a basic web page structure with three divs (one containing main information, another serving as a sidebar, and a wrapper for both).
    • display: flex: indicates that we are working with flexible items. Prefix with -webkit- for Google and Safari compatibility. No need for -moz-, -o-, or -ms-.
    • flex-direction: determines how the children elements are oriented within the parent. Options include row, column, row-reverse, or column-reverse. Since you want a main div and a sidebar, 'column' would be suitable. Use -moz- and -webkit- for broader support.
    • justify-content: controls alignment along the X-axis. Choices include flex-start, flex-end, space-between, space-around, flex-center. Consider using the fourth or fifth options.
  • Child.
    • In this context, 'child' refers to either main (not recommended) or div class="main" and aside or div class="sidebar".
    • order: rearranges the order of divs, disregarding code order. Specify values using -moz- and -webkit- prefixes.
    • flex-grow: dictates how much a child element can expand compared to others, influenced by its value and other children's settings. Don't forget -webkit- & -moz-.

For more details, refer to: http://css-tricks.com/snippets/css/a-guide-to-flexbox/ and http://www.w3schools.com/cssref/default.asp#flexible.

Furthermore, yes, you can utilize this method. Check out: http:/www.w3.org/TR/css3-flexbox/#intro. It explains

table layout, designed for laying out 2D data in a tabular format

This approach doesn't necessarily mean it's a bad idea; it simply adapts the layout in a way that mimics a table format.

Answer №7

Interestingly, the use of display: table may become a widely accepted solution in upcoming times.

Check out:

Answer №8

display: table-cell

In terms of styling, 'display: table-cell' is not the same as a traditional table structure like this:

<table>
   <tbody>
       <tr>
          <td>
          </td>
       </tr>
   </tbody>
</table>

Personally, I believe it's acceptable to use 'display: table-cell' for certain layout needs.

And actually, tables aren't as bad as some people make them out to be.

For displaying tabular data, I actually prefer using tables over a "div" structure. One reason is that users can easily copy and paste the data into a spreadsheet from their browser, which is quite convenient and saves time. Plus, it helps reduce page load by avoiding features like "download as excel" or "download as csv". Also, different browsers tend to display tables consistently.

Answer №9

Allow me to simplify things for you.

In the past, the element was popular due to its charms, leading everyone to use it. But as Semantics evolved, we realized we were overly focused on Syntax. Decisions were made, resulting in XHTML 2.0 being discarded and HTML5 being adopted. Delving into the history of WHATWG (Web Hypertext Application Technology Working Group) can help clarify these concepts.

The rationale behind removing certain elements from HTML was that they were meant for styling rather than structuring, which goes against the purpose of HTML.

HTML is intended for structuring a webpage, while CSS handles the styling of that structure. This led to the removal of elements like and , which were primarily used for styling instead of structuring.

To address the fate of the element, it was decided to keep it in the specifications since tables are essential for structuring, but similar capabilities should be added to CSS to shift the focus of web designers towards using CSS for styling elements.

Now, the HTML element serves solely for structuring tables. If you wish to style a table, you can use the CSS property display:table;, allowing you to modify and control elements as needed. For example, in Responsive Web Design (RWD), you can specify elements to change their display from table to block or any other format.

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

Unable to confirm authenticity of dynamic form using PHP

I seem to be encountering an issue while attempting to validate my dynamic HTML form using a PHP script. Upon clicking submit, I receive an error stating that there is an undefined index, even though the index is clearly defined. What sets my query apart f ...

Arrange various images in a circular formation, rotate them in orbit, and show text within the circle upon hover using Bootstrap 4

I'm attempting to create a circle with various images circling around it, resembling planets in orbit. When hovering over these images, I want the rotation to pause and display different text in the center of the circle. While I've successfully d ...

Generate a unique token through a personalized form

**HTML** <div ref="addCardForm" id="card-element"> <div class="card_digit_text">Credit Card Number <input type="text" id="cardDigit" name="email" placeholder="0000 0000 0000 0000"> </div> ...

Maintain the original URL when accessing resources on a proxied webpage

My goal is to host a site named site1 within an existing domain, specifically www.gateway.com. For example, instead of accessing www.site1.com/profile, I want it to be accessible at www.gateway.com/site1/profile. To achieve this setup, I am using an NGIN ...

The table's content extends beyond the confines of the page

I have an HTML page where I am embedding an SSRS report. The report extends beyond the page as shown in the image below: This is the table that is generated: <table cellpadding="0" cellspacing="0" id="ctl00_MainContent_FiveYearReportViewer_fixedTable" ...

CSS styling for positioning a dropdown submenu

My website is experiencing issues with the CSS sub menus, specifically the last two sub menus are extending off the screen. https://i.sstatic.net/SKhuN.png I am looking for a solution to maintain the correct position of these sub menus. The code contains ...

Transferring HTML variables to CSS

I've been searching for an answer without much luck. Is it possible to transfer variables from HTML to CSS, maybe using data attributes? I want to display different backgrounds based on vendors using media queries on brand pages. I know inline CSS d ...

Ensure to verify the presence of a null value within the ngFor iteration

I have a webpage where I am displaying information in buttons. These buttons show various objects from a list along with their corresponding fields (e.g. object.name, object.age, etc.). Sometimes, one of those fields is null. How can I check if a value is ...

Attempting to adjust the width of a text animation loaded with jQuery using Textillate, but encountering difficulties

I found a captivating animation on this website: http://codepen.io/jschr/pen/GaJCi Currently, I am integrating it into my project. #content { position: relative; margin-left: auto; margin-right: auto; width: 1000px; height: 700px; } ...

When the cursor hovers over the image, it trembles with the

I am having trouble with an image shaking on mouseover. Here is the script I am using: function displayText(id) { var el = document.getElementById(id); el.style.display = 'block'; } function hideText(i ...

Unique CSS animation effect for mouse hover interaction

I am working with a set of buttons that have an animation named "slideIn". The keyframes for this animation are as follows: @keyframes slideIn { 0% {opacity: 0; transform: translate(-200px);} 100% {opacity: 1; padding-left: 110px; w ...

What to do when encountering a problem with HTML, CSS, and JS while loading a webpage from an SD card to a WebView

I am facing an issue while loading an HTML file from the SD card to a webview. The problem is that the CSS, images, and videos are not loading properly in the webview. Compatible with Android 4.4 KitKat and Chrome version Not compatible with versions belo ...

Direct your attention to the div element using ng-click

I'm encountering a complex issue while developing an AngularJS input helper component for number fields in my web application. To better explain the problem, let me give you some background on how our components function: Each component consists of i ...

Instructions on changing row color with button click and reverting back to original color upon subsequent click

When the button is pressed, I'd like to change the color. When the same button is pressed again, I want it to revert back to its original color. Currently, when the button is clicked, all rows change color. Sample Code Snippet: <table> & ...

Unable to locate webpage stylesheet

Could anyone help me figure out why my HTML page is not able to find my stylesheet? I am using the Django framework and running it on a localhost. Here is the HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or ...

Adding a new row to a table is causing issues with jQuery

var info = [{ "pin": "015-08-0011-000-01", "arp": "015-08-0011-000-01", "tin": "342-432-423-000", "firstname": "John", "middlename": "James", "lastname": "Jones", "suffix": "", "qtr": "1st ...

Show special characters with accents within an SVG element on a webpage written in HTML

I'm having trouble with accented characters (like á, é, etc) not displaying in my SVG object on an HTML page. Here's how the page is declared: <!DOCTYPE html> <html lang="en"> <head runat="server"> <meta charset="utf-8 ...

When working with JavaScript, the `textarea` value property will not recognize line breaks or white spaces when being read

Recently, I've been developing a browser-based notebook app. However, I encountered an issue where if I type a note like this: *hello this is my first note The app displays it as: hello this is my first note Additionally, I want elements with the ...

What could be causing the unexpected white gap within my div element even though I've adjusted its dimensions to be minimal?

<div style=" height:0; width: 0 ; border: 1px solid black ; "> This code snippet displays a div element with a specified height, width, and border color. However, there seems to be an issue with the rendering as some st ...

Creating an Active Link in Django Template

I am working with a form in Django and facing a challenge of dynamically adding the correct ID at the end of the URL. I need to figure out how to do this efficiently. Below is an example code snippet: {% for dealer_payment_account in dealer.dealerpaymentac ...