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

what methods do you use to recall codes?

Hey there! I've recently started diving into the world of HTML, CSS, and Php. I'm curious, how exactly do experienced coders manage to remember so many functions? Is it something that comes with time and practice? As a beginner myself, this quest ...

the intricacies of resizing a dijit dialog

My dialog is defined as shown below: <div data-dojo-type="dijit/Dialog" id="DetailDialog" class="dijitDialog" ... I have loaded a grid inside the dialog with 10 columns. var dialog = dijit.byId("DetailDialog"); dialog.set("content", this.Details); d ...

I attempted to separate a string containing <br> tags using the explode function, but the method was not successful

In my code, there is a string that looks like this <br> ACCEPT:YES <br> SMMD:tv240245ce <br> This string is stored in a variable called $_session['result'] My task is to extract the following information either as an array or ...

The IDs and classes of HTML elements

I have two different implementations of a livechat script. On my sandbox site, the livechat is fixed to the bottom right of the page and scrolls with the window. However, on my live site (where this specific code comes from), it is attached to the footer. ...

What is the best method for decreasing the space between ordered list elements?

Help with removing unwanted top margin space I have been experiencing an issue where I am getting extra space above the list items when using a ul before an ol. The result is that only the unwanted space appears above the circle and number 1. I attempted ...

Updating the Origin of a Sound Element in HTML5

I am currently working on developing a personalized browser-based music application. I am at the initial stage of this project where my main goal is to create a button that allows users to change the song being played by an HTML5 audio player. I have attem ...

Bootstrap-tour is incompatible with a row within a table structure

Is there a way to highlight a table row effectively? I've been struggling with it and tried using the fix mentioned in this bootstrap-tour issue here Check out this demonstration on jsFiddle: jsFiddle JAVASCRIPT $("#dialog").dialog(); var t = new ...

I am looking to insert a jQuery value or variable into the plugin options

How can I insert a jQuery value or variable into plugin options? This is my current script: $(document).ready(function() { // var bannerheight = 580; if ($(window).width() < 2100) { var bannerheight = 410; var opts = JSON.parse( ...

Developing an identical design and layout but utilizing a distinct domain name for the company

After being given the task of creating a website identical to an existing one with a different domain name on WordPress, I proposed using the parent HTML source code or cloning it to speed up the project. The client agreed, explaining that starting from sc ...

Is it possible for me to utilize a type of CSS variables?

Looking to organize all the CSS code on my website in a specific manner. I want to define the type with details such as size, weight, and color. For example: <h1 bold blue>Hello world</h1 blue bold> So essentially, the CSS file will include: ...

In NextJS with SASS modules, the selector ":root" is considered impure since pure selectors are required to include at least one local class or id within them

Lately, I made the switch to using modules in my next.js project. However, I keep encountering an error in my newly created .module.scss files that says: "Selector ":root" is not pure (pure selectors must contain at least one local class or id)". It seems ...

Guide on making a flowchart using CSS

I'm working on creating a CSS flow chart diagram and I've included an image below for reference. Might there be a straightforward method to achieve this? I've been scouring the web for examples, but I'm unsure of what terminology to use ...

Pressing the different buttons on a form does not result in the form being submitted

I have a form with multiple buttons that, when submitted, will redirect to a different page and pass along some variables. echo " <form method=\"POST\" action=\"sightWordsTest.php\"> ...

Troubleshooting a Label Overlapping Problem in Materialize CSS

I've been working on a Materialize project and encountering an issue with pre-filled values in text boxes. Here is a snapshot of the problem: https://i.stack.imgur.com/u13jT.png <div class="input-field col s12 m6"> <input class="" id="us ...

Tips for adjusting the width of the box-shadow in CSS

I am facing a challenge with my button that has a box-shadow effect on hover. Currently, I am unsure how to adjust the width of the box-shadow effect. Here is a snapshot of my button's current appearance: https://i.stack.imgur.com/Mg0df.png

Be ...

The HTML output is essential for creating the content of a Bootstrap carousel

Is there a way to populate my bootstrap carousel with the content of an HTML page instead of images? I attempted to create a div and use CSS to load the contents from ac1_div using content:url(carousel_content.html), but it was unsuccessful. <!-- our ...

Issues with AngularJS UI Router functionality are being experienced specifically on localhost

What is causing the discrepancy in UI-Router functionality between JSFiddle and localhost? The localhost link is http://127.0.0.1:54046/preview/app/index.html. Have I overlooked something crucial here? There are no visible JS errors present in the console. ...

Enhance your message inbox experience with jQuery/Javascript features inspired by Gmail, including the ability to select all messages with a checkbox and

It is truly satisfying to be here working on developing a private message inbox for my website, especially after successfully implementing a complete user signup/login and activation system. A few months ago, I never believed I had enough patience to grasp ...

"Encountered a 404 Error while attempting an Ajax

I am relatively new to web development and I am currently working on making a post method function properly. Below is the ajax call that I have implemented: $.ajax({ type: "POST", url: '/Controllers/AddPropertyData', ...

CSS - Layering <divs> on top of clear <div>'s

Is there a method to eliminate the transparency of content/images within a <div> that is transparent? Below is the provided HTML: <div id="main-button-wrapper" class="left"> <div id="button-bg-layer" class="box-bg-layer corner ...