I've successfully implemented the CSS scroll snap. However, I only applied it to some sections in the middle of my page. When you start scrolling from the top and a bit of the snapping sections already show, when you continue scrolling, the snapping already begins.
Is there a way for the scroll snap to begin only when the first section occupies the entire viewport? And the last, when you scroll from the bottom?
Thank you!
The code I've tried is below:
<div class="sticky-container">
<div class="sticky-section">
<h1>Slide One</h1>
</div>
<div class="sticky-section">
<h1>Slide Two</h1>
</div>
<div class="sticky-section">
<h1>Slide Three</h1>
</div>
</div>
.sticky-container {
height: 100%;
width: 100%;
scroll-snap-type: y mandatory;
overflow-y: scroll;
}
.sticky-section {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
scroll-snap-align: start;
}
I think this is what you are looking for.
body {
margin: 0;
}
.sticky-container {
height: 100vh;
width: 100vw;
scroll-snap-type: y mandatory;
overflow-y: scroll;
}
.sticky-section {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
scroll-snap-align: start;
}
<div class="sticky-container">
<div class="sticky-section">
<h1>Slide One</h1>
</div>
<div class="sticky-section">
<h1>Slide Two</h1>
</div>
<div class="sticky-section">
<h1>Slide Three</h1>
</div>
</div>
Related
I'm 99% sure about this but thought i'd ask to see if anyone knew of any clever solutions.
Basically I wish to place an image in a 1/1 aspect containing element, and then for that image to be as as as it possibly can be, eg, landscape images always touch the left and right sides and portrait ones touch the top and bottom.
I then want a line of caption text underneath the image.
My question is, is there any way to get that size of image but without (for landscape images) the large gap between the photo and the caption?
My example image below shows the problem (please note the image is not full left to right edge here like I want).
Anyone know of any clever options to achieve this?
<figure class="photo-1 landscape">
<div class="image-wrapper gallery">
<a href="https://farm66.staticflickr.com/65535/48808899778_bb1d4d1272_b.jpg" data-pswp-width="4032" data-pswp-height="3024" target="_blank">
<img src="https://farm66.staticflickr.com/65535/48808899778_bb1d4d1272_m.jpg">
</a>
</div>
<figcaption>Hersheypark</figcaption>
</figure>
figure {
display: flex;
flex-direction: column;
gap: 0.5rem;
.image-wrapper {
aspect-ratio: 1/1;
}
a {
display: block;
}
img {
display: block;
max-width: 100%;
max-height: 100%;
}
&.landscape {
a {
min-width: 100%;
}
img {
width: 100%;
}
}
&.portrait {
a {
min-height: 100%;
}
img {
height: 100%;
}
}
figcaption {
text-align: center;
color: hsl(var(--color-secondary));
font-size: var(--fs--1);
}
}
This is what I got so far.
body {
background-color: #F8F9F9;
}
.container {
display: flex;
gap: 20px;
}
.card {
display: flex;
flex-direction: column;
flex-basis: 0;
flex-grow: 1;
gap: 10px;
}
.image {
display: flex;
align-items: center;
background-color: #ffffff;
overflow: hidden;
}
.image.hor {
flex-direction: row;
}
.image.ver {
flex-direction: column;
aspect-ratio: 1/1;
}
.image img {
flex-grow: 1;
max-width: 100%;
max-height: 100%;
}
.caption {
padding: 5px 10px;
font-size: 14px;
text-align: center;
background-color: #ffffff;
}
<div class="container">
<div class="card">
<div class="image hor">
<img src="https://dummyimage.com/100x50/000/fff" />
</div>
<div class="caption">
Landscape image (small)
</div>
</div>
<div class="card">
<div class="image hor">
<img src="https://dummyimage.com/1000x500/000/fff" />
</div>
<div class="caption">
Landscape image (large)
</div>
</div>
<div class="card">
<div class="image ver">
<img src="https://dummyimage.com/50x100/000/fff" />
</div>
<div class="caption">
Portrait image (small)
</div>
</div>
<div class="card">
<div class="image ver">
<img src="https://dummyimage.com/500x1000/000/fff" />
</div>
<div class="caption">
Portrait image (large)
</div>
</div>
</div>
the issue is that i want the images to be the same size in the container but what i get is the container growing or shrinking to fit the image or the image stretching out of the container.
<div class="grid-container">
<article class="card">
<div class="content">
<h4>Title</h4>
<p>something about the website</p>
</div>
<div class="img-box">
<a href="#"><img src="./img/work-1.jpg" alt="">
</a>
</div>
</div>
.grid-container {
display: grid;
}
.card {
display: grid;
grid-template-columns: repeat(2, 1fr);
background-color: aqua;
margin: 1rem 0;
padding: 0.5rem;
align-items: start;
justify-items: start;
}
.card img {
display: block;
}
.img {
max-width: 100%;
}
this might help you
img{
height: auto;
width: 100%;
}
I have a div using the display flex method that contains other divs also using display flex to get the proper alignment that I want. I usually develop my web pages using Firefox and then later test them with other browsers to see if they work/display correctly there too.
I want to have a super-container that arranges its items in a normal flow. These items contains two images. These images are placed next to each other, and only their widths are fixed. The heights may differ per item. Below these two images I place a bit of text. All items are aligned such, that the first line of the texts are aligned with each other. The images should be placed in the horizontal middle of the item.
The following image shows how I want it to be displayed:
Firefox correct display. In Chrome and Edge (and Safari on iOS), the image looks like this: Incorrect display.
I have the following HTML and CSS:
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
display: flex;
justify-content: space-between;
width: 100%;
}
.ItemTopLeftContainer
{
align-items: flex-end;
background-color: #ff8888;
display: flex;
justify-content: flex-end;
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: #ff0000;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-items: flex-end;
background-color: #88ff88;
display: flex;
justify-content: flex-start;
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: #00aa00;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
What am I doing wrong or what am I missing to get the display in Chrome and Edge the same as in Firefox? To be clear: in Firefox the above code snippet gives the results I want, in Chrome and Edge (and Safari on iOS) they are wrong.
UPDATE: I am not yet allowed to comment on the answers directly, but I will do it this way. Both the answers did point me in the correct direction, though they did not produce the correct results I wanted. I have updated the code in this question to reflect the changes in CSS to create the layout I want.
I have adjusted your code below and included documentation in the source. Taken out the floats. Please note floats do not work inside a flexbox.
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
width: 100%;
display: flex; /* Added */
}
.ItemTopLeftContainer
{
align-items: flex-end;
display: flex;
height: 100%;
/* justify-content: right; "right" does not exist. */
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: red;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-self: flex-end; /* Replacing align-items */
/* Not necessary
display: flex;
height: 100%;
justify-content: left;
*/
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: green;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
width: 100%;
display: flex;
align-items: center;
}
.ItemTopLeftContainer
{
align-items: flex-end;
display: flex;
float: left;
height: 100%;
justify-content: right;
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: red;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-items: flex-end;
display: flex;
float: right;
height: 100%;
justify-content: left;
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: green;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
I am trying to vertically align divs with flexbox, by using the align-content: center. It works fine in Firefox and IE, but it does not vertically align in Chrome. In Chrome the divs stays on top.
<div class="products-wrapper">
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>
And CSS:
.products-wrapper {
width: 100%;
min-height: 100vh;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
}
.product {
max-width: 250px;
padding: 10px;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
I have also been trying align-items: center; which centers the divs vertically, but it will not be a straight line with the images and text if I have different amount of text for example.
Anyone with a solution how to vertically center divs (straight), which works in all browsers?
Image example:
Edit: If you don't need the height to be dynamic, align-items: center; will work with (for example) max-height: 400px; on the .product div. Not really what I wanted though.
In order to achieve what you want you'll need to use align-items: flex-start, this way all items will be aligned to the top of their line. And to center vertically you're looking for the align-items: center property which aligns the items in the flex container along the cross axis, which in your case you'll have to use in an outer wrapper to make sure the products div is centered in the page.
The align-content: center property comes into play only when there is more than one line (wrapped content).
A better explanation about the difference between those two properties is in the answer to this stackoverflow question
.outer-wrapper {
width: 100%;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.products-wrapper {
width: 100%;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
background-color: gray;
align-items: flex-start;
}
.product {
max-width: 250px;
padding: 10px;
background-color: red;
margin: 5px;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="outer-wrapper">
<div class="products-wrapper">
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>
</div>
</body>
</html>
Make the .product element, flex container. This so you can align the content inside them.
.product {
max-width: 250px;
padding: 10px;
display: flex;
justify-content: center;
flex-direction: column;
}
Hope this helps :)
.products-wrapper {
width: 100%;
min-height: 100vh;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
}
.product {
max-width: 250px;
padding: 10px;
display: flex;
justify-content: center;
flex-direction: column;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
<div class="products-wrapper">
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>
I'm using the following code to display 5 images (without the use of background) distributed evenly across a container and with an equal height equivalent to the tallest image (although I wonder if it would be possible to do this with the shortest one). Applying flex: 1; on the image-container gives me the desired effect under firefox, but under chrome the 5 images appear vertically stretched and oversized. The desired effect is to keep every container filled under a given width while minimizing loss of aspect ratio.
Edit: I've loaded this page under both browsers and I do get the effect I wanted although they are rendered differently. Now I need to find what's wrong with my version compared to the one I posted here.
body {
width: 300px;
}
.container {
display: flex;
flex-wrap: wrap;
height: 100%;
width: 100%;
background-color: black;
}
.row1 {
display: flex;
width: 100%;
background-color: blue;
}
.column {
height: 100%;
width: 20%;
display: flex;
flex-flow: column;
position: relative;
background-color: red;
}
.image-container {
overflow: hidden;
display: flex;
flex: 1;
background-color: yellow;
}
img {
width: 100%;
height: auto;
}
.image-label {
width: 100%;
height: 35px;
display: flex;
background-color: green;
justify-content: center;
}
.row2 {
display: flex;
width: 100%;
flex-flow: row wrap;
background-color: cyan;
height: 20px;
}
<div class="container">
<div class="row1">
<div class="column">
<div class="image-container">
<img src="http://www.koka.ac.jp/admission/wp-content/themes/admission/assets/img/common/floating-btn-internet.gif">
</div>
<div class="image-label">img1</div>
</div>
<div class="column">
<div class="image-container">
<img src="http://www.moglix.com/themes/et_poostyle/img/offer.jpg">
</div>
<div class="image-label">img2</div>
</div>
<div class="column">
<div class="image-container">
<img src="http://i.imgur.com/slhFy4T.png">
</div>
<div class="image-label">img3</div>
</div>
<div class="column">
<div class="image-container">
<img src="http://alma-sys.com/images/SLIDE%20MENU%20IMG/WEB%20D%20-%20Copy.jpg">
</div>
<div class="image-label">img4</div>
</div>
<div class="column">
<div class="image-container">
<img src="http://amerpages.com/app/webroot/img/items/121/16716/pds/19682_s.jpg">
</div>
<div class="image-label">img5</div>
</div>
</div>
<div class="row2"></div>
</div>