The button's height exceeds the height of the nested content

Currently, I am utilizing bootstrap 4 alpha version.

<button class="btn btn-link p-0">
  <div style="display:inline-block; background-color:#c2f5ff; width: 100px; height: 100px;">
  </div>
 </button>

Check out the Fiddle here

In my code, I have intricately nested a div within a button element. The dimensions of the div are set with specific values for both its height and width. The width of the button perfectly accommodates the div, however, the height of the button seems to be unnecessarily larger than expected. Additionally, upon clicking the button, it reveals that the blue outline surrounding the button does not align properly with the content inside.

I'm puzzled by this unexpected behavior - why is this happening?

Answer №1

The reason for this is because of the nature of inline-block. The inner div of the button is set to inline-block, which has a default value of vertical-align: baseline causing an extra gap. If you change the vertical-align value of your div to something other than baseline (such as top, middle, or bottom), the button will have the expected size of 102x102 (content width + 1px borders).


This explanation about vertical-align: baseline can be found in this answer:

Since browsers typically set the vertical-align property to baseline by default, this behavior is normal. Refer to the image below to see where the baseline falls on text:

Elements aligned to the baseline must reserve space for characters with descenders that extend below the baseline (like the letters j, p, or g), as shown in the image above.


To achieve the desired outcome, simply remove the display: inline-block from your inner div.

Answer №2

To remove the white padding inside the button, you can simply eliminate the border: none property on the button and modify the display type of the div inside from display: inline-block to display: block.

I trust this solution will be beneficial for you!

Answer №3

To put it simply, the button is not exactly 100px wide or high.

The key is to first determine the size of the button, and then ensure that the inner div fills the button completely.

body {
    margin: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>

<button class="btn btn-link p-0" style="border-style:none;width:100px;height:100px;">
  <div style="background-color:#c2f5ff; width: 100%; height: 100%;">
  </div>
 </button>

This can be achieved by setting width:100px and height:100px on the button itself, and ensuring that the inner div expands to fit using width:100% and height:100%.

Additionally, there is a thin 1px-wide border around the button that can be removed with border-style:none

Answer №4

It may seem unconventional to have a div within a button, but here is how I approached it:

I defined the width and height of the button itself, then set the width and height of the nested div to 100%. This way, the nested div matches the size of the button.

You can view the code on this jsfiddle link.

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

Is it possible to assign different colors to the two buttons of a range slider in Shiny?

In Shiny, the ion-rangeslider is utilized from this source. By tweaking the code, I managed to change the color and some properties of the slider. The snippet below creates a standard slider with a green button and a range slider with red buttons. Now, my ...

The alignment of buttons is not defined for ASP buttons and Bootstrap dropdowns

I'm having an issue with the placement of my ASP button and button dropdown list. The dropdown always appears below the other buttons, but I want it to be in the same row as the other buttons. Below is a screenshot showing the current layout of the b ...

What is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...

I am interested in modifying the destination of the screen transition depending on the numerical input provided in the form

I have recently started working on a project using Spring Boot, Thymeleaf, and MySQL. For instance, when the input form is submitted with the value "1" as the DB ID, I expect to be redirected to http://localhost:8080/edit/1. However, I am facing an issue ...

Ensure the images are resized to perfectly fit within their parent container while still preserving their original aspect

I have a div structure below where the height of #myMenu is dynamic. How can I ensure that the images maintain their aspect ratio when the height of #myMenu changes? Here is the intended outcome: <div id="myMenu"> <div class="col col20"> ...

How can I fix the issue with Parallax scrolling not functioning properly? (Using Next.js Image + Tailwind)

What am I missing to get parallax scrolling to work? <div className="h-screen"> <div className="relative h-full w-full bg-cover bg-fixed bg-center bg-no-repeat"> <Image src="https://images.unsplash.com/photo-1454496522488-7a8e488e86 ...

Scroll automatically when dragging the mouse

My D3 tree creation is causing an issue with the horizontal scroll bar in Firefox 37. When trying to drag select a tree node, the horizontal scroll bar does not work. However, in Chrome, the horizontal scroll works fine during drag selection. <div cl ...

The two CSS classes are styled with different border-color values, but only one is being applied correctly

My goal is to modify the border color of a textarea using jQuery. Previously, I achieved this by using .css("border-color","rgb(250,0,0)"), and it was working perfectly. However, I have now been advised against using CSS directly in JavaScript and told to ...

Retrieve jQuery CSS styles from a JSON database

I've been attempting to pass CSS attributes into a jQuery method using data stored in a JSON database. However, it doesn't seem to be functioning as expected. I suspect that directly inputting the path to the JSON variable may not be the correct ...

Attempting to choose everything with the exception of a singular element using the not() function in Jquery

Within the mosaic (div #mosaique) are various div elements. My goal is to make it so that when I click on a specific div, like #langages or #libraries, the other divs become opaque while the selected div undergoes a size change (which works correctly in t ...

Guidance on Configuring Django Static Files

For setting up my Django static files, I added the following code to settings.py: STATIC_URL = '/static/' STATIC_DIRS = [ os.path.join(BASE_DIR, 'static') ] STATIC_ROOT = os.path.join(BASE_DIR, 'assets') To implement this ...

How can one utilize JSON.parse directly within an HTML file in a Typescript/Angular environment, or alternatively, how to access JSON fields

Unable to find the answer I was looking for, I have decided to pose this question. In order to prevent duplicates in a map, I had to stringify the map key. However, I now need to extract and style the key's fields in an HTML file. Is there a solution ...

Ways to adjust the size of v-btn

Objective: My current task involves enhancing the customization options for Vue components, specifically focusing on adjusting the size of a button component based on user input. Our project utilizes Vuetify. Previous Code: <v-btn @click.prevent=&quo ...

"Utilizing Primeng's P-Table Component for Enhanced Interactivity

https://i.sstatic.net/UQZYV.png Greetings to all! I am currently working with p-table, p-headercheckbox, and p-tableCheckbox along with additional filters for each column. However, I am encountering an issue where the filters are not aligning properly wit ...

Steps for resetting a div's display after adjusting the device size

I have a code that displays horizontally on desktop screens and vertically on phones. It includes an x button (closebtn) that is only displayed on phones to close the menu bar. How can I automatically display it again after resizing the page back to deskto ...

"Maximizing the functionality of HTML forms by incorporating PHP for efficient data retrieval

I have been exploring different approaches but I'm struggling to get this to function correctly. As a beginner, I am using form elements with dropdown menus to receive user input and then send the data via email. Here is the HTML and PHP code: <fo ...

Is it possible to use an onclick function to input JavaScript values into a password entry box seamlessly?

Is there a way to input password values continuously using a JavaScript onclick function into a password field? I have two images, one 'Blue' and one 'Red', that trigger an onclick function with the following values: Blue= w3! Red= T4 ...

unable to activate toggleclass('d-block') for multiple radio inputs

We have multiple radio input options. function showReferral() { document.querySelector('#referral_code').classList.remove('d-none'); } <div class="form-group row d-none" id="referral_code"> <label for="referral_code"&g ...

Mastering JavaScript: Techniques for Selecting Multiple Values in a Dropdown Menu

I have a code that works perfectly, but the issue arises when I click on the add button and a new select option is added with a value instead of an id. How can I pass the Id to option.value = array[i]; ? var array = ["PACKING & LOADING","BAG GODOWN", ...

Ways to incorporate additional CSS after an image has been uploaded

I'm having an issue with the text box not completely staying inside the gray area when I upload an image. Is there a way to automatically expand the gray area where the text box is located when an image is uploaded? Thank you! Check out the code on J ...