The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual representation of what I mean:

https://i.stack.imgur.com/yGYD1.png

(Please note that the dark theme effect is due to my GTK theme).

I am puzzled by this inconsistency, can anyone explain why this happens?

var screenWidth = $(window).width();
var screenHeight = $(window).height();
$(".userName").css("width", screenWidth / 4);
$(".passWord").css("width", screenWidth / 4);
$(".submit").css("width", screenWidth / 4);
body {
  margin: 0;
  padding: 16px;
}

.userName {
  background-color: transparent;
  border: none;
  border-bottom: 2px solid #FF8A80;
  color: #000000;
  padding: 16px;
}

.passWord {
  background-color: transparent;
  border: none;
  border-bottom: 2px solid #FF8A80;
  color: #000000;
  padding: 16px;
}

.submit {
  background-color: transparent;
  border: 2px solid #FF8A80;
  color: #000000;
  padding: 16px;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

.submit:hover {
  background-color: #FF8A80;
  border: 2px solid transparent;
  color: #FFFFFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="userName" class="userName" title="This area is for your unique username" placeholder="Your username here" required autofocus />
<br />
<input type="password" name="passWord" class="passWord" title="This area is for your unique username" placeholder="Your password here" required />
<br />
<button type="submit" name="submit" class="submit" title="Click this button if you are sure your information is right">Submit</button>
<br />

Answer №1

Your code has been copied and I've added a new block of CSS:

* {
    box-sizing: border-box;
}

/* OR */

input, button {
    box-sizing: border-box;
}

Try running the code below.

var screenWidth = $(window).width();
var screenHeight = $(window).height();
$(".userName").css("width", screenWidth / 4);
$(".passWord").css("width", screenWidth / 4);
$(".submit").css("width", screenWidth / 4);
* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 16px;
}
/* More CSS styling here */    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="userName" class="userName" title="This area is for your unique username" placeholder="Your username here" required autofocus  />
<br />
<input type="password" name="passWord" class="passWord" title="This area is for your unique username" placeholder="Your password here" required />
<br />
<button type="submit" name="submit" class="submit" title="Click this button if you are sure your information is right" >Submit</button>
<br />

The default setting for the box-sizing property is content-box for all elements.

With content-box, an element's width and height does not include padding and border in its calculation. Changing it to border-box includes these values in the width and height calculations.

For more information on box-sizing, check out resources here and here.

Note: The margin value remains unaffected in either case.

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

Setting radio button values according to dropdown selection - a beginner's guide

I am trying to dynamically set the default values of radio buttons based on the selection made in a drop-down menu. For example, if option A or B is chosen, I want the radio button value to default to "Summary", and if option C is chosen, I want the value ...

When attempting to use the jsonify method on a variable within an html file, the output is not displayed

Is it possible to store "jsonify" in a variable? I find the pure json format of "return jsonify(data)" unappealing, so I am looking for a way to enhance it using an HTML template with proper indentation. Here's the python code I have: from flask impo ...

Blank page shown when routing with Angular in Rails

Hey there, I'm currently attempting to integrate Angular into my Rails application and unfortunately I'm encountering a problem where the HTML page shows up blank. Here's the code I have so far: app/views/index.html.erb <body> ...

Ways to determine if an element is the initial child

I am currently facing issues while attempting to make my bootstrap carousel dynamic and inject data properly. My aim is to locate the first child of the injected data and apply the "active" class to it. I am utilizing Laravel Blade for rendering my data. B ...

How to retrieve distinct items from an array using Javascript

If I have the following array of objects: const arr = [{ id: 'A', version: 0, name: 'first' }, { id: 'A', version: 1, name: 'first' }, { id: 'B', version: 0, name: 'second&apo ...

Exploring the possibilities of integrating React with multiple Material UI dialogs

My goal is to have two dialog boxes, one for the sign-up page and another for the login page. When a user clicks on the sign-up button on the top page, the sign-up screen should appear. Likewise, when they click on the login button on the sign-up page, the ...

Three.js Photometric Function

Is there a way to specify a photometric function in Three.js? Currently, I am utilizing a Lambert material: new THREE.MeshLambertMaterial({ color: 0xffffff }) However, I am interested in using a Lommel Seeliger but I am unsure of the process and locatio ...

How to Manage Empty Lines in HTML Documents within WordPress

Just recently, I launched my brand new website. Everything seems to be functioning properly except for one issue that I discovered in the HTML source code. There are 15 blank lines before "<!doctype html>", which is causing some problems with SEO and ...

Attempting to clear the value of a state property using the delete method is proving to be ineffective

Within my React-component, there exists an optional property. Depending on whether this property is set or not, a modal dialog is displayed. Therefore, when the modal should be closed/hidden, the property must not be set. My state (in simplified form): i ...

Oops! A JSON parsing error occurred due to the presence of an unexpected token '}'

I encountered an issue while trying to develop a registration route using passportjs. When sending a request via Postman, I received the following error message: SyntaxError: Unexpected token } in JSON at position 119    at JS ...

Troubleshooting Problem with Scrolling Sticky Element on iOS Devices

This is specifically for mobile devices I am facing an issue with a relative positioned element where it should become fixed to the top of the screen when the scroll position exceeds the top position of the element. However, in iOS, when scrolling, the f ...

What is the process for removing a full row from a table in PHP?

Is there a way to add a delete button to delete an entire row in a MySQL database? Below you can find the code and instructions on how to delete a row using the ID, which is the primary key. <?php mysql_connect('localhost', 'off', ...

Can you identify the date stored in this JSON object?

I need to analyze some JSON data This is the specific snippet required for my query. "UpdatedDate":"\/Date(1311377875937)\/" I'm unsure about that number, but the updated date indicates when the item was last modified Moreover, how can I ...

Exploring Database with NodeJS, Express, and HTML

My goal is to create a basic web application using node.js where users can input data into a search bar and have that input sent to the server for database query. While I've successfully set up and connected my database, here's a glimpse of my co ...

Arranging by upcoming birthday dates

Creating a birthday reminder app has been my latest project, where I store names and birthdays in JSON format. My goal is to display the names sorted based on whose birthday is approaching next. Initially, I considered calculating the time until each pers ...

Ensure the text value of a collection of web elements by utilizing nightwatch.js

Recently, I started using nightwatch.js and I am trying to retrieve a list of elements to verify the text value of each element against a specific string. Here's what I have attempted: function iterateElements(elems) { elems.value.forEach(funct ...

Ensure that selected options are unique across multiple selections

Could you help me with a question that involves matching pairs of words in both Russian and English? <div class="form-group" id="question4"> <label for="q4FirstSelectEN">4</label> <div class="row"> <div class="col-lg ...

Struggling to comprehend the filtering arguments in VueJS?

While going through the VueJS Filter(orderby) API documentation, I came across some confusion regarding the arguments. Below is a sample for reference: Arguments: {String | Function} targetStringOrFunction "in" (optional delimiter) {String} [...s ...

Obtain an array of responses using jQuery ajax and pass it to PHP

I'm working on a project where I need to populate a table from MySQL based on a selection made in a select box using jQuery AJAX. Currently, this is the jQuery code that I have written. I am able to display the result in an alert box, but I am uncerta ...

Android Gmail Application: Implementing 1px horizontal spacing between inline images placed within <td> tags

Have you ever noticed that in the Android Gmail app, there can be a mysterious 1px gap between inline images? This little pixel problem can sometimes push the rightmost image outside the comfy frame of your email. It seems this glitch prefers images of cer ...