Put a text beneath an image - html

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>

Related

How to align Images properly in rows?

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.

Images stuck on same size and refuse to resize

I'm using a downloaded theme from a site and it had multiple versions of homepages. I'm using one of them but in the homepage there were set sizes for the images.
I have a dynamic site so the images will be loaded automatically. I'm trying to get rid of this stuck image size but it refuses, a single image is taking all over the container.
I've deleted all the code in that section. It was a container split into 3 parts, a side bar , middle part and right part. Because I won't know the sizes of my images I left the sidebar as is and I joined the middle and right part to be together. Now whenever I add an image it takes over the whole container and the other images just load under it which lead the container to drag more.
This is what my code looks like
.container {
display: grid;
grid-template-columns: repeat(3,1fr);
grid-auto-rows: 100px 300px;
grid-gap: 10px;
}
.gallery_container .gallery_Item {
width:100%;
height: 100%;
position: relative;
}
4.gallery_container .gallery_Item .image {
width: 100%;
height: 100%;
overflow: hidden;
}
.gallery_container .gallery_Item .img {
width:100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<div class="gallery_container">
<div class="gallery_Item">
<div class="imgInGallery">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPNy2qphPH0eDMdpoyfLsPz9D-pc6ntnktcw&usqp=CAU" />
</div>
</div>
</div>
<div class="gallery_container">
<div class="gallery_Item">
<div class="imgInGallery">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRVy3Qlw07-BSIBiWkNRxZEYSHJKqQgvynzwQ&usqp=CAU" />
</div>
</div>
</div>
<div class="gallery_container">
<div class="gallery_Item">
<div class="imgInGallery">
<img src="https://pqina.nl/media/cat.jpeg" />
</div>
</div>
</div>
<div class="gallery_container">
<div class="gallery_Item">
<div class="imgInGallery">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmK_e1W8uKhev51wmsAZVqNO0PVhOl9u2o3w&usqp=CAU" />
</div>
</div>
</div>
</div>

How do I control the size of images using CSS grid?

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>

How to show full images instead of partial images for square images

In my grid layout, each item has a square image. I'd like each square image to show the full image that I upload. Right now, it's showing only a partial image (see below).
All images that I upload are square-shaped btw. Struggling here, help would be much appreciated.
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
grid-row-gap: 80px;
grid-column-gap: 50px;
justify-content: start;
}
.grid .photo {
width: 100%;
background-size: contain;
background-position: center;
}
.grid .photo:after {
content: "";
display: block;
padding-bottom: 100%;
}
<div class="grid">
<article>
<a href="#">
<div class="photo" style="background-image: url(img/example1.jpg);"></div>
<div class="text">
<h3>Title</h3>
<p>This is a dummy text to show an example</p>
</div>
</a>
</article>
<article>
<a href="#">
<div class="photo" style="background-image: url(img/example2.jpg);"></div>
<div class="text">
<h3>Title</h3>
<p>This is a dummy text to show an example</p>
</div>
</a>
</article>
<article>
<a href="#">
<div class="photo" style="background-image: url(img/example3.jpg);"></div>
<div class="text">
<h3>Title</h3>
<p>This is a dummy text to show an example</p>
</div>
</a>
</article>
Setting background-size on the .photo class to either cover or contain could work. Likewise, the object-fit property might work...try a few of the values.
Personally though, I never really set the background image of an element, unless it is to be a full page background. I prefer to create an image within a div, then you can set the width/height/min-width/min-height/max-height/max-width properties accordingly

Specific columns

I've been doing some stuff with HTML and I need to have a few columns. I know how to make them and the basics of how they work. However, there is a certain problem that I have. I need to have 3 columns that have an image on top, then text on bottom. However, the text on bottom can't flow into the next column if the browser is resized - it just needs to go up or down. What I have so far:
body {
background-color: white;
font-family: times, serif;
color: black;
}
div {
display: flex;
margin: 50px;
flex-wrap: wrap;
}
<div>
<div class="first">
<img src="Images/australia_flag.jpg" alt="Australian Flag" title="Australian Flag" height="200" width="300"> text as well </div>
<div class="second">
<img src="Images/brazil_flag.jpg" alt="Brazilian Flag" title="Brazilian Flag"> even more text </div>
<div class="third">
<img src="Images/china_flag.jpg" alt="Chinese Flag" title="Chinese Flag" height="200" width="300"> text again
</div>
</div>
not entirely sure if you mean columns or rows? Based on your code, it looks like rows. If that's the case, I'm not sure what you mean by "flow into the next column"? You might check out the relative and absolute values for CSS position.
If, in fact, you do actually mean columns, I'd strongly advise using Bootstrap's Grid System. This is great for creating responsive columns.
Please take a look at this simple 3 column layout with a full width content area on top and bottom here: https://jsfiddle.net/7drfva0o/2/
.top, .bottom {
width:98%;
padding:1%;
background-color: red;
clear:both;
}
.cols {
width:31%;
padding:1%;
float:left;
background-color:blue;
border: 1px solid #FFF;
}
Is that what you're looking for?
First, you'll need to improve your markup: having images and texts as DOM node to be "flexed"
HTML markup improved
<div class="wrapper">
<div class="column">
<img src="..." />
<p>text</p>
</div>
<div class="column">
<img src="..." />
<p>text</p>
</div>
<div class="column">
<img src="..." />
<p>text</p>
</div>
</div>
Then, each of your div is going to have display: flex + flex-direction: column to allow the image going on top and the text going below. You will be able to adjust margin or whatever. At the minimum, I'd go like this:
CSS improved
.wrapper {
display: flex;
}
.column {
margin: 5px;
display: flex;
flex-direction: column;
}
Wrapped altogether, here is a snippet of what I think you're trying to achieve
Snippet
.wrapper {
display: flex;
}
.column {
margin: 5px;
display: flex;
flex-direction: column;
}
<div class="wrapper">
<div class="column">
<img src="http://fakeimg.pl/300x200" />
<p>text as well</p>
</div>
<div class="column">
<img src="http://fakeimg.pl/300x200" />
<p>text as well</p>
</div>
<div class="column">
<img src="http://fakeimg.pl/300x200" />
<p>text as well</p>
</div>
</div>
Then, feel free to play with flexbox properties to align, wrap, adjust alignments, etc. Great documentation on CSS-Tricks : https://css-tricks.com/snippets/css/a-guide-to-flexbox/