What is the best way to vertically center a textbox within a form using HTML/CSS, especially when padding is involved?

I've searched extensively but I'm struggling to find a solution, so please excuse if this seems like a simple question.

Essentially, I have a text box that I want to center on my page. The issue is that I have padding inside the textbox, which affects the alignment of the text with the border.

Is there a way to achieve this?

Here is the website in question: http://prntscr.com/c2m93u You'll notice that the spacing between the left and right sides is uneven. This is due to the padding I have applied.

Below is my code:

<input type="text" name="username" value="Gebruikersnaam", id="TextField" style="width:100%;height:25px;background-color:black;color:white;border:0px;margin:auto;padding-left:4px;padding-right:4px;">

Any assistance on this matter would be greatly appreciated. Thank you in advance.

Answer №1

To ensure the total width of the element includes the padding, use the CSS property box-sizing: border-box. For further details, refer to the CSS Box Model documentation on MDN.

Answer №2

HTML


<div>
  <input type="text" name="username" value="Username", id="TextField">
</div>

CSS


*
{
  box-sizing: content-box;
}
div
{
  width:90vw;
  margin:auto;
  text-align:center;
  background-color:rgba(50,50,50,1);
  padding:10px;
}
input[type='text']
{
  width:75%;
  margin:auto;
  height:20px;
  background-color:black;
  color:white;
  border:0px;
}

FIDDLE : https://jsfiddle.net/27czxvdg/

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

Issues with Mouse Hover/Image Replacement

In this particular example, I am attempting to achieve a simple functionality where an image changes when the mouse hovers over it, and reverts back to the original image when the mouse is no longer hovering. However, despite my efforts, there seems to be ...

Tips for returning an element to its starting position following animation

Hey there, I’m fairly new to the world of HTML and CSS. Recently, I was working on creating a YouTube clone website and I’ve run into an issue with the navigation. On the official YouTube website, when you click on the hamburger menu icon, the naviga ...

Checking the status of a Cisco Jabber user using JavaScript

How can I verify on my webpage if a user is available on the Cisco Jabber Client? I have both their number and username. I can currently send chat messages and start voice conversations, but I would like to display a cool icon or indicator if the user is o ...

Verify whether all the elements within a specific div are distinct by utilizing jQuery

I need to create a select box that, when an option is chosen, generates a certain number of textboxes within a div. Currently, there are three fields: display_name[], user_name[], and user_password[] The code for this functionality is as follows: <se ...

Use Styled-Components to define the appearance of child elements when their parent is hovered over

I have a straightforward element There are two variations of it - one using styled-components and the other without: No Styled Components <div id="box"> <div id="inner-box"></div> </div> #box { width: 100px; height: ...

Eliminating the Double Tap Issue on Dropdowns in Bootstrap 3.0

I am transitioning from Bootstrap 2.3 to Bootstrap 3.0 and need to eliminate the double tap feature on the li elements within a dropdown in a navbar. For example: <ul class="nav navbar-nav"> <li class="active"><a href="#">Link</a&g ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

What is the best method for directing to various pages on a GitHub Pages website?

My GitHub Pages site is live at this link. While the splash page loads perfectly, clicking on the circle to transition to the next page, 'landing.html', leads to a 404 error. I have exhausted all possible solutions to address this issue, includin ...

Menu Design Inspired by Amazon Using jQuery

Can someone please assist me in locating a jQuery Amazon-style menu example? I would like to implement something similar and am hoping to avoid having to write it from scratch. Thank you! ...

Divide the <ul> element into two columns with CSS only if the number of <li> items is more than 5

Is it feasible to divide a list into two columns using pure CSS if there are more than 5 child <li> elements? In the scenario I have in mind, this is the desired outcome: <li>1</li> <li>2</li> <li>3</li> ...

Monitoring alterations in dynamically produced input fields using jQuery

My code currently generates multiple input fields dynamically, each related to a specific database record. I utilize PHP's dynamic array notation ('[]') to store the input values. Everything is working smoothly up to this point... Now, I am ...

Tips for enhancing the clarity of an `html2canvas` screenshot

Here is the code I've been using with html2canvas to capture and download screenshots: Create HTML button <button class="btn btn-default btn-sm" style="margin:0px 0px -10px 970px; padding:2px 4px 1px 4px" onclick="genScreenshot()"><span cla ...

Is it possible to include a relative URL with a different port number in a hyperlink?

Is it possible to link to a different port number on the same server without using Javascript or server-side scripting, especially if the hostname is unknown? For example: <a href=":8080">Check out the other port</a> (Note that this example ...

Achieving proper layout in Material-UI Autocomplete by splitting long words

Exploring the Material-UI autocomplete feature for the first time has brought some challenges my way. I am working with usernames, which have a specific length limit but could exceed the space available in the autocomplete view. https://i.sstatic.net/O8t1 ...

How can I access multiple icons using jQuery?

Below is the JavaScript code I am using: $(document).ready(function() { $.getJSON("Stations.php", function(jsonData){ $.each(jsonData, function(key,value) { $('#selectStation') .append($("<option></option>") ...

Why is it that filling a DataTable with a variable doesn't work, but when I input the same value into a PHP site and request it using AJAX, it suddenly works?

Contained within the variable named "test" is the following data: {"data":[{"HistChar_ID":"4","Vorname":"Garnier","Nachname":"de Naplouse"},{"HistChar_ID":"2"," ...

Arranging Elements Using CSS

Currently, I am working on aligning and positioning my items using CSS. However, I'm a bit confused about the right approach. Here is a demo of my current progress: Demo Link I aim to achieve a layout similar to this sketch: Sketch Image Apologies ...

Exploring test automation methods for selecting HTML elements - whether based on Element ID or DataAttribute

As I work on implementing ID's for UI Test automation, I am considering whether using data-attributes (such as data-testHandle="mybutton") would be a more readable option for future developers. These ID's are solely for testing purposes. Accord ...

The problem with the CSS3 media query arises only within a particular div and not on

I've been encountering an issue with one of the media queries on my website. Specifically, the query targeting a div in the footer is not working as expected. I'm wondering if there's a problem with how I've written the notation? @medi ...

Why does the JavaScript background color change take precedence over CSS selectors for my object?

Can someone explain why my list elements are not turning blue when I hover over them, even with the CSS and JS provided below? li { background:red; } li:hover { background:blue; } Here is the JS I am using: document.getElementsByTagName("li")[0].style.b ...