Ways to add space around the td element and properly align the content within it

I've recently delved into the realm of web development, and I'm encountering some challenges with HTML alignment. Despite exploring various resources and attempting to utilize align="left"/right/center attributes, I'm still struggling to achieve the desired alignment. Any guidance on how to proceed would be greatly appreciated.

Here is my intended design: https://i.sstatic.net/oCy8h.png

This is what I have ended up with. Please provide me with some direction: https://i.sstatic.net/U2Z4b.png

Below is the code I've created. Apologies if there are any basic errors - mistakes are part of the learning process. I have included my CSS, although it doesn't seem to be updating in the snippet.

.boxUp {
background-color: #dedacf;
border-color: #dedacf #dedacf #dedacf #dedacf;
border-style: solid;
border-width: 1px 1px 1px 5px;
}
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="boxup">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="right" style="width: 25%">dropdown:</td>
<td align="left" style="width: 25%">
<select name="dropdown" class="pulldown1" id="dropdown">
<option value="Dubai">Dubai</option>
<option value="Domestic 2">Arabia</option>
<td align="right" style="width: 25%">textbox:</td>
<td style="width: 25%"><input name="textbox" type="text"
size="3" maxlength="3" class="tType1" value=""></td>
</tr>
</table>
</td>
</tr>

Answer №1

The table belonging to OP contains another table within one of its cells, complicating the layout process. The select element was missing a closing tag, as well as its parent <td>. To remedy this, I included border-spacing:5px for both tables and applied black borders to the tables along with red dashed borders to the <td> elements.

To view the corrected version, refer to Snippet 2.

You only require the following CSS code:

table {
  border-spacing: 0;
  border-collapse: separate;
}

You can adjust the border-spacing property to any desired value in pixels (px), em units, or anything else you prefer. The subsequent Snippet showcases how this property works, enabling real-time adjustments to the table's spacing.

SNIPPET 1

var in1 = document.getElementById('in1');

in1.addEventListener('change', function(e) {
  var x = document.getElementById('tableX');
  var num = parseInt(this.value, 10);
  x.style.borderSpacing = num + "px";
}, false);
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <title>TABLE</title>

  <style>
    /* CSS styles omitted for brevity */
  </style>
</head>


<body>
  <section id="ii">
    <header>
      <label>Border-Spacing:
        <input id='in1' type='number' min='0' max='15' value='0'>
      </label>
    </header>
    <table id="tableX" class="x" width='90%'>
      <thead>
        <tr>
          <th id="h0">Header: 0</th>
          <th id="h1">Header: 1</th>
          <th id="h2">Header: 2</th>
          <th id="h3">Header: 3</th>
          <th id="h4">Header: 4</th>
        </tr>
      </thead>
      <tbody>
        <!-- Table body rows omitted -->
      </tbody>
    </table>

  </section>

</body>

</html>

SNIPPET 2

table {
  border-spacing: 5px;
  border: 1px solid black;
}
td {
  border: 3px dashed red;
}
.boxUp {
  background-color: #dedacf;
  border-color: #dedacf #dedacf #dedacf #dedacf;
  border-style: solid;
  border-width: 1px 1px 1px 5px;
}
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td class="boxup">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!-- Nested table content omitted -->
      </table>
    </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

Sliding divider across two div containers with full width

Seeking a JavaScript/jQuery function for a full page slider functionality. The HTML structure I'm working with is as follows: My objectives are twofold: Both slide1 and slide2 should occupy the full width of the page. Additionally, I am looking for ...

Ensure that the height of the div element is equal to 100% of

I've been trying to figure out how to make my div take up all of the vertical height of the screen, but I haven't found a simple solution in the 100% div height discussions. Can anyone help? Here's my code: CSS #mother { width: 100%; ...

Design a sophisticated background using CSS

https://i.sstatic.net/yILwL.png I am wondering if there is a way to create a CSS3 background similar to the one shown in the image link above. The gradient should smoothly transition from white to black, spanning across a header div, and remain consistent ...

Capturing text content in an HTML attribute - using Selenium

In my research, I found that most questions were about extracting text from <p> ... </p> tags. However, what I am curious about is whether Selenium can be used to extract text from an HTML attribute like this: <li id="bullet" someid="1003"& ...

