I am building a webpage for homework purposes and I am struggling to fit in four photos with different dimensions in a grid layout. To be more specific I have to make that grid layout be responsive as a 4-grid layout on Desktop view, 2-grid layout on Tablet view, and Mobile view. I have tried a lot of advice from different articles but none of them seems to be working.
CSS code:
.services{
float: left;
width: 25%;
height:100vh;
max-width:100%;
object-fit:cover;
}
#media only screen and (max-width: 1180px) {
.services{
width: 50%;
}
}
HTML code:
<div>
<div class="services">
<img src="./Photos/services-1.jpg"></img>
</div>
<div class="services">
<img src="./Photos/services-2.jpg"></img>
</div>
<div class="services">
<img src="./Photos/services-3.jpg"></img>
</div>
<div class="services">
<img src="./Photos/services-4.jpg"></img>
</div>
</div>
Current look:
I want photos to be in a line for each occasion with certain dimensions.
Any thoughts?
Thanks for submitting your question! Instead of using markup for placing your images, I would suggest using with a background-image. Why? With background-image you can use 'background-size: cover' wich make the image cover the whole with a custom height, so the columns are all equal.
Trying to achieve this with regular tags needs a some sort of resizer or the images need to be the same width and height already.
Below my code! Tip: You don't have to close the tag, you can use it without the closing image tag.
HTML
<div>
<div class="services">
<div class="image-box" style="background-image: url('https://source.unsplash.com/random');"></div>
</div>
<div class="services">
<div class="image-box" style="background-image: url('https://source.unsplash.com/random');"></div>
</div>
<div class="services">
<div class="image-box" style="background-image: url('https://source.unsplash.com/random');"></div>
</div>
<div class="services">
<div class="image-box" style="background-image: url('https://source.unsplash.com/random');"></div>
</div>
CSS
.services{
float: left;
width: 25%;
height:100vh;
max-width:100%;
object-fit:cover;
}
.services .image-box {
width: 100%;
height: 400px;
background-size: cover;
}
#media only screen and (max-width: 1180px) {
.services{
width: 50%;
}
}
Try this, for grid you need to define display: grid; in the parent div
.grid{
display: grid;
grid-template-columns: repeat(4,1fr);
}
.services{
height: 100%;
width: 100%;
}
#media only screen and (max-width: 1180px) {
.grid{
display: grid;
grid-template-columns: repeat(2,1fr);
}
}
in HTML , in the place of 'level' add your image code it will work in both responsive
<div class="grid">
<div class="services">
level
</div>
<div class="services">
level
</div>
<div class="services">
level
</div>
<div class="services">
level
</div>
</div>
if you want the image to be cover add this in img class object-fit: cover;
This will work fine .
Related
Here is a sample image of the layout I am trying to achieve with HTML and CSS:
From what the gallery looks like,
the images are placed in a row
each row fills the width of the container
each image fills the height of the row
The height of each row looks to be slightly different (e.g. min-height: 300px; max-height: 600px;) to accomadate for the different aspect ratios of the image.
I'm trying to build this layout with html and css flexbox, but haven't really gotten too far with it:
.gallery-container {
display: block;
margin: 0 auto;
width: 50%;
}
.gallery-row {
display: flex;
min-height: 300px;
max-height: 500px;
}
.gallery-img {
position: relative;
flex-grow: 1;
max-width: 33.3%;
}
.gallery-img img {
height: 100%;
}
<div class="gallery-container">
<div class="gallery-row">
<div class="gallery-img">
<img src="https://picsum.photos/200/300"/>
</div>
<div class="gallery-img">
<img src="https://picsum.photos/300/200"/>
</div>
<div class="gallery-img">
<img src="https://picsum.photos/400/200"/>
</div>
</div>
<div class="gallery-row">
<div class="gallery-img">
<img src="https://picsum.photos/400/300"/>
</div>
<div class="gallery-img">
<img src="https://picsum.photos/100/200"/>
</div>
<div class="gallery-img">
<img src="https://picsum.photos/300/200"/>
</div>
</div>
</div>
How can I make sure that the images always are on the 'same' width and height, so they are matched in terms of those two? Now the second one is much larger and the boxes don't match up anymore. I need to get a big list with images that I got from someone, and they are not all the same dimensions sadly. The images need to be scaled proportionally
.slider-item {
width: 50%;
display:block;
float:left;
}
.slider-item img {
max-width: 100%;
}
.text {
background-color: grey;
}
<div class="slider-item">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Shaqi_jrvej.jpg/1200px-Shaqi_jrvej.jpg" alt="product">
<div class="text">
<h3>123</h3>
<h4>23</h4>
<h2>Bonte</h2>
<p>Text</p>
Order
</div>
</div>
<div class="slider-item">
<img src="https://pi.tedcdn.com/r/talkstar-assets.s3.amazonaws.com/production/playlists/playlist_398/reconnect_with_nature.jpg" alt="product">
<div class="text">
<h3>123</h3>
<h4>23</h4>
<h2>Bonte</h2>
<p>Text</p>
Order
</div>
</div>
Set a height and a width to the images. Then apply the object-fit: cover CSS rule on them.
The object-fit CSS property sets how the content of a replaced
element, such as an or , should be resized to fit its
container.
Possible values: contain, cover, fill, none, scale-down
.slider-item {
width: 50%;
display: block;
float: left;
}
.slider-item img {
width: 100%;
height: 10rem;
object-fit: cover;
}
.text {
background-color: grey;
}
<div class="slider-item">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Shaqi_jrvej.jpg/1200px-Shaqi_jrvej.jpg" alt="product">
<div class="text">
<h3>123</h3>
<h4>23</h4>
<h2>Bonte</h2>
<p>Text</p>
Order
</div>
</div>
<div class="slider-item">
<img src="https://pi.tedcdn.com/r/talkstar-assets.s3.amazonaws.com/production/playlists/playlist_398/reconnect_with_nature.jpg" alt="product">
<div class="text">
<h3>123</h3>
<h4>23</h4>
<h2>Bonte</h2>
<p>Text</p>
Order
</div>
</div>
I'm using media queries to turn my website more responsive but I'm not sure how to do that with images.
I've used position: absolute otherwise when the screen became really narrow the images would "get out" of the screen width and disappear to the left. So I would like some way that they could stay in the centre but either with a smaller size or in a column display which I can't figure it out how to do without them staying on top of each other. Can someone help me with this?
As you won't have the images to see the actual effect this is how it looks with a screen with a wide width :
This is how it looks with the properties that I used to centre them in a narrow screen:
and even more narrow
This is the code I used:
#media screen and (max-width: 1100px) {
.header-container.row,
.section-1.row,
.section-2.row,
.section-3.row,
.section-5.row,
.section-6.row,
.section-7.row {
flex-direction: column;
.work-images {
position: relative;
}
.first-line.row,
.second-line.row {
display: flex;
flex-direction: column;
position: absolute;
justify-content: center;
}
.pcontainer {
width: 350px;
}
.first-line img,
.second-line img {
width: 100%;
height: 250px;
object-fit: cover;
}
<div class="row work-images">
<div class="row first-line">
<div class="pcontainer">
<img src="magazine.jpg" width="333.33" height="250" class="image" />
</div>
<div class="pcontainer">
<img src="Magazine3.jpg" width="333.33" height="250" class="image" />
</div>
</div>
<div class="row second-line">
<div class="pcontainer">
<img src="poster.png" max-width="450" height="250" class="image" />
</div>
<div class="pcontainer">
<img src="poster2.png" max-width="450" height="250" class="image" />
</div>
</div>
</div>
I am trying to build my UI using Bootstrap. I am trying to set 3 divs in one row next to each other for medium and large screens. And for
under 768px I would like to place them one under another.
file.html
<section className="about" id="about">
<div className="container-fluid">
<div className="row boxes justify-content-md-center">
<div className="col-sm-12 col-md-4 box">
<div className="innerBox">
<div className="icons">
<img src={iconEducation} className="img-responsive" />
</div>
<div className="box-body">
<h3 className="box-title">Title </h3>
<div className="box-list">
<div className="box-list-items">
<div className="item-ul"><img src={dot} className="img-responsive" /></div>
<div>
<p>Text</p>
</div>
</div>
<div className="box-list-items">
<div><img src={dot} className="img-responsive" /></div>
<div className="item-ul">
<p>Text</p>
</div>
</div>
</div>
</div>
</div>
</div>
The HTML code is the same for all three divs.
Problem
On large and medium screens I have two divs in one row and a third underneath in new row. For tablet screens the divs do not flow one under another but are still in the same row. The layout I want is two in one row and the third underneath.
file.css
.about{
padding: 127px 0 196px 0;
}
.about .row.boxes >div{
margin: 0 20px 0 20px;
}
.about .box{
height: 550px;
width: 100%;
background-image: linear-gradient(144deg, #fdfdfd, #f9f9f9);
}
.about .innerBox{
margin: auto;
color: black;
width: 100%;
overflow: hidden;
}
.box-list-items > div {
display: inline-block;
}
.box-list-items img {
height: 35%;
width: 35%;
}
.icons {
height: 95px;
width: 95px;
float: right;
margin: 7% 5% 5% 0;
}
.icons img {
width: 100%;
height: 100%;
}
h3.box-title{
font-size: 2.7em;
}
As I was going through the Bootstrap docs I thought that naming the class as .col-md-4 would align my divs for above 768px in same row one next to each other and underneath would place them in kind of display: box view.
theres is no use of #media only screen and all ,this will work:
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">abc</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">xyz</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">123</div>
</div>
</div>
you can check:
https://jsfiddle.net/bfos8ttd/
you need to put all the code in file.html in side a div with class row and test it again.
Go to this Link for bootstrap columns (col-lg-4, col-md-4, col-sm-6, col-xs-12)
And follow these media query as per your device.
#media only screen and (min-width:1024px) and (max-width: 1200px) {
}
#media only screen and (min-width:992px) and (max-width: 1023px) {
}
#media only screen and (min-width:768px) and (max-width: 991px) {
}
#media only screen and ( max-width: 767px ) {
}
#media only screen and ( max-width: 479px ) {
}
#user9347049 This wouldn't fit in the comments, so I'm putting it here for clarity.
Your container-fluid lets you use the entire width of the screen, but it's still just a container, that contains your rows and columns. You create rows, then, you add columns. As in:
<div class="container"> <!-- you can change this class to container-fluid class if you like -->
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
</div>
<div class="col-md-4">
</div>
</div>
</div>
You can only have one container class. The row div contains all your cols divs for that row. You can have as many rows of columns as you need. If you adapt you code, you should start getting some of the results you're looking for before you look at the media query side of it.
I'm trying to place links on images in one row so that different images have different links. I'm also having this div to shrink to fit certain media screen sizes. However, the images didn't resize according to the wrapper requirements. Please help.
Here's the HTML:
.box {
width: 100%;
float: left;
margin: 0;
padding: 0;
position: relative;
}
#media screen and (min-width: 768px) and (max-width: 1024px) {
body {
text-align: center;
background: url(image/bg.png) center top;
}
#wrapper {
width: 768px;
margin: 0 auto;
background-color: #fff;
}
}
#media screen and (min-width: 1024px) {
body {
text-align: center;
background: url(image/bg.png) center top;
}
#wrapper {
width: 500px;
margin: 0 auto;
background-color: #fff;
}
<div id="wrapper">
<div class="box">
<img src="image/pea.jpg">
</div>
<div class="box">
<img src="image/pea_01.jpg">
<img src="image/pea_02.jpg">
<img src="image/pea_03.jpg">
<img src="image/pea_04.jpg">
<img src="image/pea_05.jpg">
</div>
<!-- main issue here -->
<div class="box">
<img src="image/pea_footer.jpg">
</div>
</div>
Here's a screenshot of the line up (desktop). Mobile seems to look ok after adding display:inline-block;
width:auto; to .box:
I reckon remove any static widths because you only need to detect when the viewport is a certain size and then change the img width then, as I have done here. I set each image to display block to remove any margin or padding around them. You might prefer to not do this, but I like setting this as default.
This way you can pick different breakpoints that suit you rather than setting static widths at each breakpoint. This is the beauty of responsive development. Stay flexible rather than controlling what happens to containing divs; let the content run things. Run this snippet below in Full Screen mode to see the full desktop styling (each img goes to 20% instead of 50%):
.box {
width: 100%;
float: left;
margin: 0;
padding: 0;
position: relative;
}
img {
display: block;
width: 20%;
float: left;
}
#media screen and (max-width: 767px) {
img {
width: 50%;
}
}
<div id="wrapper">
<div class="box">
<img src="http://placehold.it/100x100">
</div>
<div class="box">
<img src="http://placehold.it/100x100">
<img src="http://placehold.it/100x100">
<img src="http://placehold.it/100x100">
<img src="http://placehold.it/100x100">
<img src="http://placehold.it/100x100">
</div>
<!-- main issue here -->
<div class="box">
<img src="http://placehold.it/100x100">
</div>
</div>
Your .box could be in display:flex
.box {
display: flex;
flex-flow: row nowrap;
justify-content: space-around;
}
Keep in mind that your 5 <img> should be the icons, not containing your background (the clouds).
And I think the following code would be correct for your images:
.box img {
max-width: 20%;
}
I think it's better to not apply an explicit width or height to the image tag.
Please try:
max-width:100%;
max-height:100%;
Just use percentage based layouts rather than pixels or other measurements.
For example:
<img width="50%">: that will fill half of the containing element, at any size
<img width="500px">: that will always fill exactly 500 pixels, if it's too big or if it's too small.