Flexbox content-responsive grid-type row - html

I have the following html:
<div class="row">
<div class="item">
<img src="image1" />
</div>
<div class="item">
<video>
<source src="video1" />
</video>
</div>
</div>
I'd like it for row to automatically adjust its height and for the subchildren of row to automatically adjust their widths and heights such that the aspect ratios of the content under the img and video tags remains the same, that row will have a width of 100%, and that there is no empty space within row.
In other words, I'm trying to make a content-responsive grid row with flexbox. Like so:
Except that rows and columns automatically change size to preserve image aspect ratios without creating gaps.
My current CSS attempt is as follows:
.row {
width: 100%;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.item {
position: relative;
min-height: 100%;
}
.item video {
max-width: 100%;
max-height: 100%;
}
.item img {
max-width: 100%;
max-height: 100%;
}

There seems to be no effective way to do this with flexbox. The only example I could find of this working properly was on a wix website, which works by doing the following:
All items are positioned absolutely and have width and height set manually.
Initial width and height of all items are zero to prevent initial rendering problems.
A function is defined which manually calculates the correct position, width, and height of all items.
This function is called on every resize and immediately after page load.

Related

containers with images of different sizes; container width won't go past the image width

I have divs with images in them stacked horizontally side by side of each other. Images are of different widths and heights.
If I make the container width's smaller than the images, all the divs are uniform nicely.
But if I make the width of the container bigger than the images, the div/container width just seems to stop at the size of the image and refuse to get any bigger. What am I doing wrong or am I misunderstanding anything? I'm still learning my HTML and CSS thank you
PS - I don't want to use background: url(...) because I need my image URLs to be dynamic. Unless this is the only way?
.test__container {
width: 800px;
}
.test__img {
width: 100%;
}
<div class="test__container">
<img class="test__img" src='https://via.placeholder.com/350x150/' />
<h1 class="test__name">Davy Crocket</h1>
</div>
It is possible they are inside a flex container (that has display:flex). That makes it treat width property of children differently.
When you create a flex container (display: flex or display: inline-flex), it comes with several default settings. Among them are:... read more
(specifically it forces items to stay on one line [no matter the count])
Give the images a width of 100%. This will make them as wide as their parent, not as wide as their native size.
&__img {
width: 100%;
}
Update (based on added context): if the parent container has a display property of flex, one has to set min-width to 100% on the image. Note: flex-wrap: wrap should also be set on parent, to prevent siblings from creating a horizontal scrollbar on parent.
An alternative solution is to give the image flex-basis of 100% and flex-shrink of 0.
However, flex calculation is dependent on several other CSS attributes of the image as well as on CSS attributes and content of siblings and of parent elements. The safest option for flex remains min-width, as it trumps the result of flex calculation (basically the flex calculation starts from the given min-width and distributes the remaining space, if any, to the flexible siblings).
as you can see from the snippet below wrapping your code in a flexbox container doesn't change anything by itself. There most be either additional css or something else going on.
I edited your original post. You will get help faster if you post snippets here instead of providing a link to js fiddle.
.test__container {
width: 800px;
}
.test__img {
width: 100%;
}
}
#container{
display:flex;}
<div id='container'>
<div class="test__container">
<img class="test__img" src='https://via.placeholder.com/350x150/' />
<h1 class="test__name">Davy Crocket</h1>
</div>
</div>
<br><br>
<div class="test__container">
<img class="test__img" src='https://via.placeholder.com/350x150/' />
<h1 class="test__name">Davy Crocket</h1>
</div>
Try this.
<html>
<head>
<style>
.page {
width: 500px;
}
.container {
float: left;
width: 50%;
}
img {
float: left;
width: 100%;
height: 500px;
object-fit: cover;
}
</style>
</head>
<body>
<div class="page">
<div class="container">
<img src="https://news.harvard.edu/wp-content/uploads/2022/02/20220225_wondering_dog-2048x1366.jpg" alt="" />
</div>
<div class="container">
<img src="https://www.princeton.edu/sites/default/files/styles/full_2x/public/images/2022/02/KOA_Nassau_2697x1517.jpg?itok=Hy5eTACi" alt="" />
</div>
</div>
</body>
</html>

Remove whitespace from left and right of resized image

