Resizing images in a flex container - css

Im trying to have 3 images side by side in a flex container but the images are much too large and its stretching the page and creating a scroll bar.Tried a tip to use flex wrap but that didn't work.Should I just resize in photoshop?.
<section class="main-content">
<div class="image">
<img src="img/devil-ivy-can.jpg">
</div>
<div class="image">
<img src="img/krimson-princess-can.jpg">
</div>
<div class="image">
<img src="img/spiderwort-can.jpg">
</div>
</section>
.main-content{
display: flex;
}
div{
width:100%;
padding:10px;
margin:10px;
}

Try this
.container {
display: flex;
flex-wrap: wrap;
background-color: grey;
}
img {
width: 100%;
}
div {
flex: 1;
padding: 10px;
margin: 10px;
}
<section class="container">
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
<div class="image">
<img src="https://atlas-content1-cdn.pixelsquid.com/assets_v2/127/1273408777629996108/jpeg-600/G13.jpg">
</div>
</section>

hello i have try to solve your problem you can try this in your CSS code.
.main-content{
display: flex;
gap:10px
}
div{
width:100%;
gap:10px;
}div.image img{
width:100%;
object-fit:cover;
}
<section class="main-content">
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
<div class="image">
<img src="https://www.imgonline.com.ua/examples/random-pixels-wallpaper.jpg">
</div>
</section>
in your css code at div selector i prefer using gap to give space between items than using padding and a margin
if you give the img width 100% it will fill the div.image and set object-fit with value cover. The CSS object-fit property is used to specify how an or should be resized to fit its container.

Related

Removing padding on last-child results in different sized element

I have an image gallery with horizontal thumbnails below the featured image. I am adding padding-right:10px; to all but the last-child in an effort to equally space these elements. This works as intended on all but the last thumbnail, where it's slightly larger than the rest.
It makes sense why this is happening, but I am not sure how to fix it. This was the solution recommended on numerous other Stack Overflow posts. Any workaround would be appreciated.
<style>
.gallery-grid {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 1fr;
column-gap: 10px;
row-gap: 30px;
}
.image-thumb {
float: left;
width: 20%;
padding-right: 10px;
box-sizing: border-box;
}
.image-thumb:last-child {
padding-right: 0;
}
</style>
<div class="gallery-grid">
<img id="featured-image" src="featured.jpg" style="width:100%">
<div class="image-thumb">
<img src="featured_thumb.jpg" alt="Mountains" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Nature" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Snow" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
</div>
<div class="image-thumb">
<img src="thumb.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
</div>
</div>
If you want to stick using float insteed of flex you could use margin insteed of padding. Then you can calculate the total space taken by the margin, divide it between the number of elements and rest it to the width using calc
(margin-right:10px * 4) / 5 = 8
Example:
* {box-sizing:border-box;}
.box {
height:100px;
background-color:blue;
float:left;
width:calc(20% - 8px);
margin-right:10px;
}
.box:nth-child(5) {margin-right:0;}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS question: Flexible width to make it reflow ready

Could anyone please help me with this, How can I make the parent container flexible and make it reflow ready?
.container{
width: 350px;
border: 1px solid red;
}
.item{
margin-top:2px;
display: flex;
}
.line{
flex-grow:1;
}
<div class="container">
<div class="item">
<div class="line"><span>xyx</span></div>
<span>10 USD</span>
</div>
<div class="item">
<div class="line"><span>q</span></div>
<span>* 2</span>
</div>
<div class="item">
<div class="line"><span>total</span></div>
<span>20 USD</span>
</div>
</div>
Since you are referring reflow as browser width change and your default width for the container is 350px
just your change width: 350px to max-width: 350px. Your container is now responsive for smaller browser width
You can play around around here and change the browser here:
.container{
max-width: 350px;
border: 1px solid red;
}
.item{
margin-top:2px;
display: flex;
}
.line{
flex-grow:1;
}
<div class="container">
<div class="item">
<div class="line"><span>xyx</span></div>
<span>10 USD</span>
</div>
<div class="item">
<div class="line"><span>q</span></div>
<span>* 2</span>
</div>
<div class="item">
<div class="line"><span>total</span></div>
<span>20 USD</span>
</div>
</div>

