Is there a way to align my two tables next to each other using CSS?

.page {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 20px;
}

.items {
  float: left;
}

.secondItem {
  vertical-align: text-top;
  float: right;
}

.page-header table,
th,
td {
  border: 1px solid;
  display: block;
  background-color: white;
  opacity: 80%;
  table-layout: fixed;
}

.tableBody {
  display: inline-block;
  width: 100%;
}
<div className="page">
  <Typography>
    <table className="items">
      <tbody>
        <tr>
          <th className="header">items:</th>
        </tr>
        <th>
          Hi
        </th>
      </tbody>
      <tbody className="tableBody">
        {Items.map((item, i) => (
        <td key={item.id}>
          item:{item.id}
          <br />
        </td>
        ))}
      </tbody>
    </table>
    <Table className="secondItem">
      <TableHead>
        <TableRow>
          <TableCell>Second:</TableCell>
        </TableRow>
        <TableRow>
          <TableCell>
            Item
          </TableCell>
        </TableRow>
      </TableHead>
      <tbody className="secondBody">
        <td>
          {seconds.map((second) => (
          <tr key={second.id}>
            {second.data.text}
          </tr>
          ))}
        </td>
      </tbody>
    </Table>
    <br />
  </Typography>
</div>
); };

I am facing an issue where my two tables are stacking on top of each other instead of being displayed side by side. How can I adjust the styling to have them sit next to each other?

Despite trying CSS methods like float:left and float:right, I'm unable to achieve the desired layout for my tables. As a beginner, I'm unsure how to proceed with styling them appropriately.

Answer №1

To easily achieve this, you can utilize the power of flexbox.

Simply apply display: flex to the parent element, along with flex-direction: row to specify horizontal alignment for the child elements.

For a demonstration, check out this example: https://jsfiddle.net/o6rqsLfj/.

I recommend referring to this guide to deepen your understanding of flexbox functionality.

.wrapper {
  height: 300px;
  width: 400px;
  background-color: pink;
  display: flex;
  flex-direction: row;
}

.item {
  height: 100px;
  width: 100px;
  margin: 10px;
  background-color: yellow;
}
<div class="wrapper">
  <div class="item"></div>
  <div class="item"></div>
</div>

Answer №2

One solution is to use display:inline-flex

.container {
  width: 100%;
}

.table {
  width: 45%;
  display: inline-flex;
}
<div class="container">
  <div class="table one">
    <table>...</table>
  </div>
  <div class="table two">
    <table>...</table>
  </div>
</div>

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

What is the best way to receive several responses from a PHP file using AJAX?

My PHP file is handling 2 operations: 1. Submitting data from a form into a database table, and 2. Sending an email. I am looking to display status messages through ajax. For instance, showing "First operation done, please wait for the second" and then di ...

Can you incorporate personalized icons in ReactJS?

Is there a way to incorporate my personally designed custom icons, available in both SVG and TTF formats, into a React project? I am interested in using these icons in my navigation bar, such as creating a unique home icon for the home button. ...

To access a form element in JavaScript function, utilize the dynamic form name approach

Currently, I am facing an issue with a JavaScript alert function where I am trying to view the HTML form URL before it is submitted. Following advice from a post on StackOverflow about retrieving form submit content before submit, I attempted to implement ...

How can we ensure that the load more button disappears at the appropriate moment in this Vue 3 application?

I have been developing a news application with Vue 3 and the News API. I am currently implementing a feature for loading more articles on the page. Initially, there are 24 articles displayed, and if there are more articles available than the current numbe ...

Arrangement using the display property of inline-block not following a linear direction

I'm experiencing some issues with the arrangement of elements on this page: When the viewport width is around 800px, the layout appears misaligned. Some lines have only a few bottles even though there is space for more. Is there a solution to this pr ...

Guide on how to modify the CSS styling of a particular <td> element when its parent contains a specific class

