What is the best way to ensure that a div and button are equal in size

Can someone help me understand why the div sizes are not consistent? :/

The size should be uniform across components, based on text size.

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

Check out the Codesandbox here.

import * as React from "react";
import styled from "styled-components";

export default function App() {
  return (
    <div style={{ display: "flex" }}>
      <Select>Aa</Select>
      <Button>Aa</Button>
      <Input />
    </div>
  );
}

const Button = styled.button`
  box-sizing: content-box;
  min-height: 1.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font: inherit;
  box-shadow: inset 0 0 0 1px blue;
  margin: 5px;
  padding: 5px 10px;
  border: 0;
  border-radius: 3px;
  transition: all 0.2s ease-out;
  user-select: none;
  outline: none;
`;
const Input = styled.input`
  box-sizing: content-box;
  min-height: 1.4rem;
  margin: 5px;
  vertical-align: middle;
  padding: 5px 0;
`;
const Select = styled.div`
  box-sizing: content-box;
  display: flex;
  align-items: center;
  box-shadow: inset 0 0 0 1px blue;
  padding: 5px 10px;
  border-radius: 5px;
`;

Answer №1

To start, ensure that each element has the same height and reset the margins or assign them all the same margin. Additionally, it is important to include box-sizing: border-box; in your CSS.

button {
  box-sizing: content-box;
  min-height: 1.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font: inherit;
  box-shadow: inset 0 0 0 1px blue;
  margin: 5px;
  padding: 5px 10px;
  border: 0;
  border-radius: 3px;
  transition: all 0.2s ease-out;
  user-select: none;
  outline: none;
}

input {
  box-sizing: content-box;
  min-height: 1.4rem;
  margin: 5px;
  vertical-align: middle;
  padding: 5px 0;
}

div {
  box-sizing: content-box;
  display: flex;
  align-items: center;
  box-shadow: inset 0 0 0 1px blue;
  padding: 5px 10px;
  border-radius: 5px;
}

div,
button,
input {
  box-sizing: border-box;
  margin: 0;
}
<span style="display:flex">
  <input/>
  <div>Div</div>
  <button>Button</button>
 </span>

Answer №2

Move the button into the Div and adjust the height to 100%.

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

Mobile Menu in wordpress stays visible after being clicked

I recently created a one-page layout that includes some links. However, when I view the site on my smartphone and open the main menu using the button to click on a link (which scrolls within the same page), I noticed that the mobile menu remains visible a ...

I have noticed that the share buttons on Sharethis.com only show up after I resize the viewport

After integrating share buttons from Sharethis.com, I noticed that they do not appear when the page is refreshed. However, when I resize the viewport, they suddenly show up. <!DOCTYPE html> <html lang="en"> <head> <script t ...

Tips for showing background image in case of incorrect URL pattern

Mapping in web.xml <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> Error handling with ErrorHandler servlet RequestDispatcher vie ...

Transform inline styles in React to CSS declarations

Is there a tool available that can convert React inline style descriptions into CSS rules? For example: { minHeight: 200, position: 'relative', flexGrow: 1, flexShrink: 1, display: 'flex', flexDirection: ' ...

Heroku not optimizing stylesheets in Create React App (CRA) hosted with Node

After building my files locally, I noticed that my css folder and its contents are minimized under /build/static along with a js folder and media folder beside it. However, once I push the changes to Heroku and access my app, the css folder seems to be mi ...

Issue with AnimeJS Motion Path causing element to deviate from desired SVG path

I'm attempting to use an SVG element and the AnimeJS library to make the orange marker follow the course of this RC car race track. https://i.stack.imgur.com/8FKHC.png Despite my efforts, I am encountering strange and undesirable outcomes. At times ...

Use the :target selector to target HTML elements with various unique IDs

Is there a way to use the same CSS selector #Overlay:target that currently hides the overlay to show content instead, or is there a more effective method? Here's what I'm dealing with: I've implemented an overlay that displays a dismissible ...

Use the onClick attribute with Iframe

Is there a method to wrap an Iframe within another element to create a simulated onClick event? I am aware that Iframes do not support events, however, I require the ability to monitor clicks from my website that are being redirected through the Iframe. A ...

Tips for validating forms and displaying error messages in JQuery Mobile

Hey there, I'm new to JQM and currently working on a JQM login page. Could someone assist me with implementing form validation to display errors below the username and password text boxes if they are left empty? Additionally, I need guidance on showi ...

Using the previous page button will cause the current page button to have various states

Whenever I use the previous page browser button, the current page's button state does not reset. This causes multiple menu items to appear as if they are in their 'current' state when the previous page is revisited. If I hover over the &apos ...

Incorporate dynamic optgroup elements into an HTML dropdown menu

I created a modal form that contains two select boxes with the identical class name of current_models. <div class="row"> <div class="col-md-6> <div class="form-group"> <label class="control-label">Model</ ...

Preventing responsive elements from loading with HTML scripts

Currently, I am utilizing the Gumby framework which can be found here. Everything appears to be running smoothly. My goal is to incorporate a mobile navigation list where the links are grouped under a single button, as outlined here. Initially, this funct ...

Display a DIV element containing a button using Jquery

Can anyone guide me on how to use Jquery to load a specific DIV onto the page only when called by a button or event? I've attempted several functions without success, making me question if it's achievable at all. Being new to Jquery, I believe my ...

What is the best way to manage uncommon characters using PHP?

I have implemented a feature that allows users to input text into a textarea, save it (the data is stored in a basic .txt file with HTML content), retrieve the text file using file_get_contents(), and then display it. Everything seems to be working smoot ...

background gradient coloring and image blending

I have created a special class that includes a gradient background color: .banner { background: -moz-linear-gradient(center top , #75A319 0%, #9FCC1D 100%) repeat scroll 0 0 #9FCC1D; } Additionally, I have defined another class that adds a background ...

Tips for calculating the distance from the cursor position to the visible area

Is there a way to determine the cursor's offset from the top of a textarea's view rather than its position? While e.target.selectionStart provides the cursor position, $el.scrollTop gives the scroll offset of the textarea. Any suggestions on ho ...

Tips for increasing the size of a textarea

I'm attempting to extend a textarea by adjusting the margin-top property, but it doesn't seem to be working as intended. Here is the code snippet in question: #sqlcontainerLoggedInPage2 { margin-top: 60px; } <div class="container-fluid" i ...

Update the Kendo window scrolling feature to include fixed update and cancel buttons

I've encountered an issue while using ASP MVC and the latest version of Kendo. When I add too much content to a Kendo window, it starts scrolling vertically, which ends up hiding the cancel/update buttons at the bottom. This is not ideal as the user s ...

Displaying Fullcalendar data exclusively for the currently logged-in user

In my project, I am utilizing Fullcalendar and Spring framework. The event data from my SQL database is structured into columns: id, doctor_id, patient_id, start, and end. Currently, all events are being displayed regardless of the user logged in, but I w ...

Guide on navigating through various HTML pages with distinct parameters using Node.js (Express server)

Seeking assistance with a Node.js server that receives an ID as a query parameter. Each time a client connects with different parameters, I aim to serve them a unique HTML page containing a simple UI with 2 dynamic arrays. Everything seems to be working co ...