如何使整个按钮都可以点击,而不仅仅是按钮内部的文本。在这个问题上卡住了一段时间。
我在这里问这个问题之前试着谷歌了一下。但我得到的所有解决方案都不能解决我的问题。
/* Header Buttons Code */
.headerbutton {
border: 2px solid black;
background-color: #ffffff;
float: left;
cursor: pointer;
border-radius: 15px;
margin: 5px;
}
/* Change background color of buttons on hover */
.headerbutton:hover {
background-color: #6abe97;
}
/* Header Settings */
.header {
overflow: hidden;
background-color: #ed0404;
margin: -8px;
}
/* Links In Header Code */
.header a {
float: left;
color: black;
text-align: center;
padding: 5px;
text-decoration: none;
font-size: 17px;
line-height: 25px;
margin: 10px;
}
/* Background Of Links In Header While Hoover */
.header a:hover {
background-color: #C0A9BD;
color: black;
}
/* Active Link In Header*/
.header a.active {
background-color: dodgerblue;
color: white;
}
/* 3 Buttons Potition Setting In Header */
.header-right {
float: right;
padding-top: 20px;
padding-bottom: 20px;
}
/* Logo Position Setting */
.header-left {
float: left;
padding-top: 10px;
padding-bottom: 10px;
}
/* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
@media screen and (max-width: 500px) {
.header a {
float: none;
display: block;
text-align: left;
}
.header-right {
float: none;
}
}
/*Logo Size Setting */
.logo-container img {
width: 100px;
height: 50px;
}
/*Footer Setting*/
.footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 30px;
background-color: red;
}
<header>
<div class="header">
<div class="header-left">
<div class="logo-container">
<a href="index.html"><img src="./media/Logo.png"></a>
</div>
</div>
<div class="header-right">
<div id="Contact" class="headerbutton">
<a href="contact.html">Contact Us</a>
</div>
<div id="about" class="headerbutton">
<a href="about.html">About Us</a>
</div>
<div id="Cart" class="headerbutton">
<a href="cart.html">Cart</a>
</div>
</div>
</div>
</header>
3条答案
按热度按时间kcrjzv8t1#
在
a
标记上添加padding
而不是margin
。Padding
是元素的内容与其边框之间的空间,另一方面,margin
是元素外部的空间Padding
被认为是元素本身的一部分,所以它是可点击的,但margins
不是元素的一部分,不响应点击事件。tjvv9vkg2#
您应该在
<a>
元素上使用padding而不是margin来获得所需的大小。边距不计算元素的实际宽度。考虑使用填充。lbsnaicq3#
标签中的所有内容都是可点击的。所以把你的div放在a标签里