.webgrid-table td, th { border: 1px solid #98bf21; padding: 3px 7px 2px; } I am facing an issue where the style defined above is being applied to all td elements. However, I want to exclude the styling for a td that is within a tr element with the ...

Guide to sending a specialized SPL token using the libraries '@solana/web3.js' and '@solana/sol-wallet-adapter'

Attempting to transfer a custom SPL token using the solana-wallet adapter is proving to be challenging due to difficulty in obtaining the wallet's secret key for signing the transaction. Although I have reviewed resources on writing the transfer code ...

There appears to be an issue with Google Analytics not generating __utm.gif requests, yet no error messages are

I'm encountering an issue with implementing Google Analytics. Despite extensive research, I haven't been able to find a resolution. My objective is to include the user's email address as a custom variable in Google Analytics. I have integra ...

Switching back and forth between celsius and fahrenheit using jQuery (my mistake in the code)

I am currently using Ajax to interact with the openweather API. Everything seems to be functioning correctly except for one issue. My goal is to create a button that toggles between displaying temperature in Celsius and Fahrenheit. When I click the button ...

Guide to linking a label to a checkbox without utilizing the "for=id" method

Below is the code snippet I am currently working with: <li><input type="checkbox" id="checkboxThree" value="Alergia3" ><label for="checkboxThree">Alergia 1</label></li> <li><inp ...

Aggregate the data entered into input fields on one page and display them on a separate page

Can you help me with this task? - I have 2 pages set up (page 1 has input fields & page 2 is where the entered data should be displayed) - I want to retrieve all the text input from the first field and insert it into the "content" tag on the second page. ...

Setting up package.json to relocate node_modules to a different directory outside of the web application:

My web app is currently located in C:\Google-drive\vue-app. When I run the command yarn build, it installs a node_modules folder within C:\Google-drive\vue-app. However, since I am using Google Drive to sync my web app source code to Go ...

if the checkbox is selected, navigate to the displayed section

In my scenario, I have a total of 5 checkboxes and corresponding to each checkbox, there are 5 div elements. The functionality that I need is that when a checkbox is checked, the associated div should be displayed; otherwise, it should be hidden. The check ...

Converting Text into HTML using Node.js

Currently, I am utilizing nodemailer to send emails from my node server. The content for these emails is fetched from a MSSQL SQL server and is formatted in plain text format, including newline characters. However, when sending the email using nodemailer, ...

When an image is hovered over, the HTML background undergoes a transformation

Essentially: div:hover { body{ background-image:(bg.png); } } This code is conceptual but flawed, yet it effectively illustrates my problem. ...

Preventing duplication of code execution in recycled PHP elements

Currently, I have implemented a generic toolbar that is used on multiple pages of my web application by using PHP include. This toolbar contains log in/log out functionality and checks the log in status upon loading to update its UI accordingly. Initially ...

Automated tasks running on Firebase Cloud Functions with cron scheduling

There are two cloud functions in use: The first cloud function is used to set or update an existing scheduled job The second one is for canceling an existing scheduled job The management of scheduling jobs is done using import * as schedule from &ap ...

Utilizing HTML/CSS to display text and an image positioned above a sleek navigation bar

I am struggling to align a logo on the left side of my website, with text on the right next to it, all placed above the navigation bar. Despite my efforts, I just can't seem to get them positioned correctly! CSS .headerLogo{ float:left; marg ...

When using jQuery, the .change() function will only be triggered after the user clicks a button or

Looking for assistance with an unusual issue in my JavaScript/jQuery code. The jQuery .change() function on a simple text input element only triggers when I click somewhere on the page, open the Chrome console, or press a key on the keyboard. It doesn&apo ...

Tips for implementing text-overflow ellipsis in an HTML input box?

On my website, I have input fields where I've added the following CSS styling: .ellip { white-space: nowrap; width: 200px; overflow: hidden; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis; text-overflow: ellipsis; } H ...