Unable to adjust dimensions with CSS width and height properties

I'm currently working on developing an online game with a login screen inspired by Paper.io. I have created the username input and play button, but there seems to be an issue with the width and height specified in the CSS file for the input field. Even though it's set at 216 x 30, it doesn't display correctly when I view the website. Strangely, when I paste the code into a snippet on this forum, it works fine, but not on my own computer. This is my first attempt at creating an online HTML5 game, so please bear with me!

html,
body {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  border: none;
  overflow: hidden;
  padding: 0;
  font-family: 'Luckiest Guy';
  font-size: 22px;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
}

#game .ctx,
#ctx {
  height: 100%;
  width: 100%;
  overflow: hidden;
  position: fixed;
  left: 0;
  top: 0;
}

#pre_game {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #dff3f7;
}

#pre_game .dark,
#dark {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .8);
}

#pre_game .logo {
  position: absolute;
  top: 15px;
  left: 50%;
  width: 500px;
  height: 160px;
  margin-left: -250px;
  background: url('/img/logo.png') center center no-repeat;
  background-size: contain;
}

#pre_game .grow {
  position: absolute;
  left: 50%;
  margin-left: -168px;
  width: 336px;
  top: 250px;
}

#pre_game .grow {
  animation: leaves 2s ease-in-out infinite alternate;
  -webkit-animation: leaves 2s ease-in-out infinite alternate;
}

#pre_game input,
#contact .button {
  position: absolute;
  left: 0px;
  top: 0px;
  padding: 15px 5px;
  text-indent: 10px;
  border: 0;
  height: 30px;
  width: 216px;
  line-height: 30px;
  font-size: 25px;
  background: #ededd1;
  border-bottom: 6px solid #a1a18d;
  outline: none;
}

#contact .button {
  text-indent: 0;
}

#pre_game .button,
#contact .button {
  position: absolute;
  right: 0px;
  top: 2px;
  padding: 15px 5px;
  text-align: center;
  cursor: pointer;
  height: 30px;
  width: 90px;
}

#pre_game .button,
#contact .button {
  background: #eaec4b;
  border-bottom: 6px solid #a1a130;
  margin-top: -2px;
  color: #888a34;
  line-height: 32px;
  font-size: 34px;
}

#pre_game .button:hover,
#contact .button:hover {
  background: #fafc5b;
}

#pre_game .button:active,
#contact .button:active {
  border: none;
  border-bottom: 2px solid #a1a130;
  border-top: 4px solid #333;
}

@keyframes leaves {
  0% {
    transform: scale(1.0);
    -webkit-transform: scale(1.0);
  }
  100% {
    transform: scale(0.9);
    -webkit-transform: scale(0.9);
  }
}

@-webkit-keyframes leaves {
  0% {
    transform: scale(1.0);
    -webkit-transform: scale(1.0);
  }
  100% {
    transform: scale(0.9);
    -webkit-transform: scale(0.9);
  }
}

#pre_game:before {
  content: '';
  box-shadow: inset 0 0 320px #80969e;
  -webkit-box-shadow: inset 0 0 320px #80969e;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

input:not([type]),
input[type="email" i],
input[type="number" i],
input[type="password" i],
input[type="tel" i],
input[type="url" i],
input[type="text" i] {
  padding: 1px 0px;
}

input {
  -webkit-appearance: textfield;
  background-color: white;
  -webkit-rtl-ordering: logical;
  user-select: text;
  cursor: auto;
  padding: 1px;
  border-width: 2px;
  border-style: inset;
  border-color: initial;
  border-image: initial;
}

input,
textarea,
select,
button {
  text-rendering: auto;
  color: initial;
  letter-spacing: normal;
  word-spacing: normal;
  text-transform: none;
  text-indent: 0px;
  text-shadow: none;
  display: inline-block;
  text-align: start;
  margin: 0em 0em 0em 0em;
  font: 13.3333px Arial;
}

