scroll-margin-inline
Quick Summary for scroll-margin-inline
The scroll-margin-inline shorthand property sets the scroll margins of an element in the inline dimension.
Code Usage for scroll-margin-inline
/* <length> values */ scroll-margin-inline: 10px; scroll-margin-inline: 1em .5em ;  /* Global values */ scroll-margin-inline: inherit; scroll-margin-inline: initial; scroll-margin-inline: revert; scroll-margin-inline: unset; 
More Details for scroll-margin-inline

scroll-margin-inline

The scroll-margin-inline shorthand property sets the scroll margins of an element in the inline dimension.

Constituent properties

This property is a shorthand for the following CSS properties:

scroll-margin-inline-end scroll-margin-inline-start

Syntax

/* <length> values */ scroll-margin-inline: 10px; scroll-margin-inline: 1em .5em ;  /* Global values */ scroll-margin-inline: inherit; scroll-margin-inline: initial; scroll-margin-inline: revert; scroll-margin-inline: unset; 

Values

<length>

An outset from the corresponding edge of the scroll container.

Description

The scroll-margin values represent outsets defining the scroll snap area that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container's coordinate space), then adding the specified outsets.

Formal definition

Initial value0
Applies toall elements
Inheritedno
Computed valueas specified
Animation typeby computed value type

Formal syntax

<length>{1,2}

Examples

Simple demonstration

This example implements something very similar to the interactive example above, except that here we'll explain to you how it's implemented.

The aim here is to create four horizontally-scrolling blocks, the second and third of which snap into place, near but not quite at the right of each block.

HTML

The HTML that represents the blocks is very simple:

<div class="scroller">   <div>1</div>   <div>2</div>   <div>3</div>   <div>4</div> </div> 
CSS

Let's walk through the CSS. the outer container is styled like this:

.scroller {   text-align: left;   width: 250px;   height: 250px;   overflow-x: scroll;   display: flex;   box-sizing: border-box;   border: 1px solid #000;   scroll-snap-type: x mandatory; } 

The main parts relevant to the scroll snapping are overflow-x: scroll, which makes sure the contents will scroll and not be hidden, and scroll-snap-type: x mandatory, which dictates that scroll snapping must occur along the horizontal axis, and the scrolling will always come to rest on a snap point.

The child elements are styled as follows:

.scroller > div {   flex: 0 0 250px;   width: 250px;   background-color: #663399;   color: #fff;   font-size: 30px;   display: flex;   align-items: center;   justify-content: center;   scroll-snap-align: end; }  .scroller > div:nth-child(2n) {   background-color: #fff;   color: #663399; } 

The most relevant part here is scroll-snap-align: end, which specifies that the right-hand edges (the "ends" along the x axis, in our case) are the designated snap points.

Last of all we specify the scroll margin values, a different one for the second and third child elements:

.scroller > div:nth-child(2) {   scroll-margin-inline: 1rem; }  .scroller > div:nth-child(3) {   scroll-margin-inline: 2rem; } 

This means that when scrolling past the middle child elements, the scrolling will snap to 1rem outside the inline end edge of the second <div>, and 2rems outside the inline end edge of the third <div>.

Note: Here we are setting scroll-margin on the start and end of the inline axis (x in our case), but only the end edge is really relevant. It would work just as well here to only set a scroll margin on that one edge, for example with scroll-margin-inline: 0 1rem, or scroll-margin-inline-end: 1rem.

Result

Try it for yourself:

Specifications

Specification
CSS Scroll Snap Module Level 1 # propdef-scroll-margin-inline

See also

CSS Scroll Snap Well-Controlled Scrolling with CSS Scroll Snap

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

<frequency-percentage>

Experimental: This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
<frequency-percentage> css reference