This question already has answers here:
Fit div size to background image
(3 answers)
Closed 2 years ago.
Fiddle:
https://codepen.io/0akd0adk0asdk09asd/pen/QWbaLZa
When the browser is resized, the images are being correctly resized, and the width is changing correctly, but the height is not changing at all. If I remove height: 375px; the box collapses. How do I make the height responsive like the width currently is?
<div class="container">
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">c</div>
<div class="bc">g</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
</div>
CSS
.container {
width: 80%;
margin: 1rem auto;
background: #f0f0f0;
min-height: 375px;
padding: 0.1rem;
border-radius: 0.1rem;
display: flex;
justify-content: space-between;
flex-wrap: row;
height: 100%;
}
.card {
flex: 1 1 auto;
flex-flow: row nowrap;
background: url("https://i.imgur.com/q8WyDB6.png") no-repeat;
background-size: 100%;
margin: 0.2rem;
display: flex;
flex-direction: column;
flex-shrink: 1;
max-width: 148px;
max-height: 375px;
min-height: 100%;
color: #fff;
text-shadow: 0 0 4px rgba(0, 0, 0, .5), 0 0 1px rgba(0, 0, 0, 1);
font-family: "Segoe UI";
}
.card::after {
content: "";
}
.t {
flex: 0 0 auto;
display: flex;
}
.tl {
min-height: 30px;
background: rgba(255, 0, 0, .1);
flex: 0 0 30%;
}
.tr {
flex: 1 1 100%;
background: rgba(0, 0, 255, .1);
font-weight: 600;
font-size: 1.0rem;
text-align: center;
align-self: center;
}
.mc {
height: 100%;
flex: 1 1 100%;
background: rgba(0, 255, 0, .1);
padding: 0.5rem 0.5rem;
}
.bc {
flex: 0 0 auto;
background: rgba(255, 255, 0, .1);
padding: 1.0rem 0.5rem;
}
Add align-items: flex-start to to the container. By default it's strech.
stretch (default): stretch to fill the container (still respect min-width/max-width)
flex-start / start / self-start: items are placed at the start of the cross axis.
Related
the problem that I'm having is that when I resize the browser the left margins of both the and my card divs aren't aligning. Is there some sort of css property or maybe some JS that will make them stay aligned when resizing?
here's my sandbox
https://codesandbox.io/s/stupefied-christian-tosys?file=/src/styles.css:0-
Add the .filter margin margin: 0 80px; to your .row. and then remove justify-content: center;. It will loose the centered. But will be align on the left. To aviod the overflow from the body, you can set width: calc(100% - 160px);to your .row:
/* ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
After if you want to keep your card more center, either you use justify-content: space-between; on row, either you use margin: 10px auto; on your card.
DEMO:
body {
margin: 0 auto;
max-width: 1200px;
}
.row {
display: flex;
justify-content: center;
width: 100%;
flex-wrap: wrap;
/*ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
}
.filter {
display: flex;
margin: 0 80px;
}
.filter select {
display: inline;
width: 15rem;
margin: 10px;
border-radius: 5px;
}
.card {
width: 15rem;
margin: 10px;
border-radius: 5px;
border: 1px solid rgba(0, 0, 0, 0.125);
}
.info {
padding: 15px;
}
.img-container {
padding-top: 0;
position: relative;
}
.card img {
width: 100%;
object-fit: cover;
}
.price {
font-weight: bold;
}
<div id="root">
<div class="App">
<div class="filter-container">
<div class="product-listing-wrap">
<div class="filter">
<select class="custom-select" id="priceGroup">
<option value="1">Under $50</option>
<option value="2">$50 to $100</option>
<option value="3">$100 to $250</option>
<option value="4">Over $250</option>
</select>
</div>
<div class="row" style="
margin: 0 80px;
padding: 10px 0;
width: calc(100% - 160px);
">
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="1"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="2"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="3"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="4"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="5"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="6"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="7"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="8"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
try it:
.row {
display: flex;
justify-content: space-between;
margin: 0 80px;
max-width: 1040px;
flex-flow: wrap;
}
I am trying to get 3 columns in a row and be the same height.
I am using the HTML/CSS below. I can't figure out why the boxes arent the same height.
.container {
text-align: center;
display: block;
width: 100%;
}
.grid {
width: 100%;
height: 100%;
}
.row {
display: flex;
height: 100%;
flex-direction: row;
}
.col {
background: #444;
padding: 2em;
flex: 1;
height: 100%;
border: 1px solid blue;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
How can I make the boxes all the same height. thanks
Simply, remove the height: 100% from .col.
flex: 1; will do the job.
.row {
display: flex;
}
.col {
flex: 1;
background: #444;
padding: 2em;
border: 1px solid blue;
text-align: center;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Why is a flex item limited to parent size?
(1 answer)
Closed 2 years ago.
I am creating a layout that contains three parts: sidebar (left), page (center), and summary (right). The left will remain fixed at the width of its inner content when the viewport width decreases, which is the expected behavior.
However, once I add overflow-y: auto to the left container, it loses respect for its inner content width, and completely shrinks along with the viewport width.
How does overflow-y: auto affect an item's flex-shrink behavior? How do I apply an overflow-y while allowing it to continue respecting its inner content width?
.root {
display: flex;
}
.root .sidebar {
border: 8px solid red;
flex: 2;
background: #f3f3f5;
height: 100vh;
display: flex;
justify-content: flex-end;
padding-top: 80px;
position: sticky;
top: 0;
/*
enable this and shrink the viewport
notice how the sidebar shrinks completely
and no longer respects the inner content's fixed width
*/
/*overflow-y: auto;*/
}
.root .page {
border: 8px solid green;
flex: 8;
max-width: 810px;
padding: 80px 80px 160px 80px;
}
#media only screen and (max-width: 1024px) {
.root .page {
padding-right: 40px;
padding-left: 40px;
}
}
.root .summary {
border: 8px solid blue;
flex: 2;
padding-top: 160px;
position: sticky;
top: 0;
align-self: flex-start;
}
.root .actions {
border: 8px solid orange;
position: fixed;
bottom: 0;
padding: 10px;
width: 100%;
background: white;
display: flex;
justify-content: center;
align-items: center;
}
.root .actions .inner {
display: flex;
justify-content: space-between;
align-items: center;
flex-basis: 650px;
}
.root .stepper {
border: 4px solid #333;
flex-basis: 320px;
}
.root .stepper .step {
display: flex;
align-items: center;
margin: 0px 70px 40px 40px;
white-space: nowrap;
}
.root .stepper .step .circle {
background: #ffc2d4;
border-radius: 50%;
width: 28px;
height: 28px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
}
.root .form-stuff {
margin-bottom: 80px;
}
.root .card {
border: 4px solid #333;
align-self: flex-start;
border-radius: 8px;
padding: 10px;
max-width: 280px;
min-width: 180px;
white-space: nowrap;
}
.root .card .title {
margin: 20px 0;
}
.root .card .subtitle {
margin: 20px 0;
font-size: 14px;
}
.root .button {
background: #ffc2d4;
padding: 10px 20px;
}
* {
box-sizing: border-box;
}
<div class="root">
<div class="sidebar">
<div class="stepper">
<div class="step">
<div class="circle">
1
</div>
<div class="title">Step 1</div>
</div>
<div class="step">
<div class="circle">
2
</div>
<div class="title">Step 2</div>
</div>
<div class="step">
<div class="circle">
3
</div>
<div class="title">Step 3</div>
</div>
<div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div>
</div>
</div>
<div class="page">
<div class="form">
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
</div>
</div>
<div class="summary">
<div class="card">
<div class="title">Summary</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
</div>
</div>
<div class="actions">
<div class="inner">
<div class="button">Back</div>
<div class="button">Next</div>
</div>
</div>
</div>
Fiddle:
https://codepen.io/0akd0adk0asdk09asd/pen/QWbaLZa
When the browser is resized, the images are being correctly resized, and the width of the container is changing correctly, but the height of the container is not changing. If I remove height: 375px; the box collapses. How do I make the height responsive like the width currently is?
Basically I need to dynamically set the height: to the automatic width * aspect ratio. The images are always the same size so it's not an issue to use a fixed aspect ratio. I would rather use CSS over javascript to do this if possible.
Alternatively if there is another way to do it based on the background-image that would work too.
This is not a duplicate of Fit div size to background image. Do not close this as a duplicate of that question. That question assumes a static background image size.
<div class="container">
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">c</div>
<div class="bc">g</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
<div class="card">
<div class="t">
<div class="tl"></div>
<div class="tr">Servant</div>
</div>
<div class="mc">2</div>
<div class="bc">3</div>
</div>
</div>
CSS
.container {
width: 80%;
margin: 1rem auto;
background: #f0f0f0;
min-height: 375px;
padding: 0.1rem;
border-radius: 0.1rem;
display: flex;
justify-content: space-between;
flex-wrap: row;
height: 100%;
}
.card {
flex: 1 1 auto;
flex-flow: row nowrap;
background: url("https://i.imgur.com/q8WyDB6.png") no-repeat;
background-size: 100%;
margin: 0.2rem;
display: flex;
flex-direction: column;
flex-shrink: 1;
max-width: 148px;
max-height: 375px;
min-height: 100%;
color: #fff;
text-shadow: 0 0 4px rgba(0, 0, 0, .5), 0 0 1px rgba(0, 0, 0, 1);
font-family: "Segoe UI";
}
.card::after {
content: "";
}
.t {
flex: 0 0 auto;
display: flex;
}
.tl {
min-height: 30px;
background: rgba(255, 0, 0, .1);
flex: 0 0 30%;
}
.tr {
flex: 1 1 100%;
background: rgba(0, 0, 255, .1);
font-weight: 600;
font-size: 1.0rem;
text-align: center;
align-self: center;
}
.mc {
height: 100%;
flex: 1 1 100%;
background: rgba(0, 255, 0, .1);
padding: 0.5rem 0.5rem;
}
.bc {
flex: 0 0 auto;
background: rgba(255, 255, 0, .1);
padding: 1.0rem 0.5rem;
}
OK, I accomplished it using Javascript:
https://codepen.io/0akd0adk0asdk09asd/pen/QWbaLZa
A css-only approach would be good, but not sure this is possible.
$(document).ready(function () {
updateContainer();
$(window).resize(function() {
updateContainer();
});
});
function updateContainer() {
var containerWidth = $('.t').width() * (375/148);
$('#container').css({
height: containerWidth
});
}
I want to create responsive square grids in ionic, just like shown in the image above.
But instead, I get something like showing in the image below. I searched the web I can't find any solution that does not involve using images which I am not trying to use.
You can do it in pure CSS :
* {
box-sizing: border-box;
}
.square-container {
padding: 8px;
}
.square {
width: calc(100% / 5);
float: left;
position: relative;
padding-bottom: calc(100% / 5);
}
.square .content {
width: calc(100% - 16px);
height: calc(100% - 16px);
margin: 8px;
padding: 16px;
position: absolute;
color: white;
background-color: #0095ff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
}
<div class="square-container">
<div class="square">
<div class="content">1</div>
</div>
<div class="square">
<div class="content">2</div>
</div>
<div class="square">
<div class="content">3</div>
</div>
<div class="square">
<div class="content">4</div>
</div>
<div class="square">
<div class="content">5</div>
</div>
<div class="square">
<div class="content">6</div>
</div>
<div class="square">
<div class="content">7</div>
</div>
<div class="square">
<div class="content">8</div>
</div>
<div class="square">
<div class="content">9</div>
</div>
<div class="square">
<div class="content">10</div>
</div>
<div class="square">
<div class="content">11</div>
</div>
<div class="square">
<div class="content">12</div>
</div>
</div>