Flex with a static width container

ISSUE:
When I assign a static width to the flex'a parent container, the flex functionality stops working.
MY GOAL:
To have a functional flex layout within a parent container that has a static width.
RIGHT NOW:
This flex layout works perfectly using a "100%" parent container width. But the "flex" stops working when I assign a static width.
Meaning...
As long as the current 100% width equals the window size, the flex functions correctly ... as it adjusts to the window's width.
More info:
This flex code will be used within a themed page that only allows for HTML & JS/JQ.
This section will be within a "middle" section of the template/page.
I'm not using a CMS (i.e.: Wordpress)
The template's CSS has the "page" class as 100% width... something that I cannot change
The flex section needs to be centered horizontally in the page
JSFiddle:
The default CSS in the JSFiddle below is using the "100%" width for the container = Flex works fine. I have commented the CSS class parameter (mainwrapper) that causes the flex to stop working.
JSFiddle Link:JSFiddle
Use max-width:980px you can remove width:100% because i assigned maximum width 980px.
If i missed some thing please check fiddle code
Working fiddle
.mainwrapper {
width: 100%;
/* Here's the problem:*/
/* Asigning a static width breaks flex */
max-width: 980px;
text-align: center;
}
.page {
width: 100%;
margin: 0 auto;
}
.mainwrapper {
width: 100%;
/* Here's the problem:*/
/* Asigning a static width breaks flex */
max-width: 980px;
text-align: center;
}
/* Product Layout */
.section-holder {
display: flex;
flex-flow: row wrap;
}
.section-item {
display: flex;
flex-direction: column;
padding: 2%;
flex: 1 24%;
background-color: #FFF;
box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.5);
}
.section-image img {
width: 100%;
}
.section-info {
margin-top: auto;
padding-top: 20px;
text-align: center;
}
#media ( max-width: 768px) {
.section-item {
flex: 1 21%;
}
.section-holder .section-item {
flex: 2 46%;
}
}
#media ( max-width: 680px) {
.section-item {
flex: 1 46%;
}
}
#media ( max-width: 480px) {
.section-item {
flex: 1 100%;
}
.section-holder .section-item {
flex: 2 100%;
}
}
<div align="center" class="page">
<div class="mainwrapper">
<div class="section-holder">
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
<div class="section-item">
<div class="section-image">
<img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
</div>
<div class="section-info">
<h5>Winter Jacket</h5>
<h6>$99.99</h6>
</div>
</div>
</div>
</div>
</div>
I think you just need to change the display for the class page to flex from block. If I am understanding your question correctly.
.page {
width: 100%;
margin: 0 auto;
}
to
.page {
display: flex;
width: 100%;
margin: 0 auto;
}

How to to center anchor and img to be responsive

<div class="testt">
<div class="test">
<img src="4.jpg">
test
</div>
<div class="test">
<img src="5.jpg">
test
</div>
<div class="test">
<img src="7.jpg">
test
</div>
<div class="test">
<img src="6.jpg">
test
</div>
If you can help me for the pictures to be one line and the anchor beneath them nicely centered and also to be responsive for all devices
You can use display: flex; on the parent and that will put everything on one line, give it a relative width so that it scales with the viewport, assign text-align: center; to the .test divs to center the contents, and give the img a max-width: 100% so that it scales with the parent size.
.testt {
display: flex;
width: 90%;
margin: auto;
}
.test {
text-align: center;
}
.test img {
max-width: 100%;
display: block;
}
<div class="testt">
<div class="test">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
test
</div>
<div class="test">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
test
</div>
<div class="test">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
test
</div>
</div>

How to align an image dead center with bootstrap

