I am a student working on a website. I have the below CSS/HTML script that should be displaying cards in order horizontally, however when I view it on a website its showing up vertically instead.
Goal:
[pic1] [pic2] [pic3]
[pic4] [pic5] [pic6]
Current Output:
[pic1]
[pic2]
[pic3]
[...]
[pic6]
Current Code:
I have only included code that should be relevant to my question, please let me know if I should include additional code for guidance.
.cards {
margin: 0 auto;
max-width: 1000px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: auto;
gap: 20px;
font-family: Arial;
padding-top: 30px;
}
<div class="cards">
<div class="card">
<img src="image.jpg" alt="image" class="card__image">
<div class="card__content">
<p2>Image</p2>
</div>
<div class="card__info">
<div>
BOUNTY
</div>
</div>
</div>
</div>
<div class="cards">
<div class="card">
<img src="image2.jpg" alt="image" class="card__image">
<div class="card__content">
<p2>Image2</p2>
</div>
<div class="card__info">
<div>
BOUNTY
</div>
</div>
</div>
</div>
I would really do that with display: flex ... why? because it is easier and not that complex to work with grid and give your flex-children a flex-basis of 30% to have three children in one row.
Nevertheless, you have div class="cards" openend twice. you will probably have this div only once and div class="card__*" several times.
grid is more compatible with older versions of browsers, like IE11, but also only with the prefix, but that's just my personal thought.
Related
I want six images to align properly, for example in two rows of three images, and be level. But they are not aligning, and some of them are not even the same size.
My intial issue was when making the screen smaller the images would fall into each other. That is not an issue now, but the images are not the same size and they do not align properly.
How do I align images properly in rows?
Here is my working code:
#boxes .box img {
width: 60%;
height: 80%;
display: block;
justify-content: center;
}
<section id="boxes">
<div class="container">
<div class="box">
<h3>Yosemite National Park</h3>
<img src="https://images.unsplash.com/photo-1629233796529-4a04bf1aee52?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1287&q=80" alt="Yosemite">
</div>
<div class="box">
<h3>Redwood National Park</h3>
<img src="https://images.unsplash.com/photo-1582790670329-b14bf5c38562?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=735&q=80" alt="Redwood">
</div>
<div class="box">
<h3>Joshua Tree National Park</h3>
<img src="https://images.unsplash.com/photo-1626008007279-f41981695728?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1374&q=80" alt="Joshua Tree">
</div>
<div class="box">
<h3>Channel Islands National Park</h3>
<img src="https://images.unsplash.com/photo-1629256299843-5fb1714fe067?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1287&q=80" alt="Channel Islands">
</div>
<div class="box">
<h3>Seqouia National Park</h3>
<img src="https://images.unsplash.com/photo-1535628169704-5d0b32718ee8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80" alt="Seqouia">
</div>
<div class="box">
<h3>Pinnacles National Park</h3>
<img src="https://images.unsplash.com/photo-1624244453711-e042e81529d9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80" alt="Pinnacles">
</div>
</div>
</section>
I changed your CSS a little bit, separating the classes and defining another properties.
I recommend you see more about Aligning items in a flex container because it's an essencial property when working with responsive design.
Also you can see more about object-fit property.
#boxes {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
}
.container {
display: flex;
text-align: center;
}
.box > img {
object-fit: contain;
padding: 1%;
width: 80%;
width: 80%;
}
<section id="boxes">
<div class="container">
<div class="box">
<h3>Yosemite National Park</h3>
<img src="https://images.unsplash.com/photo-1629233796529-4a04bf1aee52?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Yosemite">
</div>
<div class="box">
<h3>Redwood National Park</h3>
<img src="https://images.unsplash.com/photo-1582790670329-b14bf5c38562?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Redwood">
</div>
<div class="box">
<h3>Joshua Tree National Park</h3>
<img src="https://images.unsplash.com/photo-1626008007279-f41981695728?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Joshua Tree">
</div>
</div>
<div class="container">
<div class="box">
<h3>Channel Islands National Park</h3>
<img src="https://images.unsplash.com/photo-1629256299843-5fb1714fe067?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Channel Islands">
</div>
<div class="box">
<h3>Seqouia National Park</h3>
<img src="https://images.unsplash.com/photo-1535628169704-5d0b32718ee8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Seqouia">
</div>
<div class="box">
<h3>Pinnacles National Park</h3>
<img src="https://images.unsplash.com/photo-1624244453711-e042e81529d9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&h=687&q=80" alt="Pinnacles">
</div>
</div>
</div>
</section>
for 2 dimensional layouts it's better to use CSS Grid which is pretty cool and also simple.
to use CSS Grid you need to set your container display to grid.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
align-content: center;
justify-content: center;
}
(height and width of images would be as big as the biggest one because justify-items and align-items by default are on stretch so it will give all images the same size but it may affect on your image quality because by stretching they wouldn't have proper ratio of width and height.)
you can also read grid documentation and use its other features to style it more specifically.
I am currently working on a website and what i want to do is I want four pictures in a square and I want a text underneath them.
I already managed to put the four images but once i try to put a text it goes everywhere but not underneath the images
HTML:
<section class="section-2">
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
</section>
CSS:
.section-2 {
margin: 200px 30px 30px 30px;
width:100%;
float:right;
min-height:1000px;
height:100%;
}
img {
float:left;
margin: 0px 100px 200px 150px;
width: 30%;
height: 30%;
The easiest and shortes way would be to simply align the cards in a grid. For that use display: grid;. To have 2 cards aligned horizontally you need to add: grid-template-columns: repeat(2, 1fr); you can change the number 2 with the numebr of cards you want to have aligned next to each other. To seperate the cards from each other, you can use grid-gap: with a value of the gap you want to have.
.section-2 {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 15px;
}
.section-2 div img {
width: 100%;
}
<section class="section-2">
<div>
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg">
<p>I'm a Syrian Hamster</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0736.jpg">
<p>I like to eat watermelons</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Areno/IMAG0865.jpg">
<p>I love to burrow tunnels and caves</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Areno/IMAG0863.jpg">
<p>And I really enjoy sleeping in my self digged caves</p>
</div>
</section>
I'm trying to set 9 images in a 3x3 grid, all of them with the same height and width. I'm able to set the grid but a couple of the images are smaller than the rest. Below I'm only showing how I formatted one of the photos. The HTML is the same for the other 8 photos.
.items {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.item > img {
object-fit: cover;
width: 100%;
max-height: 100%;
}
<div class="container">
<div class="items">
<div class="item">
<div class="item-image">
<img src="/dist/img_resources/vector1.jpg" alt="">
</div>
<div class="item-text">
<p class="item-description">Lorem, ipsum.</p>
<p class="item-price">$X.XX</p>
</div>
</div>
</div>
</div>
I'm starting to work with CSS Grid, I've been reading about the properties to help with responsiveness; so I'm trying to build a small grid with 6 elements; my intention is for them to show as 2 rows on larger devices like this:
And also to show them all stacked on smaller devices,so everything is good regarding the smaller devices, I'm using auto-fill so it stays responsive, however if I the view the page on a laptop screen or desktop it is able to fill one more column and ends up looking like this:
This is my grid layout code.
display: grid;
grid-template-columns: repeat(auto-fill, 260px);
justify-content: center;
row-gap: 18px;
column-gap: 18px;
Is there a way to keep the responsive behavior but setting a max number of columns as well? Any help is appreciated; If it can only be done with media-queries that's fine, but I'm first trying to look for ways to do it without using those. Also, I kinda made it work as intended by setting a horizontal padding to the whole grid container to compensate for the size of the additional column; but again, if there's a better way I'm all ears. Thank you!
Working Example
https://codepen.io/IvanS95/pen/NEYdxb
Use this syntax:
grid-template-columns: 260px 260px 260px;
Or
grid-template-columns: repeat(3,260px);
Instead of this:
grid-template-columns: repeat(auto-fill, 260px);
Use media queries to set less columns on smaller screens.
Also if the row and column gap is the same you can use grid-gap.
Documentation
.grid-container{
display: grid;
grid-template-columns: 260px 260px 260px;
grid-gap: 18px;
background-color: #fff;
color: #444;
justify-content: center;
}
.card {
border: 1px solid #000;
width: 260px;
height: 260px;
}
<div class="container">
<div class="grid-container">
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
</div>
</div>
Add this code to your CSS:
grid-template-columns: auto auto auto;
you just need to wrap them separately.
<div class="grid-container">
//grid-items
</div>
<div class="grid-container">
//grid-items (2nd row)
</div>
our same style code works until grid-container wraps.
In your code, white this html. Other, same css should work.
.grid-container{
display: grid;
grid-template-columns: repeat(auto-fill, 260px);
justify-content: center;
row-gap: 18px;
column-gap: 18px;
}
.card {
border: 1px solid #000;
width: 260px;
height: 260px;
}
<div class="container">
<div class="grid-container">
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
</div>
<div class="grid-container">
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
<div class="grid-item">
<div class="card"></div>
</div>
</div>
</div>
I am currently attempting to center (both vertically and horizontally) elements via a CSS grid. After some research, it seems IE11 supports css grid via different syntax than more up to date browsers (edge, safari, chrome, etc). I am using the display:-ms-grid syntax and this is putting my elements in the top left of the screen instead of centering them as I'd like. Note the posted example does work as expected with other browsers.
<div style="height:100%;display:grid;display:-ms-grid;">
<div style="margin:auto" align="center">
<img id="logo" alt="Logo" style="width:250px;margin-top:-5%" src="data:image/svg+xml;base64,xxxxxxxxxxxxx">
<div style="margin-top:2%;" class="loader"></div>
</div>
</div>
How can I overcome the centering issue for IE11?
Update: I've been able to center it vertically but not horizontally so far. This is where I'm currently at:
<div style="height:100%;display:grid;display:-ms-grid;-ms-grid-columns:2fr 2fr 2fr;">
<div style="margin:auto;-ms-grid-column:2;" align="center">
<img id="logo" alt="Logo" style="width:250px;margin-top:-5%" src="data:image/svg+xml;base64,xxxxxxxxxxxxx">
<div style="margin-top:2%;" class="loader"></div>
</div>
</div>
Looks like I found a working solution to support this in IE 10/11. Basically to define 3 columns and 3 rows for the -ms-grid by using -ms-grid-columns and -ms-grid-rows and then specify that my content I want centered should be in column 2 and row 2 (the middle) by using -ms-grid-column and -ms-grid-row. Solution as follows:
<div style="height:100%;display:grid;display:-ms-grid;-ms-grid-columns:2fr 2fr 2fr;-ms-grid-rows: 2fr 2fr 2fr;">
<div style="margin:auto;-ms-grid-column:2;-ms-grid-row:2;" align="center">
<img id="logo" alt="Logo" style="width:250px;margin-top:-5%" src="data:image/svg+xml;base64,xxxxxxxxxxxxx">
<div style="margin-top:2%;" class="loader"></div>
</div>
</div>
CSS
.grid-container {
height: 100%;
display: -ms-grid;
display: grid;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-ms-flex-line-pack: center;
align-content: center
}
HTML
<div class="grid-container">
<img id="logo" alt="Logo" width="250px" src="data:image/svg+xml;base64,xxxxxxxxxxxxx" />
</div>