How to create a Bootstrap grid inside a element that sizes based on content?

Viewed 339

I have a requiremnt to put a grid inside a modal and modal need to sizes based on the content.

Following jsfiddle shows the basic requirement

<div class="pro-modal-outer">
<div class="pro-modal">
<div class="container">
  <div class="row">
    <div class="col-6">
      1 of 2
    </div>
    <div class="col-6">
      2 of 2
    </div>
  </div>
  <div class="row">
    <div class="col-4">
      <div class="field">short</div>
    </div>
    <div class="col-4">
      <div class="field long">
        extra extra long content
      </div>
    </div>
    <div class="col-4">
      <div class="field">long content</div>
    </div>
  </div>  
</div>

https://jsfiddle.net/Nash171/5c1t7dkv/17/

As you see here, some contents of a column overflow to next column.

enter image description here

Columns width need to be size based on it's content and container width need to adjust according to it.

In real case, content will be dynamic. Any column can contain different size child elements as content

All columns need to get equal width as well

Is there any possible solution for this?

3 Answers

For "extra extra long content" use class="col-sm-6" and for long content use class="col-sm-4" and for short use class="col-sm-2" so that the columns width size adjust accordingly. If you using custom classes try to give width in "%" instead of px.

Adjust the width by giving values in % instead of px and try it...

remove this content from your css code.

.field {
    width: 100px;
}
.field.long {
    width: 250px;
}

your code works fine as you want.

the following snippet shows the result what you want.

.pro-modal-outer {
  position: fixed;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pro-modal {
  background-color: #68acff;
  padding: 20px;
  max-width: 100%;
  min-width: 660px;
}
.row {
  background: #f8f9fa;
  margin-top: 20px;
}
.col-4, .col-6 {
  border: solid 1px #6c757d;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="pro-modal-outer">
    <div class="pro-modal">
      <div class="container-fluid">
        <div class="row">
          <div class="col-6">1 of 2</div>
          <div class="col-6">2 of 2</div>
        </div>
        <div class="row">
          <div class="col-4">
            <div class="field">short</div>
          </div>
          <div class="col-4">
            <div class="field long">extra extra long content</div>
          </div>
          <div class="col-4">
            <div class="field">long content</div>
          </div>
        </div>  
      </div>
    </div>
  </div>
</body>
</html>

I hope this will works fine for you.

Thank You...

Related