When the line-height is adjusted, nearby buttons and the text underneath them will shift downwards after a button is clicked

I am facing a challenge with my .button class. When the button is in its active state, I have added an inset box shadow and increased the line-height by 2 to create a button press effect. However, the adjacent button and text below it also move down, which is not desired. Can you suggest a way to achieve this effect without causing any other elements to move?

Note: My goal is for only the text inside the button to move down by 2px when pressed, which is why I opted to adjust the line-height.

.button{
  display: inline-block;
  height: 36px;
  padding: 0 18px;
  background: cyan;
  color: black;
  border: none;
  line-height: 36px;
  margin: 6px;
}

.button:active, .button:focus{
  outline: none;
}

.button:active{
  box-shadow: 0 2px 0 0 blue inset; 
  line-height: 38px;
}
<button class="button">Hello!</button>
<button class="button">Bye!</button>
<div>Hello!</div>

Answer №1

To resolve the alignment issue when a button is focused, simply include vertical-align: top; in your CSS for the .button class.

The default value for the vertical-align property is base-line. When a button is focused and its line-height changes, it can cause misalignment of the .button elements, pushing down the content below.

.button{
  display: inline-block;
  vertical-align: top;
  height: 36px;
  padding: 0 18px;
  background: cyan;
  color: black;
  border: none;
  line-height: 36px;
  margin: 6px;
}

.button:active, .button:focus{
  outline: none;
}

.button:active{
  box-shadow: 0 2px 0 0 blue inset; 
  line-height: 38px;
}
<button class="button">Hello!</button>
<button class="button">Bye!</button>
<div>Hello!</div>

Answer №2

To maintain the layout consistency, my recommendation is to set the line height to 38px and apply a transparent box shadow (with the same dimensions) when the button is inactive. By doing this, no element will shift in its active state.

Answer №3

One neat trick to achieve this effect is by using a transformation:

.button{
  display: inline-block;
  height: 36px;
  padding: 0 18px;
  background: cyan;
  color: black;
  border: none;
  line-height: 36px;
  margin: 6px;
}

.button:active, .button:focus{
  outline: none;
}

.button:active{
  box-shadow: 0 2px 0 0 blue inset; 
  transform:translateY(2px);
}
<button class="button">Hello!</button>
<button class="button">Bye!</button>
<div>Hello!</div>

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

Spacious width that doesn't require a horizontal scrollbar for navigation

My dilemma is with the width of my body and background image. I set the body width to 1920px to match the background-image for the header. However, since I have a narrower wrapper inside the body, the header background's width gets cut off by the wrap ...

Is there a way to create a navigation menu that highlights as we scroll down the page?

Check out this example of what I am looking for. https://i.stack.imgur.com/fdzvZ.png If you scroll, you will notice that the left menu highlights. Sub-menus will extend when the corresponding menu is highlighted and collapse when other menus are highlig ...

The issue of exceeding margins

I am facing an issue with some CSS. Below is my HTML code: <body> <div id="cn"> <div id="hd"> <ul> <some li's> </ul> </div><!-- /#hd --> <div id="bd"> ...

Scrolling horizontally in a container using the mouse wheel

Is there a way to enable horizontal scrolling in a div using the mouse wheel or drag functionality with jQuery? I attempted using draggable, but it did not work effectively in my specific code scenario. Currently, I have a horizontal scrollbar. Are there ...

PHP HTML failing to recognize "mandatory" attributes

I'm facing an issue with adding validation for an empty field. When the field is left empty, the form should not be submitted. However, the "required" attributes seem to be ineffective in achieving this. Birthdate: <select name="month" r ...

What is the best way to send information to a different webpage?

I am in search of a solution to a rather simple query that has me puzzled. Should this question already exist elsewhere, I humbly request that you guide me to the answer. My apologies in advance if this is a duplicate. I currently have two URLs: http://12 ...

Consolidate all CSS links into a single line

Within my HTML document, I currently have the following CSS links: myPage.html <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet"> <link href="https://fonts.googleapis.com/icon?family ...

I'm attempting to utilize a basic webcam capture upload feature, but it seems that the upload function is not functioning properly

UPDATE: This is the complete code that I simply copied and pasted. <!DOCTYPE HTML> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="JavaScript" type="text/javascrip ...

Menu Drop on top of Flash player

A challenge I am currently facing is with my live event site and the list of events displayed in a mouseover menu. The issue arises when the flash streaming player remains in front of the mouseover menu, causing interference across all versions of Interne ...

Creating a hierarchical structure in HTML to represent a JSON object as a tree: techniques and best practices

I need help with displaying a nested JSON object that shows a one to many relationship between a parent node and its children in an organizational chart format using HTML. I have looked into utilizing Google Charts for this purpose, but I am unsure if it a ...

Establish a specific character limit for input text

Is there a method to restrict the input length of a textbox to exactly 9 characters? I am looking for a solution and any assistance would be greatly valued. Thank you in advance! ...

unable to utilize the If - Else statement for checking element existence in Javascript

After writing a JavaScript function to create a table in both HTML and JS, I encountered an issue. When I click submit, the table is created, but if I click submit again, it creates another table. I attempted to use if-else statements in the function, but ...

Order of random div classes

After receiving advice from several individuals, I have revised my question by removing unnecessary code and keeping only the essential parts. I hope this simplification is clear enough for my question to be reopened. Here is the fiddle link: http://jsfid ...

Identify Pressed Shift Key Direction - Leveraging JQuery

I am currently working on a web application that requires users to enter capital letters. However, I want to restrict them from using the right shift key for certain keys like a, s, d, f and the left shift key for h, j, k, l in order to detect whether they ...

A guide on utilizing buttons within ion list items to execute actions independently for both the button and the list item

Currently, I have a list item with a button that is displayed based on a certain condition. My issue is that when I click the button, a popup should appear, but instead, it also navigates to the next page in the background. Can someone help me figure out h ...

What could be causing the unordered list in my CSS to not have proper spacing with padding?

Struggling with spacing out links on my class website. Here's the code I have: body { background-color: #eee; } nav { width: 100%; height: 100px; background-color: #fff; } ul { padding: 0; margin: 0; } ul li { list-style: none; d ...

Using jQuery does not automatically pre-check multiple checkboxes. This issue may arise when using PHP and AJAX together

I'm attempting to dynamically check multiple checkboxes based on previous entries from a MySQL database. I have the data stored in a variable: var = ObjektLevHur; The potential data values are: frittlev, frittfabrik, or mont. When I make my first sele ...

Presenting a data table in Angular

I'm new to using Angular and need help creating a table to organize my data, which is being fetched from a JSON file on the server. Here's the content of data.component.html: <div class="container"> <h1>Search history</h1> ...

Troubleshooting: Unable to submit data from Boostrap Form to database

I'm facing an issue with my bootstrap form inside a modal where I am unable to submit the data to my database. Below is the code for my modal form (HTML): <div class="modal-body"> <form method="post" action="insertRecord.php"> ...

The custom error message for invalid input remains unchanged even after the box is selected

Currently working on implementing an input (checkbox) that displays a custom message when it is considered invalid (unchecked). However, even after the user checks the box, the message remains displayed and the box is still considered invalid. I suspect th ...