How to set image size dynamically in ReactJS? - html

I have structure like this:
img {
width: auto;
height: 200px;
}
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
}
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
}
.info {
padding: 1em;
border-top: none;
}
<div class='cards'>
<div class="card">
<img src="https://picsum.photos/id/1004/5616/3744" alt="1004" />
<div class="info">
<h3>Greg Rakozy</h3>
<div><small>https://unsplash.com/photos/SSxIGsySh8o</small></div>
</div>
</div>
</div>
on computers with long width image is rendered a little wrong.
how can I fix this so that it displays correctly, i.e. sticks to the '.card' block?

First you need to limit the width of you main container:
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
max-width: 1440px; /* whatever you desire */
margin-left: auto; /* center the container */
margin-right: auto; /* center the container */
}
Then each image should take 100% for it's container:
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
flex: 0 0 25%; /* each card will be 25% width */
}
img {
width: 100%;
height: 200px;
object-fit: cover;
}

Adding those to .card class
width: 100%;
height: auto;
Google how to make image responsive with css, it's not related to React.

.card {
width: 100%;
height: auto;
}

You can either put those images in a div.img-container and set the div width & height like this.
.img-container {
width: 100%;
height: // as you want;
}
and then put that image inside .img-container and set the image width to 100%.
.container {
width: 350px;
height 350px;
border: 2px solid black;
border-radius: 5px;
}
.container .img-container {
width: 100%;
height: 200px;
}
.container .img-container img {
width: 100%;
height: 100%;
}
.container .card-info {
width: 100%;
height: auto;
}
<div class="container">
<div class="img-container">
<img src="https://picsum.photos/200">
</div>
<div class="card-info">
<h5>Title</h5>
<small>Your link here</small>
</div>
</div>
and either set image width 100% and height auto.

Related

Images falling outside container with max height

I have a container that contains 3 images. I want the image on the left to take up 100% of the container height, and the two on the right to take up 50% each and be stacked.
However when I set a max height on my gallery container of 400px, as the images are taller they fall out.
How can I fix this?
HTML
<div class="container">
<div class="gallery">
<div class="main-image">
<img src="https://via.placeholder.com/710x533" />
</div>
<div class="sub-image">
<img src="https://via.placeholder.com/357x266" />
<img src="https://via.placeholder.com/357x266" />
</div>
</div>
</div>
css
.container {
max-width: 1100px;
margin: 0px auto;
padding: 1rem;
}
.gallery {
display: flex;
flex-flow: row wrap;
max-height:400px;
border: 5px solid red;
};
.main-image {
img {
height: 100%;
}
flex: 1 0 66%;
min-height: 500px;
background-size: cover;
background-position: 50% 50%;
}
.sub-image {
flex: 1 0 33%;
flex-direction: column;
justify-content: stretch;
align-items: stretch;
height: 100%;
};
I have the code in codepen
https://codepen.io/roynev123/pen/abWPQyw
You can achieve what you're looking for with the following CSS:
.container {
max-width: 1100px;
margin: 0px auto;
padding: 1rem;
}
.gallery {
border: 5px solid red;
display: flex;
justify-content: space-around;
max-height: 400px;
}
img {
display: block;
height: 100%;
object-fit: cover;
object-position: center;
width: 100%;
}
.main-image {
flex-shrink: 0;
width: 66.67%;
}
.sub-image img {
height: 50%;
}
Basically, .gallery acts a a flexbox container, with .main-image having a static width.

how to align two images using DIV which has row and column spanning image and texts in CSS

