英文:
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 -->
<div class="btn">
<a href="<insert-url-here>">Text Here</a>
</div>
<!-- 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 -->
<a href="<insert-url-here>" class="btn">Text Here</a>
<!-- end snippet -->
答案2
得分: 0
- 如果您可以使用JS,只需使用一个按钮
<button type='button' onclick="location.href='https://www.google.com'">
转到谷歌
</button>
- 使用表单提交
<form action='https://www.google.com'>
<button>转到谷歌</button>
</form>
- 单独使用
a
元素并相应地设置样式
<a class='btn' href='https://www.google.com'>转到谷歌</a>
.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
来使元素居中。
最后,按钮的默认文本颜色是黑色,因此如果您使用按钮,您还需要更改文本颜色或更改黑色背景,否则它将看起来像一个黑色的框。
英文:
- If you can use JS, just use a button
<button type='button' onclick="location.href='https://www.google.com'">
Go to google
</button>
- Use a form submit
<form action='https://www.google.com'>
<button>Go to google</button>
</form>
- Use an
a
by itself and style it accordingly
<a class='btn' href='https://www.google.com'>Go to google</a>
.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论