What is the best way to align a div with a td within a table?

I have a table and I inserted a div inside it, but now the alignment with the td is off. I applied display: inline-block to both the td and div elements, but the td only shifted slightly. How can I correct this? Here's my HTML code for the table :

#box4 {
  height: 200px;
  width: 300px;
  background-color: #f2f2f2;
}

th,
td {
  padding: 15px;
}
<table style="margin-left:5%; margin-bottom: 10%;">
  <tr>
    <td>
      <h3>Name:</h3>
    </td>
    <td>Course name placeholder.</td>
  </tr>
  <tr>
    <td>
      <h3>Description:</h3>
    </td>
    <td>Course description placeholder.</td>
  </tr>
  <tr>
    <td>
      <h3>Chapters:</h3>
    </td>
    <td>
      <select name="cars" id="cars">
        <option value="volvo">Chapter 1</option>
        <option value="saab">Chapter 2</option>
        <option value="mercedes">Chapter 3</option>
        <option value="audi">Chapter 4</option>
      </select>
    </td>
  </tr>
  <tr>
    <td style="display: inline-block; position: relative;top:-20%">
      <h3>Video:</h3>
    </td>
    <td>
      <div style="display: inline-block;" id="box4"></div>
    </td>
  </tr>
  <tr>
    <td>
      <h3>Quiz:</h3>
    </td>
    <td><button>Take quiz</button></td>
  </tr>
  <tr>
    <td>
      <h3>Course trial:</h3>
    </td>
    <td><button>Start trial</button></td>
  </tr>
  <tr>
    <td></td>
    <td><button>Community</button></td>
  </tr>
</table>

View how it appears https://i.stack.imgur.com/jAt8j.png

Answer №1

It is common for H3 elements to come with built-in padding that causes them to be offset downward.

To correct this issue, simply include the following CSS code:

h3 {
    padding: 0;
}

Answer №2

Check out this snippet, it includes some CSS code and a class defined in the h3 tag

#box4 {
  height: 200px;
  width: 300px;
  background-color: #f2f2f2;
}

.td-video {
  margin-top: -5px;
}

th,
td {
  padding: 15px;
}
<table style="margin-left:5%;margin-bottom: 10%;">
  <tr>
    <td>
      <h3>Name:</h3>
    </td>
    <td>The course name will be displayed here.</td>
  </tr>
  <tr>
    <td>
      <h3>Description:</h3>
    </td>
    <td>The course description goes here.</td>
  </tr>
  <tr>
    <td>
      <h3>Chapters:</h3>
    </td>
    <td>
      <select name="cars" id="cars">
        <option value="volvo">Chapter 1</option>
        <option value="saab">Chapter 2</option>
        <option value="mercedes">Chapter 3</option>
        <option value="audi">Chapter 4</option>
      </select>
    </td>
  </tr>
  <tr>
    <td style="display: inline-block; position: relative;">
      <h3 class="td-video">Video:</h3>
    </td>
    <td>
      <div style="display: inline-block;" id="box4"></div>
    </td>
  </tr>
  <tr>
    <td>
      <h3>Quiz:</h3>
    </td>
    <td><button>Take Quiz</button></td>
  </tr>
  <tr>
    <td>
      <h3>Course Trial:</h3>
    </td>
    <td><button>Start Trial</button></td>
  </tr>
  <tr>
    <td></td>
    <td><button>Join Community</button></td>
  </tr>
</table>

Answer №3

Modify the line css style by changing from top: 19% to margin-top: -19px.

#box4 {
  height: 200px;
  width: 300px;
  background-color: #f2f2f2;
}

th,
td {
  padding: 15px;
}
<table style="margin-left:5%;margin-bottom: 10%;">
  <tr>
    <td>
      <h3>Name:</h3>
    </td>
    <td>Here will be the name of the course.</td>
  </tr>
  <tr>
    <td>
      <h3>Description:</h3>
    </td>
    <td>Here will be the description of the course.</td>
  </tr>
  <tr>
    <td>
      <h3>Chapters:</h3>
    </td>
    <td>
      <select name="cars" id="cars">
        <option value="volvo">Chapter 1</option>
        <option value="saab">Chapter 2</option>
        <option value="mercedes">Chapter 3</option>
        <option value="audi">Chapter 4</option>
      </select>
    </td>
  </tr>
  <tr>
    <td style="display: inline-block; position: relative;margin-top:-20px">
      <h3>Video:</h3>
    </td>
    <td>
      <div style="display: inline-block;" id="box4"></div>
    </td>
  </tr>
  <tr>
    <td>
      <h3>Quiz:</h3>
    </td>
    <td><button>Do quiz</button></td>
  </tr>
  <tr>
    <td>
      <h3>Course trial:</h3>
    </td>
    <td><button>Start trial</button></td>
  </tr>
  <tr>
    <td></td>
    <td><button>Community</button></td>
  </tr>
</table>

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

Trouble with recognizing nested functions when calling them in an onClick event

