How to make all pixels of a button in CSS and HTML work, not just when clicking on button text

huangapple go评论67阅读模式
英文:

How to make all pixels of a button in CSS and HTML work, not just when clicking on button text

问题

我有一个按钮,应该将我引导到特定的页面,当我点击它时,它可以工作。但是,只有当我点击按钮内部的实际文本时,它才有效,而不是在按钮的任何位置都有效。

我能够在网上找到一些用于创建按钮的CSS,并稍微编辑了一下,改变了颜色、圆角、边距和其他一些属性。以下是当前的CSS代码:

.btn {
  display: inline-block;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background-color: black;
  font-size: 1.19rem;
  text-align: center;
  padding: 12px 24px;
  margin-left: 45vw;
  margin-right: 45vw;
  border: 3px solid black;
  outline: 3px solid black;
}
<div class="btn">
  <a href="<insert-url-here>">Text Here</a>
</div>

在我稍微更改CSS之前,我可以点击按钮的任何位置来访问URL。然而,在仅仅更改了颜色和边距之后,我认为这不会导致这个问题,我必须点击文本才能访问URL,否则按钮什么都不会发生。

英文:

I have a button that should direct me to a certain page, and when I click it, it works. However, it only works when I click the actual text inside the button, not anywhere on the button itself.

I was able to find some CSS online for making a button, and I just edited it a bit, changed the color, radius, margins, and some other attributes. Here is the current CSS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.btn {
  display: inline-block;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background-color: black;
  font-size: 1.19rem;
  text-align: center;
  padding: 12px 24px;
  margin-left: 45vw;
  margin-right: 45vw;
  border: 3px solid black;
  outline: 3px solid black;
}

<!-- language: lang-html -->

&lt;div class=&quot;btn&quot;&gt;
  &lt;a href=&quot;&lt;insert-url-here&gt;&quot;&gt;Text Here&lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

Before I changed the CSS a bit, I could click anywhere on the button to go to the URL. However, after just changing the color and margins, which I don't think would cause this problem, I have to click on the text to go the URL, or else the button does nothing.

答案1

得分: 0

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->
.btn {
  display: inline-block;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background-color: black;
  font-size: 1.19rem;
  text-align: center;
  padding: 12px 24px;
  margin-left: 45vw;
  margin-right: 45vw;
  border: 3px solid black;
  outline: 3px solid black;
}

<!-- 语言:lang-html -->
<a href="<插入网址>" class="btn">这里是文本</a>

<!-- 结束代码片段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.btn {
  display: inline-block;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background-color: black;
  font-size: 1.19rem;
  text-align: center;
  padding: 12px 24px;
  margin-left: 45vw;
  margin-right: 45vw;
  border: 3px solid black;
  outline: 3px solid black;
}

<!-- language: lang-html -->

&lt;a href=&quot;&lt;insert-url-here&gt;&quot; class=&quot;btn&quot;&gt;Text Here&lt;/a&gt;

<!-- end snippet -->

答案2

得分: 0

  1. 如果您可以使用JS,只需使用一个按钮
&lt;button type=&#39;button&#39; onclick=&quot;location.href=&#39;https://www.google.com&#39;&quot;&gt;
    转到谷歌
&lt;/button&gt;
  1. 使用表单提交
&lt;form action=&#39;https://www.google.com&#39;&gt;
    &lt;button&gt;转到谷歌&lt;/button&gt;
&lt;/form&gt;
  1. 单独使用a元素并相应地设置样式
&lt;a class=&#39;btn&#39; href=&#39;https://www.google.com&#39;&gt;转到谷歌&lt;/a&gt;
.btn {
    margin: 0 45vw;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    height: 2rem;
    width: max-content;
    font-size: 1.19rem;
    background: black;
    border: 6px solid black;
    border-radius: 10px;
}

根据您的使用方式,您还应该考虑使用margin: auto来使元素居中。

最后,按钮的默认文本颜色是黑色,因此如果您使用按钮,您还需要更改文本颜色或更改黑色背景,否则它将看起来像一个黑色的框。

英文:
  1. If you can use JS, just use a button
&lt;button type=&#39;button&#39; onclick=&quot;location.href=&#39;https://www.google.com&#39;&quot;&gt;
    Go to google
&lt;/button&gt;
  1. Use a form submit
&lt;form action=&#39;https://www.google.com&#39;&gt;
    &lt;button&gt;Go to google&lt;/button&gt;
&lt;/form&gt;
  1. Use an a by itself and style it accordingly
&lt;a class=&#39;btn&#39; href=&#39;https://www.google.com&#39;&gt;Go to google&lt;/a&gt;
.btn {
    margin: 0 45vw;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    height: 2rem;
    width: max-content;
    font-size: 1.19rem;
    background: black;
    border: 6px solid black;
    border-radius: 10px;
}

Depending on how you're using it, you should also look into centering your element with margin: auto.

Lastly, the default button text color is black so if you use a button, you'll also want to either change the text color or change your black background, otherwise it will just look like a black box.

huangapple
  • 本文由 发表于 2023年8月4日 01:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定