HTML Image grid images not in the grid - html

IM very new to css grid and i am having difficulties getting my images to fall into the grid for whatever reason. they seem to be representing on top of my grid not within.. any help would be appreciated..
Ive added colors to the borders to outline how i want the shape and stuff.
* {
width: 100% margin: 0;
padding: 0;
box-sizing: border-box;
}
.grid-container {
width: 100% position: absolute;
display: grid;
height: 100vh;
border: 10px solid green;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(5, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
justify-items: flex-center;
}
.grid-item {
background-color: Grey;
border: 4px solid orange;
padding: 10px;
font-size: 30px;
}
.container2 {
object-fit: cover;
max-width: 50% height:auto;
}
.grid-container .image {
position: relative;
max-width: 100%;
height: auto;
}
.div1 {
grid-area: 1 / 1 / 3 / 3;
max-width: 100%;
height: auto;
object-fit: contain;
}
.div2 {
grid-area: 1 / 3 / 2 / 4;
max-width: 100%;
}
.div3 {
grid-area: 1 / 4 / 2 / 5;
max-width: 100%;
}
.div4 {
grid-area: 1 / 5 / 2 / 6;
max-width: 100%;
}
.div5 {
grid-area: 2 / 3 / 3 / 4;
max-width: 100%;
}
.div6 {
grid-area: 2 / 4 / 3 / 5;
max-width: 100%;
}
.div7 {
grid-area: 2 / 5 / 3 / 6;
max-width: 100%;
}
<div class="grid-container">
<div class="grid-item div1">
<div class="container2">
<img src="https://www.wendtcorp.com/wp-content/uploads/2020/08/Sikirica-600x600-1.jpg" alt="Srdjan Sikirica" class="image"> </img>
<div class="overlay">Srdjan Sikirica</div>
</div>
</div>
<div class="grid-item div2">Test2
</div>
<div class="grid-item div3 ">test3
</div>
<div class="grid-item div4 ">test4
</div>
<div class="grid-item div5">test5
</div>
<div class="grid-item div6 ">test6
</div>
<div class="grid-item div7">test7
</div>
I watched some videos and this was as far as i got, i am still learning how and when to combine classes and stuff. I did some goodgle searching and most people just put blurbs of information together which wasnt helpful at all.
Thank you for any and all help you can provide!!

Is this what you want?
I added the object-position: 0% 0%; if you want the image to be in the center you need to use 50% 50%
.container2 {
width: 100%;
height: 100%;
}
.container2 img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
object-position: 0% 0%;
}

Related

How to add text on top of two side by side images

I'm trying to put text on top of two images that are side by side and together fill out the entire site. Here is the code:
HTML:
<div class="main">
<img src="city.jpg" class="city" alt="city">
<img src="couple.jpg" class="couple" alt="couple">
</div>
CSS:
.main {
display: flex;
justify-content: space-between;
width: 100%;
height: 152.5vh;
}
.city {
flex-basis: 50%;
height: 100%;
object-fit: cover;
display: block;
}
.couple {
flex-basis: 50%;
height: 100%;
object-fit: cover;
display: block;
}
Both of the images are side by side without any space in between. I want to add text to the middle of the side so sort of on top of both images. What could I try to do?
I have looked online to find a solution but nothing worked or was simply too hard to understand and to implement. Thank you in advance!
personally I wouldn't do that with flex but with grid
Simple grid 2rows, 2 cols.
text div take 2 cells of first row, and image on second row, each in 1 cell
body {
margin: 0;
padding: 0;
}
.main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto 1fr;
max-width: 100%;
height: 152.5vh;
}
.text {
grid-area: 1 / 1 / 2 / 3;
align-self: center;
justify-self: center;
color: #222222;
}
.city {
grid-area: 2 / 1 / 3 / 2;
width: 100%;
height: 100%;
}
.couple {
grid-area: 2 / 2 / 3 / 3;
width: 100%;
height: 100%;
}
.city img,
.couple img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="main">
<div class="text">
1 title for 2 image
</div>
<div class="city">
<img src="https://picsum.photos/id/237/800/600" alt="city">
</div>
<div class="couple">
<img src="https://picsum.photos/id/85/800/600" alt="couple">
</div>
</div>
so to answer your latest comment "thats right. over the two images on the center"
we still can use grid for images, in add here I put the grid in position relative, and the text in absolute. You can play with top value.
body {
margin: 0;
padding: 0;
}
.main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr;
max-width: 100%;
height: 152.5vh;
position: relative;
}
.text {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
color: #222222;
}
.city {
grid-area: 1 / 1 / 2 / 2;
width: 100%;
height: 100%;
}
.couple {
grid-area: 1 / 2 / 2 / 3;
width: 100%;
height: 100%;
}
.city img,
.couple img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="main">
<div class="text">
1 title for 2 image
</div>
<div class="city">
<img src="https://picsum.photos/id/237/800/600" alt="city">
</div>
<div class="couple">
<img src="https://picsum.photos/id/85/800/600" alt="couple">
</div>
</div>

