Adjusting div to not be absolutely positioned

Trying to create a div with animated borders, but it only works when positioned absolutely in the center. For illustration, here is the HTML and CSS code:

.bb,
.bb::before,
.bb::after {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.bb {
  width: 200px;
  height: 200px;
  margin: auto;
  color: #69ca62;
  box-shadow: inset 0 0 0 1px rgba(105, 202, 98, .5);
}

.bb::before,
.bb::after {
  content: '';
  z-index: -1;
  margin: -5%;
  box-shadow: inset 0 0 0 2px;
  animation: clipMe 8s linear infinite;
}

.bb::before {
  animation-delay: -4s;
}

.bb:hover::after,
.bb:hover::before {
  background-color: rgba(255, 0, 0, .3);
}

@keyframes clipMe {
  0%,
  100% {
    clip: rect(0px, 220px, 2px, 0px);
  }
  25% {
    clip: rect(0px, 2px, 220px, 0px);
  }
  50% {
    clip: rect(218px, 220px, 220px, 0px);
  }
  75% {
    clip: rect(0px, 220px, 220px, 218px);
  }
}

html,
body {
  height: 100%;
}

body {
  position: relative;
  background-color: #0f222b;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}
<div class="bb"></div>

Facing an issue where I require flexibility for positioning this element wherever needed. When trying to move it away from the center, the animation stops working.

Works correctly when centered: https://jsfiddle.net/uydkwb8f/

Removing the positioning causes it to fail: https://jsfiddle.net/uydkwb8f/1/

Seeking advice on making this design flexible without depending on specific positioning configurations.

Answer №1

To achieve the desired layout, wrap it in a div with a position:relative and then adjust the position of the container div to your specifications. Here is the CSS code you need to add:

.cont{
    position:relative;
    margin: 20px;
}
.cont{
    position:relative;
    margin: 20px;
}

.bb, .bb::before, .bb::after {
     position: absolute;
     top: 0;
     bottom: 0;
     left: 0;
     right: 0;
}
 .bb {
     width: 200px;
     height: 200px;
     color: #69ca62;
     box-shadow: inset 0 0 0 1px rgba(105, 202, 98, .5);
}
 .bb::before, .bb::after {
     content: '';
     z-index: -1;
     margin: -5%;
     box-shadow: inset 0 0 0 2px;
     animation: clipMe 8s linear infinite;
}
 .bb::before {
     animation-delay: -4s;
}
 .bb:hover::after, .bb:hover::before {
     background-color: rgba(255, 0, 0, .3);
}
 @keyframes clipMe {
     0%, 100% {
         clip: rect(0px, 220px, 2px, 0px);
    }
     25% {
         clip: rect(0px, 2px, 220px, 0px);
    }
     50% {
         clip: rect(218px, 220px, 220px, 0px);
    }
     75% {
         clip: rect(0px, 220px, 220px, 218px);
    }
}
 html, body {
     height: 100%;
}
 body {
     position: relative;
     background-color: #0f222b;
}
 *, *::before, *::after {
     box-sizing: border-box;
}
<div class="cont">
    <div class="bb"></div>
</div>

Answer №2

To resolve the issue, simply add position: relative to the element with class .bb.

The pseudo-elements .bb::after and .bb::before should remain absolutely positioned.

Please see the updated demo in the following link for a solution to your problem:

https://jsfiddle.net/ac1nmkbq/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for eliminating flicker upon the initial loading of a webpage caused by dynamic changes in CSS styles through JavaScript

Struggling with the load order of my CSS and JavaScript... Currently, I have a div with a blue background that is styled to appear sliced diagonally. However, upon initial page load (or CTRL+SHIFT+R), there's a brief moment where the slice effect doe ...

Do we need to use aria-labelledby if we already have label and input associated with "for" and "id"?

Here is the HTML structure for the Text Field component from Adobe Spectrum: The <label> element has a for attribute and the <input> has an id which allows screen readers to read out the label when the input is focused. So, why is aria-label ...

Overflow - conceal the scrollbar and prevent scrolling without cutting off the element

Whenever I implement the code snippet below: body { overflow: hidden; } The overflowing element gets clipped. However, when I try this code instead: body { overflow: auto; } The element is no longer clipped, but now a scrollbar appears where it ...

What are the implications of adding custom attributes to HTML elements?

Similar Questions: Custom attributes - Good or Bad? Non-Standard Attributes on HTML Tags. What are Your Thoughts? While working on a current learning project, I encountered the need to add an attribute that would store a numerical value. Initially ...

There seems to be a display issue with the DataTables tables

Utilizing Bootstrap 4, I followed the example provided by DataTables on their website: link. The specific code that was implemented can be found here: link Take a look at how it appears: http://pastebin.com/HxSNUnLq Any suggestions on how to resolve th ...

Try implementing a body template when using the mailto function

Is there a way to customize an HTML template for the mailto() function body? For example, suppose I have the following link: echo "<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f2faf6fefbd7f0faf6fefbb ...

What is the best way to retrieve the information stored in an object?

let items = { 1001: {item: 'Chocolates', price: 10, quantity: 10}, 1002: {item: 'Biscuits', price: 10, quantity: 10}, 1003: {item: 'Bread', price: 20, quantity: 5}, 1004: {item: 'Milk', price: 25, quantity: 5}, 1005: ...

What steps should be taken to validate an HTML5 form without actually submitting it?

I am working on a form that includes various HTML attributes such as: <form> <label>Full Name</label> <input type="text" name="firstname" class="span3" required="required"> <label>Email Address</label&g ...

In IE8, displaying an element with jQuery does not cause the footer to be pushed down

I've tried various methods to solve this issue, but none have been successful so far. To better illustrate my problem, I have created a FIDDLE. When the button is clicked, a hidden element is displayed. However, it does not adjust the footer position ...

Scraping the web using Python for HTML data retrieval

I'm working on a Python program to retrieve the card sub-brand and brand based on a card BIN number. The URL for this information is: . The code snippet below fetches the card type from the page. page = requests.get('https://www.cardbinist.com/s ...

Clone content upon pressing the Enter key

I am facing an issue with my editable container where I have a div inside it. Whenever I press enter, the div duplicates, resulting in two divs stacked on top of each other. The container has contenteditable set to true, which might be causing this problem ...

I am experiencing the issue of unexpected borders showing up on the HTML/CSS page that I designed

Greetings, amazing community at StackOverflow! I recently completed an HTML/CSS Project as part of a Codecademy Intensive course. If you're curious to see the project in action, feel free to check it out on GitHub by visiting this link. One thing I ...

Ways to customize the appearance of a specific column in a k-grid td

While working with a kendo grid, I encountered an issue with setting a tooltip for one of the columns. After some experimentation, I discovered that in order for the tooltip to display correctly, I needed to override the overflow property on the k-grid td ...

What is the best way to modify React state?

Can someone help me troubleshoot an issue with toggling React state after a button click? I want to be able to change "Work From Office" to "Work From Home" and vice versa, but it's only working once. Is there a way to achieve this using an if stateme ...

Issues with Laravel 5.8 and Bootstrap not functioning properly in Jquery

Check out this link for using Bootstrap Select. The code works perfectly in the view, but when I try to implement it with jQuery below, it doesn't work. Can someone help me fix this issue? View Code: <table id="tableAppointment" style=&q ...

What is the best way to trigger card opening - through clicking or hovering?

Once the Set tag/status button on the left is clicked, I aim to display a card similar to this one on the right side. What would be the best approach to achieve this in React? Should I use regular CSS, Material UI, or React-bootstrap? https://i.stack.img ...

What is the best way to incorporate progressive JPEG images onto a website?

I am currently working on a website called winni.in that is built using Java, Html, and Javascript. I would like to incorporate progressive image rendering upon page load, but I lack the necessary knowledge. I have come across and explored the following re ...

Explore the versatile Bootstrap Table for class

Recently, I created a table with the following structure: <table id="table" class="table table-bordered table-hover"> <thead> <tr> <th data-field="id" class="hidden">ID</th> <th data-fie ...

Use Javascript to set cookies and remember the show state when clicked

Can you offer some advice? I am looking to add cookies to my simple JavaScript code. Currently, the div is displayed when clicking on a link, but it hides again when the page reloads. I would like to implement cookies to remember the show state for 7 days. ...

In the production and development environments, the interpretation of CSS classnames is being rearranged

Currently utilizing create-react-app v2. I've encountered an issue with a component that has multiple classnames: "x15 x14 login-form__field". Strangely, in the production build, the order of '.x14' and 'login-form__field' seems t ...