如何让Bootstrap 5卡片可点击?添加锚标签后仍无效的问题
Bootstrap 5服务卡片无法点击跳转的解决方法
问题说明
我是Bootstrap 5、HTML和CSS的新手,正在搭建网站。用Bootstrap 5创建了服务卡片,希望让卡片可点击跳转到站内其他页面,但添加锚标签后卡片仍然无法点击,以下是我的代码:
Test.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Home |</title> <meta name="description" content="Write an awesome description for your new site here. It will appear in your document head meta (for Google search results) and in your feed.xml site description." /> <link rel="stylesheet" href="/_bridgetown/static/css/main.c7d4dd3f1984a290e9be.css" /> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="../css/index.css" /> <link rel="stylesheet" data-purpose="Layout StyleSheet" title="Default" disabled href="/css/app-937c1ff7d52fd6f78dd9322599e2b5d4.css?vsn=d" > <link rel="stylesheet" data-purpose="Layout StyleSheet" title="Web Awesome" href="/css/app-wa-8d95b745961f6b33ab3aa1b98a45291a.css?vsn=d" > <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/all.css" > <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-solid.css" > <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-regular.css" > <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-light.css" > <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <!-- SERVICES SECTION START --> <section> <div class="container"> <div class="row py-5"> <h2 class="display-6 mb-4">Our Services</h2> <div class="col-lg-4 col-md-6"> <a href="/about.html" class="nav-link"></a> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-camera-cctv fa-3x"></i> <h4>CCTV Services</h4> <p class="p-2"> <b>CCTV Services</b> are an essential component of any business. CCTV provides real-time monitoring of multiple locations; the security they supply is indispensable, allowing businesses to have peace of mind knowing that... </p> </div> </div> <!-- <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2"> <i class="fa-solid fa-sign-hanging fa-3x"></i> <h4>Digital Signage</h4> <p class="p-2"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam voluptatum, voluptate, quibusdam, quia voluptas quod quos exercitationem quae voluptatibus doloribus quidem. Quisquam </p> </div> </div> --> <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-database fa-3x"></i> <h4>Datacenter Support</h4> <p class="p-2"> <b>Datacenters</b> are the foundation of many modern businesses, providing the infrastructure and resources necessary to save, process, and dispense data. They produce an off-premises location to secure and back up your business's data. </p> </div> </div> <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-sign-hanging fa-3x"></i> <h4>Digital Signage</h4> <p class="p-2"> <b>Digital Signage</b> has grown rapidly over the last two decades well beyond just a flashy trend for big businesses in the realm of marketing and advertising, and the general dissemination of information. Now it has become the expected strategy. </p> </div> </div> <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-headset fa-3x"></i> <h4>General Support</h4> <p class="p-2"> <b>General Support</b> is a crucial element to every modern business; providing essential support to businesses of all shapes and sizes. The focal point of General Support is to... </p> </div> </div> <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-server fa-3x"></i> <h4>Network Support</h4> <p class="p-2"> <b>Network Support</b> includes making sure all networking hardware, software, and other related dependencies are both up to date and functioning properly. Monitoring the network for potential problems... </p> </div> </div> <div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-bars fa-3x"></i> <h4>Additional Services</h4> <p class="p-2"> <br/> <br/> <br/> <br/> <br/> </p> </div> </div> </div> </div> </section> <!-- SERVICES SECTION CLOSE --> </body> </html>
Test.css
@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap'); body { background: hsl(233, 47%, 96%); font-family: 'Poppins', sans-serif; } hr { border: 1.5px solid hsl(216, 90%, 44%) } .crd { background: hsl(233, 47%, 96%); transition: all; } .crd:hover { background: hsl(216, 90%, 44%); color: #fff; transition: 0.5s; } .card img { margin: auto; } #rounded_circle { width: 76px; aspect-ratio: 1; background-color: hsl(0, 0%, 75%); } footer a { color: hsl(0, 0%, 100%); } #footer { color: hsl(0, 0%, 100%); } #copyright { border-top: 1px solid hsl(0, 0%, 20%) ; } .c-item { height: 480px; } .c-img { height: 100%; object-fit: cover; filter: brightness(0.6); } #team-img { height: 100%; object-fit: cover; } @media (min-width: 576px) { .dropdown:hover > .dropdown-menu { display: block; margin-top: 0; } }
问题原因
你现在的锚标签是空标签,放在卡片外部,没有覆盖任何可点击区域,点击卡片时根本不会触发跳转事件。
解决方案
给你两种简单的实现方式,任选其一即可:
方式1:用Bootstrap 5内置的stretched-link类(推荐)
这个类可以让锚标签自动覆盖整个父容器(卡片),无需额外写CSS。操作步骤:
- 删除卡片外的空锚标签
- 在卡片内部添加带
stretched-link类的锚标签,位置任意(建议放在内容最后) - Bootstrap的
.card默认是相对定位,无需额外设置position: relative
修改后的单张卡片代码:
<div class="col-lg-4 col-md-6"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-camera-cctv fa-3x"></i> <h4>CCTV Services</h4> <p class="p-2"> <b>CCTV Services</b> are an essential component of any business. CCTV provides real-time monitoring of multiple locations; the security they supply is indispensable, allowing businesses to have peace of mind knowing that... </p> <!-- 新增跳转锚标签 --> <a href="/about.html" class="stretched-link"></a> </div> </div>
方式2:用锚标签直接包裹整个卡片
把整个.card元素放在锚标签内部,同时给锚标签设置display: block(a标签默认是行内元素,包裹块级元素需改为块级),另外添加text-decoration: none和color: inherit保持卡片原有样式。
修改后的单张卡片代码:
<div class="col-lg-4 col-md-6"> <a href="/about.html" style="display: block; text-decoration: none; color: inherit;"> <div class="card text-center p-4 mb-2 crd"> <i class="fa-solid fa-camera-cctv fa-3x"></i> <h4>CCTV Services</h4> <p class="p-2"> <b>CCTV Services</b> are an essential component of any business. CCTV provides real-time monitoring of multiple locations; the security they supply is indispensable, allowing businesses to have peace of mind knowing that... </p> </div> </a> </div>
注意事项
- 所有卡片都需要按上述方式修改,才能实现各自的跳转功能
- 方式2中如果不想用内联样式,可以把样式写到Test.css里:
.card-link-wrapper { display: block; text-decoration: none; color: inherit; }
然后给锚标签加class="card-link-wrapper"即可。
内容的提问来源于stack exchange,提问作者PFL
相关产品推荐
相关产品推荐

