Positioning divs in CSS can be achieved similarly to how table cells are structured in HTML

Once again today, I came across a familiar issue with CSS layouts. I want to have 5 divs in a horizontal row, each with different widths:

  • 1: 60px,
  • 2: 30%,
  • 3: 40px,
  • 4: *,
  • 5: 100px

Back in the day, tables were the way to go for layouts, but now they are considered outdated due to accessibility reasons. I am looking for a modern solution that can work for any scenario.

Is there a generator, lightweight javascript solution (maybe a jQuery plugin), tutorial, book, or a magic trick that can help me solve this problem once and for all?

While a javascript-based solution is an option, I would prefer a non-script solution if possible.

Answer №1

To achieve this desired outcome, you can utilize the CSS property display:table. I have created a quick demonstration on a sandbox.

By using display:table, you can make each individual div behave like a table cell with the surrounding container acting as the table. In my example, I opted for a section for a cleaner code structure, but a div could also serve the purpose.

When implementing this approach, you might observe that the table cells shrink beyond your defined dimensions on smaller window sizes due to the default behavior of tables. To address this issue, simply include a min-width property with the same value as the width to ensure consistent sizing.

Answer №2

http://jsfiddle.net/abc123/def456/

#box1 {
    width: 80px;
}
#box2 {
    width: 50%;
}
#box3 {
    width: 60px;
}
#box4 {
}
#box5 {
    width: 120px;
}
.layout {
    display: table;
}
.cell {
    display: table-cell;
}​
<html>
    <head>
        <title>DIV AS TABLE</title>
    </head>
    <body>
        <div class="layout">
            <div id="box1" class="cell">1</div>
            <div id="box2" class="cell">2</div>
            <div id="box3" class="cell">3</div>
            <div id="box4" class="cell">4</div>
            <div id="box5" class="cell">5</div>
        </div>
    </body>
</html>

​Keep your fingers crossed! Modern browsers support this now!

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

Retrieving data from router in Angular

I have been working on a web application using Angular, and I have implemented a fixed header in the index.html file. The header appears on every page, with content injected through a ui-view. However, I now want to display this header only after the user ...

Developing procedural steps using CSS

I've been working on designing a step process using CSS, but I'm struggling to create the arrows that connect each circle, especially when the screen resolution changes. https://i.sstatic.net/GLod9.png This is my current attempt: .process-ro ...

Combine two JSON objects which share a common attribute

I am currently working with two JSON feeds. One feed contains the basic information about a course, while the other contains more administrative details. Here is an example to illustrate what I mean. FIRST {"courses": {"course":{"id":"4","title":"Usi ...

Creating custom generic functions such as IsAny and IsUnknown that are based on a table of type assignability to determine

I attempted to craft a generic called IsAny based on this resource. The IsAny generic appears to be functioning correctly. However, when I implement it within another generic (IsUnknown), it fails: const testIsUnknown2: IsUnknown<any> = true; // iss ...

Steps for placing the table within the box

I am working on a website to enhance my CSS skills using templates. Currently, I am creating a table with 8 attributes. I am seeking advice on how to keep the table within the box borders without overflowing. I attempted to use overflow, but I believe I m ...

What are some effective replacements for SoundManager2 worth considering?

While Soundmanager2 is a useful app, many find the code to be overly complex and difficult to navigate. It almost seems as if it was purposely written in a way to confuse rather than clarify. Are there any recommended alternatives to Soundmanager2 that are ...

Retrieving PHP information in an ionic 3 user interface

We are experiencing a problem with displaying data in Ionic 3, as the data is being sourced from PHP REST. Here is my Angular code for retrieving the data: this.auth.displayinformation(this.customer_info.cid).subscribe(takecusinfo => { if(takecusi ...

Rely on the razor method for generating URLs

I need to direct to a specific page, so I have implemented a JavaScript function in my MVC project: function rootUrl(url) { var _rootUrl = '@Url.Content("~")'; var x = url; if (url. ...

Changing the color of a Highcharts series bar according to its value

Playing around with Highcharts in this plunker has led me to wonder if it's possible to dynamically set the color of a bar based on its value. In my current setup, I have 5 bars that change values between 0 and 100 at intervals. I'd like the colo ...

The TypeScript error states that the argument type 'string | undefined' cannot be assigned to the parameter type 'string'

Receiving TS error that says "Object is undefined" I am attempting to retrieve the "userid" from my headers. However, I keep encountering the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'str ...

Guide to adding a CSS class to an Ionic2 toast

i found a helpful resource on applying cssClass to my toast component. within my HTML, I have buttons: <button ion-button (click)="presentToast()"> toast</button> Here is the code snippet from my .ts file: presentToast() { let toast = t ...

What could be the reason for esbuild not being included in the installation process of a fresh Rails 7 application that

After initiating a fresh Rails 7 app, I utilized the command line syntax: $ rails new app_name --css=bootstrap An error occurred during app creation specifically when including --css=bootstrap: Install esbuild run yarn add esbuild from ".& ...

Unable to deploy a node.js package via a jenkins job

I'm looking to set up a Jenkins job that will publish a package to npmjs.com. The source code for the package is located in a GitHub repository. While I am able to successfully publish the package from my personal computer by running 'npm publis ...

Ways to enhance widget titles with borders on Blogger

As a Blogger user, I am looking for guidance on how to add a border around the title of each widget/gadget. When I place widgets such as About Me, Follow Us, etc., in my sidebar, I want them to have borders like in this image. While I know how to customize ...

"Change opacity smoothly with the FadeToggle feature for a

I have a question that might seem silly, but I can't quite figure it out. How can I extend the duration of the fade effect? window.switchIn = function() { $('.red').fadeToggle(function(){ $('.blue').fadeToggle(function() { ...

Activating a certain class to prompt a drop-down action

My PHP code is displaying data from my database, but there's a bug where both dropdown menus appear when I click the gear icon. I want only one dropdown to appear when clicking the gear of a specific project. Can you help me fix this issue? It's ...

What does it mean when an "Unidentified column in the field list" error occurs?

I've encountered a problem with my HTML+PHP code. Whenever I try to input data into this table within my database, an error message pops up saying "Unknown column 'phone_outlet' in 'field list'"... What should I do? :( I can't ...

showing javascript strings on separate lines

I need assistance with displaying an array value in a frontend Angular application. How can I insert spaces between strings and show them on two separate lines? x: any = [] x[{info: "test" + ',' + "tested"}] // Instead of showing test , teste ...

How can I deactivate the today button in the angular-ui bootstrap datepicker popup?

Currently, I am working with two dates: StartDate and EndDate which are being managed using the angular-ui-bootstrap datepicker. When a user selects a StartDate (which must be greater than today's date), I want to ensure that the min-date of the EndD ...

Is it possible to retrieve the var name from an interpolated expression using template literals?

Suppose I have a variable like this: myVar = `Some text with ${eggs} and ${noodles} or ${pies}`; Is there a method to obtain myVar as an unprocessed string prior to variable substitution, essentially "Some text with ${eggs} and ${noodles} or ${pies}"? M ...