Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle?

That's my only query, but I can't post it alone due to mostly having code in my post. What an inconvenient feature.

https://i.sstatic.net/SmKQQ.png

Navbar.js

import React from 'react';
import { BsFillPersonFill } from 'react-icons/bs';
import { FiMail } from 'react-icons/fi';
import { FaPlus, FaSearch } from 'react-icons/fa';
import { AiTwotoneBell, AiOutlineSearch } from 'react-icons/ai';
import './navbar.css';
function Navbar() {
  return (
    <nav id="nav-bar">
      <div className="container">
        <h2 className="homeBtn">VIZZEY</h2>
        <div className="search">   
          <input type="search" placeholder="Search" className="form-control" />
          <button className="searchBtn"><AiOutlineSearch/></button>
        </div>
        <ul className="ugh-buttons">
          <li className="btn">
            <button className="icon-btn">
              <FiMail/></button>
          </li>
          <li className="btn">
            <button className="icon-btn"><FaPlus/></button>
          </li>
          <li className="btn">
            <button className="icon-btn"><AiTwotoneBell/></button>
          </li>
          <li className="btn">
            <button className="icon-btn"><BsFillPersonFill/></button>
          </li>
        </ul>
      </div>
    </nav>
  )
}
export default Navbar;

navbar.css

* {
    margin:0;
    padding:0;
    box-sizing:border-box;
  }

ul {
    list-style: none; 
    display: flex;
} 

a {
    text-decoration: none; 
    color: #fff;
} 

.homeBtn {
    text-align: center; 
    justify-content: center; 
    padding-left: 25%; 
    color:#00ce7f; 
}

#nav-bar {
    background-color: #626466;
    overflow: hidden;
} 

.container {
    display: grid; 
    grid-template-columns: 1fr 6fr 1fr;
    align-items: center; 
    height: 55px;
} 

.form-control {
    background-color: rgb(255, 255, 255); 
    border-color: rgb(133, 133, 133);  
    border-top-left-radius: 5px !important;
    border-bottom-left-radius: 5px !important; 
    height: 38px; 
    width: 70%; 
    border: none; 
    padding-left: 10px;   
    font-size: 20px; 
}   


.search { 
    padding-left: 15%;  
}

.btn { 
    padding-right: 10px; 
} 

.ugh-buttons {
padding-right: 20px; 
}

.icon-btn {
    height: 40px; 
    width: 40px; 
    border-radius: 5px; 
    background-color: #00ce7f;
    color: white; 
    border: none;  
    font-size: x-large; 
    
}


button:active {
    outline: none !important;
    box-shadow: none !important; 
    background-color: rgb(111, 0, 255);
} 

button:focus {
    outline: none !important;
    box-shadow: none !important; 
}

button:hover {
    cursor: pointer;
} 

.searchBtn {
    color: #fff;
    background-color: #00ce7f;
    height: 38px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px; 
    width: 40px;   
    border: none;    
    font-size: large; 
  
}       

.buttons {
    color: #fff;
    background-color: #00ce7f;
    height: 38px; 
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px; 
    border-top-left-radius: 5px; 
    border-bottom-left-radius: 5px; 
    width: 40px;   
    border: none; 
}
 
[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
  appearance: none;
}

input {
    outline: none; 
}


Answer №1

To perfectly center your icon in a button, consider adding the following CSS to your stylesheet:

button[class*="icon-"],
button.searchBtn {
    display: flex;
    justify-content: center;
    align-items: center;
}

If this method does not yield the desired result, double-check the size of the icon in the browser's F12 mode. Some icons may appear to have the same dimensions, but they do not.

Another approach you can try is using the position: absolute technique:

button[class*="icon-"],
button.searchBtn {
    position: relative;
}

button[class*="icon-"] > svg,
button.searchBtn > svg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Hopefully, these solutions will help you achieve the desired result! 😊

Answer №2

There is no need to make extensive changes. By utilizing the display flex property on the parent element, the child elements naturally align themselves side by side. To ensure they each occupy an equal amount of space (assuming that is your desired outcome), you should incorporate the flex wrap property on the child elements.

ul {
display:flex;
}

li {
flex-wrap: 1;
}

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 steps can be taken to enlarge the select button within a <select> field?

Is there a way to enlarge the size of the downward-pointing arrow button within a <select> element? Here is an example of what my code looks like: <select> <option>Select City</option> <option>City1</option> <o ...

