Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS inset Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 14 - Views
⏳ 4 mins
💬 1 Comment
CSS inset Property

Photo Credit to CodeToFun

🙋 Introduction

The inset property in CSS is a shorthand property for setting the top, right, bottom, and left properties all at once.

This property can simplify your CSS by reducing the need to specify each side individually, making your code cleaner and more concise.

The inset property is especially useful when positioning elements with absolute or fixed positioning.

💡 Syntax

The syntax for the inset property allows you to define one, two, three, or four values, corresponding to the top, right, bottom, and left properties.

Syntax
Copied
Copy To Clipboard
element {
  inset: top right bottom left;
}
  • If one value is given, it applies to all four sides.
  • If two values are given, the first applies to the top and bottom, and the second to the right and left.
  • If three values are given, the first applies to the top, the second to the right and left, and the third to the bottom.
  • If four values are given, they apply to the top, right, bottom, and left respectively.

🎛️ Default Value

The default value of the inset property is auto for all sides, meaning the element will not have any specific positioning applied.

🏠 Property Values

ValueDescription
lengthSpecifies a fixed size for the offset (e.g., 10px, 2em).
percentageSpecifies the offset as a percentage of the containing block's size (e.g., 50%).
autoThe default value which means the position is determined by other factors (e.g., position property).

📄 Example

In this example, we'll position a box 10px from the top, 20px from the right, 30px from the bottom, and 40px from the left of its containing 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 inset Property Example</title>
  <style>
    .container {
      position: relative;
      width: 200px;
      height: 200px;
      background-color: #f0f0f0;
    }
    .box {
      position: absolute;
      inset: 10px 20px 30px 40px;
      background-color: #ff5733;
    }
  </style>
</head>
<body>
  <h1>Box with Custom Inset Property</h1>
  <div class="container">
    <div class="box"></div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The inset 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 inset property is a convenient shorthand for setting the top, right, bottom, and left properties in CSS.

By using this property, you can simplify your CSS and make your positioning rules more readable. Experiment with different values and see how this property can help streamline your web design process.

👨‍💻 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