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;
.....
}
Related
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>
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>
I use some inline styling in the HTML Doc. I would like to achieve a flexbox with n divisions where divs are squared. Within these divs I want to add certain images (here a placeholder)
I was looking up some other threads where there was padding used as a measure to adjust the box "height" since it is calculated upon width. However this solution only expands the current box in height (outlined with the blue border).
Has anyone a tip on how to avoid this?
EDIT: Apparently the padding solution works while using units like vh and vw instead of percentage and as long as I do not insert an image
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.image {
position: relative;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>
We now have access to aspect-ratio in CSS although it is poorly supported at the time of writing.
The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions.
.container {
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
margin: 2.5%;
flex: 1;
aspect-ratio: 1;
border: 5px solid blue;
overflow: hidden;
}
.image {
max-width: 100%;
display: block;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>
To manage this, there is a little known trick (tbh I didn't know you could do this) with setting an aspect ratio on divs of unknown/dynamic widths. See this article.
I ended up adding position: absolute for the images to not mess with the height of the divs after applying this 1:1 ratio for your scenario:
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
This might be what you are trying to do:
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
.image {
position: absolute;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>
Please see my HTML structure below - I am trying to have the .prod divs to be to the right of the logo, and the logo to be the full height of the .row div
I know this can be done using tables and floats but I want to try and avoid using those.
Here's my structure:
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
}
.row .prod {
width: 75%;
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Wrap the additional info inside another div and give it the remaining width.
I have given it 74% because of the extra space from inline-block elements. Adjust it to your requirement. I would prefer flexbox if you are implementing it for modern browsers.
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
vertical-align: top; /* Default to baseline, align to the top */
}
.row .product_info {
width: 74%; /* Remaining width */
}
.row .product_info .prod {
/* width: 75% */ Remove
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="product_info">
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
</div>
flexbox can do that.
.row {
display: flex;
}
.image,
.prod {
flex: 1;
background: lightblue;
text-align: center;
height: 75px;
}
.image {
flex: 0 0 auto;
background: orange;
display: flex;
flex-direction: column;
}
.image img {
height: 100%;
width: auto;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="http://lorempixel.com/output/food-q-c-50-50-1.jpg" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Flexbox?
I'm still pretty new to flexbox myself, but you could use the align-items: stretch property on your container to cause your items to stretch, but give the .prod div's a max-height so that they only stretch so much. You can also set various widths properties using the flex property on your flex items so that you get the width your after.
For more information: https://css-tricks.com/snippets/css/a-guide-to-flexbox/