How can I use TailwindCSS in NextJS to remove the "gap" className from a component?

Currently, I am working on button components in NextJS with Tailwindcss and I am encountering a problem with some variants. Everything works fine, except when I remove the children (button text), I notice something strange that I can't quite figure out. When a button has both text and an icon, it looks good. But when there's just a single icon, there seems to be some extra space on the left. How can I get rid of this space?

I have tried removing the gap and adding padding to the text, but the empty space still remains.

Here is my screenshot for reference

My code:

import { ElevatedButton } from "../components";
import { MdDataSaverOff } from "react-icons/md";

export default function Home() {
  return (
    <>
       <ElevatedButton
        iconRight={<MdDataSaverOff size={18} />}
        type={"tonal"}
      ></ElevatedButton>
    </>
 );
}

Component:

import { Icon } from "../../../elements/Icon";

function ElevatedButton(props) {
  return (
    <>
      <button className={`btn ${props.type}`} onClick={props.onClick}>
        <Icon iconLeft={props.iconLeft} />
        <p>{props.children}</p>
        <Icon iconRight={props.iconRight} />
      </button>
    </>
  );
}

export { ElevatedButton };

Element:

function Icon(props) {
  return (
    <>
      {props.iconLeft}
      {props.iconRight}
    </>
  );
}

export { Icon };

Tailwind settings:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  /*Common Buttons*/
  .btn {
    @apply mt-[4px] flex h-[40px] items-center gap-[8px] rounded-full py-[10px] px-[24px];
  }

  .elevated {
    @apply bg-blue-500;
  }

  .tonal {
    @apply bg-blue-100;
  }
}

Screenshot 2

Screenshot 3

Answer №1

It has come to my attention that we are deleting text from within the div element, but not eliminating the div itself. As a result, when applying styling for spacing, extra space is created between the elements. To address this issue, it is recommended to completely remove the div wrapper.

import { Icon } from "../../../elements/Icon";

function ElevatedButton(props) {
  return (
    <>
      <button className={`btn ${props.type}`} onClick={props.onClick}>
        <Icon iconLeft={props.iconLeft} />
        {props.children}
        <Icon iconRight={props.iconRight} />
      </button>
    </>
  );
}

export { ElevatedButton };

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

Issue encountered while working with PostgreSQL and Sequelize when using the array_append function

I'm encountering issues with the following code snippet let user = req.user.id; await MyTable.update( {'interested': Sequelize.fn('array_append', Sequelize.col('interested'), user)}, {'where ...

Incorporate some flair into the Twitter iframe with a Style tag

I have been attempting to add a style tag into the head of an embedded Twitter timeline in order to customize some of the styling. I wrote the CSS and added it within a style tag. My initial idea was to inject this into the iframe using jQuery, but unfortu ...

Divs gracefully appear one after the other in a sequential manner

I am attempting to showcase a sequence of 4 divs in a row using the ease-in CSS transition feature. I came across this example that demonstrates what I am aiming for: http://jsfiddle.net/57uGQ/enter code here. However, despite my best efforts, I am unable ...

Transforming JSON into array format with key-value pairs using JavaScript

Currently, I am working on a web application that is receiving data in a specific format from a node server: "{""elements":[{"10sr2b2":{"total":0,"bad":22,"clients":["fc8e7f","fc8e7e"],"zone":"101900"}}]}" The issue lies in the fact that this data is str ...

What is the process for modifying a task on my to-do list with a long press?

I'm currently working on implementing a task update feature in my project. However, I've encountered an issue where the prompt only works in the browser environment. Is there a way to make this work in React Native or are there any alternative so ...

Ensuring InputAdornment does not impact the width of TextField in Material UI v5

Is there a way to prevent the TextField width from shrinking when adding an InputAdornment? Image: Here's my current code: <TextField label="Username" {...formik.getFieldProps("username")} InputProps={{ end ...

What steps should I follow to enable my bot to generate or duplicate a new voice channel?

I needed my bot to either create a new voice server or clone an existing one. The variable "voic" contains the ID of the voice channel. voic.voiceChannel.clone(undefined, true, false, 'Needed a clone') // example from ...

Encountered an issue during npm install resulting in an ERR! with the code EINTEGRITY

npm ERR! code EINTEGRITY npm ERR! sha512-LMIgiIrhigmPrs03JHpxUh2yISK3vLFPkAodPeo0+BuF7wA2FoQbkEg1u8gBYBThncu7e1odfIXVuTqLRUjw== integrity verification error occurred while using sha512: expected sha512-LMIgiIrhigmPrs03JHpxUh2yISK3vLFPkAodPeo0+BuF7wA2FoQbkE ...

You cannot reassign NodeJS global variables

Currently, I am in the process of writing unit tests for code that utilizes a JavaScript library. This particular library sets a global variable if it does not already exist using the following pattern: var GLOBAL_VAR = GLOBAL_VAR || {} While this method ...

Creating a custom React component with a unique color scheme for added personality

I have successfully created a custom component named InfoCard and passed the necessary information through props. The component is functioning properly, but I am facing an issue where I would like to define different colors for the Icon prop element when i ...

Invoking a function sharing the same name as a local variable

The code snippet below is causing me some trouble... var firstPlay = 1; if (firstPlay == 1) { firstPlay(); } If I remove the if statement and simply have: firstPlay(); It works fine, but for some reason it doesn't work with the if statement. ...

Adding a list without a specific order into a paragraph with the help of jQuery

Working on a client-side class, I am in the process of changing an XHR request and getElementById function to a jQuery ajax request along with jQuery document manipulation. The task at hand is to display descriptions of items available from "Rob's Roc ...

"Trying to activate a hover effect on a div using JQuery, but it's not functioning

Is there a way to make these divs animate each time I hover over them? Currently, the animation only works once and does not repeat when hovering again. Can anyone provide guidance on how to achieve this? function Rotate() { $('#gear1,#gear2,#gea ...

How can you access the preloaded resolve value in AngularJS ui-router when the $stateChangeSuccess event is triggered?

$stateProvider.state('home', { url: '/', resolve: { person: function() { return 'good' } } Can you help me figure out how to access the value of 'person' in the $stateChangeSuccess callback f ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

Exploring the magic of the (!!!) operator in JavaScript!

The !! operator proves to be quite helpful when converting non-boolean data types into Boolean values, mainly for "True" conditions. However, when it comes to false conditions, is using !!! necessary? ...

Executing a Sequence of SQL Queries in Node.js

I am facing the challenge of performing nested queries to retrieve values from the database in order to generate a chart. The current approach involves executing a total of 12 queries, each aggregating the number of customers for every month of the year. ...

What is the best way to transfer a user-generated PNG file to my backend server?

I'm in the process of developing a website that enables users to generate personalized graphics and easily download them directly from the platform. My approach involves allowing users to customize an svg using a javascript-powered form, which is the ...

Implementing the useState Hook with Material UI Toggle Button Group in React.js

In the App.js file, I am importing Box from "@mui/system", Header from "./components/Header", ProjectList from "./components/ProjectList", CardLayout from "./components/CardLayout", and React, useState from "react". The goal here is to render the ProjectLi ...

Positioning a fixed div within a responsive div using AngularJS and Bootstrap

I'm currently working on a page layout that consists of a fixed sidebar on the left side and a main container taking up the rest of the page on the right. Inside this right-side container, which is a div, there are 2 elements. <div class="col-sm-1 ...