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

Managing multiple sets of radio buttons using the useState hook

Within my renderUpgrades-function, I handle the options of an item by including them in radio-button-groups. Each item has multiple options and each option has its own radio-button-group. Typically, a radio-button-group can be managed using useState, wit ...

Display the same data point on a Google Map from two different rows of a database

I'm looking to display multiple values for profFName and profLName on a map with just one point showing this information. If I have (2) pID connected to the same marker, I want it to show both profFName and profLName on a single point. Currently, it ...

Generate interactive jQuery slideToggle containers that monitor user clicks via Google Analytics

I have been working on a PHP and mySql project where I need to retrieve a list of businesses from a database. Once I have the desired businesses, I want to display them consecutively inside their own "clickDiv" container using jQuery slideToggle to reveal ...

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

Utilizing Vue.js 2.x to send a REST API request with a collection of objects

I currently have an array of objects stored in state, and my goal is to send this entire structure to a back end API for processing and receive a new set of values in return. Below is a simplified representation of this structure as viewed in the develope ...

Incorporating source files from an Express server into an HTML document

Recently, I delved into the world of Node.js with Express and Socket.io to create a web application, specifically a game. In my project, I have a designated /public folder where I aim to serve the necessary files for the client-side operations. Typically, ...

Displaying a div after a successful form submission using Jquery

I created two popup windows. One is used to ask the user whether they want to submit the form, and if they click 'yes', the form is submitted. The issue I encountered is that the other popup window should be shown after the form is successfully s ...

Tips for customizing babel's preset plugin configurations

I've integrated babel-preset-react-app into my project with the following .babelrc configuration: { "presets": ["react-app"], "plugins": [ "transform-es2015-modules-commonjs", "transform-async-generator-functions" ] } Currently, I&apos ...

Steps to properly create your own angular service with XHR

Exploring the world of AngularJS, I embarked on a mission to create a file upload feature along with other form data. While there are numerous scripts and plugins available, I decided to utilize my own custom service using $xhr. Despite successfully sendin ...

Highlighted Navigation Options

When visiting , take note of the top navigation bar. The background color on this page is white, but as you navigate to other pages, the tab changes to a different color. How can you modify this behavior and change the class? Is PHP or jQuery necessary f ...

What is the best way to extract data from a proxy in VUE3?

Currently, I am utilizing the ref() function to store data retrieved from Firebase. However, when attempting to filter and retrieve a single record, the outcome is not as expected. Instead of returning a single object, something different is being displaye ...

How to use jQuery Animate to create a step-by-step animation with image sprites?

I'm working on creating an image sprite using jQuery and I'm curious if it's feasible to animate it in steps rather than a linear motion. I initially tried using CSS3 animations, but since IE9 is a requirement, the animations didn't wor ...

Exploring the power of Jade and Angular through implementing a for loop within a table structure

I'm brand new to using Jade and Angular, and I could really use a hint from someone more experienced. ... - for (var i = 0; i < p.length; i++) tr td= i + 1 td= price(value='p[i].somedbstuff') ... I want the la ...

Custom Bootstrap design layout where the right column wraps under the left column

Need assistance with my bootstrap layout. Prior to the 980px breakpoint, the right column wraps under the left column. I want it to remain in its position without wrapping. The challenge is ensuring the left column has a fixed width while allowing the ri ...

Divergence in results: discrepancy found in the outputs of nodejs crypto module and tweetnacl.js

Consider this code snippet in nodejs: import crypto from 'crypto'; const pkey = `-----BEGIN PRIVATE KEY----- MC4CAQAwBQYDK2VwBCIEIJC52rh4PVHgA/4p20mFhiaQ/iKtmr/XJWMtqAmdTaFw -----END PRIVATE KEY-----`; // placeholder key function sign_message(m ...

"Discrepancy found in results between Node-Fetch POST and Firefox POST requests

I have encountered a challenge while trying to replicate a successful log-in process on a website using node-fetch after being able to do so with Firefox. The login process consists of three stages: Visiting /login which returns a sessionToken from the we ...

Transfer data as JSON from Flask to JavaScript

Having trouble sending data from Flask to JavaScript. I have the information from the database and added it to a dictionary. Now, I want to convert this data into a JSON object in JavaScript to display it on a map. Despite using JSON.parse in JavaScript, i ...

How Keyof can render an object undefined and prevent accurate verification

Encountering TS2532 error: Object is possibly 'undefined' while attempting to access an object's value by dynamically selecting the key. TypeScript seems to be restricting me from checking the field values, and I'm unsure of the underly ...

The module located at "c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx" does not have a default export available

I am currently delving into the realm of RxJs. Even after installing rxjs in package.json, why am I still encountering an error that says [ts] Module '"c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx"' has no default export ...

Attempting to showcase the quantity of rows

I am attempting to show the count of rows in a column within an HTML card by using EJS for data insertion. Below is the code I am using for data insertion: app.get('/home', async (req, res) => { const result = await db.query('SELECT CO ...