What causes the hidden buttons to shift the focus when tabbing on the keyboard, resulting in the div above and the box title being pushed upwards in the HTML?

As you hover over the box, two buttons will appear. However, when tabbing for accessibility, the buttons appear without pushing up the content inside the div.

Tabbing to a link within the description div causes the img-block class div to be pushed up. Why is this happening and how can it be fixed?

#square .square-wrapper-inner {
  height: 50%; /* Changed from 100%*/
  width: 100%;
  position: absolute;
  z-index: 9999;
  top: 250px; /* Changed from 600 */
  padding: 15px 20px;
  -webkit-transition: 0.1s;
  -moz-transition: 0.1s;
  -ms-transition: 0.1s;
  -o-transition: 0.1s;
  transition: 0.1s;
  background: rgba(255, 0, 0, 0.8) !important;
  box-sizing: border-box;
}

#square {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
}
#square h2 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -40%);
  width: 100%;
  text-align: center !important;
  font-family: "Merriweather", serif !important;
  font-size: 20px !important;
  letter-spacing: 0.8px;
  line-height: 24px !important;
  color: #fff !important;
  text-shadow: 0 0 7px rgb(0, 0, 0);
  font-weight: 700 !important;
}

#square .description a {
  display: block;
  padding: 10px 20px;
  background-color: #000 !important;
  color: #fff !important;
  text-decoration: none;
  text-align: center !important;
  font-family: "Karla", sans-serif !important;
  font-size: 14px !important;
  margin-bottom: 10px;
}

#square .description a:hover,
#square .description a:focus {
  color: #fff;
}

#square .description a:first-child {
  text-transform: uppercase;
}

#square img {
  width: 250px;
  height: 250px;
  object-fit: cover;
  object-position: top;
}

#square .square-wrapper {
  overflow: hidden;
  position: relative;
  width: 250px;
  height: 250px;
}

#square .square-wrapper:hover .square-wrapper-inner,
#square .square-wrapper:focus .square-wrapper-inner {
  top: 50%;
}
<div id="square">
  <div class="square-wrapper">
    <div class="img-block" style="background-color: #000; width: 250px; height: 250px;" >
      <h2>Box Title</h2>
    </div>
    
    <div class="square-wrapper-inner">
      <div class="description">
        <a href="#">Link #1</a>
        <a href="#">Link #2</a>
      </div>
    </div>
  </div>
</div>

Answer №1

The root of the problem lies in the overflow: hidden attribute applied to the .square-wrapper element. As stated on this page, using overflow: hidden still allows for scrolling within the element programmatically, while overflow: clip does not permit this behavior.

As a result, when you navigate using the tab key, the content inside the .square-wrapper is scrolled down to focus on the selected element.

Unfortunately, it has been discovered that overflow:clip is only compatible with the most recent version of Chrome, 90. Therefore, if your userbase includes individuals who may not have the latest Chrome version, using this property will not be feasible.

Another issue arises from the fact that :focus cannot be utilized on a div element. In this scenario, only the <a> tags are capable of receiving focus. However, incorporating :focus-within should trigger the desired action.

Hence, the suggested changes are as follows:

overflow:hidden -> overflow:clip
.square-wrapper:focus -> .square-wrapper:focus-within

Refer to the adjustments below:

#square .square-wrapper-inner {
  height: 50%; /* Modified from 100%*/
  width: 100%;
  position: absolute;
  z-index: 9999;
  top: 250px; /* Modified from 600 */
  padding: 15px 20px;
  -webkit-transition: 0.1s;
  -moz-transition: 0.1s;
  -ms-transition: 0.1s;
  -o-transition: 0.1s;
  transition: 0.1s;
  background: rgba(255, 0, 0, 0.8) !important;
  box-sizing: border-box;
}

#square {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
}
#square h2 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -40%);
  width: 100%;
  text-align: center !important;
  font-family: "Merriweather", serif !important;
  font-size: 20px !important;
  letter-spacing: 0.8px;
  line-height: 24px !important;
  color: #fff !important;
  text-shadow: 0 0 7px rgb(0, 0, 0);
  font-weight: 700 !important;
}

#square .description a {
  display: block;
  padding: 10px 20px;
  background-color: #000 !important;
  color: #fff !important;
  text-decoration: none;
  text-align: center !important;
  font-family: "Karla", sans-serif !important;
  font-size: 14px !important;
  margin-bottom: 10px;
}

#square .description a:hover,
#square .description a:focus {
  color: #fff;
}

#square .description a:first-child {
  text-transform: uppercase;
}

#square img {
  width: 250px;
  height: 250px;
  object-fit: cover;
  object-position: top;
}

#square .square-wrapper {
  overflow: clip; /* MODIFIED THIS LINE */
  position: relative;
  width: 250px;
  height: 250px;
}

#square .square-wrapper:hover .square-wrapper-inner,
#square .square-wrapper:focus-within .square-wrapper-inner { /* MODIFIED THIS LINE */
  top: 50%;
}
<div id="square">
  <div class="square-wrapper">
    <div class="img-block" style="background-color: #000; width: 250px; height: 250px;" >
      <h2>Box Title</h2>
    </div>
    
    <div class="square-wrapper-inner">
      <div class="description">
        <a href="#">Link #1</a>
        <a href="#">Link #2</a>
      </div>
    </div>
  </div>