I have the below code and the css. The left image comes properly. However, when adding two images to the right side, it is showing one below the other and size bigger. So I have added styles rightImg with object-fit: scale-down; which reduced the size to some extent, but it is not coming side by side like columns. And still there is more space around the images, it is not aligned to left, top.
<div className="col-md-12">
<div className={styles.Content}>
<img src={imgUriL1} />
<div className={styles.rightside}>
<p>
first text1
<br />
second text2
<br />
Third text3
<br />
</p>
<div classname={styles.rightImg1}>
<img src={imgUriR1} />
</div>
<div classname={styles.rightImg2}>
<img src={imgUriR2} />
</div>
</div>
</div>
</div>;
CSS
.Content {
display: flex;
flex-direction: row;
padding: 15px;
background: #ffffff;
img {
min-height: 270px;
max-height: 320px;
width: 320px;
margin-right: 30px;
object-fit: cover;
}
.rightside {
display: flex;
width: 100%;
flex-direction: column;
padding-top: 8px;
p:first-child {
margin: 0;
font-size: 16px;
}
p {
margin: 8px 0;
font-size: 13px;
}
}
}
.rightImg1 {
position: relative;
width: 100%;
object-fit: cover;
&::before {
content: "";
display: block;
padding-top: 40%;
}
img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: scale-down;
}
}
.rightImg2 {
position: relative;
width: 100%;
object-fit: cover;
&::before {
content: "";
display: block;
padding-top: 40%;
}
img {
position: absolute;
top: 0;
left: 150;
width: 100%;
height: 100%;
object-fit: scale-down;
}
}
#media (max-width: 530px) {
.Content {
flex-direction: column;
margin: 15px;
img {
min-height: 0;
width: 100%;
}
.rightside {
padding-bottom: 0;
}
}
}
The display should be like below. The right side images are bigger in size and they should reduce the size to fit into the cells below. Any help highly appreciated.
I have created a snippet using flex width instead of the set widths you had so you can get an idea of the structure. I also added some basic borders, again so you can see the structure. I replaced the images with text and destructured the CSS to default CSS for demonstrative purposes.
If you want to make the right images side by side, make sure to wrap it in a parent container and add display flex to the parent (which automatically puts the children in a row). Using flex to determine widths makes the content flexible and evenly spaced. For example, setting the left content to flex: 1 and the right content to flex: 2 makes it so the right content takes up 2x the space of the left content
.Content {
display: flex;
background: #ffffff;
border: 1px solid black;
}
.leftImg {
display: flex;
flex: 1;
border-right: 1px solid black;
}
.leftImg img {
width: 100%;
height: 100%;
object-fit: cover;
}
.rightside {
display: flex;
flex: 2;
width: 100%;
flex-direction: column;
padding-top: 8px;
}
.rightside p:first-child {
margin: 0;
font-size: 16px;
}
.rightside p {
margin: 8px 0;
font-size: 13px;
border-bottom: 1px solid black;
flex: 1;
}
.allRightImgs {
display: flex;
flex: 1;
}
.rightImg1 {
position: relative;
flex: 1;
border-right: 1px solid black;
object-fit: cover;
}
.rightImg1 img {
width: 100%;
height: 100%;
object-fit: cover;
}
.rightImg2 {
position: relative;
display: flex;
flex: 1;
align-items: flex-end;
object-fit: cover;
}
.rightImg2 img {
width: 100%;
height: 50%;
object-fit: cover;
}
#media (max-width: 530px) {
.Content {
flex-direction: column;
margin: 15px;
}
.leftImg {
border-right: none;
border-bottom: 1px solid black;
}
.rightside {
padding-bottom: 0;
}
}
<div class='col-md-12'>
<div class="Content">
<div class="leftImg"><img src="https://images.unsplash.com/photo-1537151608828-ea2b11777ee8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1878&q=80" /></div>
<div class="rightside">
<p>first text1<br/>
second text2<br/>
Third text3<br/>
</p>
<div class="allRightImgs">
<div class="rightImg1"><img src="https://images.unsplash.com/photo-1523480717984-24cba35ae1ef?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80" /></div>
<div class="rightImg2"><img src="https://images.unsplash.com/photo-1587300003388-59208cc962cb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80" /></div>
</div>
</div>
</div>
</div>

How to make an horizontal scrollbar responsively fit to the div width?

I have an horizontal scrollbar class="filter_list" into a wrapper div class="wrapper". I want this scrollbar to be always 100% of the wrapper div and I want this wrapper div to be responsive.
It's working fine if I only have one item in my filter list but as soon as I put more than the wrapper width size, it's not responsive anymore.
Here are some pictures to illustrate the problem :
Responsive and working fine :
OK
The scrollbar is blocking the width of the wrapper that doesn't shrink to fit the dimension of the window (we can see that the picture of the girl is no longer it's 100% square size):
NOT OK
Here is the code :
body {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper {
width: 800px;
display: flex;
justify-content: flex-start;
flex-direction: column;
align-items: center;
}
.magic_wand {
margin: 15px 0px 20px;
max-width: 50px;
}
.ico_magic_wand {
width: 100%;
height: 100%;
object-fit: contain;
}
.picture_preview {
width: 100%;
margin-bottom: 50px;
height: 100px;
}
.picture_preview img {
width: 100%;
height: 100%;
object-fit: contain;
}
.filter_list {
width: 100%;
background-color: blueviolet;
overflow-x: auto;
white-space: nowrap;
font-size: 0;
}
.filter:last-child {
margin-right: 0px;
}
.filter {
display: inline-block;
height: 150px;
width: 150px;
background-color: blue;
margin-right: 15px;
}
<body>
<div class="wrapper">
<div class="magic_wand">
<img src="img/ico/magic_wand.png" class="ico_magic_wand">
</div>
<div class="picture_preview">
<img src="https://images.unsplash.com/photo-1527514086375-0a7bae9089be">
</div>
<div class="filter_list">
<div class="filter">
</div>
<div class="filter">
</div>
<div class="filter">
</div>
<div class="filter">
</div>
</div>
</div>
</body>
I would like to understand why the div class="filter_list" width won't shrink with it's parent div while reducing the width of the window and how to fix the problem, thanks a lot !
Try this code.. I can't understand your question.. it may ur expectation,, else explain it clearly..
css
.filter {
display: inline-block;
height: 150px;
width: 21.3%;
background-color: blue;
margin-right: 15px;
}
.filter:nth-child(4n+4) {
margin-right: 0px;
}
Please remove ur css code and add this codes.. I think display:flex; is the issue for ur code..
body {
margin:0px;
}
img {
max-width: 100%;
height: auto;
}
.wrapper {
width: 800px;
margin: 0 auto;
text-align: center;
}
.magic_wand {
margin: 15px auto 20px;
max-width: 50px;
}
.picture_preview {
width: 100%;
margin-bottom: 50px;
height: 100px;
}
.picture_preview img {
height: 100%;
}
.filter_list {
width: auto;
background-color: blueviolet;
overflow-x: auto;
white-space: nowrap;
font-size: 0;
}
.filter:last-child {
margin-right: 0px;
}
.filter {
display: inline-block;
height: 150px;
width: 150px;
background-color: blue;
margin-right: 15px;
}
#media only screen and (max-width: 1024px) {
.wrapper {
width: 100%;
}
}

