CSS 翻转卡 : How can I activate the flip via tab, 是否可以通过键盘访问?

标签 css accessibility flip

我找到了一些关于如何使用 CSS 制作翻盖卡的很棒的教程。我的问题是如何使这个键盘可访问。换句话说,对于只使用键盘的残障用户来说,希望他们能够只使用 tab 按钮(从而获得焦点),并且卡片会翻转以显示后面的内容并允许通过 tab 来选择链接在卡的背面。

我在谷歌上搜索并找到了一些建议(请参阅下面我尝试过的 jsfiddle),但我无法成功。

这是一个具有此功能的很棒的网站,但我不知道它们是如何工作的:
https://businessexpress.maryland.gov/

请注意,如果您在上面的页面上按住 Tab 键,最终您的卡片会翻转,然后您可以 Tab 键浏览它们上的链接。例如,对于第一张翻转卡片,有一个链接“/plan”,然后它有像“/plan/create-business-plan”这样的子链接。

请注意,我试图在第 21 行放入一些 CSS,这会影响“active”和“focus”伪类。但只有悬停会使卡片翻转。我希望点击任何链接都会翻转卡片,就像上面的 maryland.gov 示例一样。

我在这里包含了一个 jsfiddle(有一个小输入元素,所以你可以从它开始跳格):
https://jsfiddle.net/anrbhcmv/

HTML:

    <div id="content">
        <h1>Small Business Resources</h1>

    <input type="text">
    <br><br>

        <div class="flip-card">
            <div class="flip-card-inner">
                <a href="#" id="flip-card-inner">
                    <div class="flip-card-front">
                        <div>Card-front content</div>
                    </div>
                </a>
                <div class="flip-card-back">
                    <a href="https://www.google.com">Google</a>
                    <div>Text</div>
                </div>
            </div>
        </div>

    </div><!-- end #content -->

CSS:
/* The flip card container - set the width and height to whatever you want. We have added the border property to demonstrate that the flip itself goes out of the box on hover (remove perspective if you don't want the 3D effect */
.flip-card {
    background-color: transparent;
    width: 300px;
    height: 200px;
    // border: 1px solid #f1f1f1;
    // perspective: 1000px; /* Remove this if you don't want the 3D effect */
}

/* This container is needed to position the front and back side */
.flip-card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.8s;
    transform-style: preserve-3d;
}

/* Do an horizontal flip when you move the mouse over the flip box container */
.flip-card:hover .flip-card-inner, .flip-card:active .flip-card-inner, .flip-card:focus .flip-card-inner{
    transform: rotateY(180deg);
}

/* Position the front and back side */
.flip-card-front, .flip-card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden; /* Safari */
    backface-visibility: hidden;
}

/* Style the front side (fallback if image is missing) */
.flip-card-front {
    background-color: #008CCC;
    background-color: azure;
    color: white;
    color: black;
}

/* Style the back side */
.flip-card-back {
    background-color: #99CC66;
    color: white;
    transform: rotateY(180deg);
}

最佳答案

您可以使用 :focus-within pseudo-class :
.flip-card:focus-within .flip-card-inner

/* The flip card container - set the width and height to whatever you want. We have added the border property to demonstrate that the flip itself goes out of the box on hover (remove perspective if you don't want the 3D effect */

.flip-card {
  background-color: transparent;
  width: 300px;
  height: 200px;
  // border: 1px solid #f1f1f1;
  // perspective: 1000px; /* Remove this if you don't want the 3D effect */
}


/* This container is needed to position the front and back side */

.flip-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}


/* Do an horizontal flip when you move the mouse over the flip box container */

.flip-card:hover .flip-card-inner,
.flip-card:focus-within .flip-card-inner,
.flip-card:active .flip-card-inner,
.flip-card:focus .flip-card-inner {
  transform: rotateY(180deg);
}


/* Position the front and back side */

.flip-card-front,
.flip-card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  /* Safari */
  backface-visibility: hidden;
}


/* Style the front side (fallback if image is missing) */

.flip-card-front {
  background-color: #008CCC;
  background-color: azure;
  color: white;
  color: black;
}


/* Style the back side */

.flip-card-back {
  background-color: #99CC66;
  color: white;
  transform: rotateY(180deg);
}
<div id="content">
  <h1>Small Business Resources</h1>

  <input type="text">
  <br><br>

  <div class="flip-card">
    <div class="flip-card-inner">
      <a href="#" id="flip-card-inner">
        <div class="flip-card-front">
          <div>Card-front content</div>
        </div>
      </a>
      <div class="flip-card-back">
        <a href="https://www.google.com">Google</a>
        <div>Text</div>
      </div>
    </div>
  </div>

</div>
<!-- end #content -->

关于CSS 翻转卡 : How can I activate the flip via tab, 是否可以通过键盘访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62418249/

相关文章:

html - Firebase 未经授权的访问

javascript - 我想停止预加载器。当页面加载时,我希望它应该隐藏

html - 如何强制我的跨度具有定义的高度?

css - 屏幕阅读器能看到背面可见性吗?

python - 网络X : Flip graph

javascript - 单击另一个 DIV 后恢复翻转

css - 悬停 div 时如何影响 UPPER 元素?

winapi - Microsoft Active Accessibility 在枚举控件时 super 慢。帮助!

html - 屏幕阅读器如何阅读 <abbr> 标签?

css flipped block interaction with non flipped, only webkit specific