I have a fixed height container in which I'd like to display an image and a caption. The requirement is that the image should scale to fit the available height while maintaining aspect ratio. The caption could be of variable height.
Below is a JS fiddle of what I have so far. I am unable to get the whitespace on the right and left side of the images to disappear (I've added a border to the img tag to show what I mean). Is it possible to remove the excess whitespace?
https://jsfiddle.net/peL1a7vj/1/
HTML
<div class="container">
<div class="item">
<img class="image" src="https://www.stockvault.net/data/2009/07/14/109489/thumb16.jpg"/>
<div class="text">
Caption 1
</div>
</div>
<div class="item">
<img class="image" src="https://images.unsplash.com/photo-1591870509558-26b7eee6d549"/>
<div class="text">
Caption 2
</div>
</div>
</div>
CSS
.container {
height: 200px;
flex: 1 1 auto;
display: flex;
flex-flow: row nowrap;
overflow-x: auto;
overflow-y: hidden;
}
.item {
display: flex;
flex-direction: column;
}
.text {
flex: 0 0 auto;
}
.image {
flex: 1 1 auto;
overflow: hidden;
object-fit: contain;
max-height: 100%;
width: auto;
border: solid;
}
This is similar to CSS object-fit: contain; is keeping original image width in layout but I was unable to fix it using the methods suggested in the accepted answer
"The requirement is that the image should scale to fit the available height while maintaining aspect ratio" - that's what it does in your code! If you would stretch the width ("to fill the whitespace"), either you would loose the aspect ratio and get a distorteded image, or - if the ratio is kept - the top and bottom of the image would be cut off since it would grow due to the extended width.
There is no other solution: Either there is some whitespace, or some of the image is cut off, or the image is distorted. Unless you change the dimensions of the parent container:
The only situation where your wish could be fulfilled: When the parent of the image has the same height/width proportion as the original image. So you would have to set height and width for the container accoring to the proportions of the image.

Four Column Layout that Converts to Single Column without Media Query

I'm trying to create a four column layout where each column grows and shrinks with the size of the window, but each column has a minimum width and when the window is too small for all four columns to fit in a single row, it transitions to a single column with each section taking up the full width.
I've been unable to do this with either flex-box or CSS grid. And I'd like to do this without a media query. Using a media query would solve the issue pretty easily, but I don't like them!
.col {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.section {
margin: 10px 0px 0px 10px;
min-width: 250px;
height: 400px;
background-color: gray;
flex: auto;
}
<div class="col">
<div class="section">
</div>
<div class="section">
</div>
<div class="section">
</div>
<div class="section">
</div>
</div>
And a codepen: https://codepen.io/WriterState/pen/oRKxMj
Media queries are great, but they are not always a viable substitute for container queries (which sadly do not exist).
A horizontal to vertical layout switch can be achieved using CSS calc when you know how many columns you will have.
.child{
min-width: 25%; /* 100% divide number columns */
max-width: 100%;
width: calc((50rem - 100%) * 1000); /* Replace 50rem with size of .parent when you want switch to happen */
}
The width is calculated as your desired breakpoint minus the width of the parent container. This either generates a negative number and the min-width is applied, or a large number in which case the max-width takes over.
If you are using flex-box then width can also be flex-basis.
Source: https://www.sitepoint.com/responsive-css-patterns-without-media-queries/

How can I make div container with 3 images auto resizable?

I am trying to achieve similar effect as is on this page: http://ketrawars.com
When you try to resize a browser window, all images resize along with it. I can get that working if my div contains one image to which I set width 100%. However, I have a problem when I need to put 3 images one next to another.
My code:
<div class="content">
<img src="images/main_01.png" alt="" />
</div>
<div class="content">
<img src="images/main_02.png" alt="" />
<img src="images/main_03.png" alt="" />
<img src="images/main_04.png" alt="" />
</div>
CSS:
.content {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: relative;
top: 0;
left: 0;
}
This is what it does:
And this is what is desired:
With the option to write text on the middle image (second one).
If you have three equally sized images, then set each of their widths to 32%:
.content img {
width:32%;
}
img elements are displayed as inline, by default. This means that the browser will add inline space between them, causing line breaks- you must subtract a percentage or two to compensate for this space.
I recommend displaying the images as blocks and then floating them to remove the inline space.
.content img {
display:block;
margin:0;
padding:0;
float:left;
width:33%;
}
If your images aren't equally sized, simply set their percentages so that all of the elements' widths add to 100.
Another good way to ensure that things will resize with the screen to use viewport units: vw and vh. They're defined as 1/100 the width and height of the viewport, respectively. Only Gecko based browsers will update them dynamically, however.
Codepen

how to make dynamically loaded image with different width and height responsive in css

I load images dynamically into my webpage and make them responsive:
<div id="images"><img id="dynamic_img" src=""/></div>
css:
#images{
height: 80%;
width: 30%
}
img{
max-height: auto
max-width:100%
}
Problem now is, that the images have different heights and widths. Now this works when the width of an image is longer than its height but not the other way around. (in this case max-height would be 100% and max-width: auto
Is this possible to switch these two values according to the image loaded in CSS or do I need to use JS for that?
Thanks in advance
Here you have an example where images are fitted horizontal and vertically.
I used
img {
max-width: 100%;
max-height: 100%;
}
Here you are: https://jsfiddle.net/jormaechea/j219ucnc/1/
Update
The key to achieve this is to set
html, body {
height: 100%;
}
<div id="images"><img class="img" src="" alt="" /></div>
.img
{
width=100%;
}
your div should have width=100% for example.
don't use max-width
you can set the #images div also a with of 80% - it will be responsive and the image refits to the divs width