如何使整个按钮不仅可点击按钮内的文本?(仅使用HTML和CSS)

gj3fmq9x  于 2023-10-21  发布在  其他
关注(0)|答案(3)|浏览(157)

如何使整个按钮都可以点击,而不仅仅是按钮内部的文本。在这个问题上卡住了一段时间。
我在这里问这个问题之前试着谷歌了一下。但我得到的所有解决方案都不能解决我的问题。

  1. /* Header Buttons Code */
  2. .headerbutton {
  3. border: 2px solid black;
  4. background-color: #ffffff;
  5. float: left;
  6. cursor: pointer;
  7. border-radius: 15px;
  8. margin: 5px;
  9. }
  10. /* Change background color of buttons on hover */
  11. .headerbutton:hover {
  12. background-color: #6abe97;
  13. }
  14. /* Header Settings */
  15. .header {
  16. overflow: hidden;
  17. background-color: #ed0404;
  18. margin: -8px;
  19. }
  20. /* Links In Header Code */
  21. .header a {
  22. float: left;
  23. color: black;
  24. text-align: center;
  25. padding: 5px;
  26. text-decoration: none;
  27. font-size: 17px;
  28. line-height: 25px;
  29. margin: 10px;
  30. }
  31. /* Background Of Links In Header While Hoover */
  32. .header a:hover {
  33. background-color: #C0A9BD;
  34. color: black;
  35. }
  36. /* Active Link In Header*/
  37. .header a.active {
  38. background-color: dodgerblue;
  39. color: white;
  40. }
  41. /* 3 Buttons Potition Setting In Header */
  42. .header-right {
  43. float: right;
  44. padding-top: 20px;
  45. padding-bottom: 20px;
  46. }
  47. /* Logo Position Setting */
  48. .header-left {
  49. float: left;
  50. padding-top: 10px;
  51. padding-bottom: 10px;
  52. }
  53. /* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
  54. @media screen and (max-width: 500px) {
  55. .header a {
  56. float: none;
  57. display: block;
  58. text-align: left;
  59. }
  60. .header-right {
  61. float: none;
  62. }
  63. }
  64. /*Logo Size Setting */
  65. .logo-container img {
  66. width: 100px;
  67. height: 50px;
  68. }
  69. /*Footer Setting*/
  70. .footer {
  71. position: fixed;
  72. bottom: 0;
  73. left: 0;
  74. width: 100%;
  75. height: 30px;
  76. background-color: red;
  77. }
  1. <header>
  2. <div class="header">
  3. <div class="header-left">
  4. <div class="logo-container">
  5. <a href="index.html"><img src="./media/Logo.png"></a>
  6. </div>
  7. </div>
  8. <div class="header-right">
  9. <div id="Contact" class="headerbutton">
  10. <a href="contact.html">Contact Us</a>
  11. </div>
  12. <div id="about" class="headerbutton">
  13. <a href="about.html">About Us</a>
  14. </div>
  15. <div id="Cart" class="headerbutton">
  16. <a href="cart.html">Cart</a>
  17. </div>
  18. </div>
  19. </div>
  20. </header>
kcrjzv8t

kcrjzv8t1#