Image in Lightbox hangs outside of container

I build a responsive lightbox with flexbox-styling and the image in this lightbox doesnt fit in the container. (Its working fine if there is enough vertical space).
Here is a image to visualize the problem:
Thats the HTML code:
<div id="dialog-container">
<div id="dialog">
<div id="dialog-content">
<div class="image-wrapper">
<img src="https://spotwild.org/css/images/dist/header/header-07-1600.jpg">
</div>
<div class="thumbs">
Here are the thumbs
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
}
#dialog-container {
position: absolute;
width: 100%;
height: 100%;
background: black;
display: flex;
justify-content: center;
align-items: center;
}
#dialog {
max-width: 80%;
max-height: 80%;
background: white;
padding: 50px;
box-sizing: border-box;
position: relative;
}
#dialog-content {
display: flex;
flex: 1 1 100%;
flex-direction: column;
box-sizing: border-box;
height: 100%;
max-height: 100%;
}
.image-wrapper {
display: flex;
flex: 1 1 auto;
justify-content: center;
align-items: center;
min-width: 0;
min-height: 0;
}
img {
min-width: 0;
min-height: 0;
max-height: 100%;
max-width: 100%;
display: flex;
flex: 0 0 auto;
}
.thumbs {
background: #eee;
padding: 20px;
line-height: 25px;
box-sizing: border-box;
}
And here is the corresponding jsfiddle:
https://jsfiddle.net/ppkzt7m6/1/
Does somebody has a solution and an explanation why this happens?
The important part is to define the height on all the parent containers for the <img> element to make the percentage heights to work properly.
And use object-fit: cover; on the <img> element. Note, the current IE11 and Edge don't support it, works fine on all other modern browsers though, see the support tables.
jsFiddle
You can also do it without flexbox, the key is to set the image caption to absolute position
jsFiddle
I suggest to use background image if you do need to support IE, example:
jsFiddle
<div class="image-wrapper" style="background: url('https://spotwild.org/css/images/dist/header/header-07-1600.jpg') center / cover;">
add height in #dialog
#dialog {
background: white none repeat scroll 0 0;
box-sizing: border-box;
height: 100%;
max-height: 80%;
max-width: 80%;
padding: 50px;
position: relative;
}
USE THIS.
img {
display: block;
height: 100%;
max-height: 100%;
max-width: 100%;
min-height: 0;
min-width: 0;
width: 100%;
}
#dialog {
background: #ffffff none repeat scroll 0 0;
box-sizing: border-box;
height: 100%;
max-height: 80%;
max-width: 80%;
padding: 50px;
position: relative;
}

vertical / horizontal alignment without top left transform on element with uknown width / height

How can i vertically / horizontally align this item with auto generated width / height without using the classic top:50% left:50% transform: translate(-50%,-50%)
css setup without alignments
#container {
width: 200px;
height: 100px;
background-color: lightblue;
}
#myImage {
width: auto;
height: auto;
max-height: 100px;
max-width: 200px;
}
I'm not entirely sure what you're trying to achieve but this is the image with no fixed height or width set in the CSS. I assume from your OP that you want is centered as such - so if the image dimensions changed it will stay centralized along both axis.
Note the only limitations you face are those older browsers that don't support flex. If that's a problem you can use display: table; and display: table-cell; to good effect.
http://jsfiddle.net/dLLan/29/
css:
#container {
width: 250px;
max-height: 250px;
height: 250px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
}
#myImage {
//removed
}
You can use something like that:
#helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
#container {
width: 200px;
height: 100px;
background-color: lightblue;
text-align:center;
}
#myImage {
width: auto;
height: auto;
max-height: 100px;
max-width: 200px;
vertical-align: middle;
}
<div id="container">
<div id="helper"></div>
<img id="myImage" src="http://s3.amazonaws.com/hirethings/photo/7250/images/thumbnail.jpg?1274508483" />
</div>
Flexbox should be fine here depending on your Browser support requirements.
There is no need to position the image...the flex will do that.
.container {
width: 200px;
height: 100px;
background-color: lightblue;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 1em;
}
.large {
width: 300px;
height: 200px;
}
.myImage {
width: auto;
height: auto;
}
<div class='container'>
<img class='myImage' src='http://s3.amazonaws.com/hirethings/photo/7250/images/thumbnail.jpg?1274508483' />
</div>
<div class='container large'>
<img class='myImage' src='http://lorempixel.com/image_output/food-q-c-250-160-5.jpg' />
</div>