CSS Properties
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.
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
Value | Description |
---|---|
length | Specifies a fixed value for the scroll margin. This can be any valid CSS length unit such as px, em, rem, vh, etc. |
percentage | Specifies 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS scroll-margin-block-start Property), please comment here. I will help you immediately.