The button shifts position when a new field is added to the list above

Hey everyone!

Here's the issue: I have 3 tables that display lists of items. Below each list, there are 2 buttons (cancel/submit). However, when I add a row to one of the lists, the buttons move to the right inexplicably. What could be causing this strange behavior?

If anyone has a solution to this problem, it would really save me some stress: https://jsfiddle.net/sqonLbv4/

Thank you in advance

HTML

  <div class="cols3">
    <label for="field1">Field 1</label>
    <table id="tableField1"></table>
    <input id="field1" name="field1" type="text" />
    <button id="field1Add" name="field1Add" class="btn btnOrange" type="button" onclick="field1Add()">Add</button>
    <button id="field1Delete" name="field1Delete" class="btn btnBlack" type="button" onclick="field1Delete()">Delete last</button>
  </div>
  <div class="cols3">
    <label for="field2">Field 2</label>
    <table id="tableField2"></table>
    <input id="field2" name="field2" type="text" />
    <button id="field2Add" name="field2Add" class="btn btnOrange" type="button" onclick="field2Add()">Add</button>
    <button id="field2Delete" name="field2Delete" class="btn btnBlack" type="button" onclick="field2Delete()">Delete last</button>
  </div>
  <div class="cols3">
    <label for="field3">Field List 3</label>
    <table id="tableField3"></table>
    <input id="field3" name="field3" type="text" />
    <button id="field3Add" name="field3Add" class="btn btnOrange" type="button" onclick="Field3Add()">Add</button>
    <button id="field3Delete" name="field3Delete" class="btn btnBlack" type="button" onclick="field3Delete()">Delete last</button>
  </div>
</div>
<div id="btnsForm" class="subcontainer">
  <button id="btnCancel" name="btnCancel" class="btn btnBlack" type="button">Cancel</button>
  <button id="btnSubmit" name="btnSubmit" class="btn btnOrange" type="submit">Submit</button>
</div>

JS

var field1 = document.querySelector("#field1");

function field1Add() {
  var table = document.querySelector("#tableField1");
  var cell = table.insertRow(0).insertCell(0);
  cell.innerHTML = field1.value;
}

function field1Delete() {
  var table = document.querySelector("#tableField1");
  table.deleteRow(0);
}

var fieldList2 = [];
var field2 = document.querySelector("#field2");

function field2Add() {
  var table = document.querySelector("#tableField2");
  var cell = table.insertRow(0).insertCell(0);
  cell.innerHTML = field2.value;
}

function field2Delete() {
  var table = document.querySelector("#tableField2");
  table.deleteRow(0);
}

var fieldList3 = [];
var field3 = document.querySelector("#field3");

function field3Add() {
  var table = document.querySelector("#tableField3");
  var cell = table.insertRow(0).insertCell(0);
  cell.innerHTML = field3.value;
}

function field3Delete() {
  var table = document.querySelector("#tableField3");
  table.deleteRow(0);
}

CSS (aka the devil)

    box-sizing: border-box;
}

.subcontainer {
  padding: 0 2rem;
}

.cols3 {
  width: 33%;
  float: left;
  padding: 0 0.3rem 3rem;
}

.btn {
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  background-color: transparent;
  border: 1px solid transparent;
  border-radius: 0;
  cursor: pointer;
  display: inline-block;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.btnCentered {
  text-align: center;
}

.btnTitle {
  vertical-align: middle;
  margin-left: 2rem;
}

.btnOrange {
  background-color: chocolate;
  border-color: chocolate;
}

.btnOrange:hover {
  color: #fff;
  background-color: #000;
  border-color: #000;
}

.btnBlack {
  color: #000;
  border-color: #000;
}

.btnBlack:hover {
  color: #fff;
  background-color: #000;
  border-color: #000;
}

.btnBlack:active {
  color: #f16e00;
  border-color: #f16e00;
}

.btnsTable {
  white-space: initial;
}

#btnsForm {
  text-align: center;
}

Answer №1

In this scenario, it is recommended to avoid using float: left for positioning elements next to each other. Instead, consider utilizing a flexbox for a more efficient layout. Update the CSS as follows to achieve a side-by-side display:

.cols3 {
    width: 33%;
    padding: 0 0.3rem 3rem;
}

#numbers {
    display: flex;
    justify-content: space-between;
}

Note that modern techniques like flexbox and grid offer superior ways of positioning elements both horizontally (X axis) and vertically (Y axis). Flexbox is ideal for aligning items along one axis, while grid enables alignment on both axes simultaneously.

Answer №2

Success!

.cols3 {
    width: 33%;
    padding: 0 0.3rem 3rem;
}

.subcontainer {
    display: flex;
}

In the HTML section, all h3 titles have been relocated ahead of the subcontainer div element. By doing this, everything has been resolved.

However, as Terry pointed out, utilizing grid properties would have been more beneficial for arranging objects in both X and Y positions.

Appreciate it.

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 method to identify the specific values causing a div to overflow in every direction?

