Reputation: 269
Having some trouble after transitioning to bootstrap 4.1. I had horizontal cards working fine in 4.0-alpha, but now, not so much. The following screenshot shows two attempts at aligning horizontal cards with about 75% card-text and 25% the image thumbnail on the right. The first attempt is a bare bones row with two columns and the second attempt was taken from an example here (see card halfway down). I prefer simplicity for maintenance, but the second example obviously looks better (if you remove the border colors), but in either case I'm getting extra padding around the image and the 75:25 ratio is not stable depending on the image size. Would like the most responsive solution and maintainable solution if possible (i know... the cake eating beggar).
GOAL: Have equally sized horizontal cards aligned in a center column, with each card roughly 75% card-text and 25% image. I want to be able to adjust this ratio based on the size of the image I want or alternatively the size of the image div. So if I increase the image size/max-width it will increase the image column/flex-row/whatever, or increasing the image div will decrease the card-text div. But regardless of the native image size, it should fill the column with no padding.
*Also, both cards are in the same parent container, so I'm not sure why one is wider than the other.
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="static/style.css">
</head>
<body>
<div class="container">
<div class="row-fluid" style="margin-top:3%;">
<div class="card" style="border:solid;border-color:black;">
<div class="d-flex flex-row" >
<div class="p-8 flex-grow-1" style="border:solid;border-color:green;">
<h3>Card Title</h3>
This text should expand to 75% of the parent container. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
</div>
<div class="p-4 flex-grow-2" style="border:solid;border-color:red;">
This should be 25%
<img class="card-img-right flex-auto d-lg-block nopadding" src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" style="width:70%;">
</div>
</div>
</div>
</div>
<div class="row" >
<div class="card flex-md-row mb-4 box-shadow h-md-250" style="border:solid;border-color:black;">
<div class="col-lg-8" style="border:solid;border-color:green;">
<div class="card-body d-flex flex-column align-items-start">
<div class="card-text mb-auto">
<h3>Card Title</h3>
This text should expand to 75% of the parent container. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
</div>
</div>
</div>
<div class="col-md-4" style="border:solid;border-color:red;">
This should be 25% with no padding
<img class="card-img-right flex-auto d-lg-block nopadding" src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" style="width:70%;">
</div>
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</body>
</html>
Looked at a number of solutions, but I'm not sure they apply since they are much older and 4.1 is so new.
UPDATE 1
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="static/style.css">
</head>
<body>
<div class="container">
<div class="row-fluid no-gutter">
<div class="card" style="border:solid;border-color:black;">
<div class="col-8" style="border:solid;border-color:green;">
<h3>Card Title</h3>
This text should expand to 75% of the parent container. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
Extra text. Extra text. Extra text. Extra text. Extra text. Extra text. Extra text.
</div>
<div class="col-4" style="border:solid;border-color:red;">
This should be 25% with no padding
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" style="width:70%;">
</div>
</div>
</div>
<div class="card flex-md-row mb-4 box-shadow h-md-250 m-3">
<div class="card-body d-flex flex-column align-items-start flex-grow-1 pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
<a href="#">Continue reading</a>
</div>
<img class="d-none d-md-flex" data-src="holder.js/200x200" alt="200x200" src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png"
data-holder-rendered="true" style="width: 200px; height: 200px;">
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</body>
</html>
UPDATE 2
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="static/style.css">
</head>
<body>
<div class="container">
<div class="card flex-md-row box-shadow h-md-250 mx-3 mt-3">
<div class="row no-gutters">
<div class="col-sm-8 d-flex order-1 order-sm-0">
<div class="card-body d-flex flex-column align-items-start pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in.</p>
<a href="#">Continue reading</a>
</div>
</div>
<div class="col-sm-4">
<img class="d-block img-w-100" data-src="holder.js/200x200" alt="200x200" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22200%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20200%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_1648332c5cf%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A10pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_1648332c5cf%22%3E%3Crect%20width%3D%22200%22%20height%3D%22200%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2273.640625%22%20y%3D%22104.5%22%3E200x200%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
data-holder-rendered="true">
</div>
</div>
</div>
<div class="card flex-md-row box-shadow h-md-250 mx-3 mt-3">
<div class="row no-gutters">
<div class="col-sm-8 d-flex order-1 order-sm-0">
<div class="card-body d-flex flex-column align-items-start pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in.</p>
<a href="#">Continue reading</a>
</div>
</div>
<div class="col-sm-4">
<img class="d-block img-w-100" data-src="holder.js/200x200" alt="200x200" src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png"
data-holder-rendered="true">
</div>
</div>
</div>
<div class="card flex-md-row mb-4 box-shadow h-md-250 m-3">
<div class="card-body d-flex flex-column align-items-start flex-grow-1 pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
<a href="#">Continue reading</a>
</div>
<img class="d-none d-md-flex" data-src="holder.js/200x200" alt="200x200" src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png"
data-holder-rendered="true" style="width: 400px; height: 400px;">
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</body>
</html>
(Screenshot is at 25% browser zoom)
UPDATE 3
Current suggestion looks almost good but when the image is changed it doesn't adhere to the set width it seems. Or am I using data-src and src incorrectly?
Upvotes: 0
Views: 3466
Reputation: 90038
Note p-*
classes are not similar to col-*
classes. "p" here stands for "padding" and p-4
cannot replace col-4
.
If you want a 75/25 ratio, use row no-gutters
on parent and col-8
and col-4
on the immediate children. If the image has a native width smaller than the column width, it is normal not to fill the column. To have it fill the column, give it
display:block; /* or d-block class */
width: 100%; /* or w-100 class */
height: auto; /* or h-auto class */
Now the columns will keep the ratio and you can play with margin or padding classes starting from this.
Please note col-*
should be direct children of .row
or .row-fluid
. The gutters system is smart and responsive, but relies on this markup.
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="card flex-md-row box-shadow h-md-250 mx-3 mt-3">
<div class="row w-100 no-gutters">
<div class="col-sm-8 d-flex order-1 order-sm-0">
<div class="card-body d-flex flex-column align-items-start pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in.</p>
<a href="#">Continue reading</a>
</div>
</div>
<div class="col-sm-4">
<img class="d-block w-100 h-auto" data-src="holder.js/200x200" alt="200x200" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22200%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20200%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_1648332c5cf%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A10pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_1648332c5cf%22%3E%3Crect%20width%3D%22200%22%20height%3D%22200%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2273.640625%22%20y%3D%22104.5%22%3E200x200%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
data-holder-rendered="true">
</div>
</div>
</div>
</div>
If you want to use flexbox, to allow the text to grow in the available space - width minus image, which is what flexbox was created for in the first place, use this example:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="card flex-md-row mb-4 box-shadow h-md-250 m-3">
<div class="card-body d-flex flex-column align-items-start flex-grow-1 pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
<a href="#">Continue reading</a>
</div>
<img class="d-none d-md-flex" data-src="holder.js/200x200" alt="200x200" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22200%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20200%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_1648332c5cf%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A10pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_1648332c5cf%22%3E%3Crect%20width%3D%22200%22%20height%3D%22200%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2273.640625%22%20y%3D%22104.5%22%3E200x200%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
data-holder-rendered="true" style="width: 200px; height: 200px;">
</div>
m{*}-*-*
classes set margin
, p{*}-*-*
set padding
, h-*-*
set height
and d-*-*
set display
.
To have two cards on same row, place them inside a row/col structure:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-lg-6">
<div class="card flex-md-row box-shadow h-md-250 mx-3 mt-3">
<div class="card-body d-flex flex-column align-items-start flex-grow-1 pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
<a href="#">Continue reading</a>
</div>
<img class="d-none d-md-block" data-src="holder.js/200x200" alt="200x200" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22200%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20200%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_1648332c5cf%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A10pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_1648332c5cf%22%3E%3Crect%20width%3D%22200%22%20height%3D%22200%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2273.640625%22%20y%3D%22104.5%22%3E200x200%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
data-holder-rendered="true" style="width: 200px; height: 200px;">
</div>
</div>
<div class="col-lg-6">
<div class="card flex-md-row box-shadow h-md-250 mx-3 mt-3">
<div class="card-body d-flex flex-column align-items-start flex-grow-1 pr-md-3">
<h3>
<a href="#">Some title</a>
</h3>
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
<a href="#">Continue reading</a>
</div>
<img class="d-none d-md-block" data-src="holder.js/200x200" alt="200x200" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22200%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20200%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_1648332c5cf%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A10pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_1648332c5cf%22%3E%3Crect%20width%3D%22200%22%20height%3D%22200%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2273.640625%22%20y%3D%22104.5%22%3E200x200%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
data-holder-rendered="true" style="width: 200px; height: 200px;">
</div>
</div>
</div>
</div>
Upvotes: 1