简体   繁体   English

更改Bootstrap手风琴配置

[英]Change Bootstrap accordion configuration

I have this Bootstrap accordion implemented on my web using Bootstrap 4.1.0 我使用Bootstrap 4.1.0在我的Web上实现了这个Bootstrap手风琴

<div id="accordion" class="mt-3">
   <div class="card">
       <div class="card-header bg-dark text-white" id="headingOne">
           <h5 class="mb-0 fontBig text-center" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
               Title 1
           </h5>
       </div>

       <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
           <div class="card-body">
               Content 1
           </div>
       </div>
   </div>
   <div class="card">
       <div class="card-header bg-dark text-white" id="headingTwo">
           <h5 class="mb-0 fontBig text-center" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
               Title 2
           </h5>
        </div>
        <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
           <div class="card-body">
               Content 2
           </div>
        </div>
   </div>
</div>

The problem is that if I want to open or close any card of the accordion I have to click on the h5 tag, which is clearly thinner than the div where it is contained. 问题是,如果要打开或关闭手风琴的任何卡,我必须单击h5标签,该标签显然比包含它的div薄。 You can see what I mean on this pic: 您可以在这张图片上看到我的意思:

在此处输入图片说明

If you dont click on that h5 the accordion won't open. 如果您不单击该h5,则手风琴将不会打开。 I want to change it so that it opens when you click anywhere on the whole card-header. 我要更改它,以便在您单击整个卡头上的任何位置时将其打开。

So you've already explained the problem in your post: .card-header applies a padding that isn't clickable since the JavaScript powering the Accordion component looks for events on your <h5> . 因此,您已经在帖子中解释了该问题: .card-header应用了不可单击的填充,因为为Accordion组件提供动力的JavaScript会在您的<h5>上查找事件。 The simplest approach then would be to remove the padding from .card-header and apply it to <h5> . 然后,最简单的方法是从.card-header删除填充并将其应用于<h5>

Bootstrap provides utility classes that allow you to do that without any additional custom CSS: p*-* Bootstrap提供的实用程序类使您无需任何其他自定义CSS即可执行此操作: p*-*

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> <div id="accordion" class="mt-3"> <div class="card"> <div class="card-header bg-dark text-white p-0" id="headingOne"> <h5 class="mb-0 p-3 fontBig text-center" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Title 1</h5> </div> <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion"> <div class="card-body">Content 1</div> </div> </div> <div class="card"> <div class="card-header bg-dark text-white p-0" id="headingTwo"> <h5 class="mb-0 p-3 fontBig text-center" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Title 2</h5> </div> <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion"> <div class="card-body">Content 2</div> </div> </div> </div> 

In the code above I've removed the padding from .card-header by applying .p-0 which removes all padding from X/Y margins. 在上面的代码中,我通过应用.card-header .p-0.card-header中删除了填充,这从X / Y边距中删除了所有填充。 Padding is then added to <h5> with .p-3 which adds padding to all margins. 然后使用.p-3将填充添加<h5> ,这会将填充添加到所有页边距。

You can learn more about Bootstrap 4s spacing utilities via the documentation: 您可以通过文档了解有关Bootstrap 4s间隔实用程序的更多信息:

https://getbootstrap.com/docs/4.1/utilities/spacing/ https://getbootstrap.com/docs/4.1/utilities/spacing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM