Buttons are misaligned and do not align on a straight line

Below is the code output:

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

I am attempting to enhance the symmetry of these buttons.

The minus (-) button appears slightly smaller than the plus (+) button even though they have the same padding.

I have experimented with adjusting the values individually for each button, but it seems to be trial and error and results in an unpleasant look.

I suspect that the text symbols + and - may be causing this issue, but I'm not certain.

I searched on Google regarding this problem but could not find any relevant answers, which led me to create this question.

let count = 0
let errorMessage = "*Error Cannot Go Below 0*"

function increment() {
    count++;
    console.log("The button was clicked");
    document.getElementById("countNumber").innerText = count;
    document.getElementById("errorMessage").innerText = "";
    console.log(count);
}


function decrement() {
    if (count <= 0) {
        document.getElementById("errorMessage").innerText = errorMessage;
    } else {
        count--;
        document.getElementById("countNumber").innerText = count;
        document.getElementById("errorMessage").innerText = "";
    }
}
* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
    font-family: 'montserrat';
    color: white;
}

body {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    height: 100vh;
    background-image: url("https://media.istockphoto.com/videos/loopable-color-gradient-background-animation-video-id1182634371?b=1&k=6&m=1182634371&s=640x640&h=jqdmV3r0EeOvcbeJaQHu4do8s74YvzsxHWymZAf_MNg=");
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

h1 {
    font-size: 50px;
}

h2 {
    font-size: 90px;
}

#decrement {
    background-color: rgba(255, 0, 0, 0.589);
    /* margin-top: -45px; */
    margin-right: 8vw;
}

button {
    /* margin: 0px; */
    /* margin-bottom: -45px; */
    font-size: 90px;
    padding: 10px 20px;
    border-radius: 10px;
    border: 0;
    background-color: rgba(172, 255, 47, 0.671);
}

p {
    color: red;
    font-size: 25px;
    font-weight: 800;
    margin-top: -10vh;
}

div {
    display: flex;
    flex-direction: row;
    text-align: center;
    justify-content: center;
    align-items: center;
    width: 100vw;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>

<body>
    <h1>Simple Counter</h1>
    <p id="errorMessage"></p>
    <h2 id="countNumber">0</h2>
    <div>
        <button id="decrement" onclick="decrement()">-</button>
        <button onclick="increment()">+</button>
    </div>
    <script src="index.js"></script>
</body>

</html>

Answer №1

To enhance the button symbols, consider using a different font or even an SVG icon instead.
When utilizing a font, keep in mind that (depending on the font family) the character will align to the baseline. In such cases, adjust the paddings or utilize the vertical-align property until you achieve satisfactory vertical centering.

  • Apply gap for the parent flex container and set a min-width for the buttons.
  • Eradicate any unnecessary alignment margins.
  • Do not include text-align: center; in the parent CSS since it is already implementing justify-content.
  • Always specify type="button" attribute for non-submit buttons.
  • Avoid burdening users with error messages. Design a more intuitive UI and leverage the disabled attribute when necessary.
  • Avoid inline JavaScript attributes like on*, just as you would avoid inline style properties. Keep JS and CSS separate by using scripts or tags. Utilize Element.addEventListener instead.
  • Refactor rather than duplicating JS code. Implement reusable methods/functions for tasks like updating a UI counter.
  • Avoid using broad div selectors in your CSS. Opt for more specific targeting.

let count = 0
const EL_num = document.querySelector("#countNumber");
const EL_dec = document.querySelector("#decrement");
const EL_inc = document.querySelector("#increment");

const updateCount = () => {
  if (count < 0) count = 0;
  EL_dec.disabled = !count;
  EL_num.textContent = count;
};

const inc = () => {
  count++;
  updateCount();
};

const dec = () => {
  count--;
  updateCount();
}

EL_inc.addEventListener("click", inc);
EL_dec.addEventListener("click", dec);

updateCount();
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  font-family: 'montserrat';
  color: white;
}

body {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  min-height: 100vh;
  background-image: url("https://media.istockphoto.com/videos/loopable-color-gradient-background-animation-video-id1182634371?b=1&k=6&m=1182634371&s=640x640&h=jqdmV3r0EeOvcbeJaQHu4do8s74YvzsxHWymZAf_MNg=");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

h1 {
  font-size: 50px;
}

h2 {
  font-size: 90px;
}

#buttons {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  gap: 1em;
}

#increment,
#decrement {
  font-family: monospace;
  font-size: 5em;
  padding: 0.1em 0.3em 0.2em;
  border-radius: 10px;
  border: 0;
  background-color: rgba(172, 255, 47, 0.671);
  min-width: 1.4em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#decrement {
  background-color: rgba(255, 0, 0, 0.589);
}

#decrement[disabled] {
  opacity: 0.3;
}
<h1>Simple Counter</h1>
<h2 id="countNumber"></h2>
<div id="buttons">
  <button id="decrement" type="button">-</button>
  <button id="increment" type="button">+</button>