</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

Creating Interactive Buttons with Dropdown Menus in Bootstrap

I am using a Bootstrap dropdown menu with various categories for users to select from. I want the 'Department' button to dynamically change to display the selected category after a user makes a selection. Below is the HTML code for the dropdown: ...

In IE11, the property of table.rows.length is not functioning as expected

In my HTML page, I have the following script: var table = document.getElementById(tableID); var rowCount = table.rows.length; While this script works fine in IE8, in IE11 it does not return the exact row count. Instead, it only returns "0" when the actua ...

Understanding the Camera's View in Three.js

I am currently developing a game using html5 and THREE.js, where I have implemented a camera that rotates with the euler order of 'YXZ'. This setup allows the camera to rotate up, down, left, and right – simulating a first person view experien ...

Adjust the tabs to fit perfectly within the container

I am currently facing an issue with my navigation bar. I have placed the <nav> tags within a container set to 100% width. However, when I adjust the height of the container by a certain percentage, the <ul> & <li> elements in the nav ...

What is the best way to maintain the selected radio button on the following page using PHP?

Image of My Project I am working with a file and need some assistance. I want to ensure that when a user selects an answer, the radio button value remains unchanged if they click back. Can someone please help me with this? while( $row = mysqli_fetch_arra ...

How can I customize the appearance of the file input button in Angular Material?

Despite browsing numerous similar questions, I have yet to find a solution to my issue. My setup includes Angular 11, Angular Material 8, and a file input structured like this: <div class="form-group"> <input #myInput type="fil ...

Incorrect div being autoscroll by Jquery

After searching through over 10 threads, I still haven't found a solution to my issue. I need the autoscroll function to target div2 instead of the main div, but it's not working as expected. Here is my code: <div id="wrapper"> ...

Incorporate additional code into WordPress without relying on theme CSS files

Currently, my WordPress theme is looking good. However, I am facing an issue while trying to add a custom HTML form to it. Despite including the necessary CSS and JS paths within the code, the form ends up looking terrible due to conflicts with the theme&a ...

Positioning of the dropdown in Material UI Autocomplete

Can the Material UI Autocomplete be customized easily to position the Popper dropdown relative to the text cursor, similar to the VS Code Intellisense dropdown? The input field is a multiline Textfield component. The code snippet below demonstrates: impor ...

Creating a dynamic slider with CSS and JavaScript

My slider functions correctly, but I want to add arrows underneath the blocks like in the image. How can I achieve this? https://i.sstatic.net/TpMnw.png The image shows arrows placed under the blocks. Here is the CSS: body { margin: 0; } .slider { ...

Choosing elements in jQuery

Having some trouble with the subnav on my current website project. I think the issue lies in how I am selecting items in my jquery code. It seems like a small fix that needs to be made, but I'm unsure of the correct approach. http://jsfiddle.net/ZDEr ...

jQuery can help in determining the cost based on chosen preferences

Here is my unique html code : <select class="form-control B3 pricing" name="B3"> <option data-price="0" data-cheap="0">0</option> <option data-price="20" data-cheap="30">1</option> <option data-price="40" data-cheap ...

information not visible on the webpage

Recently, I made an alarming discovery on my website. I have implemented two different types of menus - one for the front page (designated as .navbar-inverse) and another for all other pages (.navbar-default). The front page menu is static while the other ...

HTML code to make navbar change color when clicked

Having some trouble with my very first website. I managed to put together a navigation bar at the top that is close to what I want (aside from the colors). The problem is, when I click on different links, I want the box to change color but it's stuck ...

What is the best way to link to this list of options?

#episode-list { padding: 1em; margin: 1em auto; border-top: 5px solid #69c773; box-shadow: 0 2px 10px rgba(0,0,0,.8) } input { width: 100%; padding: .5em; font-size: 1.2em; border-radius: 3px; border: 1px solid #d9d9d9 } <div id="epis ...

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

Having trouble with the Cordova button not functioning on Android 9? A beginner seeks assistance

I attempted to create a button with the id of "clickme" that links to index.html and js/buttonexample.js. Although I could see the button on both the browser and android emulator, it wasn't functioning as expected when clicked. js/buttonexample.js d ...

"Stay entertained with a captivating animated gif that appears when you refresh the

I just implemented this code snippet to enable animated gifs to work after refreshing a webpage. It's functioning properly in Chrome, Safari, and Internet Explorer except for Firefox. Can someone please offer assistance? jQuery: $(img).css("backgrou ...

Dependencies in Scala.js for CSS styling

I am currently having an issue implementing Scala.js with the bootstrap library. Including the js file was a simple and straightforward process: jsDependencies +="org.webjars" % "bootstrap" % "3.3.7-1" / "bootstrap.js" minified "bootstrap.min.js" However ...

Guide to correctly inserting an image into a webpage using JavaScript

Currently, I am working on a tutorial for creating a dynamic webpage. The objective is to display the time and an image that changes based on the current hour. However, the method used by the instructor to insert the image is unfamiliar to me, and despit ...