ive been trying to make an infinite image slider with html and css, but everytime after the initial few photos are shown, there is a huge empty space before it starts appearing again, but 3 of the first photos dont show too. i can't seem to make it load infinitely like i want it to.
here's my html:
<div class="container">
<div class="banner">
<div class="profile">
<img src="img1.png" alt="">
</div>
<div class="profile">
<img src="img2.png" alt="">
</div>
<div class="profile">
<img src="img3.png" alt="">
</div>
<div class="profile">
<img src="img4.png" alt="">
</div>
<div class="profile">
<img src="img5.png" alt="">
</div>
<div class="profile">
<img src="img6.png" alt="">
</div>
<!---->
<div class="profile">
<img src="img1.png" alt="">
</div>
<div class="profile">
<img src="img2.png" alt="">
</div>
<div class="profile">
<img src="img3.png" alt="">
</div>
<div class="profile">
<img src="img4.png" alt="">
</div>
<div class="profile">
<img src="img5.png" alt="">
</div>
<div class="profile">
<img src="img6.png" alt="">
</div>
</div>
</div>
css:
.container{
height: 250px;
width: 90%;
position: relative;
display: grid;
place-items: center;
overflow: hidden;
}
.banner{
position: absolute;
overflow: hidden;
white-space: nowrap;
display: flex;
width: calc(250px*12);
animation: scroll 40s linear infinite;
}
.profile{
height: 500px;
width: 150px;
display: flex;
align-items: center;
padding: 15px;
perspective: 100px;
}
.profile img{
width: 100%;
}
thank you in advance!
This method - where there are 2 copies of the elements and a translation of -50% (so the changeover is seamless) - does not work if the elements do not cover at least the full width of the parent.
There are fixed widths in the code given which make it non responsive and on some viewports the elements will not cover the width.
This snippet removes the fixed 150px and sets the width of each element to a proportion of the viewport width to ensure coverage. If you need the actual images to be less than that in width you can of course set their width back down to 150px.
You will need to work on vertical height. The profiles have a height larger than the parent but with overflow hidden. That is a different question from getting the carousel to work however.
The keyframes are simple - a translation from 0 to half the overall width, so that the second section of images is replaced by the first half when it reaches the left hand side so making things look continuous.
.container {
height: 250px;
width: 90%;
position: relative;
/* display: grid;
place-items: center; */
overflow: hidden;
margin: 0 auto;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.banner {
position: absolute;
overflow: hidden;
white-space: nowrap;
/*display: flex;
width: calc(250px*12);*/
animation: scroll 40s linear infinite;
font-size: 0;
/* to get rid of gaps between inline elements */
}
#keyframes scroll {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
.profile {
height: 500px;
/* width: 150px; */
width: calc(100vw / 5);
display: inline-flex;
align-items: center;
padding: 15px;
perspective: 100px;
font-size: initial;
}
.profile img {
width: 100%;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<div class="container">
<div class="banner">
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
<!---->
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1015/200/300" alt="">
</div>
<div class="profile">
<img src="https://picsum.photos/id/1016/200/300" alt="">
</div>
</div>
Related
I have read all answers I think to this SO question, and changed max-height to every different combination, but it still bothers me;
I have the following
<div class="content-section">
<div class="products-container">
{% for p in current_lists %}
<div class="item">
<div class="image-wrapper">
<img src="{{p.image_url}}" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
{% endfor %}
</div>
</div>
and CSS
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
max-width: 30%;
max-height: 50%;
margin: 10px;
background-color: blueviolet;
}
img{
max-width: 100%;
max-height: 100%;
}
The issue is, that the image expands outside the blue-box
I have tried changing max-height in img and in products-container .item down to 10%, but it does not scale the big image at all.
If height is the issue then I would say:
width:100%;
height:auto;
I added your bits into a codepen and didn't have the issue:
https://codepen.io/liam88/pen/zYwYNVN
<div class="content-section">
<div class="products-container">
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/user/erondu/600x600" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/collection/190727/600x600" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/daily" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
</div>
</div>
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
max-width: 30%;
max-height: 50%;
margin: 10px;
background-color: blueviolet;
}
img{
width: 100%;
height: auto;
}
There is a possible solution:
Setting the container height to auto, and manipulating the width. I also used align-self: flex-end to make sure everything is aligned to the bottom of the blue-box. Using proportional images will make it all of them the same height, after auto-resizing.
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
width: 10%;
height: auto;
margin: 10px;
background-color: blueviolet;
align-self: flex-end;
}
.image-wrapper img{
max-width: 100%;
max-height: 100%;
}
<div class="content-section">
<div class="products-container">
<div class="item">
<div class="image-wrapper">
<img src="https://cdn-images.farfetch-contents.com/14/66/46/09/14664609_23855407_1000.jpg" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://images-na.ssl-images-amazon.com/images/I/41DIxdHUYCL._AC_UL1020_.jpg" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://assets.burberry.com/is/image/Burberryltd/2d6345e1d1fe0353ef7f95098c4d890c0a76ebb1.jpg?$BBY_V2_SL_1x1$&wid=2800&hei=2800" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
</div>
</div>
This question already has an answer here:
Centre images with text below [duplicate]
(1 answer)
Closed 1 year ago.
Hi Im making a website for a school project and I need the images to be centered in the middle of the page with the text below. Right now the images and text are pushed to the left side. I need the images to be centered in the page and spread out, with the text still below.
HTML
<section class="middle">
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
</section>
CSS
.middle {
position: relative;
overflow: hidden;
}
.rowone {
float: left;
width: 200px;
margin: 1% 1% 45px 1%;
}
.text {
position: absolute;
bottom: 0px;
margin: 30px;
background-color: gray;
}
.rowone img {
width: 100%;
display: block;
}
This should do the trick.
HTML
<section class="middle">
<center>
<br>
<br>
<br>
<br>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="images/logofootball.png" height="200" width="200">
<div class="text">Text</div>
</div>
</center>
</section>
CSS
.middle {
position: relative;
overflow: hidden;
}
.rowone {
padding-left: 100px;
float: left;
width: 200px;
margin: 1% 1% 45px 1%;
}
.text {
position: absolute;
bottom: 0px;
margin: 30px;
background-color: gray;
}
.rowone img {
width: 100%;
display: block;
}
Take a look at following example. This can be comfortable solution for you. Image and Text are wrapped by .wrapper and
flex: 1; basically says, that all wrapper elements should have the same size within .rowone .
.middle {
position: fixed;
top: 0;
left: 0;
width: 100%;
overflow: hidden;
}
.rowone {
display: flex;
}
.wrapper {
display: inline-block;
text-align: center;
max-height: 200px;
flex: 1;
}
.text {
background-color: gray;
}
img {
display: inline-block;
}
<section class="middle">
<div class="rowone">
<div class="wrapper">
<img src="https://via.placeholder.com/150">
<div class="text">Text</div>
</div>
<div class="wrapper">
<img src="https://via.placeholder.com/150">
<div class="text">Text</div>
</div>
<div class="wrapper">
<img src="https://via.placeholder.com/150">
<div class="text">Text</div>
</div>
<div class="wrapper">
<img src="https://via.placeholder.com/150">
<div class="text">Text</div>
</div>
</div>
</section>
Try to learn and use CSS Flex Container
Now your parent container .middle will look like this:
.middle{
display: flex; // this will help you align your elements.
justify-content: center; // with the center value, it will align your items at the center of your parent container.
align-items: center; // and this will align your items in the middle of the parent container which is what you're trying to achieve.
}
and you should leave out the float: left on your .rowone items. This is what makes your items be pushed on to the left side of the container.
I have used flex to spread out the images. I think it is the best way to divide a section into columns.
And a simple text align center for centered text.
JSFiddle
HTML
<section class="middle">
<div class="rowone">
<img src="https://via.placeholder.com/200" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="https://via.placeholder.com/200" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="https://via.placeholder.com/200" height="200" width="200">
<div class="text">Text</div>
</div>
<div class="rowone">
<img src="https://via.placeholder.com/200" height="200" width="200">
<div class="text">Text</div>
</div>
</section>
CSS
.middle {
/*position: relative;
overflow: hidden;*/
display: flex;
justify-content: space-between;
}
.rowone {
/*float: left;
width: 200px;
margin: 1% 1% 45px 1%;*/
text-align: center;
}
.text {
/*position: absolute;
bottom: 0px;
margin: 30px;*/
background-color: gray;
display:inline-block;
}
.rowone img {
width: 100%;
display: block;
}
I am trying to fit my images into the div which will be a inside a drag slider. I have tried a few object-fit properties but didnt seem to work. All of my images seem to take the entire width/height of the divs they are inside but I want there to be space between the images inside the div
<div class="homes-container">
<div class="home-container-item">
<div class="home-container-img">
<img src="./assets/content-images/desktop-development-1.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/desktop-development-2.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/desktop-development-3.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/desktop-development-4.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/mobile-development-1.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/mobile-development-2.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/mobile-development-3.jpg" alt="">
</div>
<div class="home-container-img">
<img src="./assets/content-images/mobile-development-4.jpg" alt="">
</div>
</div>
</div>
.homes-container{
position: absolute;
left: 10%;
top: 12%;
width: 80%;
height: 50rem;
overflow: hidden;
background: chartreuse;
}
.home-container-item{
position: absolute;
top: 0;
left: 0;
height: 100%;
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 20px;
pointer-events: none;
transition: 1s;
object-fit: scale-down;
}
You can set the image style width and height to 100%. This should fill the div with the image.
<style>
img {
width: 100%;
height: 100%;
}
</style>
Similar to this question, I want to create a perfect square grid, but inside of each grid cell, I want to place an image with 100% height.
The problem is because I did the padding-bottom: 100%; height: 0 hack, height: 100% no longer works on the image because the browser thinks the container has 0 height.
How can I set the height of the image to match that of the cell?
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 4px;
}
.item {
overflow: hidden;
position: relative;
background-color: red;
width: 100%;
padding-bottom: 100%;
height: 0;
}
img {
position: relative;
left: 50%;
transform: translateX(-50%);
display: block;
/*height: 100%;*/
}
<div class="grid">
<div class="item">
<img src="https://picsum.photos/400/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/500/250">
</div>
<div class="item">
<img src="https://picsum.photos/600/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/400">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
</div>
I should clarify that my intent is to stretch the image height to 100% but maintain aspect ratio. The left and right edges of the image should be clipped as needed, and the image should be centered.
You could create a "hidden" cell with a pseudo element (width:0px; height: 0px; padding-bottom: 100%;), overlap it with the real first cell and set all cells to the same height with grid-auto-rows: 1fr; and make the images absolutely positioned.
More in this article: https://medium.com/cloudaper/how-to-create-a-flexible-square-grid-with-css-grid-layout-ea48baf038f3
.grid::before {
content: '';
width: 0;
padding-bottom: 100%;
grid-row: 1 / 1;
grid-column: 1 / 1;
}
.grid > *:first-child {
grid-row: 1 / 1;
grid-column: 1 / 1;
}
.item {
overflow: hidden;
position: relative;
background-color: red;
width: 100%;
/* padding-bottom: 100%; */
/* height: 0; */
}
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
min-width: 100%;
min-height: 100%;
}
You could use object-fit: cover and then give the grid items an explicit height so that the replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. object-fit MDN
This would replace your height: 0 and padding-bottom: 100% hack. When using object-fit: cover, one thing to note is that if the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.
You could not use object-fit: cover with the following CSS and the images will continue to fill 100% of the grid items height, just without the "clipped to fit" part.
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 4px;
}
.item {
overflow: hidden;
position: relative;
background-color: red;
width: 100%;
height: 18rem; /* vary this to what you need */
}
img {
display: block;
max-width: 100%;
object-fit: cover;
width: 100%;
height: 100%;
}
<div class="grid">
<div class="item">
<img src="https://picsum.photos/400/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/500/250">
</div>
<div class="item">
<img src="https://picsum.photos/600/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/400">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
</div>
You can do it like below:
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 4px;
}
.item {
background-color: red;
aspect-ratio: 1; /* square items */
}
img {
/* no size contribution */
width: 0;
height: 0;
/* */
/* fill the item space*/
min-height: 100%;
min-width: 100%;
/* */
display: block;
object-fit: cover; /* avoid distortion*/
}
<div class="grid">
<div class="item">
<img src="https://picsum.photos/400/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/500/250">
</div>
<div class="item">
<img src="https://picsum.photos/600/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/400">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
<div class="item">
<img src="https://picsum.photos/300/300">
</div>
</div>
I am currently trying to create an Image Gallery where the user can scroll through the images for each page horizontally (left-right swiping as this is meant for mobile phones and tablets). I can not figure out where I went wrong, anyone have any idea?
My code is found at
http://jsfiddle.net/8tDYk/
CSS:
.media{
width: 100%;
height: 288px;
float: left;
display: inline-block;
overflow-x: auto;
-ms-overflow-x: auto;
overflow-y: hidden;
-ms-overflow-y: hidden;
white-space: nowrap;
}
.image{
float: left;
height: 240px;
width: 75%;
margin: 3.2%;
text-align:center;
border: 7px solid white;
}
HTML:
<div class="media">
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
</div>
If an element has float:left;, it will position itself to the right of the previous element (if there is space)
Otherwise, the element will position itself on the line beneath it.
You can see this when you set overflow-y: visible; on .media
One way of preventing this from happening, is to make an extra inner div, in which you set the width explicitely (Javascript is a big help there) like this:
<div class="media">
<div class="imageContainer">
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt="">
</div>
</div>
</div>
You could then use jquery to set a width for imageContainer like this:
<script type="text/javascript">
var imageWidth = 240;
$(document.ready()
{
$(".imageContainer").width($(".image").length*imageWidth);
});
</script>
::UPDATE::
Working jsfiddle here
The problem is in your .media element,it is set to 100% so how the overflow-x will work.Check this fiddle.
According to this the content you want to scroll is in another div having different style.
The Markup is
<div class='scroll'>
<div class="media">
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt=""/>
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt=""/>
</div>
<div class="image">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSGr4F-VQME__rGsSQitz9PkWGJ7C1XbUlhkleUw-5JVyYImWJ6hg" height="250px" style="max-width: 100%;" alt=""/>
</div>
</div></div>
The CSS is
.media
{
width: 100%;
height: 288px;
float: left;
display: inline-block;
overflow-x: auto;
-ms-overflow-x: auto;
overflow-y: hidden;
-ms-overflow-y: hidden;
white-space: nowrap;
}
.image
{
float: left;
height: 240px;
width: 75%;
margin: 3.2%;
text-align:center;
border: 7px solid white;
}
Here is Fiddle
in your .image class set width in pixels as
.image
{
width: 400px;
.....
}