button,
meter,
progress {
  -webkit-writing-mode: horizontal-tb;
}
<link href='//fonts.googleapis.com/css?family=Luckiest Guy' rel='stylesheet'>


<div id="pre_game" style="display: block;">
  <div class="dark"></div>
  <div class="logo"></div>
  <div class="grow">
    <div class="username">
      <input type="text" placeholder="Your Name" style="left: 0px;">
    </div>
    <div class="button play" style="right: 0px;" onclick="game_start()">PLAY</div>
  </div>
</div>

UPDATE: I want to clarify that the code snippet provided displays as intended, but when I run the same code on my computer and open the website, it appears differently. Here are screenshots illustrating the problem. Screenshot of incorrect display The dimensions shown in this picture are incorrect (206 x 0 instead of 216 x 30). Screenshot displaying wrong dimensions

ANSWER: Surprisingly, I managed to resolve the issue although I don't fully understand why. I experimented with normalizing the CSS and tried additional lines suggested by Timber Hjellum, but it made things worse. Upon further investigation, I discovered that the padding values were subtracted from the original width and height, resulting in the unexpected dimensions. By adjusting the values (adding 10 to width and 36 to height), I was able to achieve the desired appearance. Thank you all for your assistance!

Answer №1

Consider implementing a CSS reset or normalize.css in your project to ensure consistent rendering across browsers and devices. This will save you from dealing with pixel inconsistencies.

Also, try using the following box sizing CSS snippet:

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;
}

Check out this link for more information on inheriting box sizing: https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/

By adding normalize and ensuring it includes the box-sizing: border-box reset, you may see improvements in how width and height are calculated in your project.

Addressing your specific question about setting element dimensions with padding, remember to adjust the width to account for padding:

element {
width: 206px; /* 216 - (5px padding + 5px padding) = 206px */
padding: 5px; /* top: 5px, right: 5px, bottom: 5px, left: 5px; */
}

In conclusion, add padding to the total width when setting element dimensions with padding to achieve the desired result.

Answer №2

If you're experiencing issues with the sizing not being 216x30, it may be due to factors like padding and border dimensions that need to be taken into account. Rest assured, the functionality is working properly.

Keep in mind that when specifying the height and width of an element, this applies to the content area only, excluding any margins, padding, or borders.

Learn more about the CSS Box Model here

Answer №3

To achieve the perfect height for your button and input styles, consider splitting them out and experimenting with different attributes:

padding: 15px 5px;
height: 30px; <-- not necessary for the input
line-height: 30px;
font-size: 25px;

If you're still facing challenges, check out this Fiddle for reference: https://jsfiddle.net/timhjellum/8mytme49/10/. It looks fantastic in the Fiddle :-)

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 best way to emphasize case-insensitive searchtext matches in JavaScript?

If I have data containing words like Krishna, krishna, KRISHNA and I enter the search text as 'krish', it will retrieve all three words. However, when I want to highlight the matching result, only the exact matching part of the string is highligh ...

Is there a way to recover a deleted element from an array?

I have a list of user cards, and my task is: Clicking the "undo" button: will restore the last card that was deleted (using an array) Question 1: How can I create an array from the displayed cards list? Question 2: How do I restore the last deleted card? ...

In the conditional statement, document location or window location href will be ineffective in the if clause but will be functional in the else clause

When validating a password in a form, I have encountered an issue where the code only works for one specific outcome. If the correct password is entered, the user should be moved to site X, but if it's incorrect after 3 tries, they should be moved to ...

Rectangles on canvas, each with identical dimensions, appear in varying sizes when positioned at different locations

In my canvas, I have created two rectangles using the rect element. Both rectangles are supposed to be 40 units wide and 20 units tall. The first rectangle starts at coordinates 0,0 for its top-left corner, while the second one begins at 60,40. Interesti ...

Facing Challenges with Python Selenium while Accessing Specific Form Elements in Salesforce

