Navigating size measurements in percentages and pixels to achieve flawless alignment

Utilizing the calc property in a solution, I am facing challenges when trying to calculate the percentage width of an element with margins, padding, and borders set in pixels.

Take a look at this demo:

#form {
    margin: 200px auto;
    width: 360px;
    background:green;
    padding:0
}

 input[type="text"], input[type="password"] {
    width: calc(50% - 42px);
    margin: 10px;
    padding: 10px;
    border: solid 1px #000;
}

The form contains two inputs, and I aim for them to completely fill their parent div. My formula is:

100%= (input_width + ( margin_width + padding_width + border_width ) * 2 ) * 2

=> input_width= 50% - 42px

However, currently, the maximum width I can use is 50% - 45px to keep both inputs on the same line. What am I missing? http://jsfiddle.net/uL2syf4m/3/

Answer №1

The issue arises from the excess white space caused by your HTML structure and how it interacts with inline-block elements. By removing the line breaks and extra tabs between your input tags, you will be able to effectively apply the desired CSS rule width: calc(50% - 42px);. For more information and insights, refer to this detailed article HERE

You can view a working example in this FIDDLE

Answer №2

To achieve the desired outcome, you must ensure that they are floating properly.

#custom {
    margin: 20px auto;
    width: 280px;
    background-color: blue;
    padding: 0;
}
#custom::before, #custom::after{
    content: " ";
    display: table;
    clear: both;
}

input[type="email"], input[type="password"] {
    float: left;
    width: calc(50% - 32px);
    margin: 8px;
    padding: 8px;
    border: solid 1px #000;
}
<div id="custom">
	<input type="email" id="field1"/>
    <input type="password" id="field2" />
</div>

For further reference, please visit the jsfiddle below:

http://jsfiddle.net/aB4d7cW1/9/

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 are the best strategies for aligning the content on the front page of my website?

Creative Website Concept Current Progress Snapshot The initial image showcases the desired appearance for my website. The title is positioned in the center of the header elements and features a sleek black border. Below the header and title, there is an i ...

Creating a customized notification icon featuring a count of notifications

I am trying to add a notification icon to my website similar to Facebook. On Facebook, the notification icon is displayed in the top left corner with a number indicating the total notifications. I would like to replicate this feature on my site as well. Be ...

What is the best way to customize the color of the border and icon in an outlined MaterialUI input field

I'm a newcomer to materialUI and I have a question regarding customizing an outlined input field with an icon. Due to my dark background, I need the icon, border, and text color to be lighter, such as grey. Can someone please advise on the materialUI ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...

Move the cursor within the text area upon clicking the button

When the "+header" button is clicked, I am looking to automatically position the insertion point inside the text area. Currently, after pressing the button, the text box displays information like address, date, time etc. but the cursor does not start insid ...

What is the proper way to include a URL when submitting an AJAX form?

When I try to call a servlet using HTML, jQuery and Ajax by submitting a form, I keep getting a 404 error saying the resource cannot be found. My project is built with Maven. The Servlet is located in the src/main/java package under : com.mycompany.sample ...

Creating an icon on the right side of a Bootstrap panel: A step-by-step guide

Seeking assistance with aligning a cool icon within a bootstrap panel. I am trying to position this icon on the right side of the panel heading, but the code is not cooperating. Can anyone provide guidance? <div class="panel panel-primary"> < ...

Upon loading the page, IE11 activates a CSS transition even when a media query that is not applied is present

Encountering a unique situation with IE11 where a panel (DIV) transition is only activated on pageload if a media query matched the highest CSS specificity for the element. The issue does not occur on Chrome, Firefox, or Safari on tablets and phones. The d ...

Unable to make the Show/Hide Loading GIF function work as intended

Could someone please help me with my code? I'm trying to display a loading GIF image while waiting for an ajax request to complete. When the button is pressed, I want the loader to be shown until the data is returned from the server. I would greatly ...

text/x-handlebars always missing in action

I'm currently working on my first app and I'm facing an issue with displaying handlebars scripts in the browser. Below is the HTML code: <!doctype html> <html> <head> <title>Random Presents</title> ...

Using the select tag in Rails with Blueprint CSS

I am currently working with Rails 3. To adjust the size of a select drop-down menu to 330px, I made changes in my css file like so: select { width: 330px; { However, I encountered an issue when trying to set another page's drop-down menu width t ...

Having trouble with my navbar toggler arrow malfunctioning

I have developed a toggler button with an arrow icon that transforms into bars when clicked. However, I am experiencing some issues with the code: 1.) After clicking the toggler for the first time, the arrow icons change to bars successfully. But when I c ...

Is it possible to implement addEventListener.delay() functionality?

Hello, I am working on a Qualtrics code that utilizes jQuery. I am looking to incorporate a delay function for event listening in my code, which currently allows key presses as inputs. Specifically, I need to disable the keys for the initial 2000ms before ...

What is the best way to ensure only one item is being selected at a time?

In the initial state, I want 'ALL' to be automatically selected. Clicking on another item should change the classes so that only 'this' has the class and the others do not. However, I am facing an issue where 'ALL' cannot be r ...

Unable to properly display the message in Angular

<!DOCTYPE html> <html ng-app> <head> <script data-require="angular.js@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script> <link rel="stylesheet" href="style.css" /> ...

Eliminating unused CSS space on a website

I am in need of assistance with removing the white space between the border of the Marquee text and the slider image on my website. I realize this may not be the appropriate place to ask, but any help would truly save my day. You can view the issue at . ...

Tick the box to activate the ability to uncheck multiple boxes when there are already a certain number of boxes checked

I am currently developing a multiple-choice form in my MVC5 app and I am struggling to find a way to disable unchecked boxes once a specific number of boxes have been checked. For example, if 3 out of 5 boxes are checked, the remaining 2 should be disabled ...

Unlock the power of nested dynamic content creation with JavaScript

Every time I attempt to use getelementbyid on a dynamically loaded div, I receive null as the result. This occurs even after trying both window.onload = function () { and $(window).load(function() { index.html: <main > <div id="main-div"> ...

Implementing dynamic width with ng-style in AngularJS

I am trying to dynamically resize a div when an event is fired from S3. In the code below, I pass the progress variable to the updateProgress function as a parameter. This function resides in the top scope of my Angular controller. s3.upload(params).on(& ...

What is the best way to customize the label of Material UI's Tab component?

Is there a way to customize the text color inside the Tab component while still allowing it to change color when selected as an active Tab? For example: <Tabs centered onChange={handleChange} value={value} textColor={'secondary'} indicatorCol ...