[英]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.