HTML structure consisting of a 3 by 3 grid

Creating a 3x3 grid with cells that have varying heights depending on content is proving to be challenging.

Except for the cells marked with red arrows, the rest of the grid has fixed height in pixels.

I attempted to build a 3x3 grid using divs to experiment:

<div id="container">
<div id="one">1</div> <div id="two">2</div> <div id="three">3</div>
<div id="four">4</div> <div id="five">5</div> <div id="six">6</div>
<div id="seven">7</div> <div id="eight">8</div> <div id="nine">9</div>
</div>

#container {
    width: 300px;
    margin: 0px auto;
}

#one, #two, #three, #four, #five, #six, #seven, #eight, #nine {
    width: 100px;
    float: left;
    padding: 0px; 0px;
    text-align: center;
}

However, adjusting the height of one cell causes others to misalign.

Any suggestions or solutions?

Answer №1

If you are looking for a way to create columns, consider this structure:

<div class="column-thing">
    <div class="left-thing">
        <div>A</div>
        <div>C</div>
    </div>
    <div class="right-thing">
        <div>B<br>B</div>
        <div>D</div>
    </div>
</div>

To achieve the desired layout, apply some absolute positioning using the following CSS:

.column-thing {
    position: relative;
}

.left-thing {
    left: 0;
    position: absolute;
    right: 50%;
}

.right-thing {
    left: 50%;
    position: absolute;
    right: 0;
}

For a live example, check out this JSFiddle link.

Answer №2

Another option is to utilize lists like ul/ol, li (no need for javascript)

<ul class="list">
    <li>
        <ul>
            <li><div>1<br/>1</div></li>
            <li><div>2</div></li>
            <li><div>3</div></li>
        </ul>
    </li>
    ...
</ul>

http://jsfiddle.net/xKjNG/

Answer №3

To guarantee that each row contains three divs, it is essential to have a wrapping div around every trio of div elements...

Answer №4

Give this a shot:

<style>
#first, #second, #third, #fourth, #fifth, #sixth, #seventh, #eighth, #ninth {
    width: 100px;
    float: left;
    padding: 0px; 0px;
    text-align: center;
    outline: 1px solid black;
}

.clearfix {
    clear: both;
}
</style>

<div id="container">
<div id="first">1</div> <div id="second">2</div> <div id="third">3</div><div class = "clearfix"></div>
<div id="fourth">4</div> <div id="fifth">5</div> <div id="sixth">6</div><div class = "clearfix"></div>
<div id="seventh">7</div> <div id="eighth">8</div> <div id="ninth">9</div><div class = "clearfix"></div>
</div>

Answer №5

It seems like you're avoiding using a table for the content, so here's an alternative approach using a faux table:

<div class="table">
  <div class="tr">
    <div class="td">
    </div>
    <div class="td">
    </div>
  </div>
</div>

You can style it by adding CSS display:table to .table and display:table-cell to .td.

By following this naming convention, working with the code becomes easier as it mimics a table's structure.

I created a demonstration here (still needs some polishing): http://jsfiddle.net/cyv6y/1/

It may need some cleanup, but the concept is functional.

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

CSS :contains selector after adding a script through Ajax append operation

Is there a way to change the text color in $('td:contains("text")').css('color','red') after an Ajax load script? Here is the main code snippet <div id="datatable"></div> <script src="https://code.jquery.com/j ...

the navigate feature is not redirecting to the intended page as expected

I am currently working on a React application and facing an issue with configuring the navigation. The problem lies in the code of the Navbar component, where upon clicking the logout button, the authentication context should be cleared and the user should ...

AngularJS constants are values that can be accessed and