I'm using the bootstrap framework and trying to get an image centered horizontally without success..
I've tried various techniques such as splitting the the 12 grid system in 3 equal blocks e.g
<div class="container">
<div class="row">
<div class="span4"></div>
<div class="span4"><img src="logo.png" /></div>
<div class="span4"></div>
</div>
</div>
What's the easiest method to get an image centered relative to the page?
Twitter Bootstrap v3.0.3 has a class: center-block
Center content blocks
Set an element to display: block and center via margin. Available as a mixin and class.
Just need to add a class .center-block in the img tag, looks like this
<div class="container">
<div class="row">
<div class="span4"></div>
<div class="span4"><img class="center-block" src="logo.png" /></div>
<div class="span4"></div>
</div>
</div>
In Bootstrap already has css style call .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
You can see a sample from here
The correct way of doing this is
<div class="row text-center">
<img ...>
</div>
Update 2018
The center-block class no longer exists in Bootstrap 4. To center an image in Bootstrap 4, use mx-auto d-block...
<img src="..." class="mx-auto d-block"/>
Add 'center-block' to image as class - no additional css needed
<img src="images/default.jpg" class="center-block img-responsive"/>
Twitter bootstrap has a class that centers: .pagination-centered
It worked for me to do:
<div class="row-fluid">
<div class="span12 pagination-centered"><img src="header.png" alt="header" /></div>
</div>
The css for the class is:
.pagination-centered {
text-align: center;
}
You could use the following. It supports Bootstrap 3.x above.
<img src="..." alt="..." class="img-responsive center-block" />
Assuming there is nothing else alongside the image, the best way is to use text-align: center in the img parent:
.row .span4 {
text-align: center;
}
Edit
As mentioned in the other answers, you can add the bootstrap CSS class .text-center to the parent element. This does exactly the same thing and is available in both v2.3.3 and v3
Works for me. try using center-block
<div class="container row text-center">
<div class="row">
<div class="span4"></div>
<div class="span4"><img class="center-block" src="logo.png" /></div>
<div class="span4"></div>
</div>
</div>
I need the same and here I found the solution:
https://stackoverflow.com/a/15084576/1140407
<div class="container">
<div class="row">
<div class="span9 centred">
This div will be centred.
</div>
</div>
</div>
and to CSS:
[class*="span"].centred {
margin-left: auto;
margin-right: auto;
float: none;
}
You can use margin property with the bootstrap img class.
.name-of-class .img-responsive { margin: 0 auto; }
I am using justify-content-center class to a row within a container. Works well with Bootstrap 4.
<div class="container-fluid">
<div class="row justify-content-center">
<img src="logo.png" />
</div>
</div>
Seems we could use a new HTML5 feature if the browser version is not a problem:
in HTML files :
<div class="centerBloc">
I will be in the center
</div>
And in CSS file, we write:
body .centerBloc {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
And so the div could be perfectly center in browser.
Just use following bootstrap class to fix it
<img class="mx-auto d-block" src="../path/to/.." alt="Image">
You should use
<div class="row fluid-img">
<img class="col-lg-12 col-xs-12" src="src.png">
</div>
.fluid-img {
margin: 60px auto;
}
#media( min-width: 768px ){
.fluid-img {
max-width: 768px;
}
}
#media screen and (min-width: 768px) {
.fluid-img {
padding-left: 0;
padding-right: 0;
}
}
I created this and added to Site.css.
.img-responsive-center {
display: block;
height: auto;
max-width: 100%;
margin-left:auto;
margin-right:auto;
}
A lot of the above options didn't work for me.
However, this worked!
<div class="container">
<div class="row">
<div class="col-sm-12" style="text-align: center;"><img class="center-block" src="img_path" /></div>
</div>
</div>
IN Bootstarp 5.0
Use
.mx-auto .d-block
You could change the CSS of the previous solution as below:
.container, .row { text-align: center; }
This should center all the elements in the parent div as well.

Resources