Recently, I've been using Selenium to automate some data entry tasks on Salesforce. So far, my script successfully loads the webpage, allows me to log in, and clicks an "edit" button. Now, I'm encountering an issue when trying to input data into ...

Creating a sticky positioning effect in CSS

Can someone assist me in implementing the position: sticky as demonstrated below? Currently, the upcoming date overlaps the current date. This causes the opacity and shadow of the date to reach 100%, which can create a cluttered appearance if there are m ...

Validating HTML forms using Javascript without displaying innerHTML feedback

Hey, I'm just diving into web development and I'm struggling to figure out why my innerHTML content isn't displaying on the page. Can anyone offer some assistance? I'm trying to display error messages if certain fields are left empty, b ...

Ways to remove the default classes added by ngbootstrap

My current challenge involves utilizing ngbootstrap for popovers, yet desiring to override its default styling. Specifically, I have a form that should function as a popover triggered by a button click, and it needs to maintain its own distinct styles. Up ...

Struggling to delete items from an array in react.js

I am attempting to remove an item from a nested childArray within another Array. This is my current approach: const childArrayHandler = (childData, sub, questionId, data, btnId) => { // Manage color change on click const isInList = selectedBtnL ...

What is the best way to redirect nodejs requests to a separate nodejs application?

Hello! I am currently working on a project to create an API gateway using Node.js and Express. The goal is to showcase a small-scale microservices architecture by routing requests to different microservices. For instance, if I have microservices A, B, and ...

Error message due to an undefined function in Angular datatables Fixed Columns

I recently implemented angular datatables with fixed column in my application. Here is the HTML code I used for the view: <div class="row" ng-controller="PerformanceCtrl"> <table id="example" datatable="" class="stripe row-border or ...

Bootstrap Tags Input - the tagsinput does not clear values when removed

I am attempting to manually remove the input value from bootstrap-tags-input when the x button is clicked, but the values are not changing in either the array or the inputs. This is the code I have tried: $('input').tagsinput({ allowDuplica ...

Simulating an ES6 module that returns a factory function for moment.js

Disclaimer: I'm a beginner with Jest so please bear with me. I'm currently working on testing a Vue2.js filter named DateFilter using Jest. This filter simply formats a date that is passed to it. DateFilter.js import Vue from 'vue'; ...

Adjust the top margin of a div to match the height of the screen within an iframe, ensuring cross-browser

Trying to adjust the margin-top of a div to 100% of screen height within an iframe seems to be causing issues with jQuery, as it either returns 0 or inaccurate values. While CSS3's 100vh can work as an alternative, it may not be supported in older an ...

Using Primeng to implement pagination and lazy loading in a dataView

Looking for a way to search through product data with filters and display it using primeng dataview? Consider implementing pagination in your API that pulls products from the database page by page. Set the products array as the value in dataview so that wh ...

Customize each carousel item by adding a unique background image to enhance the visual appeal

I'm looking to customize my Bootstrap carousel by adding unique background images to each item. Here's a snippet of my HTML: <!-- Start of carousel --> <div id="mainCarousel" class="carousel slide carousel-fade" d ...

Move the location of the mouse click to a different spot

I recently received an unusual request for the app I'm developing. The requirement is to trigger a mouse click event 50 pixels to the right of the current cursor position when the user clicks. Is there a way to achieve this? ...

Requesting for a template literal in TypeScript:

Having some trouble with my typescript code, it is giving me an error message regarding string concatenation, const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format(&apo ...

Creating a dynamic linear gradient background color using Angular 2 binding

Using static values works perfectly <div style="background: linear-gradient(to right, #0000FF 0%, #0000FF 50%, #FFA500 50%, #FFA500 100%);"></div> in my TypeScript file. this.blueColor = '#0000FF'; this.orangColor = '#FFA500&a ...

Unable to save the user's identification in the session

I am in the process of retrieving the user ID of a logged-in user and storing it in the session. This way, when a user submits something, I can include their user ID in the submission to the database. My login page is quite rudimentary, so any guidance wou ...