Is it feasible to inject one constant into another constant using AngularJS? For example: var app = angular.module('myApp'); app.constant('foo', { message: "Hello" } ); app.constant('bar', ['foo', function(foo) ...

Choosing elements that are visible on the webpage using CSS selectors or XPath

The CSS selector for the parent element (which is always visible) can be found at this XPath: /html/body/section/div/div[2] After a few seconds (due to some JavaScript), one of its children will become visible. I need to locate and select this visible ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Exploring the power of regular expressions in Javascript when used between

Consider the scenario outlined in the text below I desire [this]. I also desire [this]. I do not desire \[this] I am interested in extracting the content enclosed within [], but not including \[]. How should I approach this? Currently, I have ...

Unable to attach a javascript eventListener to a newly created element

I've utilized JavaScript to generate 625 <div class="box"><div> elements and now I'm trying to attach an event listener to each box. The boxes are being created successfully, however, the listener does not seem to be working ...

Can you explain the concept of a framework operating "on top of" node.js in a way that would be easy for a beginner to understand?

If someone is new to JavaScript, how would you explain the concept of "on top of node.js" in simple programming language? I am looking for a general explanation as well as specific reference to Express on top of node.js in the MEAN stack. Appreciate your ...

Combine two or more Firebase Observables

Currently, I am working on creating an Observable using FirebaseObjectObservable. However, before I can accomplish this, I need to query a Firebase list to obtain the key IDs required for the FirebaseObjectObservable. The structure of my data is as follow ...

Solutions for resolving the issue of not being able to load images when using merged-images in JavaScript

I have a base64 image here and it has already been converted. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUTEhQWFhUXGBoaGBgYGBcXGhgXGBgYGhgbHhoZHiggGholHhgYITEhJSkrLi4uHR8zODMtNygtLisBCgoKDg0OGhAQGi0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0 ...

Creating an array object in JavaScript is a straightforward process that involves using the array

Looking to achieve the following object list structure: myObj = {"foo":[1,2,3,4], "bar":[3,5,7,8]} Initial attempt was unsuccessful: var myObj = new Object(); myObj["foo"].push(1) myObj["foo"].push(2) #...etc Seeking guidance on the correct m ...

Experiencing difficulties with updating populated inputs that are connected to fetched data as they are not reflecting changes when modified

I am currently facing challenges with handling text fields in my web application. I need to retrieve data from the database, populate it in the appropriate fields, allow users to edit the information, and save any changes made. However, I have encountered ...

Encountered an error: data.map is not functioning as expected in the React component

Hi there, I've encountered a small issue with my modal component. The error message I'm getting is: Uncaught TypeError: meatState.map is not a function. Any suggestions on what may be causing this problem? Your assistance would be greatly appreci ...

Tips for removing duplicate objects from an array

I am utilizing the underscore.js plugin in my code I experimented with it on jsfiddle var basket=[{ "basketitems":[{"items":[]}], "itemdetails":[{ "amountPledged": "100", "bActivity": "Handloom Wo ...

Creating a flash message following a redirect in AngularJS

Just diving into Angular JS and need some clarification. How can I set a flash message after a redirect? Here's my scenario: I have a form where I save data using an HTTP request. Upon success, I navigate to another page using window.location(). Now, ...

Every time I switch tabs in Material UI, React rebuilds my component

I integrated a Material UI Tabs component into my application, following a similar approach to the one showcased in their Simple Tabs demo. However, I have noticed that the components within each tab — specifically those defined in the render method ...

What methods does Angular use to display custom HTML tags in IE9/10 without causing any issues for the browser?

Exploring web components and utilizing customElements.define tends to cause issues in IE9/10. I've noticed that Angular is compatible with IE9/10, and upon inspecting the DOM tree, it seems like Angular successfully displays the custom element tags. ...

Centralized navigation bar

I am currently facing a challenge with centering my nav bar and I'm not sure how to proceed. The nav bar is placed within a class and a div, which is causing confusion for me. I have attempted to align it to the center and also tried using float:cente ...

The React Query devtools are failing to display

I am currently working on a project using React Query, but for some reason, the DevTools icon is not appearing on my screen. I have checked the console for errors, but there are none. I am following a tutorial on YouTube to help me with this. Here is a sn ...

Using Asynchronous JavaScript and XML (AJAX) to make calls to a web service

My program is showing an internal server error var parameters = "<?xml version='1.0' encoding='utf-8'?>" + "<soap:envelope xmlns:xsi='ttp://www.w3.org/2001/xmlschema-instance' xmlns:xsd='http://www.w3. ...