Attach text to center of image only using grid template

I have a background image which has on top another div containing some elements.
I success to add the white-container on top of the background div using grid-template-area.
I try to do the same thing only using grid and not position. But text hello world doesn't go on top of love icon using grid area love-svg.
I think perhaps because img tag has no closed tag, but can't find a way to solve this problem.
HTML
<div class="container">
<img src="/images/site/trust_pilot_bg_mb.jpg" >
<div class="white-container">
<div class="love-section">
<img class="love-ico" src="/images/site/love_svg_icon.svg"><h2 class="hello">Hello world</h2> </img>
</div>
</div>
</div>
SCSS
.container {
display: grid;
justify-items: center;
grid-template-areas: "content-wrapper";
&>* {
grid-area: content-wrapper;
}
img {
width: 100%;
}
.love-section
{
display: grid;
background-color: white;
top: 40px;
position: relative;
}
.love-ico {
width: 100px;
grid-template-areas: "love-svg";
}
.hello {
color: red;
grid-area: love-svg;
}
}
In blue this is what I want to achieve. Please note that I want the text to be on top of the love logo but allow it to be bigger than it.
if you want to use grid to do that, you'll need to play with the align-self, and justify-self for the grid-item.
Now there are several ways to do that with grid, the way you are using it is not the simplest.
I'm proposing just one here:
html,
body {
margin: 0;
padding: 0;
}
#grid {
display: grid;
grid-template-rows: repeat(7, 1fr);
grid-template-columns: repeat(7, 1fr);
gap: 0;
width: 100vw;
min-height: 100vh;
}
#div1 {
grid-area: 1 / 1 / 8 / 8;
}
#div1 img,
#div2 img {
width: 100%;
height: 100%
}
#div2 {
grid-area: 3 / 3 / 6 / 6;
}
#div3 {
grid-area: 4 / 4 / 5 / 5;
align-self: center;
justify-self: center;
}
<div id="grid">
<div id="div1">
<img src="https://picsum.photos/300/200">
</div>
<div id="div2">
<img class="love-ico" src="https://picsum.photos/300/200">
</div>
<div id="div3">
<h2>Hello world</h2>
</div>
</div>
Now I don't know if it's mandatory for you to use grid, otherwise the old technic of relative, absolute works too:
html,
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
background-image: url("https://picsum.photos/300/200");
width: 100vw;
height: 100vh;
}
#container img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#container h2 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div id="container">
<img class="love-ico" src="https://picsum.photos/300/200">
<h2>Hello world</h2>
</div>

Images not adjusting inside the grid

