Ensure that the following div remains positioned beneath the current one

After reading through this question on Stack Overflow, about creating a responsive table with both vertical and horizontal headers, I came across this particular issue, demonstrated in this codepen example:

Currently, the second table appears directly below the first one. I have managed to push it below by adding multiple <br> elements, but I'm sure there must be a more efficient way to achieve this...

Here is the CSS code I am using:

.aa table {
  border-collapse: collapse;  
} 

.aa td, .aa th {
  border: 1px #ddd solid;
}
.aa table {
  margin: 20px 0;
}
.aa td, .aa th {
  padding: 10px;
}

@media all and (min-width: 640px) {

.aa table  {
  float: left;
  display: block;
}


.aa table ~ table td, 
.aa table ~ table th  { border-left: 0;  }

.aa table ~ table tr:first-child  th:first-child  { display: none; }
.aa table ~ table tr:not(:first-child) th  { display: none; }
}

And here is the corresponding HTML code:

<!-- Table 1 -->
<h3>Table 1</h3>
<div class='aa'>
<table>
    <tr>
        <th></th>
        <th scope="col">BMW</th>
    </tr>
    <tr>
        <th scope="row">GPS</th>
        <td>12001</td>
    </tr>
    <tr>
        <th scope="row">Bluetooth</th>
        <td>7001</td>
    </tr>
    <tr>
        <th scope="row">Sensors</th>
        <td>12301</td>
    </tr>
</table>

<table>
   ...

Answer №1

Check out this code snippet

The reason for this behavior is the usage of float:left in the .aa table class. To cancel out the float, you need to clear it. You can achieve this by adding clear: both; in the style for h3 like so:

h3{
      clear: both;
}

Answer №2

The issue lies with the aa table class. Floating the tables to the left will cause them to be positioned next to each other. To resolve this, you can either remove the class declaration or clear the float.

One potential solution is:

<table>
      ...
</table>
<div class="clearfix"></div>
<table>
      ...
</table>


.clearfix {
   clear:both;
}

Answer №3

To achieve the desired outcome, try adjusting the table container div elements to use the display: inline-block style.

Here's an example of how you can apply this style to your aa class:

.aa {
  display: inline-block;
}

For reference, you can view the updated codepen example.

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

Difficulty encountered in getting html email signature to appear correctly on Outlook

I have developed a personalized email signature using HTML. Here's the unique code: <html> <!-- Insert your company logo here --> <div id="far_left" style="width: 50px; height: 50px; float: left; ...

Default behavior in Fullcalendar 6 allows for rendering links on both days of the month and weekdays

Is there a way to customize the rendering of days and weekdays in Fullcalendar React? Currently, they are displayed as links by default (<a>{dayContent}</a>), but I'm looking to have them rendered as <div> or <span>. Any sugges ...

The CSS Image Gallery refuses to be centered

.imageContainer{ width: 100%; margin: 0 auto; position: relative; text-align: center; } .imageContainer img{ width: 250px; height: 250px; float: left; } .imageContainer img:hover{ opacity: 0.60; } I am having trouble gett ...

Zoom in to see the header spanning the entire width of the page

http://jsfiddle.net/CPSKa/ Whenever I zoom in on the header of my website, the line underneath it starts to shrink. Is there a way to prevent this from happening? Here is the HTML Code: <div id="main_header"> <div id="main_header_wrapper"&g ...

Bootstrap 5 and Vue 3 team up to create a stunning masonry layout

I've been struggling to find a proper solution for implementing a masonry layout in Vue 3 with Bootstrap. I tried using Bootstrap 5 cards with the Masonry CDN, but it resulted in squeezed and overlapping cards, failing to achieve the desired layout. I ...

IE's inconsistent float alignment causing display issues

My challenge is getting my div elements to float correctly in Internet Explorer. They are displaying perfectly in Chrome and Firefox, but IE seems to be messing up the code. The jsfiddle link showcasing the issue can be found here: http://jsfiddle.net/vlya ...

What is the standard size for PHP files, or the suggested maximum limit?

I created a tool at where users can upload a .php file with comments and spaces, and it will compress it for a smaller file size. It functions similar to but is web-based and provides instant results. Once you click upload, you receive a prompt to downl ...

Modifying the calendar from a 7-day week to a 5-day week

I am currently in the process of developing a 7-day calendar (Sunday to Saturday) which is functioning well. However, I am interested in adding an option to switch it to a 5-day calendar (Monday to Friday). I was wondering if there is a way to modify the e ...

jQuery UI Accordion - Adjusting Panel Height to Content Size

Currently, I am utilizing jQuery UI's Accordion feature from http://jqueryui.com/demos/accordion/, and my goal is to adjust it so that it resizes based on the contents of each panel rather than just fitting the largest one. In addition, I am seeking ...

Choose the first element of its type and disregard any elements nested within it

I need to change the color of only the first p element within a specific div. Using :first-child would work in a simple example, but in my project, there are multiple elements and more could be added in the future. I want to avoid using :first-child alto ...

Creating links to external websites in PHP files

I'm currently developing a new website with PHP, and I’m facing an issue where all the URL links are directing users to 'localhost' instead of the correct IP address. This is causing a problem as they should be directed to the IP, not &apo ...

Compiling SCSS to CSS in Vue.js is a breeze

I am working on a vue js project that includes scss files. My goal is to compile these scss files into css and store them in the public folder. How can I achieve this? Should I make changes to the vue.config.js file? const path = require('path'); ...

Center two distinct elements using Bootstrap's center-block class

Check out my code snippet below: <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row"> <div class="center-block" style="w ...

What causes the NavBar to show and hide within a specific range?

Recently, I encountered a perplexing issue with my navbar. It functions correctly except for one strange behavior that has left me baffled. Why does the menu appear when I adjust the width to 631px, but disappear at 600px? And vice versa – why does it wo ...

How can we ensure that the borders of a text field automatically adjust to fit the dimensions of the text field using CSS?

I've encountered an issue with the borders of text fields while working on a project involving CSS/HTML. The problem is that in the browser, the borders appear shorter than the text fields. Initially, I attempted to adjust the border size to match th ...

What is the syntax for implementing an if-else statement?

Just starting with algolia and looking for a hit template. Please take a look at the script below for the text/html template needed: <script type="text/html" id="hit-template"> <div class="hit"> <div class="hit-image"> <i ...

Is it acceptable to use a readonly input with an HTML5 datalist?

Within my application, there is an html5 datalist implemented as shown below: <input type="text" list="mydatalist" /> <datalist id="mydatalist"> <option>Option 1</option> <option>Option 2</option> <option> ...

Android not showing scroll bar in ion-scroll feature

I am experiencing an issue where a div with a fixed height displays a scroll bar on browsers but not on an Android phone. <ion-scroll style="height:300px;" scrollbar-y='true'> //content </ion-scroll> ...

Updating div visibility based on form content in PHP

I am struggling to find a way to filter a page filled with divs based on form input to determine whether they should be displayed or not. The layout is akin to a collection of articles, where each div showcases an image and some text that links to the comp ...

<p> The box is massive, and its size is a mystery to me

https://i.sstatic.net/xPoDz.png Why is the box around my paragraph tag so large? I suspect this could be why I am unable to move the <p> tag down with margin-top: 50px; .train p { margin: 0; font-size: 2.5vw; } <div class="train"> < ...