转换 CSS 属性 不适用于 <a> 元素

Transform CSS property doesn't work with <a> element

我想 scale(x,y) 我的 <a> 元素,但它不起作用。我使用 Mozilla Firefox 网络浏览器 运行 程序。

这是我的代码:

scaleElement.html

<html>
    <head>
        <title>CSS3 Transform and Transition</title>
        <style>
            a{
                background-color: green;
                color: white;
                padding: 10px 20px;
                text-decoration: none;
                border: 2px solid #85ADFF;
                border-radius: 30px 10px;
                transition: 2s;
            }
            a:hover{
                transform: scale(2,2);
            }
        </style>
    </head>

    <body>
        <center><a href="xyz.html">click here</a></center>
    </body>
</html>

使用display:block并给它一个高度和宽度

a {
  background-color: green;
  color: white;
  padding: 10px 20px;
  text-decoration: none;
  border: 2px solid #85ADFF;
  border-radius: 30px 10px;
  transition: 2s all ease-in;
  display: block;
  width:100px;
  height:50px;
}
a:hover {
  transform: scale(2, 2);
}
<center><a href="xyz.html">click here</a>
</center>

transform不适用于<a>等行内元素。您可以将 link 显示为 inline-blockblock 以使转换生效:

transformable element

A transformable element is an element in one of these categories:

  • an element whose layout is governed by the CSS box model which is either a block-level or atomic inline-level element, or whose display property computes to table-row, table-row-group, table-header-group, table-footer-group, table-cell, or table-caption [...]

其中atomic inline-level个元素包括:

replaced inline-level elements, inline-block elements, and inline-table elements

a { display: inline-block; }
a:hover { transform: scale(2,2); }

此外,CSS 中没有点击 状态可用。可能的选项是 :active:hover,或使用 checkbox hack.

a {
  background-color: green;
  color: white;
  padding: 10px 20px;
  text-decoration: none;
  border: 2px solid #85ADFF;
  border-radius: 30px 10px;
  transition: all 2s;
  display: inline-block; /* <-- added declaration */
}
a:hover{ transform: scale(2,2); }
/* just for demo */
body { padding: 2em; text-align: center; }
<a href="xyz.html">click here</a>