Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Topics

CSS scroll-margin-inline-start Property

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

Photo Credit to CodeToFun

🙋 Introduction

The scroll-margin-inline-start property in CSS is used to set the margin at the start of the inline direction (left in left-to-right writing modes, right in right-to-left writing modes) for a scroll container.

This property is useful when you want to create space around an element when it is being scrolled into view, ensuring it is not positioned directly at the edge of the container.

💡 Syntax

The syntax for the scroll-margin-inline-start property is straightforward and can be applied to any scrollable element.

Syntax
Copied
Copy To Clipboard
element {
  scroll-margin-inline-start: length | percentage;
}

🎛️ Default Value

The default value of the scroll-margin-inline-start property is 0, meaning no additional margin is added at the start of the inline direction when the element is scrolled into view.

🏠 Property Values

ValueDescription
lengthSpecifies a fixed size margin, such as 10px, 1em, etc.
percentageSpecifies a margin as a percentage of the scroll container's inline size, such as 5%.

📄 Example

In this example, we'll add a margin of 20px to the start of the inline direction for a div element when it is scrolled into view.

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-inline-start Example</title>
  <style>
    .container {
      width: 200px;
      height: 200px;
      overflow: auto;
      border: 1px solid #ccc;
    }
    .content {
      width: 500px;
      height: 500px;
    }
    .target {
      scroll-margin-inline-start: 20px;
      background-color: lightblue;
      width: 100px;
      height: 100px;
      margin: 200px 0;
    }
  </style>
</head>
<body>
  <h1>Scroll Margin Inline Start Example</h1>
  <div class="container">
    <div class="content">
      <div class="target">Scroll to me</div>
    </div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The scroll-margin-inline-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-inline-start property is a useful addition to your CSS toolkit, allowing you to control the positioning of elements when they are scrolled into view.

By adding margins, you can enhance the user experience by preventing elements from being positioned too close to the edge of their container. Experiment with different values to see how this property can improve the layout and usability of 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