</div>

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

Preload-webpack-plugin does not support pre-fetching files

I have a query about prefetching and preloading content. In my vue app, I noticed that after building, I have duplicate files loaded in my dist/index.html file. Here is an example: Additionally, the "scripts" are not being preloaded/prefetched as expec ...

Using HTML5 date input on an android browser

As I develop a web page catering to Android users, I have incorporated a date form control for users to input dates. However, I have noticed that on most devices, the native date selector does not pop up. Interestingly, on certain Samsung devices like the ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

Developing a sliding menu with AngularJS

Currently, I am developing an AngularJS application. One of the features I am working on involves having a menu at the top of my page that, when an item is selected, will slide down to reveal content specific to that selection in the same area as the menu. ...

Retrieve the href attribute from a hyperlink positioned above a different element using Selenium

My challenge in using selenium is to extract an href from a link buried within numerous tags! The only identifiable detail I can work with is the presence of the text "Test text!" in the h3 tag as shown in the example below: <a href="/li ...

Sending information from a parent component to a nested child component in Vue.js

Currently, I am facing a challenge in passing data from a parent component all the way down to a child of the child component. I have tried using props to achieve this as discussed in this helpful thread Vue JS Pass Data From Parent To Child Of Child Of Ch ...

Moving up stacked columns with bootstrap or any other CSS technique

Is there a way to make column [3] move up if column [2] is taller than column [1]? [1][2] [3][4] Check out this bootply example of the layout : http://www.bootply.com/KbAf8UOk9c Currently, there is empty space between column [1] and [3] that I would li ...

What is the best way to incorporate additional list items into a field input?

I have been working on developing a simple to-do app and I am encountering an issue. After the user clicks enter in the input box, nothing happens as expected. Despite trying various methods, the problem persists. Below is the recent code that I have been ...

Discovering more about this topic

Looking for a way to create an expandable box that enlarges when "read more" is clicked, revealing text below it. And also looking to add a button that closes the expanded text back up. Experimented with the toggletext JavaScript command, as found on this ...

Help me figure out how to independently toggle submenus on my navbar by utilizing the Vue.js composition API

I am currently working on developing a navbar using the Vue.js composition API. My primary objective is to toggle the submenus when a user clicks on them. However, since I am iterating through an object to display the submenus, I am faced with the challeng ...

Troubleshooting: EJ Syncfusion React Scheduler Issues

I have recently implemented a syncfusion calendar for managing appointments in my ReactJs project, following the code examples from this link . After editing the JS and CSS codes (available at ), I encountered an issue with the CSS not displaying correctl ...

Ensure the width of ancestor flexbox divs is limited to the width of its child by setting a specific width

I have a specific element that incorporates flex display and I've defined its width with the flex-basis property like this: .flex-row input { flex: 0 1 450px; } There is a flex-row division nested inside a flex-container division. My goal is for th ...

Solving the issue of overflowing in the animation on scroll library

I've recently started using a fantastic library called animation on scroll (aos) for my web development projects. This library offers stunning and visually appealing animations. However, I encountered an issue where it causes overflow problems in my H ...

unable to show image retrieved from JSON data

Looking to showcase the data from my JSON objects, which are displayed in 2 images below https://i.stack.imgur.com/yhqFy.png https://i.stack.imgur.com/DUgFO.png In the data, I have properties like c_name, max_slots, and an array slots. Within the array, ...

Discovering the root cause of why a particular CSS style is not being implemented correctly

Currently, I am in the process of developing a secondary theme for one of my websites. It's going to be a dark theme. I'm facing an issue where a specific CSS style is not being applied to a particular element, even though it works perfectly fine ...

When hovering over the main menu, the submenu appears hidden behind it

I am struggling with setting up a dropdown submenu on my website that uses a responsive Joomla template. The submenu always appears behind the main menu and slider, despite trying z-index and relative positioning. Can anyone help me figure out what I' ...

Can we incorporate the radix-ui/colors variables into a CSS module file?

For my personal project, I am incorporating Radix components alongside radix-ui/colors. However, when attempting to import color variables into the CSS Module, I encountered an error. It seems that using the :root selector in the _app file is necessary, as ...

Populate the containing div with an image element

I am looking to fill a parent div with an img, but I want the image to be a standalone img element inside the div, rather than setting it as a background property. To clarify, instead of this: div { background-image: url('someimg.jpg'); ...

Enhancing your website with HTML5 Audio Player's Next and Previous functionalities

I've successfully created a playlist that is working, but I'm struggling to make the Next and Previous Buttons actually move forwards and backwards in the playlist. It seems like using the playNext function when next is clicked might work, but I ...

What is the best way to set specific filenames in order to transmit them to the server?

Is there a way to send images files to a server using NODEJS without utilizing the traditional input file tag? I want to hard code the filepaths in an HTML client to achieve this. Are there any solutions available for this? Thanks in advance. ...