I got 2 columns grid with following layout:
My issue is that when I use images inside the right column (1 image inside each box)..Images overflow and whole grid kind of acts weird.
It looks something like this:
Codepen Link: https://codepen.io/kazmi066/pen/MWXGgaL?editors=1100
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.grid {
background: green;
width: 100%;
max-height: 70vh;
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
}
.col1 {
height: 100%;
background: red;
}
.col2 {
height: 100%;
background: orange;
}
.box1 {
width: 100%;
height: 50%;
border: 1px solid black;
}
.box2 {
width: 100%;
height: 50%;
border: 1px solid blue;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<div class="col1"></div>
<div class="col2">
<div class="box1"><img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property"/></div>
<div class="box2"><img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property" /></div>
</div>
</div>
I want the images to adjust inside the boxes perfectly without the need of custom height and width so that any size of image can work in this scenario.
fit would be object-fit: contain; for the image not cover.
but if the ratio of the image is not the ratio of the box, you'll have blank
you can put the image in background of box1, box2... with a background size cover. It will cover entirely and clipped the overflow. If box ratio "totally" different of the image, lot of image can be clipped, but it's not so often.
I've found a way, only CSS, nothing is changed in your HTML
2 points:
1- it's using clip-path
2- image fill box space, but are clipped otherwise blank space
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.grid {
background: green;
width: 100%;
max-height: 70vh;
display: grid;
gap: 2vh;
grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
grid-template-rows: 100%;
}
.col1 {
background: red;
}
.col2 {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 34vh 34vh;
background: orange;
justify-content: center;
}
.box1 {
width: 100%;
border: 1px solid black;
clip-path: inset(0);
}
.box2 {
width: 100%;
border: 1px solid blue;
clip-path: inset(0);
}
img {
width: 100%;
object-fit: contain;
}
<div class="grid">
<div class="col1"></div>
<div class="col2">
<div class="box1"><img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property" /></div>
<div class="box2"><img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property" /></div>
</div>
</div>
based on your grid max-height in vh, I defined all others same kind of values in vh. Lot more consistent and avoid some little strange pixels or lines here or there depending of window size.
I put a nested grid inside col2 where box1 box2 go. box have a clip-path with inset 0, meaning clipping everything out.
The solution that worked for me:
I used grid-auto-rows to create 2 rows with specific height.
Then added span to adjust the column accordingly to cover both rows.
.grid {
width: 100%;
border: 1px solid red;
display: grid;
gap: 14px;
grid-template-columns: repeat(12, 1fr);
grid-auto-rows: 280px 280px;
}
.col1 {
height: 100%;
grid-column: 1/8;
grid-row: span 2;
background: red;
}
.col2 {
grid-column: 8/13;
height: 100%;
background: orange;
}
.col2 img:first-child {
margin-bottom: 11px;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
#media (max-width: 768px) {
.grid {
grid-auto-rows: 220px 120px;
}
.col1 {
grid-column: 1/13;
}
.col2 {
grid-column: 1/13;
grid-row: span 2;
}
}
<div class="grid">
<div class="col1">
<img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property"/>
</div>
<div class="col2">
<img src="https://images.unsplash.com/photo-1564013799919-ab600027ffc6?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhdXRpZnVsJTIwaG91c2V8ZW58MHx8MHx8&w=1000&q=80" alt="property"/>
<img src="https://images.pexels.com/photos/186077/pexels-photo-186077.jpeg?cs=srgb&dl=pexels-binyamin-mellish-186077.jpg&fm=jpg" alt="property" />
</div>
</div>
Final output now:
Solution-2: Found Another better solution with Grid-template-areas I guess which looks more cleaner:
.grid {
width: 100%;
border: 1px solid red;
display: grid;
gap: 14px;
grid-template-areas:
"mainImage mainImage otherImage1"
"mainImage mainImage otherImage1"
"mainImage mainImage otherImage2"
"mainImage mainImage otherImage2"
}
.mainImage {
grid-area: mainImage;
}
.otherImage1 {
grid-area: otherImage1;
}
.otherImage2 {
grid-area: otherImage2;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<img src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8N3x8fGVufDB8fHx8&w=1000&q=80" alt="property" class="mainImage" />
<img src="https://images.unsplash.com/photo-1564013799919-ab600027ffc6?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhdXRpZnVsJTIwaG91c2V8ZW58MHx8MHx8&w=1000&q=80" alt="property" class="otherImage1" /> <img src="https://images.pexels.com/photos/186077/pexels-photo-186077.jpeg?cs=srgb&dl=pexels-binyamin-mellish-186077.jpg&fm=jpg" alt="property" class="otherImage2" />
</div>
<h1>something else here</h1>

overlaying one block on another css

i need to overlaying div to div like this img the gray div should cover the yellow div in the upper right corner
I tried it like this, but yellow does not get into the position I need
.grid {
display: grid;
/* grid-auto-rows: 100px;
grid-auto-columns: 100px; */
}
.item-1 {
grid-row: 1 / 2;
grid-column: 5 / 4;
}
.item-2 {
grid-row: 2 / 3;
grid-column: 2 / 4;
}
.item-1 img {
width: 200px;
height: 170px;
}
.item-2 img {
width: 368px;
height: 350px;
}
<div class="grid">
<div class="item-1">
<img src="img/square.png" alt="">
</div>
<div class="item-2">
<img src="img/Rectangle 24.png" alt="">
</div>
</div>
You should be able to do this simply by doing this.
Just place the element you want inside the element you want it to overlap, give it a position absolute and just assign the left/right values.
.box1 {
background: yellow;
position: relative;
width: 200px;
height: 200px;
margin: 100px;
}
.box2 {
background: gray;
position: absolute;
width: 200px;
height: 200px;
right: 120px;
top: 120px;
}
<div class="box1">
<div class="box2"></div>
</div>

Image spilling out of parent div in CSS Grid item (Firefox only)

Is there a better way to achieve centering a text caption on an image that is nested in a CSS Grid item (and can be "justify-items: start/end/etc")
I like how the example is rendering in Safari & Chrome but in Firefox the image is spilling out of the parent div. Any help would be greatly appreciated! Thank you :)
Safari / Chrome Example
Firefox Example (Image spilling out of parent div)
https://codepen.io/jcha4849/pen/ywgMde
<body>
<div class="container-main"><article id="post-number-1">
<div class="standard-container-1">
<div class="caption-1">CENTERED CAPTION 1</div>
<img src="https://images.unsplash.com/photo-1516472096803-187d3339b36f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80"></div>
</article>
<article id="post-number-2">
<div class="standard-container-1">
<div class="caption-1">CENTERED CAPTION 2</div>
<img src="https://images.unsplash.com/photo-1516472096803-187d3339b36f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80"></div>
</article>
</div>
<!-- container-main -->
</body>
<style>
body {
padding: 0;
margin: 0;
}
.container-main {
width: 100%;
display: grid;
grid-template-columns: repeat(10, 9vw);
grid-auto-rows: 9vw;
margin: 0 0 0px 0;
padding: 0;
min-width: 0;
padding: 5vw 5vw 15vw 5vw;
min-width: 0;
background-color: teal;
}
.container-main article {
border: 3px solid black;
}
.container-main article .standard-container-1 {
position: relative;
max-height: 100%;
max-width: 100%;
}
.container-main article .standard-container-1 .caption-1 {
min-width: 80%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
.container-main article .standard-container-1 .caption-1 a {
color: #0f0;
font-family: sans-serif;
}
.container-main article .standard-container-1 img {
display: block;
height: auto;
margin: 0 auto;
max-height: 100%;
max-width: 100%;
}
.container-main article#post-number-1 {
display:grid;
align-items: start;
justify-items: start;
grid-column: 1 / 4;
grid-row: 1 / 4 ;
}
.container-main article#post-number-2 {
display:grid;
align-items: start;
justify-items: end;
grid-column: 8 / 11;
grid-row: 1 / 4 ;
}
</style>