Reputation: 34840
In the following example, I want to format the divs like a table, but I don't want to spread the text over 100% of the width of the page. I want the columns to be as close to each other so it will read as normal text. For the example below I want to display it as:
aaa bb ccc ddd
aaaaa bbbb c dd
When I try to use Bootstrap 4's .row
and .col
I get the table formatting I want, but then the table spreads the text over 100% of the width of the page. How can I get the above formatting?
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div style="font-family: Consolas;">
<div class="d-flex p-2">
<div>aaa </div>
<div>bb </div>
<div>ccc </div>
<div>ddd </div>
</div>
<div class="d-flex p-2">
<div>aaaaa </div>
<div>bbbb </div>
<div>c </div>
<div>dd </div>
</div>
</div>
EDIT: I realize using a normal table
without the CSS of Bootstrap might do what I want.
Upvotes: 0
Views: 503
Reputation: 90208
If you want the behavior of a <table>
, that's what you should use.
Bootstrap only styles <table>
s having the class of table
, to allow using <table>
s for any other purpose:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<table>
<tbody>
<tr>
<td>aaa </td>
<td>bb </td>
<td>ccc </td>
<td>ddd </td>
</tr>
<tr>
<td>aaaaa </td>
<td>bbbb </td>
<td>c </td>
<td>dd </td>
</tr>
</tbody>
</table>
To have the Bootstrap's <table>
specific styling and make the table not stretch the entire width of its parent you can give the table width: auto
:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table" style="width: auto">
<tbody>
<tr>
<td>aaa </td>
<td>bb </td>
<td>ccc </td>
<td>ddd </td>
</tr>
<tr>
<td>aaaaa </td>
<td>bbbb </td>
<td>c </td>
<td>dd </td>
</tr>
</tbody>
</table>
You can also rely on Bootstrap's grid system to limit the width of the <table>
or of its parent.
Upvotes: 2