Is there a method to accurately determine the specific quantities by which a div is overflowing in each direction? The information I found online only helped me calculate total horizontal or vertical overflow values. However, I am interested in knowing t ...

How can I use jQuery to prevent a click function from running when input fields are left empty

I have step cards with input fields on each card. A "Next" button is provided for each card to change the view. However, I need to prevent the "Next" button from functioning if any input fields on the form are left empty. Below is the code snippet: va ...

PHP: How to create a custom function to convert JSON data to HTML-encoded text

I am faced with a challenge involving an array containing values from a database, some of which include HTML tags. My goal is to output this array in JSON format, so I am utilizing json_encode for that purpose. However, I am encountering an issue when tr ...

Eliminate odd white spacing within nested div elements in Chrome

Is there a way to eliminate the odd white space between two nested divs in Chrome? <div class="bar"> <div class="progress"> </div> </div> .bar { width: 200px; height: 6px; border: 1px solid black; ...

Tips for eliminating the glowing effect when pressing down on the thumb of the material ui slider

Visit Material-UI documentation for Slider component. Despite setting the disabled flag for the entire slider, the thumb continues to show a halo effect when clicked. ...

An example of a functional AngularJS menu with accordion-style dropdowns, dynamic data, and submenus created using list items (li) - see

My goal is to create a versatile accordion-style navigation menu with submenus in AngularJS. This menu should be able to dynamically load menu data, determine which items have submenus, and more. I have experimented with angular-ui/bootstrap accordion elem ...

Navigating discreetly with invisible scroll bar

I am attempting to design a webpage where the scroll bar is hidden, but users can still scroll or click on links to navigate down the page. I have managed to hide the scroll bar, but I am facing an issue where the page jumps to 100% when scrolling. How can ...

Creating a website layout suited for printing using CSS

One of my clients has a website that needs to be converted into a brochure format. When he prints the website, it must be printed on paper with a specific layout. Some sections need to be removed and others modified. I have utilized Bootstrap 4 and create ...

I am having trouble getting my footer to align properly in a vertical position

My goal is to have my li elements remain on the same line, but unfortunately, they are not cooperating (they are aligning horizontally, but that's all). Here is the code snippet: <footer> <div class="container"> <div id="coi ...

Ways to ensure that the height is truly at 100%

When setting the width or height of something to 100% in CSS, it actually only fills up 100% of the browser window. Is there a way to make it take up 100% of the entire page? To clarify: I want a div element to occupy the whole page, regardless of how muc ...

Center align the mat-expansion-panel material component in a vertical orientation

When working with the mat-expansion-panel component alongside a mat-accordion, I've encountered an issue where the items are not aligning vertically at the center/middle. I'm unsure of the proper method to achieve vertical alignment for the conte ...

Tips for altering the color of spans that share a common top position without affecting the surrounding sibling elements above or below

Is there a way to change the color of spans with the same top position, excluding sibling elements above or below, in a paragraph made up of spans? I've been working on how to change the color of the line of span tags that contain another span with t ...

Tips on ensuring CSS is properly loaded on a Django HTML page

Despite numerous attempts, I cannot get my CSS to render properly on any of the HTML pages within a Django project. I've tried various solutions from Stack Overflow, such as adding the following code snippet: <link rel="stylesheet" href=& ...

How can I remove the unsightly scrollbar caused by overflow: auto?

I've encountered a problem while working on my website. The inner div #content wasn't expanding to the full size of the page, causing the text to overflow messily over the background image and making it difficult to read. I initially used overflo ...

Is there a way to customize the JavaScript click event for specific elements only?

I've been following a tutorial from codrops where each item has a hover event and a click event that triggers an anime.js function. My goal is to prevent certain items (grid cells) from triggering the anime.js function when clicked, while still allow ...

CSS Tip: Enhance your divs by adding a vertical pipe to each one

I am looking to insert a pipe character between links, with the exception of after the last one. The HTML is being rendered in such a way that it has the same class applied. <!DOCTYPE html> <html> <head> <style> p:last-of-type { ...

Creating a single form field with two inputs and applying custom styling in Angular Material can be achieved by following these steps

In my Angular Material project, I am creating a sample application with a form that includes a field with an input and dropdown. Currently, I have utilized flex styles and attributes as shown below: <div class="row"> <mat-label>De ...

Ways to correctly import various CSS files for individual routes in a Vuejs application

Currently, I am in the process of developing a project using Vue.js and Laravel. This project will have distinct layouts for the admin/user panel and landing page, requiring different CSS files to be loaded for each route. At the moment, I am utilizing va ...

What is the best way to add a CSS rule to JavaScript?

animation: scaleUp 0.3s linear 0.4s forwards; animation: scaleDown 0.3s linear forwards; Greetings! I'm currently working on adding animations to my content filtering functionality. Specifically, I want to incorporate the aforementioned CSS rules in ...

Trouble concealing information in Gmail and Outlook, issue persists

I need to create an email with two different links, one to be visible only in Gmail and the other only in Outlook. I discovered that I should use mso-hide for Outlook and display none for Gmail, but neither seem to be working as expected. When I use displa ...