a标记上添加padding而不是margin
Padding是元素的内容与其边框之间的空间,另一方面,margin是元素外部的空间
Padding被认为是元素本身的一部分,所以它是可点击的,但margins不是元素的一部分,不响应点击事件。

  1. /* Header Buttons Code */
  2. .headerbutton {
  3. border: 2px solid black;
  4. background-color: #ffffff;
  5. float: left;
  6. cursor: pointer;
  7. border-radius: 15px;
  8. margin: 5px;
  9. }
  10. /* Change background color of buttons on hover */
  11. .headerbutton:hover {
  12. background-color: #6abe97;
  13. }
  14. /* Header Settings */
  15. .header {
  16. overflow: hidden;
  17. background-color: #ed0404;
  18. margin: -8px;
  19. }
  20. /* Links In Header Code */
  21. .header a {
  22. float: left;
  23. color: black;
  24. text-align: center;
  25. text-decoration: none;
  26. font-size: 17px;
  27. line-height: 25px;
  28. padding: 10px;
  29. border-radius: 15px;
  30. }
  31. /* Background Of Links In Header While Hoover */
  32. .header a:hover {
  33. background-color: #C0A9BD;
  34. color: black;
  35. }
  36. /* Active Link In Header*/
  37. .header a.active {
  38. background-color: dodgerblue;
  39. color: white;
  40. }
  41. /* 3 Buttons Potition Setting In Header */
  42. .header-right {
  43. float: right;
  44. padding-top: 20px;
  45. padding-bottom: 20px;
  46. }
  47. /* Logo Position Setting */
  48. .header-left {
  49. float: left;
  50. padding-top: 10px;
  51. padding-bottom: 10px;
  52. }
  53. /* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
  54. @media screen and (max-width: 500px) {
  55. .header a {
  56. float: none;
  57. display: block;
  58. text-align: left;
  59. }
  60. .header-right {
  61. float: none;
  62. }
  63. }
  64. /*Logo Size Setting */
  65. .logo-container img {
  66. width: 100px;
  67. height: 50px;
  68. }
  69. /*Footer Setting*/
  70. .footer {
  71. position: fixed;
  72. bottom: 0;
  73. left: 0;
  74. width: 100%;
  75. height: 30px;
  76. background-color: red;
  77. }
  1. <header>
  2. <div class="header">
  3. <div class="header-left">
  4. <div class="logo-container">
  5. <a href="index.html"><img src="./media/Logo.png"></a>
  6. </div>
  7. </div>
  8. <div class="header-right">
  9. <div id="Contact" class="headerbutton">
  10. <a href="contact.html">Contact Us</a>
  11. </div>
  12. <div id="about" class="headerbutton">
  13. <a href="about.html">About Us</a>
  14. </div>
  15. <div id="Cart" class="headerbutton">
  16. <a href="cart.html">Cart</a>
  17. </div>
  18. </div>
  19. </div>
  20. </header>
展开查看全部
tjvv9vkg

tjvv9vkg2#

您应该在<a>元素上使用padding而不是margin来获得所需的大小。边距不计算元素的实际宽度。考虑使用填充。

lbsnaicq

lbsnaicq3#

标签中的所有内容都是可点击的。所以把你的div放在a标签里

  1. /* Header Buttons Code */
  2. .headerbutton {
  3. border: 2px solid black;
  4. background-color: #ffffff;
  5. float: left;
  6. cursor: pointer;
  7. border-radius: 15px;
  8. margin: 5px;
  9. }
  10. /* Change background color of buttons on hover */
  11. .headerbutton:hover {
  12. background-color: #6abe97;
  13. }
  14. /* Header Settings */
  15. .header {
  16. overflow: hidden;
  17. background-color: #ed0404;
  18. margin: -8px;
  19. }
  20. /* Links In Header Code */
  21. .header a {
  22. float: left;
  23. color: black;
  24. text-align: center;
  25. padding: 5px;
  26. text-decoration: none;
  27. font-size: 17px;
  28. line-height: 25px;
  29. margin: 10px;
  30. }
  31. /* Background Of Links In Header While Hoover */
  32. .header a:hover {
  33. background-color: #C0A9BD;
  34. color: black;
  35. }
  36. /* Active Link In Header*/
  37. .header a.active {
  38. background-color: dodgerblue;
  39. color: white;
  40. }
  41. /* 3 Buttons Potition Setting In Header */
  42. .header-right {
  43. float: right;
  44. padding-top: 20px;
  45. padding-bottom: 20px;
  46. }
  47. /* Logo Position Setting */
  48. .header-left {
  49. float: left;
  50. padding-top: 10px;
  51. padding-bottom: 10px;
  52. }
  53. /* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
  54. @media screen and (max-width: 500px) {
  55. .header a {
  56. float: none;
  57. display: block;
  58. text-align: left;
  59. }
  60. .header-right {
  61. float: none;
  62. }
  63. }
  64. /*Logo Size Setting */
  65. .logo-container img {
  66. width: 100px;
  67. height: 50px;
  68. }
  69. /*Footer Setting*/
  70. .footer {
  71. position: fixed;
  72. bottom: 0;
  73. left: 0;
  74. width: 100%;
  75. height: 30px;
  76. background-color: red;
  77. }
  1. <header>
  2. <div class="header">
  3. <div class="header-left">
  4. <div class="logo-container">
  5. <a href="index.html"><img src="./media/Logo.png"></a>
  6. </div>
  7. </div>
  8. <div class="header-right">
  9. <a id="Contact" class="headerbutton" href="contact.html">Contact Us</a>
  10. <a id="about" class="headerbutton" href="about.html">About Us</a>
  11. <a id="Cart" class="headerbutton" href="cart.html">Cart</a>
  12. </div>
  13. </div>
  14. </header>
展开查看全部

相关问题