Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
936 views
in Technique[技术] by (71.8m points)

bootstrap 4 card-deck containing cards with different width

I am trying to use a card-deck in bootstrap 4 which contain cards with different width.

For single cards you do the following to change the width (or use class w-25, w-50 or w-75 if the 25%, 50% or 75% width fit your needs):

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="card">
    <div class="card-header">Header</div>
    <div class="card-body">
      <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisici elit.</p>
    </div>
  </div>
  <br>
  <div class="card" style="width: 30%">
    <div class="card-header">Header</div>
    <div class="card-body">
      <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisici elit.</p>
    </div>
  </div>
  <br>
  <div class="card" style="width: 70%">
    <div class="card-header">Header</div>
    <div class="card-body">
      <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisici elit.</p>
    </div>
  </div>

</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As indicated in the docs, the card-deck is equal width cards. If you want different width wrap the cards in the Bootstrap grid...

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <div class="card h-100">
                ...
            </div>
        </div>
        <div class="col-md-3">
            <div class="card h-100">
                ...
            </div>
        </div>
        <div class="col-md-3">
            <div class="card h-100">
                ...
            </div>
        </div>
    </div>
</div>

https://www.codeply.com/go/8qpkLw5Dfv

Or, use custom CSS to set the flex basis and max-width of the card-deck cards...

.card-deck .cw-30 {
    flex: 1 0 30%;
    max-width: 30%;
}

.card-deck .cw-70 {
    flex: 1 0 70%;
    max-width: 70%;
}

https://www.codeply.com/go/JiAEZdT0Ob


Related: Cards with different sizes in Bootstrap 4 card-group


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...