Misalignment of input and label elements due to varying widths

Why are the input elements not aligning with the button and other elements?

Upon inspection in Layout (Firefox Developer Edition), it appears that the sizes of the input and label elements are different even though they are within the same div.

https://i.sstatic.net/mj0ax.png

What is causing this discrepancy? And how can we align them elegantly without resorting to trial and error, such as adjusting margins?

Highlighted section from NewTransaction.js file: https://i.sstatic.net/5kDtu.png

CSS file for NewTransaction: https://i.sstatic.net/feLwx.png

SOURCE CODE: https://github.com/yanichik/react-course/tree/main/full-course/expense-tracker-v2

Answer №1

Due to the padding of 2px on both sides of the input element, combined with the default box-sizing setting of content-box in the user agent stylesheet.

The content-box option provides the standard CSS box-sizing behavior. When a width of 100 pixels is set for an element, the actual content box will be 100 pixels wide, with any additional border or padding increasing the final rendered width beyond 100px.

To address this issue, it is recommended to use box-sizing: border-box.

For further information, please see: https://www.w3schools.com/css/css3_box-sizing.asp

Your revised code can be viewed here: https://codesandbox.io/embed/expense-tracker-yan-forked-80tfk?fontsize=14&hidenavigation=1&theme=dark

Answer №2

It is important to standardize the styles,

The style in your code that caused an issue was box-sizing. I simply set all elements to border-box, but you can also just add box-sizing: border-box; to the input and it will work as well.