Changing color of entire SVG image: a step-by-step guide

Check out this SVG image I found: https://jsfiddle.net/hey0qvgk/3/ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" width="90" height="9 ...

AngularJS Treeview with Vertical Line Styling using CSS

Struggling with styling, I am attempting to create a tree view in AngularJS. Currently, I am facing an issue aligning vertical and horizontal lines for the tree items. Check out my Codepen for reference. <html lang="en"> <head> <meta cha ...

What steps should I take to prevent my <mat-card> from expanding whenever messages are shown in Angular 9?

I have a <mat-card> containing a login form on my page. However, when error messages are displayed, the vertical size of the <mat-card> changes and I need it to remain the same. Is there a way to prevent this from happening? Below, you will ...

Clicking on the mat-icon-button with the matSuffix in the password field will always trigger a

Seeking assistance with implementing mat-icon-button matSuffix in Angular for a login page. This is my first time working with Angular and I am facing an issue with the password field. I have used mat-icon-button matSuffix, but whenever I click on the ico ...

Switching my Selenium code to HtmlUnit: A Step-by-Step Guide

Currently, my Selenium code is working perfectly fine. However, I am looking to convert this code into HtmlUnit. I know I can use the HtmlUnitDriver like WebDriver driver = new HtmlUnitDriver(); I want to make it purely HtmlUnit. Below is the code that I ...

Understanding how to activate a React navbar button is essential for creating a seamless user

How can I make my sidebar button change color when I navigate to the page it links to? ...

Filling out a Form in Advance

I'm having trouble populating a form in a new window using JQuery. The new window opens as expected, but I can't get the form to populate successfully. $( "body" ).delegate( ".propose", "click",function() { var url = './prop_form.php& ...

Pattern matching: Identify text elements specifically within an HTML tag

I've been experimenting with a text highlighting script. Check out my initial attempt here. Here's the code snippet: http://jsfiddle.net/TPg9p/3/ However, I encountered a problem – it only works with simple strings and not those containing HT ...

Verifying the selection state of a dynamically generated checkbox with JavaScript

I have a table with checkboxes. Each time a button is clicked, I dynamically add new checkboxes to the table like so: var cell3 = row.insertCell(2); cell3.innerHTML = '<input type="checkBox" value=\"selected?\" style="cursor:poin ...

Implementing checkboxes for each API data in JavaScript

I am fairly new to this and I am currently working on a to-do list. I want to include a checkbox next to each item from the API to indicate whether it has been completed or not. <script type="text/javascript"> fetch('http://localhos ...

Tips for keeping div and input tags selected even after a user clicks

I am working on a quiz script and I am trying to customize it so that when a user clicks on the div or input tags, they remain selected with a different background color. Currently, I have achieved changing the background color of the div when clicked, ...

What is a way to leverage the array or object data within the method object in order to reference the specific link in VUEJS?

How do I utilize the "LINK" property in the methods object? You might be able to infer my intentions, but just in case, here's what I'm aiming for: In Vue.js, I am attempting to extract all the data from the Data method's object so that I ...

Issues encountered while attempting to convert HTML Image and Canvas Tags to PDF within Angular 2

I am currently facing an issue with my code. My requirement is to download HTML content as a PDF file. I have been successful in downloading text elements like the <p> tag, but I am encountering difficulties when trying to download images and canvas ...

Dealing with ng-repeat and button alignment across Chrome, Edge, and Firefox

Can someone help me understand this strange behavior I am experiencing across all browsers? <div style="margin-top:5px"> <button translate="clear" ng-click="xyz.clear()" class="btn btn-default"></button> <butt ...

What causes images to shift when resizing in Chrome, but not in Firefox or Safari?

My layout includes images - one on the left and two on the right. As I resize the browser window, the images scale accordingly. The issue arises when viewing my layout in Chrome: the image positions shift as I resize the browser, unlike Safari and Firefox ...

What is the way to determine the length of a list in a velocity template?

Is there a way to determine the size of a list in a velocity template? I am currently working with version 1.4 of Velocity. List<String> list = new ArrayList<String>(); ...