When padding is added, the size of the containing div will still be affected even with the box-sizing property set to

If you click the Button in the following example, it will add padding to a div called label. If you click it 12 times or more, the size of the containing div named button will start to change size and turn red.

I have already applied box-sizing: border-box; and overflow: hidden. I need to:

  • Maintain the fixed size of the div named button
  • If label cannot fit due to padding, the div named label should not be visible outside the container

Can anyone help me fix my script? Thank you in advance! :)

let btn = document.getElementById('btn');
let label = document.getElementById('label');
let button = document.getElementById('button');
let padding = 5;
btn.addEventListener('click', event => {
      padding += 5;
      button.style.padding = `${padding}px`;
});
* {
  box-sizing: border-box;
}

#button {
  width: 200px;
  height: 100px;
  background-color: red;
  overflow: hidden;
}

#label {
  font-size: 50px;
}
<button id="btn">Click me to add more padding</button>
<div id="button">
  <div id="label">
    Click me!
  </div>
</div>

Answer №1

Place it in a designated receptacle

<button id="btn">Press here to increase padding</button>
<div class="button-container">
    <div id="button">
        <div id="label">
            Press me!
        </div>
    </div>
</div>

CSS

* {
  box-sizing: border-box;
}  

.button-container {
  width: 200px;
  height: 100px;
  overflow: hidden;
}    

#button {
  width: 100%;
  height: 100%;
  background-color: red;
}  

#label {
  font-size: 50px;
}

Answer №2

The reason for this is that when the padding of the DIV is doubled, it ends up exceeding the original height of the element. This causes the large padding to not be able to fit within the original height, which is typically addressed by using box-sizing: border-box;.

Answer №3

I have wrapped a div around the button with a hidden overflow which has resolved the issue ;)

HTML :

<button id="btn">Click me to add more padding</button>
<div class="over">
  <div id="button">
    <div id="label">
      Click me!
    </div>
  </div>
</div>

CSS :

.over { overflow: hidden; height: 100px; width: 200px; } 

Check out this fiddle

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's the best way to group rows in an angular mat-table?

I am working on a detailed mat-table with expanded rows and trying to group the rows based on Execution Date. While looking at this Stackblitz example where the data is grouped alphabetically, I am struggling to understand where to place the group header c ...

What is the typical output value that fluctuates?

I only have one input to work with. My goal is to set the input value to "5", then display "3" in the "total" field and "4" in the "total2" field. Additionally, for every increment of +1 to the input value, I want the "total" and "total2" fields to also in ...

Choosing the subsequent div after the current one using Jquery

I am currently in the process of creating a drop-down menu button that, when clicked, reveals the previously hidden div underneath it. The code is functioning properly without the use of $(this).next, but it is displaying all divs with the class .menudrop ...

Simple steps to change the appearance of the delete button from an ajax button to an html button

I need help transitioning the delete button from an ajax button to an html button in my code. Currently, the delete button functions using ajax/javascript and when clicked, a modal window pops up asking for confirmation before deleting the vote. However, ...

Creating a dynamic menu layout using CSS

I've experimented with various methods to create a fluid menu, but none have been successful. Currently, I have an interactive menu that sometimes displays 6 items and other times 7 items. When I have 7 items, the menu aligns perfectly across the wid ...

Empty Firebase currentUser Redirected After Successful Sign In

I have a webpage named index.html which serves as the sign-in page. After a user successfully signs in, I want to redirect them to a new page called home.html, where their email will be displayed. However, I am encountering an issue with getting a null va ...

Time to reverse the string

I currently have a timestamp formatted like this: Thu Sep 12 04:28:37 +0000 2013 While I am aware that PHP's strtotime function allows for inputting things like '2 days ago' or 'last week', my goal is to take the existing timesta ...

Can the tab button be used to move to the next field?

Is it possible to navigate to the next field by pressing the tab button? If so, how can we achieve this? Currently, I am utilizing Bootstrap classes col-md-6. Thank you! <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4. ...

Using jQuery to show and hide elements on a webpage

One issue I'm facing is changing the content on a page depending on the clicked link. The problem arises when the displayed content for one link persists even after clicking another link, despite setting it to not display when another link is clicked. ...

Updating variable values in AngularJS while navigating through routes

How can I dynamically set the flag icon inside the page header based on the selected language using AngularJS? The language selection is done in a separate .htm file and all managed by AngularJS routing. My application has a single controller called "appCo ...

"The combination of Windows, @font-face, and Cyrillic characters presents a

I am encountering an issue with font-face and Cyrillic characters. The fonts display properly in any browser on OS X, but fail to render on a Windows 7 machine (chrome, ie etc). Even after putting the fonts through Font Squirrel and testing the demo files ...

What are the steps to connecting incoming data to an Angular view utilizing a reactive form?

Hello, I am currently fetching data from an API and have successfully displayed the teacher values. However, I am unsure of how to utilize the incoming array values for "COURSES" in my Angular view. This is the response from the REST API: { "courses ...

Making Angular table cells interactive with clicks

My table consists of multiple rows, and I want to change the color of a cell to red when clicked. However, only one cell should be red at a time. Whenever I click on a new cell, the previously red cell should return to its original color. How can I achieve ...

Can JSS support creating variables similar to Sass?

Currently, I am developing a project in React and utilizing Material-ui with JSS for styling. In Sass, we have the ability to define variables like $color: rgb(255, 255, 255) for later use. I am curious if it is possible to implement variable usage simi ...

Creating a Javascript countdown timer that does not involve displaying the

I stumbled upon this code on a website, but there's one tweak I'd like to make. Unfortunately, I can't seem to figure it out myself, so I'm reaching out for some help. What I want to achieve is removing the year from the date so that th ...

horizontal alignment of row content to the right in HTML

Trying to align a text and a small image horizontally to the right: <div class="row float-right"> <div class="title" >Stream Chat</div> <img src="~assets/chat.svg" /> </div> ...

Is there a way to incorporate a dropdown feature into a search bar using Ant Design?

I'm currently working on a project that requires me to incorporate two drop-down menus inside the search box. Despite following the guidelines provided in the documentation (https://ant.design/components/input), I encountered a problem when trying to ...

Replacing current CSS styles within a CMS template

Working on customizing the Whitehouse WordPress theme has been quite a challenge lately. It seems that some predefined style rules within the theme are interfering with the tags I'm implementing, causing some frustrating issues to deal with. Specific ...

Center the navigation links within the navigation block

I am using the startbootstrap-small-business template and customizing it to fit my requirements. I have inserted a new logo which caused me to adjust the height of the navbar to approximately 120px. Now, my goal is to vertically align the links on the righ ...

Enhance your content with stylish text additions

Can anyone help me with merging 2 pieces of text in React JS, where one part of the text has unique styling? Below is an example of the code: <div> <div> {'If you haven\'t received an email, please check your spam filte ...