html {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

*,
*::before,
*::after {
  -webkit-box-sizing: inherit;
  -moz-box-sizing: inherit;
  box-sizing: inherit;
}

.form {
  margin-top: 15px;
}

input {
  display: flex;
  width: 100%;
}

label {
  display: flex;
  justify-content: flex-start;
}

button {
  display: block;
  width: 100%;
  margin-top: 5px;
  background-color: purple;
  border-color: purple;
  color: white;
  font-weight: bold;
}

.amount,
.description {
  /* display: flex; */
  flex-direction: column;
  margin-bottom: 5px;
  margin-top: 5px;
}
<div className="form">
  <form onSubmit={submitHandler}>
    <div class='description'>
      <label htmlFor="description" pointing="true">
                        Description
                    </label>
      <input type="text" id='description' placeholder="Type Something" ref={descrRef}/>
    </div>
    <div class='amount'>
      <label htmlFor="amount" pointing="true">
                        Amount
                    </label>
      <input type="number" id='amount' step="0.01" placeholder="$" ref={amountRef} />
    </div>
    <div>
      <button type="submit">Add Transaction</button>
    </div>
  </form>
</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

How can I access the variable name in an array of objects in React components?

Here is the code I have in my React project: const [value, setValue] = React.useState<CountriesEntityData | null>({ id: '', name: '', regions: [ { id: '', name: '', di ...

Unable to prevent ordered lists from overlapping with other content I attempt to place beneath them in HTML

function filterImages() { let input = document.getElementById('searchbar').value; input = input.toLowerCase(); let images = document.getElementsByClassName('gallery'); for (let i = 0; i < images.length; i++) { ...

Providing data from a javascript xml file upon page initialization

I am aiming to extract multiple values from an XML file as soon as an HTML page is loaded. The XML file remains constant and will not change over time. It will be stored in the same directory as the HTML page. The purpose of this XML file is to fill severa ...

Just delving into Django and having some trouble with loading CSS files despite following tutorials

I am facing an issue with my website where the server is only showing HTML without any CSS. I have read that CSS files should be kept in a static folder within the project directory. My HTML template is located in the templates folder and works fine when l ...

How come the dropdown menu consistently disrupts my CSS design?

Whenever I add a dropdown menu, my CSS layout gets all messed up. Can someone please explain why this happens and how to fix it? Below is the code snippet: <asp:Label ID="projnamelbl" runat="server" CssClass="editlabels" Text="Project Name"></as ...

Guide to dynamically loading data from a database using PHP, jQuery, and AJAX as the user scrolls down the page

I'm struggling with my eCommerce site development. I want to dynamically load carts from the database as the user scrolls, but I need to first display "Filter Categories." Below is a snippet of the script: <span class="applied-amount">Applied F ...

Guide to converting TypeScript classes into functional components in ReactJS

Struggling to convert a typescript class component to functional components and encountering some issues that I can't seem to resolve. Seeking guidance on how to fix this problem. https://i.sstatic.net/QbWwo.png class component import * as React fro ...

How can I create space between a checkbox and its label in Google Web Toolkit (GWT)?

How can I create space between a Checkbox and its content in GWT? Checkbox c = new Checkbox("checkme"); c.setStyleName("checkbox_style"); When I try using padding or margin, the entire checkbox and its content move together. Is there a way to achieve a g ...

Encountered an issue with location.state during the execution of gatsby

While using the state with the gatsby component Link, I encountered an issue where it worked when testing with gatsby develop, but failed with gatsby build due to an error related to undefined values. The error message displayed in the terminal was Webpack ...

An error occurs when trying to access the Constants property in the RNCSafeAreaViewConfig object because it is undefined

There was a TypeError: undefined is not an object (evaluating 'RNCSafeAreaViewConfig.Constants') <global> InitialWindowSafeAreaInsets.ts:9:38 loadModuleImplementation require.js:322:6 <global>> index.tsx:6 loadModuleImp ...

displaying a div upon checking the checkbox

How can I make a div push the content below it when it is in the show state? Currently, when I check the checkbox, the textarea covers the submit button, hiding it. Is there a way for the textarea to push the button down instead? You can find my code here: ...

Background image color not displaying correctly

I've been attempting to overlay a transparent color on top of a background image. While the background image is displaying correctly, I'm having trouble getting the color to show up. I noticed that this question has been asked before. I tried im ...

How can I add a JSON object to another JSON object in a React application?

How can I merge two JSON objects in reactjs? Here is the initial JSON format: First JSON Object: { "results": { "Action": "Success!" }, "report": { "Strategy": "Yes", ...

Creating a visually appealing layout by dividing HTML content into two columns within a WebView

Utilizing WebView in my application to display html content for reading ebooks presented a challenge during development. After parsing the ebook and converting it into an html string, I load this string into the WebView component. myView.setVerticalScro ...

Menu options arranged vertically, revealing submenus that fly out upon mouse hover

I am attempting to develop a vertical menu with flyouts, which includes sub-menus. Can you point out any issues in the code provided below? <html> <head> <title>Untitled Document</title> <style type="text/css ...

Is it possible to modify the class within TypeScript code while utilizing the CSS library for styling?

In my custom style sheet coustume-webkit.css, I have the following code snippet: .tabella .pagination > li:last-child > a:before, .tabella .pagination > li:last-child > span:before { padding-right: 5px; content: "avanti"; ...

What is the proper way to select multiple elements in jQuery based on semantics?

One method I frequently employ is using classes to group related elements, such as: <input value="10" class="sum-this" /> <input value="20" class="sum-this" /> <input value="30" class="sum-this" /> The sum-this class does not have any a ...

Reset hover effect upon exiting the current website

Hey there! I am currently dealing with an interesting situation regarding a link styled as a button on my website. <a href="http://externalsite" class="button" target="_blank">go to external site</a> Additionally, I have applied a hover effec ...

Inquire about the width of the SVG text element

I am working with an SVG text element that looks like this: <text data-model-id=​"k10673" x=​"584" y=​"266" fill-opacity=​"1" style=​"font:​ 12px Arial,Helvetica,sans-serif;​ " fill=​"#a5d16f">​BU YIL SONU​</text>​ The t ...

I encountered an issue while attempting to fetch table data, receiving the following error message: "Uncaught TypeError: result.rows.product is not a function at products.html:134."

https://i.sstatic.net/WZ5CC.pngHere is the HTML I have written <form> <br/> <label for="products1">Product Name:</label> <input type="text" name="pdt" id="pr ...