简体   繁体   English

为什么键盘将焦点放在隐藏按钮上会推动 HTML 顶部的 div 和框标题?

[英]Why does keyboard tabbing the focus on the hidden buttons push up the div on top in the HTML and the box title?

Hover over the box to see the two buttons appear. Hover 过框看到两个按钮出现了。 I want it so that when tabbing, for accessibility, the buttons appear but without pushing up the div and title inside the div on top of it.我想要它,以便在切换时,为了可访问性,按钮会出现,但不会在其顶部的 div 内向上推 div 和标题。

Tabbing to a link within the description div pushes up the div with the class of img-block.跳转到描述 div 中的链接会使用 img-block 的 class 向上推 div。

Why is it doing this and how can I correct it?为什么要这样做,我该如何纠正?

 #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>

The issue is the overflow: hidden property on the .square-wrapper element.问题是.square-wrapper元素上的overflow: hidden属性。 According to this , overflow: hidden still allows programmatic scrolling within the element, while overflow: clip does not. 据此overflow: hidden仍然允许在元素内进行编程滚动,而overflow: clip不允许。

So when you tab, the interior of .square-wrapper is scrolling down to the focused element.因此,当您使用选项卡时, .square-wrapper的内部会向下滚动到焦点元素。

Unfortunately, it turns out that overflow:clip is only supported on the most recent version of Chrome, 90 , so if you need this for a userbase than may not have the most recent version of Chrome, you won't be able to use it.不幸的是,事实证明overflow:clip仅在最新版本的 Chrome 90上受支持,因此如果您的用户群需要它而不是最新版本的 Chrome,您将无法使用它.

The other issue is that you can't use :focus on a div .另一个问题是您不能在div上使用:focus The <a> tags are the only elements receiving focus here. <a>标签是这里唯一获得焦点的元素。 You can use :focus-within , and that will cause the trigger you want, I think.我认为您可以使用:focus-within ,这将导致您想要的触发器。

So:所以:

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

See below:见下文:

 #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. clip: /* CHANGED THIS */ position. relative: width; 250px; height: 250px; } #square .square-wrapper:hover .square-wrapper-inner, #square .square-wrapper:focus-within .square-wrapper-inner { /* CHANGED THIS */ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将键盘标签焦点设置到页面顶部/使用 JavaScript/jQuery 重置键盘焦点 - Set keyboard tabbing focus to the top of the page / resetting keyboard focus with JavaScript / jQuery 当我浏览页面时,为什么整个页面都会向上推几个像素? - Why does the whole page push itself up a few pixels when I am tabbing through the page? 单击时从顶部推送隐藏的 div - Push hidden div from top on click 将html按钮保留在div的顶部 - Keep html buttons at the top of a div 当标签的相应控件隐藏在HTML表单中时,标签时能否在标签上获得焦点? - Can Label get focus while tabbing when its corrsponding control is hidden in HTML Form? 带有两个制表符和focus()的html焦点输入 - html focus inputs with BOTH tabbing and focus() 为什么“ overflow:hidden”会影响具有上/下边距嵌套div的div的背景可见性? - Why does overflow:hidden affect background's visibility of a div that has a nested div with top/bottom margin? 为什么盒子阴影被另一个div隐藏? - Why is box-shadow hidden by another div? 为什么chrome总是在html文件的主体顶部添加一个div? - Why does chrome always add a div on top of the body of html files? 软键盘不会在 iPhone 上向上推元素 - soft keyboard does not push elements up on iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM