9

I'm trying to build a web app but I've encountered a problem.

Most of the app's interfaces consist of lots of buttons and I've encountered a queer phenomenon whereby I can't click some of the buttons (there's no clicking animation and the onclick() doesn't run).

I've noticed that where there are multiple buttons stacked on top of each other, only the last line of buttons is clickable.

I have created a minimal, complete and verifiable example.

<html> <body> <div style="padding-top: 20%;" id="div1"> <button> Button 1 </button><br /> <button> Button 2 </button> </div> <script> document.getElementById("div1").style.display = "initial"; </script> </body> </html>

Further observations/notes:

When you go to select an element from the developer menu, you can't select the malfunctioning buttons.

I'm using Firefox. I get a similar issue with my app in Chrome but the buttons which misbehave are different to the ones that break in Firefox.

I'm trying to create a single-page web app by hiding and showing various <div> elements (different screens) by setting display: none; and display: initial; respectively. That's the reason for the <script>. Is there a better way I should be doing this?

14
  • 2
    It's because of the initial style being added. Is there a reason for wanting that? Commented Jul 4, 2019 at 13:29
  • yes, @ComputerLocus is right. This behavior is because of the style. Commented Jul 4, 2019 at 13:32
  • 5
    It is because of display inline (initial will default to inline for the display property) in combination with the top padding. Commented Jul 4, 2019 at 13:32
  • 2
    @KoshVery initial set display to inline and not to default browser Commented Jul 4, 2019 at 13:54
  • 1
    @KoshVery It does not set to the browser/element default value but the specification default value which is inline for the display property. Commented Jul 4, 2019 at 13:55

2 Answers 2

8

display: initial; is equivalent to display: inline; in your case.

The padding-top is then applied to each line of content, making the second line cover the first line. It is easier to see if you add a background to the containing div.

#div1 {	padding-top: 15px;	display: initial;	background: #f00; }
<div id="div1">	<button>	Button 1	</button>	<br>	<button>	Button 2	</button> </div>

Sign up to request clarification or add additional context in comments.

5 Comments

it's no really related to box decoration but to the fact that the height of linebox is only defined by line-height and everything else (padding,border,margin, etc) will simply overflow
I thought the padding was applied to the whole <div> element. How do I make it work that way?
@TemaniAfif Yes, my bad, I will remove that part of the answer.
@IssaChanzi any display other than inline (initial)
@IssaChanzi there are a few ways, the simplest being to use display: inline-block; instead.
-4

<html> <body> <div style="padding-top: 20%;"> <button> Button 1 </button><br /> <button> Button 2 </button> </div> </body> </html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.