Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS scroll-margin-block-start Property

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
👁️ 7 - Views
⏳ 4 mins
💬 1 Comment
CSS scroll-margin-block-start Property

Photo Credit to CodeToFun

🙋 Introduction

The scroll-margin-block-start property in CSS is used to set the margin at the start of the block axis for an element when it is scrolled into view.

This property is particularly useful for controlling the position of an element when it is brought into view via scrolling, such as with scrollIntoView(). The block axis depends on the element's writing mode, which means it will be vertical in horizontal writing modes and horizontal in vertical writing modes.

💡 Syntax

The syntax for the scroll-margin-block-start property is simple and allows you to set a margin using any CSS length value.

Syntax
Copied
Copy To Clipboard
element {
  scroll-margin-block-start: value;
}

Here, value can be a length (e.g., 10px, 2em, 5%, etc.).

🎛️ Default Value

The default value of the scroll-margin-block-start property is 0. This means that by default, no additional margin is added at the start of the block axis when the element is scrolled into view.

🏠 Property Values

ValueDescription
lengthSpecifies a fixed value for the scroll margin. This can be any valid CSS length unit such as px, em, rem, vh, etc.
percentageSpecifies a percentage of the element's containing block.

📄 Example

In this example, we'll set the scroll margin at the start of the block axis to 50px for a paragraph element.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS scroll-margin-block-start Example</title>
  <style>
    .container {
      height: 200px;
      overflow-y: scroll;
      border: 1px solid #ccc;
    }

    .content {
      height: 800px;
      background: linear-gradient(to bottom, #fff, #ccc);
    }

    .target {
      scroll-margin-block-start: 50px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <h1>CSS scroll-margin-block-start Example</h1>
  <div class="container">
    <div class="content">
      <p class="target">Scroll to me!</p>
    </div>
  </div>
  <button onclick="document.querySelector('.target').scrollIntoView();">Scroll to Target</button>
</body>
</html>

🖥️ Browser Compatibility

The scroll-margin-block-start property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The scroll-margin-block-start property is a useful tool for web developers aiming to control the scroll position of elements more precisely.

By setting the margin at the start of the block axis, you can ensure that elements are displayed in a visually appealing and accessible manner when scrolled into view. Experiment with different values to see how this property can improve the user experience on your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy