To create a “fixed border” that remains visible at the edge of the page while scrolling, you can use a combination of CSS properties such as position:fixed and the ::before/::after pseudo elements. Here’s how to achieve this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sticky Border Example</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="content"> <!-- Your page content goes here --> <p>Scroll down to see the sticky border in action...</p> </div> </body> </html>
CSS:
- The
::before
and::after
pseudo-elements create the top and bottom borders. - Additional
::before
and::after
pseudo-elements create the left and right borders. - These elements are fixed in position to ensure they stay in place during scrolling.
body, html { margin: 0; padding: 0; height: 100%; overflow: auto; } .content { height: 200vh; /* For demonstration purposes */ padding: 20px; } body::before, body::after { content: ''; position: fixed; width: 100%; height: 10px; background-color: black; z-index: 9999; } body::before { top: 0; left: 0; } body::after { bottom: 0; left: 0; } body::before, body::after { content: ''; position: fixed; height: 100%; width: 10px; background-color: black; z-index: 9999; } body::before { top: 0; left: 0; } body::after { top: 0; right: 0; }
Explanation:
- HTML: A simple structure with a
div
containing the page content. - CSS:
- The
::before
and::after
pseudo-elements create the top and bottom borders. - Additional
::before
and::after
pseudo-elements create the left and right borders. - These elements are fixed in position to ensure they stay in place during scrolling.
- The
With this setup, you’ll have a border around the edges of the page that remains visible as you scroll. Adjust the background-color
and dimensions as needed
Leave a Reply