Here is the HTML
<section className="section">
<div className="image-grid">
<img src="../imagegrid/img1.jpg" alt="grid" ></img>
<img src="../imagegrid/img2.jpg" alt="grid" ></img>
<img src="../imagegrid/img3.jpg" alt="grid" ></img>
<img src="../imagegrid/img4.jpg" alt="grid" ></img>
<img src="../imagegrid/img5.jpg" alt="grid" ></img>
<img src="../imagegrid/img6.jpg" alt="grid" ></img>
<img src="../imagegrid/img7.jpg" alt="grid" ></img>
<img src="../imagegrid/img8.jpg" alt="grid" ></img>
<img src="../imagegrid/img9.jpg" alt="grid" ></img>
<img src="../imagegrid/img10.jpg" alt="grid" ></img>
<img src="../imagegrid/img11.jpg" alt="grid" ></img>
<img src="../imagegrid/img12.jpg" alt="grid" ></img>
<img src="../imagegrid/img13.jpg" alt="grid" ></img>
<img src="../imagegrid/img14.jpg" alt="grid" ></img>
<img src="../imagegrid/img15.jpg" alt="grid" ></img>
<img src="../imagegrid/img16.jpg" alt="grid" ></img>
</div>
</section>
Here is the CSS, I have used a grid to display images like this, and I want to keep the images looking like the attached picture below, but don't want to change the image size
.image-grid{
padding: 60px;
box-sizing: border-box;
display: grid;
width: 100%;
grid-template-columns: repeat(8,1fr);
grid-auto-columns: 100px;
grid-auto-rows: 200px;
gap: 15px;
}
.image-grid img{
margin: 0;
padding: 0;
width: 70%;
height: 100%;
object-fit: cover;
/* border: solid red; */
border-radius:20px ;
}
.image-grid img:nth-child(odd){
margin-top: 50px;
}
.image-grid img:nth-child(9){
opacity: 0;
}
This is what it looks like now
Here is an example of what I want this to look like
In your grid container, you should replace grid-template-columns: repeat(8,1fr); by grid-template-columns: repeat(8,100px);.
You can remove this: grid-auto-columns: 100px;
And by your image set them to width: 100%;.
The gap in your code comes from this width: 70%; what is the point of this ?
Demo:
.image-grid{
padding: 60px;
box-sizing: border-box;
display: grid;
width: 100%;
/*grid-template-columns: repeat(8,1fr);*/
/* Replaced by: */
grid-template-columns: repeat(8,100px);
/*grid-auto-columns: 100px;*/ /* This is useless */
grid-auto-rows: 200px;
grid-gap: 15px;
}
.image-grid img{
margin: 0;
padding: 0;
width: 100%; /* Shoud be 100% not 70%, width of the column should be manage in the grid container setting */
height: 100%;
object-fit: cover;
/* border: solid red; */
border-radius:20px ;
margin-right:-30%;
}
.image-grid img:nth-child(odd){
margin-top: 50px;
}
.image-grid img:nth-child(9){
opacity: 0;
}
<section class="section">
<div class="image-grid">
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
<img src="https://dummyimage.com/600x400/00ff04/357f94" alt="grid" />
<img src="https://dummyimage.com/600x400/357f94/00ff04" alt="grid" />
</div>
</section>
I fixed it, I change the code like this:
.image-grid{
justify-content: center;
padding: 60px;
box-sizing: border-box;
display: grid;
width: 100%;
grid-template-columns: repeat(8,150px);
grid-auto-columns: 10px;
grid-auto-rows: 200px;
grid-gap: 15px;
}
.image-grid img{
padding: 0;
margin: 0;
width: 150px;
height: 200px;
object-fit: cover;
border-radius:20px ;
}
What you're looking for is grid-gap
.image-grid{
padding: 60px;
box-sizing: border-box;
display: grid;
width: 100%;
grid-template-columns: repeat(8,1fr);
grid-auto-columns: 100px;
grid-auto-rows: 200px;
grid-gap: 15px; /* here instead of gap */
}
Related
import "./portfolio.scss"
export default function portfolio() {
return (
<div className="portfolio" id="portfolio">
<h1>Portfolio</h1>
<ul>
<li className="active">Featured</li>
<li>Web app</li>
<li>Mobile app</li>
<li>Design</li>
<li>Branding</li>
</ul>
<div className="container">
<div className="item">
<img
src="assets/default-product-image.png"
alt="" />
<h3>Coming soon</h3>
</div>
<div className="container">
<div className="item">
<img
src="assets/default-product-image.png"
alt=""/>
<h3>Coming soon</h3>
</div>
<div className="container">
<div className="item">
<img
src="assets/default-product-image.png"
alt=""/>
<h3>Coming soon</h3>
</div>
</div>
</div>
</div>
</div>
)
}
This is my .jsx I am making div's with containers in them and the containers seem to not be aligned properly, see the picture below.
I am trying to get them all aligned with even margin but the third one seems to be off, when i change the device on which i view it tho they align perfectly below each other.
and this is my scss
#import "../../global.scss";
.portfolio{
background-color: lightblue;
display: flex;
flex-direction: column;
align-items: center;
h1{
font-style: 500px;
}
ul{
margin: 10px;
padding: 0;
list-style: none;
display: flex;
li{
font-size: 14px;
margin-right: 9px;
padding: 7px;
border-radius: 10px;
cursor: pointer;
&.active{
background-color: $mainColor;
color: white;
}
}
}
.container{
width: 70%;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
.item{
width: 220px;
height: 150px;
border-radius: 20px;
border: 1px solid rgb(240, 234, 234);
margin: 10px 20px;
display: flex;
align-items: center;
justify-content: center;
color: white;
position: relative;
h3{
position: absolute;
font-style: 20px;
}
img{
width: 100%;
height: 100%;
object-fit: cover;
z-index: 1;
}
&:hover{
background-color: $mainColor;
img{
opacity: 0;
z-index: 0;
}
}
}
}
}
This is what it looks like right now I want the items to be evenly spread apart.
The way you have the <div class='container'>s nested is probably what is causing misalignment. I don't see any images in your question, but I'm willing to bet your code is producing containers that get smaller and smaller the further down the tree it gets, which is exaggerated on small screens.
Start by closing your container divs before opening another. Also, use consistent indentation - that will help make this issue more obvious when you're reading your code. I have cleaned up your code and changed nothing except the HTML markup. The top example in the linked pen has cleaned up markup, while the bottom example has the containers grouped.
https://codepen.io/the_Northway/pen/QWqapbg?editors=1100
.portfolio {
background-color: lightblue;
display: flex;
flex-direction: column;
align-items: center;
}
.portfolio#portfolio_fixed {
background-color: orange;
}
.portfolio h1 {
font-style: 500px;
}
.portfolio ul {
margin: 10px;
padding: 0;
list-style: none;
display: flex;
}
.portfolio ul li {
font-size: 14px;
margin-right: 9px;
padding: 7px;
border-radius: 10px;
cursor: pointer;
}
.portfolio ul li.active {
background-color: #445566;
color: white;
}
.portfolio .container {
width: 70%;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
.portfolio .container .item {
width: 220px;
height: 150px;
border-radius: 20px;
border: 1px solid #f0eaea;
margin: 10px 20px;
display: flex;
align-items: center;
justify-content: center;
color: white;
position: relative;
}
.portfolio .container .item h3 {
position: absolute;
font-style: 20px;
}
.portfolio .container .item img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
z-index: 1;
}
.portfolio .container .item:hover {
background-color: #445566;
}
.portfolio .container .item:hover img {
opacity: 0;
z-index: 0;
}
<div class="portfolio" id="portfolio_fixed">
<h1>Portfolio</h1>
<ul>
<li class="active">Featured</li>
<li>Web app</li>
<li>Mobile app</li>
<li>Design</li>
<li>Branding</li>
</ul>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt="" />
<h3>Coming soon</h3>
</div>
</div>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt=""/>
<h3>Coming soon</h3>
</div>
</div>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt=""/>
<h3>Coming soon</h3>
</div>
</div>
</div>
<div class="portfolio" id="portfolio_broken">
<h1>Portfolio</h1>
<ul>
<li class="active">Featured</li>
<li>Web app</li>
<li>Mobile app</li>
<li>Design</li>
<li>Branding</li>
</ul>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt="" />
<h3>Coming soon</h3>
</div>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt=""/>
<h3>Coming soon</h3>
</div>
<div class="container">
<div class="item">
<img src="assets/default-product-image.png" alt=""/>
<h3>Coming soon</h3>
</div>
</div>
</div>
</div>
</div>
Im new with css, Im one of our homework in class is to make a box with 4 imgs, that while im mouse hover image it will be bigger in the center of the box.
each image must be with 2 files, small one will start with th and the big one start with big. need to practice the attribute selector.
so I make something, but I just cant fiqure why its not working.
body>div {
width: 100%;
height: 800px;
border: 2px solid black;
}
div {
position: relative;
}
div>div {
position: absolute;
top: 200px;
left: 400px;
}
img {
display: block;
margin: 20px 10px;
}
img[src*="th"] {
width: 250px;
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/big"] {
display:none;
}
img[src*="th"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="th"]:hover+img[src*="/big"] {
display:block;
}
</style>
<div>
<img src="/HTML/HW/HW27_5/thCar.jpg" alt="">
<img src="/HTML/HW/HW27_5/thHome.jpg" alt="">
<img src="/HTML/HW/HW27_5/thTree.jpg" alt="">
<img src="/HTML/HW/HW27_5/thPlane.jpg" alt="">
<div>
<img src="/HTML/HW/HW27_5/bigCar.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigHome.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigTree.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigPlane.jpg" alt="">
</div>
</div>
Try it by keeping the imgs next to each other.
The "+" works for next sibling element only.
.outer-div {
width: 100%;
height: 500px;
border: 2px solid black;
}
.outer-div {
position: relative;
}
img {
display: block;
margin: 15px 10px;
}
img[src*="/100"] {
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/200"] {
display: none;
}
img[src*="/100"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="/100"]:hover+img[src*="/200"] {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="outer-div">
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
</div>
The +-selector select an element that is directly after another element.
Therefor your big image div must be directly next to the smaller image. And you have use this selector img[src*="th"]:hover+div img[src*="/big"] instead of img[src*="th"]:hover+img[src*="/big"] to address the nested image.
body>div {
width: 100%;
height: 800px;
border: 2px solid black;
}
div {
position: relative;
}
div>div {
position: absolute;
top: 200px;
left: 400px;
}
img {
display: block;
margin: 20px 10px;
}
img[src*="th"] {
width: 250px;
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/big"] {
display: none;
}
img[src*="th"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="th"]:hover+div img[src*="/big"] {
display: block;
}
</style>
<div>
<img src="https://dummyimage.com/250x50/f00&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200/f00&text=/big" alt="">
</div>
<img src="https://dummyimage.com/250x50&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200&text=/big" alt="">
</div>
<img src="https://dummyimage.com/250x50/0f0&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200/0f0&text=/big" alt="">
</div>
<img src="/HTML/HW/HW27_5/thPlane.jpg" alt="">
<div>
<img src="/HTML/HW/HW27_5/bigPlane.jpg" alt="">
</div>
</div>
Help me adjust my code so that it will be working in Safari. Initially I used answer from here with a few adjustments. I needed my card to be responsive and has some additional elements at the bottom.
Here's my code:
.gallery {
display: grid;
gap: 15px;
grid-template-columns: repeat(2, 1fr);
}
.card {
background-color: #fff;
border-radius: 4px;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.14);
max-width: 200px;
padding: 10px 10px 20px;
cursor: pointer;
position: relative;
}
.wrapper {
width: 100%;
position: relative;
margin-bottom: 20px;
}
.wrapper::before {
content: '';
padding-bottom: 100%;
display: block;
}
.img-grid {
display: grid;
border-radius: 4px;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 3px;
overflow: hidden;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.img-grid > div {
position: relative;
width: 100%;
height: 100%;
}
.img-grid > div:first-child {
grid-column: 1 / span 2;
}
img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
.logo {
width: 50px;
height: 50px;
background: gainsboro;
float: left;
margin-right: 15px;
}
h4,p {
margin: 3px 0;
}
p {
font-size: 13px;
}
<div class="gallery">
<div class="card">
<div class="wrapper">
<div class="img-grid">
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
</div>
</div>
<div class="logo"></div>
<h4>Logo name</h4>
<p>Some description</p>
</div>
<div class="card">
<div class="wrapper">
<div class="img-grid">
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
<div>
<img loading="lazy" src="https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510_960_720.jpg"></img>
</div>
</div>
</div>
<div class="logo"></div>
<h4>Logo name</h4>
<p>Some description</p>
</div>
</div>
It works fine in Chrome, but it doesn't work in Safari(current and older). It's because I needed my cards to be responsive and always maintain square grid. So I used same trick on the wrapper that's widely used to maintain responsive squares. But as a result of that height: 100% is no longer working in Safari. Is there any way to make it work? I would appreciate some your help.
I am trying to prevent an image from extending outside the border of a grid item that has a height set of 0 and padding-top of 100% to maintain a 1:1 aspect ratio. It works fine when using an <img> element (on the left), but not a <picture> element (on the right).
.grid {
display: grid;
grid-template-columns: 1fr 1fr
}
.item {
display: block;
position: relative;
padding-top: 100%;
height: 0;
background-color: grey;
border: 1px solid black;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: grid;
grid-template-rows: minmax(20px, 1fr) auto auto minmax(20px, 1fr);
}
.text {
grid-row-start: 2;
text-align: center;
margin-top: 0;
margin-bottom: 15px;
}
.img {
grid-row-start: 3;
max-height: 100%;
width: auto;
max-width: 100%;
display: block;
margin-left: auto;
margin-right: auto;
}
<ul class="grid">
<li class="item">
<div class="inner">
<p class="text">Text</p>
<img class="img" src="https://images.unsplash.com/photo-1621250755294-57cf625b4a9a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80" />
</div>
</li>
<li class="item">
<div class="inner">
<p class="text">Text</p>
<picture class="img">
<source srcset="https://images.unsplash.com/photo-1621250755294-57cf625b4a9a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80 1x, https://images.unsplash.com/photo-1618236507249-9960f2c6a53d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80 2x"
media="(min-width: 360px)">
<img src="https://images.unsplash.com/photo-1621250755294-57cf625b4a9a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80" />
</picture>
</div>
</li>
</ul>
Put a class to the img as well.
<img src="https://images.unsplash.com/photo-1621250755294-57cf625b4a9a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80" />
to
<img class="img" src="https://images.unsplash.com/photo-1621250755294-57cf625b4a9a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80" />
Add overflow: auto to class img
Fiddle
I am using flex to display pictures. For wide screen all works well but as for for smaller media sizes I would like to see div with the class "base" apper under pictures even when they are displayed in a second an third row. How could I implement this?
Take a look at it on Codepen
<div class="container">
<div class="bookshelf">
<div class="bookshelf-background">
<div class="base"></div>
</div>
<div class="bookshelf-images">
<ul class="container">
<li class="flex flex1">
<a href="#" class="img">
<div style="width: 250px">
<img src="https://image.shutterstock.com/image-illustration/amazing-word-pop-art-retro-
600w-1112517635.jpg" style="width: 150px;">
</div>
</a>
</li>
<li class="flex flex1">
<a href="#" class="img">
<div style="width: 250px">
<img src="https://image.shutterstock.com/image-illustration/amazing-word-pop-art-retro-
600w-1112517635.jpg" style="width: 150px;">
</div>
</a>
</li>
</ul>
</div>
</div>
</div>`
Can you please check with this code, hope it will resolve your query. First, we remove static "base" class div and add background in separate list item using pseudo-element.
.flex {
margin-bottom: 50px;
color: white;
font-size: 20px;
text-align: center;
position: relative;
}
.bookshelf-images .flex {
position: relative;
padding-bottom: 14px;
}
.bookshelf-images .flex:before {
content: "";
width: 100%;
height: 45px;
border-bottom: 9px solid fuchsia;
position: absolute;
left: 0;
bottom: 0;
background: #35091b;
}
.bookshelf-images .flex a{
position: relative;
}
.container {
max-width: 800px;
background: none;
margin-bottom: 0px;
text-align: center;
display: flex;
flex-wrap: wrap;
margin-top: -3px;
}
.container .bookshelf {
display: flex;
position: relative;
}
.container .bookshelf-images {
height: 100%;
}
.container .bookshelf-image {
height: 100%;
position: relative;
max-width: 100%;
display: flex;
}
li {
list-style-type: none;
}
ul {
margin-left: 0;
padding-left: 0;
}
.container .bookshelf-background {
position: absolute;
border: 0px solid #ff0000;
left: 0;
bottom: 45px;
width: 100%;
height: 42px;
}
.container .bookshelf-background .base {
width: 100%;
height: 100%;
border: 0px solid #fff000;
box-shadow: 0px 9px 1px fuchsia;
position: absolute;
background: #35091b;
}
<div class="container">
<div class="bookshelf">
<!-- <div class="bookshelf-background">
<div class="base"></div>
</div> -->
<div class="bookshelf-images">
<ul class="container">
<li class="flex flex1">
<a href="#" class="img">
<div style="width: 250px">
<img
src="https://image.shutterstock.com/image-illustration/amazing-word-pop-art-retro-600w-1112517635.jpg"
style="width: 150px;"
/>
</div>
</a>
</li>
<li class="flex flex1">
<a href="#" class="img">
<div style="width: 250px">
<img
src="https://image.shutterstock.com/image-illustration/amazing-word-pop-art-retro-600w-1112517635.jpg"
style="width: 150px;"
/>
</div>
</a>
</li>
<li class="flex flex1">
<a href="#" class="img">
<div style="width: 250px">
<img
src="https://image.shutterstock.com/image-illustration/amazing-word-pop-art-retro-600w-1112517635.jpg"
style="width: 150px;"
/>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>