Encountering a `not defined` error on the console when attempting to call nested functions with an `onClick` event in an HTML page. After searching for solutions without success, I suspect it may be a scope or closure issue, but I am unsure of how to addre ...

Smartphone screen cluttered with overlapping paragraphs

I am encountering a paragraph overlap issue while using a bootstrap template, specifically on smartphones like iPhone 6 with Safari. Interestingly, the problem does not appear when testing on a browser by shrinking the window size to the minimum. On the ph ...

Failed PHP response when jQuery was called

I'm working on a project that involves two files: one PHP and one HTML. The HTML file acts as the user interface where users input their queries, while the PHP file handles the processing and events before returning the output back to the HTML file. I ...

Position the label and the select dropdown side by side within the sweetalert 2 component

Can anyone provide an example of aligning a label and dropdown in the same row using sweetalert2? I attempted to customize the label placement, but it appears on a separate line. I would like to log the selected dropdown item upon clicking the OK button. ...

Could Google Adsense be causing issues with my website's navigation bar?

There seems to be an irritating bug that I've come across. While navigating my website, [the menu (located in the top right corner) functions correctly]. However, when you land on a page with a Google AdSense ad, the menu suddenly appears distorted ...

Custom CSS file for individual tenants in the Twig main template

Our Symfony 2.4 application is a multi-tenant system, meaning we share the same codebase among several organizations and differentiate access by domain name. For example: organization1.domain.com organization2.domain.com organization3.domain.com We util ...

Seeking a template for a server-side programmer who lacks proficiency in CSS and design?

Hey there all you wise folks! So here's the deal - I'm a PHP/JavaScript wizard when it comes to logic and server-side stuff, but I really suck at design, CSS, and all things arty. Does anyone have any recommendations for a template or tool that ...

Is it possible to load a webpage in a WebBrowser control without displaying certain HTML elements by their IDs

Is there a way to load a specific page using the WebBrowser control without displaying unwanted advertisement banners in the 'tb' DIV element? I've searched online and came across an example that uses the mshtml reference, but I can't ...

modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item ...

Tips for aligning 2 columns perfectly in a 3 column layout

I am facing an issue with a grid section where the grid-template-column is set for 3 columns, but sometimes the content loaded dynamically only fills 2 columns. I am attempting to center the columns when there are only 2. Despite going through the grid CS ...

What is the best way to display two radio buttons side by side in HTML?

Looking at my HTML form in this JSFiddle link, you'll see that when the PROCESS button is clicked, a form with two radio buttons appears. Currently, they are displayed vertically, with the female radio button appearing below the male radio button. I& ...

Preventing Jquery Append from Adding Previous Elements

I am struggling to figure out how to display the star rating for each hotel individually. I have 5 hotels, each with a different star rating. Here is my Javascript code: function GetStarHotel() { var parent = $(' p.star '), imagePat ...

Setting the color for the :hover and :active states on a react JSX component: A step-by-step guide

<button onClick={fetchExchangeRates} style={{ backgroundColor: `${selectedColor}` }} className="hover text-white px-3 py-1 flex justify-center items-center gap-2 text- rounded-md" > Convert <FontAwesomeIcon className=&apo ...

What are the steps to develop an ElectronJS application that displays "Hello world!" in the console after a button click?

Can anyone offer a helping hand? From the depths of imagination to the realms never before conceived by humans. Check out this HTML snippet: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hell ...

The CSS code isn't functioning properly on both desktop and mobile views

Here is what I desire to achieve: The CSS code I have is: And the HTML code looks like this: <body> <div class="contanier"> <div id="rightcol">Right Section</div> <div id="content">Content Section</div> < ...

What is the most efficient way to implement OR conditions in JavaScript for shorter code

Hello there! I have a question about optimizing conditions in code. Is there a more elegant way to write the following conditions? state === 'su' || state === 'ja' || state === 'fa' I was thinking if it could be simplified ...

Default close x button not functioning to close modal dialog

When I click the [X] button in my modal dialog box, it doesn't close. Here is an example of my code: $('#apply_Compensation_Leave').show(); This is the modal code: <div class="modal" id="apply_Compensation_Leave" tabindex="-1" role="di ...

Is your Facebook send button appearing strangely? Find out how to fix it here!

I recently integrated the Facebook send button on my website, allowing users to easily share information about each drive listed. However, a new issue has arisen where clicking on the send button opens a popup inside a table, negatively affecting the page& ...

What steps do I need to take to create a basic API that links to a MySQL database?

I'm currently trying to develop an API for PHP that will enable one web server to communicate with another web server. Additionally, I want this API to be able to update MySQL code. Previously, I was utilizing $_GET parameters for this purpose but rea ...

Stop options from being hidden in a select dropdown using HTML

Can I keep the options visible when a user selects an item in the 'select' dropdown? I want to add more options to the 'select' when the user clicks on the 'op2' item, without closing the list of options. <select> <o ...