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

Minimum width of Angular Material's mat-menu

I am looking to create a compact Material mat-menu using Angular 15. Below is the code I have: <mat-menu #flagMenu="matMenu"> <button mat-menu-item> <img src="assets/flags/en.png" class="flag"/> ...

Optimizing the appearance of an unordered horizontal list in the most effective manner

When attempting to make lists horizontal and hide default bullets, is it necessary to apply {display:inline} and {float:left} to both the <li> tags or would just one of these properties suffice? <ul> <li><a href="#">First item< ...

React Router Link Component Causing Page Malfunction

Recently, I delved into a personal project where I explored React and various packages. As I encountered an issue with the Link component in React Router, I tried to find solutions online without any luck. Let me clarify that I followed all installation st ...

Using the Play framework to showcase data in an HTML table

I have a project where I am working with multiple tables, one of which is a Person table containing id and name attributes. I have successfully retrieved the data from the database using MySql and converted it into JSON. However, I am struggling to find a ...

A guide on identifying the data type of a value entered into an HTML table using JavaScript

Currently, I am tackling a contenteditable HTML table challenge. My goal is to enforce the insertion of only numeric values while alerting the user if they attempt to input strings or non-numeric characters. Can anyone provide guidance on how to achieve th ...

The JQuery CSS function is unable to alter the height of the element

I am working with a grid that contains various elements, each with the class item. When I click on one of these elements, I toggle the class expand to resize the element. <body> <div class="grid"> <a href="#"> < ...

Issue with retrieving body class in Internet Explorer on Magento platform. The body class is not being recognized in IE, but works fine in Mozilla and Chrome

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>" dir="ltr"> <?php echo $this->getChildHtml('head') ?> <bod ...

Having trouble with the responsive design not functioning properly?

Having trouble with mobile view in my simple HTML Bootstrap code. Everything looks fine in desktop view but not in mobile. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < ...

Alignment issue: Center alignment failing to work correctly

I'm currently working on a navigation bar with a central title and links to other pages aligned to the right. Despite using text-align: center, the title remains off-center. I've implemented a flexbox to ensure that the title and links are on the ...

Unable to load module/controller file from Angular 1 into view/html

var app = angular.module("appModule",[]); app.controller("viewController",function($scope){ $scope.greeting = "Welcome to the future"; }); <html> <head> <script src="Scripts/script.js"></script> <script ...

Ensure that the div extends to the bottom of the page

I'm currently working on a web design assignment for one of my courses, and I'm facing some challenges with getting the div elements to span the entire page in terms of color. http://jsfiddle.net/vmm1s4Lt/ http://codepen.io/bmxatvman14/pen/fih ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

Troubleshooting: Django project not functioning properly post transfer from Windows to Mac

Using Django 3.1.7 on a MacBook Pro now, I recently moved the Django project from a Windows 10 machine. Despite setting up the database and superuser again, running pipenv sync didn't result in serving any of my URLs or Admin page CSS styles. The serv ...

What is the best way to retrieve the dimensions of a custom pop-up window from the user?

Currently, I am in the process of developing a website that allows users to input width and height parameters within an HTML file. The idea is to use JavaScript to take these user inputs and generate a pop-up window of a custom size based on the values pro ...

Load the flexslider once the fancybox container is opened

In my experience, I have found flexslider and fancybox to be very useful plugins. Individually, they work perfectly fine on a website that I am currently working on. However, when I tried placing a flexslider gallery inside a fancybox div, I encountered a ...

What is the reason behind this HTML/CSS/jQuery code functioning exclusively in CodePen?

I have encountered an issue where this code functions properly in JSFiddle, but not when run locally in Chrome or Firefox. I suspect there may be an error in how the CSS or JavaScript files are being linked. In the Firefox console, I am receiving an error ...

Using Slick Slider and Bootstrap CSS to display text on top of images

Currently, I am utilizing the slick slider from and I want to overlay text on top of the image. I tried using bootstrap carousel-caption, which works well with any image. However, with slick slider, it seems like the text is not at the highest level as I ...

In Python using Selenium, the text property of a WebElement may cut off duplicate whitespaces

Today, I came across an interesting observation regarding the text property of WebElement. Here is a PDF link that sparked my curiosity: https://i.stack.imgur.com/1cbPj.png Upon closer inspection, I noticed that the file name contains triple whitespace. W ...

Implementing Multiple Shared Footers in Rails 3

Looking for a solution to display unique footers in Rails? For instance, displaying one footer on the homepage and another footer on the rest of the site. Has anyone successfully implemented this before? I was considering using an if statement based on th ...

Converting Beautifulsoup4's findAll() method output of HTML into a list of strings using Python

I'm just starting to explore Python, as I have a project idea in mind that I believe could be achieved by web crawling with Python. Currently following a tutorial series, I know that some consider findAll() to be outdated (I'm not sure why, but ...