I am working in a Nuxt app and using CSS based smooth scrolling to anchor links. The smooth scroll itself works but the scroll-margin property is achieving nothing... it is seemingly completely ignored. The exact same code works fine outside of the Nuxt app so I wonder whether it is something to do with Nuxt.
Slimmed down version of the code is as follows and I have also created a (working) non-Nuxt codepen to show the code in action outside of Nuxt.
For the sake of clarity, I want to use CSS only to achieve smooth scrolling with a scroll margin in Nuxt. I do not want to use JavaScript or built in Vue / Nuxt features. If I have to I will but, at the very least, I'd like to know why scroll-margin is not doing anything.
HTML
<main>
<header>Fixed header</header>
<section>Click this link to scroll to a section further down the page and show the targeted state. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quidem iste fuga quae fugit molestiae accusamus dolorum ea doloremque veritatis totam! Eum exercitationem nostrum nam doloribus, blanditiis quidem inventore perspiciatis ullam?</section>
<section>Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum architecto explicabo accusamus! Ab fuga fugiat hic recusandae, quo, dignissimos tempore velit aliquam facere, accusamus explicabo pariatur at enim modi doloremque.</section>
<section>Lorem ipsum dolor sit amet adipisicing elit. Quidem iste fuga quae fugit molestiae accusamus dolorum ea doloremque veritatis totam! Eum exercitationem nostrum nam doloribus, blanditiis quidem inventore perspiciatis ullam?</section>
<section id="scroll-section"> <strong class="show-on-target">Check out the space between this block and the header 👆 The block is clear of the header thanks to the <code>scroll-margin</code> property. The block was blue before you clicked the link and this text wasn't here either.</strong> Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum architecto explicabo accusamus! Ab fuga fugiat hic recusandae, quo, dignissimos tempore velit aliquam facere, accusamus explicabo pariatur at enim modi doloremque.</section>
<section>Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum architecto explicabo accusamus! Ab fuga fugiat hic recusandae, quo, dignissimos tempore velit aliquam facere, accusamus explicabo pariatur at enim modi doloremque.</section>
<section>Lorem ipsum dolor sit amet adipisicing elit. Quidem iste fuga quae fugit molestiae accusamus dolorum ea doloremque veritatis totam! Eum exercitationem nostrum nam doloribus, blanditiis quidem inventore perspiciatis ullam?</section>
</main>
SCSS
* {
box-sizing: border-box;
font-family: helvetica;
margin-inline: 0;
line-height:1.5;
}
html {
scroll-behavior: smooth;
}
header {
position: fixed;
display: grid;
place-content: center;
inset-block-start: 0;
inset-inline-start: 0;
width: 100%;
height: 3rem;
background: #ed6a5a;
font-size: 1.5rem;
}
section {
background: #9bc1bc;
padding: 50px 30px;
&:nth-child(2n) {
background: #f4f1bb;
}
&:first-of-type {
padding-block-start: 20vh;
}
}
strong {
font-size: 1.2em;
}
code {
font-family: monospace;
}
:target {
scroll-margin: 5rem;
background: #F2D7EE;
.show-on-target {
display: block;
margin-block-end: 20px;
}
}
.show-on-target {
display: none;
}
What I tried:
Using the code above to create a CSS-only smooth-scroll including a scroll-margin. I also tried tweaking the code above to use section, [id], section[id] and #myAnchor in place of :target but to no avail.
What I was expecting:
Page to scroll smoothly to the anchor leaving a 5 rem margin above it.
What actually happens:
Page scrolls smoothly to the anchor but with no margin above it
Related
I'm trying to find a way to make an element resizable so that it will simultaneously reveal more of the element's content as the height increases while covering the content of the other element that it is taking space from.
An example of this is of Gmail's left sidebar, these two images show before and after the resize drag: Sidebar Before - Sidebar After
I was thinking it could be done with the CSS resize property, but while the lower element can be resized downwards, it won't resize upwards taking space from the above element, and the resize grabber also seems to be difficult to change its position from the bottom right to the top.
Thank you in advance.
You can use the resize property together with a flex setting that causes the other element to occupy the rest of the space.
html,
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
height: 80vh;
margin:1em;
}
.container>div {
padding: 1em;
border: 2px solid rgba(0, 0, 0, 0.5);
overflow: auto;
}
.top {
resize: vertical;
height: 50%;
}
.bottom {
flex: 1;
}
<div class="container">
<div class="top">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga repellat laborum maxime ipsam sed quisquam, numquam eligendi doloribus non nisi unde vero deleniti velit dolore nesciunt error, sapiente provident at sint sunt! Cumque nostrum, placeat nisi,
minima, maiores molestiae quas praesentium mollitia hic natus suscipit nemo accusantium, beatae itaque rem aut omnis? Quisquam, natus dicta eveniet! Voluptates repellat, assumenda fugit. At, culpa quos nulla. Dolorem et modi quisquam velit quaerat
aspernatur iste natus eum magnam vero, id eius expedita quo placeat maiores enim quas iusto amet dolor ullam aperiam sequi laboriosam. Omnis voluptas, a exercitationem cum quidem quaerat ullam deleniti.
</div>
<div class="bottom">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet a magni culpa quidem in et facilis, ducimus voluptate explicabo saepe repellat, eius sequi molestias voluptatum! Maiores animi quasi quia nisi, pariatur id aperiam! Veritatis error molestias
minima modi suscipit ipsam eius sed perspiciatis est nobis illo, incidunt itaque harum, distinctio?
</div>
</div>
I have this css and its working fine the text appears at right and is aligned.
However the same code of that fiddle in a pdf, the text is apeparing like this (like the example at right, at left is how it should display):
Do you know what can be the issue?
.container {
display: flex;
}
.span {
margin-right: 5px;
}
<div class="container">
<span class="span">1.</span>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sint vel aut quidem consequatur quaerat optio ab! Iste fugit nostrum odio dolorum sequi, odit ratione omnis, atque sunt perferendis commodi, iure.</span>
</div>
You can solve this problem by using text-align
.container {
display: flex;
text-align: justify;
text-justify: inter-word;
}
.span {
margin-right: 5px;
}
<div class="container">
<span class="span">1.</span>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sint vel aut quidem consequatur quaerat optio ab! Iste fugit nostrum odio dolorum sequi, odit ratione omnis, atque sunt perferendis commodi, iure.</span>
</div>
#mehran text-justify:inter-word; is not a valid CSS property
I have a simple scroll snapping code below which works perfectly in Firefox 80.0.1 where as in Google Chrome v85.0.4183.102 he scrolling behavior isn't smooth and there is a delay in the snapping effect. Here is a link to a screen recording in Firefox and Google Chrome. From what I know scroll snapping should be supported in the major browsers as from caniuse.
Here are my code snippets
<body>
<section>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni cumque ut veritatis accusantium exercitationem, accusamus dolores eveniet. Adipisci hic soluta reiciendis nihil accusantium quam facilis. Animi earum culpa voluptate magni.</section>
<section>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni cumque ut veritatis accusantium exercitationem, accusamus dolores eveniet. Adipisci hic soluta reiciendis nihil accusantium quam facilis. Animi earum culpa voluptate magni.</section>
<section>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni cumque ut veritatis accusantium exercitationem, accusamus dolores eveniet. Adipisci hic soluta reiciendis nihil accusantium quam facilis. Animi earum culpa voluptate magni.</section>
</body>
html{
margin: 0;
padding: 0;
overflow-x: hidden;
scroll-snap-type: y mandatory;
overflow-y: scroll;
scroll-behavior: smooth;
}
body{
scroll-behavior: smooth;
}
section{
width: 100vw;
height: 100vh;
scroll-snap-align: start;
}
Here is a workable demo.
https://jsfiddle.net/cb5nraxq/
Is this a known bug on Google Chrome or did I set something wrong? I'm on Windows BTW
EDIT
The issue only occurs when scrolling use mousewheel
<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
.content {
width: 200px;
height: 150px;
padding: 20px;
background: yellow;
overflow: scroll;
box-sizing: border-box;
}
http://jsfiddle.net/v6yjLdnp/
Why doesn't the padding-bottom work in this case when scrolling? And how do I make this possible?
I suppose you mean the padding at the bottom of the scrolled content. This seems to be a browser issue - see the comments. But with the following code it should work properly in all browsers.
Transfer some of the settings to the container, then it works as desired:
body {
margin: 0;
}
.container {
width: 200px;
height: 150px;
overflow: scroll;
}
.content {
padding: 20px;
background: yellow;
}
<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
The problem is probably be caused by the scroll bars.
This is a workaround with using pseudo elements in css, but it might solve your problem:
.content::after { content: ''; display: block; width: 100%; height: 20px; }
EDIT: Remove the padding bottom of the content element to make it work in all browsers:
.content { padding-bottom: 0px; }
What should I expect? I expect the aside to grab the bottom of the viewport by 30px as you scroll down.
You need to run this in Firefox as it's the only browser that supports position: sticky.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
line-height: 1.5;
color: #fff;
padding: 3rem;
}
p {
padding: 30px;
}
img {
max-width: 100%;
display: block;
margin-bottom: 30px;
}
img:last-of-type {
margin-bottom: 0;
}
section {
background: #00ff7f;
max-width: 700px;
min-height: 1000px;
margin: auto auto 30px;
}
section:after {
content: '';
display: table;
clear: both;
}
article {
background: #1e90ff;
padding: 30px;
float: left;
width: calc(100% * 2 / 3 - 30px);
margin-right: 30px;
}
aside {
background: #ff6347;
float: left;
width: calc(100% * 1 / 3);
position: -webkit-sticky;
position: sticky;
bottom: 30px;
}
<section>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, quas? Maxime dolore explicabo nostrum quidem suscipit eveniet libero quam voluptatibus, recusandae exercitationem assumenda voluptates nulla vel temporibus, fuga possimus dolores officiis veritatis quae maiores nisi impedit itaque? Quos optio libero, non nostrum iste, quo natus ex, eos sint itaque omnis? Illum eos, ducimus accusamus totam quasi consequuntur consectetur excepturi temporibus blanditiis aut laborum esse dolorum modi, explicabo illo eligendi cum architecto veniam?
</article>
<aside>
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
</aside>
</section>
<section>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur sunt, unde fuga, ex tempora nam molestiae autem maiores ducimus, debitis, impedit mollitia laboriosam! Nulla esse voluptatibus laudantium ipsa illo adipisci pariatur, nostrum perferendis quo saepe magnam, veritatis commodi doloribus inventore aspernatur cupiditate assumenda. Cupiditate porro dolorum quo magni ipsum adipisci sit repellendus dolore voluptatibus, omnis enim unde velit quidem expedita iusto modi maxime placeat reprehenderit dignissimos fuga ad atque soluta. Soluta velit natus qui ad hic suscipit dolorum vitae minus quisquam necessitatibus asperiores illum sed dolorem debitis, laborum, praesentium quos dicta molestiae. Iusto animi perferendis dignissimos ut necessitatibus nostrum earum pariatur velit reiciendis quibusdam veritatis neque saepe, mollitia, unde ab ipsam.
</p>
</section>
http://codepen.io/corysimmons/pen/adjabm?editors=1100
I expect the aside to grab the bottom of the viewport by 30px as you scroll down.
You're close. Sticky positioning means that the aside will hug the bottom of the viewport, but only when it would otherwise exceed the bounds of the viewport. Otherwise, it will behave like a regular, relatively positioned box. This is what distinguishes position: sticky from position: fixed — the latter means the box always hugs the bottom of the viewport, regardless of its surrounding layout and of the scroll position.
Because both your article and aside are floating, the aside element doesn't ever get pushed past the bottom of the viewport, and so it doesn't get a chance to stick to the viewport.
Removing the float declaration from the article element — thereby keeping it in the flow — allows it to push the aside beyond the viewport. Note that if the aside is taller than the viewport (depending on how you view the snippet), a bottom sticky position will scroll as far as it needs to get 30px (the value of bottom) away from the viewport before it begins to stick, after which it will scroll away once the bottom of its containing block (the section) exceeds 30px distance from the viewport.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
line-height: 1.5;
color: #fff;
padding: 3rem;
}
p {
padding: 30px;
}
img {
max-width: 100%;
display: block;
margin-bottom: 30px;
}
img:last-of-type {
margin-bottom: 0;
}
section {
background: #00ff7f;
max-width: 700px;
margin: auto auto 30px;
}
section:after {
content: '';
display: table;
clear: both;
}
article {
background: #1e90ff;
padding: 30px;
width: calc(100% * 2 / 3 - 30px);
margin-right: 30px;
}
aside {
background: #ff6347;
float: left;
width: calc(100% * 1 / 3);
position: -webkit-sticky;
position: sticky;
bottom: 30px;
}
<section>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, quas? Maxime dolore explicabo nostrum quidem suscipit eveniet libero quam voluptatibus, recusandae exercitationem assumenda voluptates nulla vel temporibus, fuga possimus dolores officiis veritatis quae maiores nisi impedit itaque? Quos optio libero, non nostrum iste, quo natus ex, eos sint itaque omnis? Illum eos, ducimus accusamus totam quasi consequuntur consectetur excepturi temporibus blanditiis aut laborum esse dolorum modi, explicabo illo eligendi cum architecto veniam?
</article>
<aside>
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
</aside>
</section>
<section>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur sunt, unde fuga, ex tempora nam molestiae autem maiores ducimus, debitis, impedit mollitia laboriosam! Nulla esse voluptatibus laudantium ipsa illo adipisci pariatur, nostrum perferendis quo saepe magnam, veritatis commodi doloribus inventore aspernatur cupiditate assumenda. Cupiditate porro dolorum quo magni ipsum adipisci sit repellendus dolore voluptatibus, omnis enim unde velit quidem expedita iusto modi maxime placeat reprehenderit dignissimos fuga ad atque soluta. Soluta velit natus qui ad hic suscipit dolorum vitae minus quisquam necessitatibus asperiores illum sed dolorem debitis, laborum, praesentium quos dicta molestiae. Iusto animi perferendis dignissimos ut necessitatibus nostrum earum pariatur velit reiciendis quibusdam veritatis neque saepe, mollitia, unde ab ipsam.
</p>
</section>