Tips for using jQuery to verify the most recent entry in a form

Struggling with this issue - I can't seem to get jquery to function the way I need it to: Essentially, when a user exits a text field, jquery is supposed to capture the input value and respond accordingly: for example, if the user types 'value& ...

Showcasing certain elements as the user scrolls

Looking for a way to display an element (such as a div) when the user scrolls without using JQuery? Here's an example that tries to achieve this: var scroll = document.body.scrollTop; var divLis = document.querySelectorAll("div"); for(let i = 0; i ...

Unexpected results occurring during JavaScript refactoring process

Having this repetitive code snippet that switches between two radio buttons being checked within my $(document).ready(): $(document).ready(function () { $("#New").click(function () { var toggleOn = $("#New"); var tog ...

Styling with CSS to show an image and text on the same line

I would like to show text and image on the same line, with the image slightly above the text. View Demo html img.del { display: inline-block; float: right; cursor: pointer; margin-right: 74% } .astext { text-align: left; margin-le ...

Update the image source every 1 second with Jquery and Javascript

I've been experimenting with creating a script that dynamically changes the source of an image every two seconds based on a list. Essentially, my current approach involves using a for loop to iterate over the provided list: $(document).ready(functio ...

Determining the file path in HTML5

Can anyone help me with retrieving the file path using html5 javascript once a user selects a file? I require the file path for a specific scenario: In this case, the user uploads a file and pauses it (currently only supported by Mozilla browsers), then c ...

Access WordNet using JavaScript

Currently developing a web application that involves NLP tasks. In my past projects, I have used the JWNL library in Java which has always served me well. I am curious to know if you have any advice on the most effective approach for querying the WordNet ...

Is there a more efficient way to handle post data without increasing its size when encoding JSON within a URI

I have a significant amount of data to post, and it needs to be minimized for performance reasons. Initially, my data consists of JavaScript objects which I then convert to JSON format before sending it via POST request. The issue is that my data include ...

Problems with the main title formatting in the header

I am currently working on a new website and have encountered an issue with the header design. The Mainline "PersIntra" is overlapping the "log out button" box, which I would like to be positioned beside it instead. Despite my efforts with CSS and nesting ...

How to delete rows from a table in bootstrap with JavaScript

My goal is to create a table that allows users to delete specific rows using JavaScript and bootstrap. I attempted the standard method, but it doesn't seem to be working: <form action="scrivi.php" method="POST"> <t ...

Is it possible to eliminate the css class prefix when using Modular css in React?

When working with React & NextJS, I have observed that my modular SCSS files are automatically prefixing my classnames with the name of the file. Is there a way to turn off this feature as it is making the DOM structure difficult to interpret? For instanc ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

How come this particular design is being passed down from a predecessor (and not a direct parent) div?

Web development can be frustrating at times. Hopefully, someone out there can shed some light on this issue and offer a solution. You can view the HTML/CSS code below or check out this example on JSFiddle The problem arises when I have a header div and a ...

Triangle-shaped image mapping and interactive hover effects

I am attempting to create a simple hover effect using two triangles. One triangle should start below the other, and upon hovering, it should appear above the first triangle. The problem I am encountering is that the surrounding boxes are obstructing the e ...

JavaScript Loading Screen - Issues with window.onload functionality

I am currently working on creating a loading screen for my project. I need to use JavaScript to remove the CSS property "Display: none" from the page, but for some reason, my code is not functioning as expected. The Issue: I discovered that using window. ...

What is the most effective method for updating edited rows in a DataGrid MUI and transferring them to

Is there a way to update the values I just edited in firebase after using editRow (please correct me if I'm mistaken)? Within DataGrid //Enable row edit mode editMode="row" //Any updates made in the row are stored in this variable, overrid ...

WordPress site experiencing issues with sub-menus disappearing following recent upgrade

I'm currently investigating a problem on a WordPress website where sub-menus are not showing up after upgrading to WP 3.9.1. The website, which can be found here, is using the Zeus theme (v. 1.1.0) and it seems that the behavior of the sub-menus is co ...

Solving Empty Array Element Issues (Using MongoDB, React, and Axios)

I'm currently developing an application that utilizes Axios to make a GET request to a server I've created. Below is the relevant snippet of the server code that might shed light on the issue: app.get("/getScheduledTimes/:day/:stationId", (req, ...