How to animate a left border shifting to the center using JavaScript

As I'm modifying my current div, I need to add a vertical line in the center of it. I've come across various solutions where a left border is added and then shifted using the left property by 50% (which effectively places it in the middle).

Here's how I'm structuring my div:

if (id == "Metricbar" ) {
      var Parentdiv = document.getElementById("homeContainer");
      var rowDiv = document.createElement("article");
      rowDiv.setAttribute("class", "col-sm-12 col-md-12 col-lg-12");
      rowDiv.style.border = "1px solid #ddd";
      rowDiv.style.marginBottom = "1px";
      rowDiv.style.marginTop = "1px";
      rowDiv.style.borderLeft = "2px solid #ddd";
      Parentdiv.appendChild(rowDiv);

      var ctrlDiv = document.createElement("div");
      ctrlDiv.setAttribute("data-widget-editbutton", "false");
      ctrlDiv.className = "jarviswidget";
      ctrlDiv.setAttribute("data-widget-fullscreenbutton", "false");
      ctrlDiv.id = id;
      var temp = $compile(ctrlDiv)($scope);
      angular.element(rowDiv).append(temp);
      rowDiv.appendChild(ctrlDiv);
} 

The question arises on how to centrally position this line. Is there a method to achieve that?

I have attempted the following:

rowDiv.style.borderLeft.marginLeft = "50%";
rowDiv.style.borderLeft.Left = "50%";
rowDiv.style.borderLeft.leftMargin = "50%";

Unfortunately, none of these approaches seem to be effective. Can anyone provide guidance in the right direction?

HTML File :

<section id="widget-grid" class="" style="padding-left:13px;padding-right:13px;">
   <div class="row" id="homeContainer"></div>
   <div class="modaldialogs" style="display: none">
      <div class="center">
         <img alt="" src="img/ajax-loader.gif" />
      </div>
   </div>
</section>

It's worth mentioning that I utilize this div multiple times based on its id.

The desired outcome is to have a black line displayed as depicted in this image https://i.sstatic.net/gitej.png

Answer №1

If you are looking to enhance the design of your article element by adding a vertical line, CSS provides a simple solution!

To achieve this effect, first apply either position: relative or position: absolute to the article element. Then, utilize the :after pseudo-element to create the desired vertical bar.

No changes are required in the HTML and Javascript code (aside from minor adjustments to the article borders), as only a few lines of CSS need to be added for the visual enhancement.

var Parentdiv = document.getElementById("homeContainer");
 var rowDiv = document.createElement("article");
 rowDiv.setAttribute("class", "col-sm-12 col-md-12 col-lg-12");
 rowDiv.style.marginBottom = "1px";
 rowDiv.style.marginTop = "1px";
 rowDiv.style.borderTop = "2px solid #ddd";
 rowDiv.style.borderRight = "2px solid #ddd";
 rowDiv.style.borderBottom = "2px solid #ddd";
 rowDiv.style.borderLeft = "2px solid #ddd";
 Parentdiv.appendChild(rowDiv);
#homeContainer article {
  height: 300px;           // For demo purposes since content is not included
  position: relative;
}

#homeContainer article:after {
  content: "";
  height: 100%;
  width: 0px;
  position: absolute;
  border-right: 3px solid #000000;
  right: 50%;
}
<section id="widget-grid" class="" style="padding-left:13px;padding-right:13px;">
  <div class="row" id="homeContainer"></div>
  <div class="modaldialogs" style="display: none">
    <div class="center">
      <img alt="" src="img/ajax-loader.gif" />
    </div>
  </div>
</section>

Answer №2

I stumbled upon a solution. By utilizing JavaScript, we can create a new div with a unique class name that includes a border on the left side.

Here is the proposed code:

var newElement = document.createElement('div');
newElement.className = "divider";
parentElement.appendChild(newElement); \\adding it to the parent element

For styling, you can use the following CSS:

.divider {
  content: "";
  position: absolute;
  z-index:1;
  top: 0;
  bottom: 0;
  left: 45%;
  border-left: 2px solid #C0C0C0;
}

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

Caution: The presence of <li> within another <li> is not permitted in validateDOMNesting()

I am facing a challenging background error warning that I am struggling to solve. As a beginner, I understand the need to change li, but I'm not sure where to begin. Can someone please assist me with this error warning: next-dev.js?3515:20 Warning: va ...

Is it possible to implement CSS code from a server request into a React application?

With a single React app that hosts numerous customer websites which can be customized in various ways, I want to enable users to apply their own CSS code to their respective sites. Since users typically don't switch directly between websites, applying ...

The WP archive.php template is malfunctioning

Having an issue with my website Whenever I try to view the monthly archive () the design appears flipped upside down. I initially assumed that if the archive.php file was missing, WordPress would default to using index.php for the archive page. So why is ...

Error: The document has not been defined - experimenting with vitest

I'm currently working on a Vite project using the React framework. I have written some test cases for my app using Vitest, but when I run the tests, I encounter the following error: FAIL tests/Reservations.test.jsx > Reservations Component > d ...

Guide on retrieving data parameter on the receiving page from Ajax response call

I am working on dynamically opening a page using Ajax to avoid refreshing the browser. The page opens and runs scripts on the destination page, but before running the script, I need to retrieve parameters similar to request.querystring in JavaScript. Belo ...

Is it possible to conceal solely the content within the Bootstrap Modal?

Is there a unique approach to only concealing the content within the modal, without hiding the modal itself? The goal is to click "next" and have the content of the next modal displayed, without reopening the modal itself but simply transitioning to the ne ...

PHP script redirects to its own URL successfully, but the functionality fails when triggered by CRON

I am facing an issue with a program that calls itself iteratively. redirect($_SERVER["PHP_SELF"]); function redirect($url,$seconds=0) { $html="<html><head><meta http-equiv='refresh' content='$seconds; URL=$url'>< ...

What is the proper location for inserting image copy code within my codebase?

I encountered an issue with using this code: copy('imgurl', 'images/covers/file.jpeg'); The code successfully copies an image URL to a file on my website when placed in a PHP page alone, but it fails to work within my actual code. He ...

Does anyone know if there is a way to use JavaScript to implement LESS in CSS files

Is there a way to perform variable substitution, especially in CSS using JavaScript? I am looking for a solution that offers more features than just variable substitution, such as the LESS syntax. Has anyone come across a JavaScript implementation of LES ...

Issue with arranging blocks in a horizontal line in Opera

I am currently experiencing an issue with a wide page that contains an information block and numerous images positioned to the right. The layout is designed to be used with a horizontal scrollbar. While this setup works perfectly in Firefox, Safari, Chrom ...

The $_REQUEST variable is functioning properly, while the $_POST variable seems to

I currently have an HTML form using the post method. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type='text/javascript& ...

Issues persisting with vertical alignment in table cells on Firefox browser

I've successfully managed to vertically center the contents of .onesizechart in Chrome and Safari, but I'm facing issues with Firefox and IE. Interestingly, the contents of .homepage-sizechart are displaying correctly, leading me to believe that ...

What is the method for obtaining the WordPress user ID?

I am looking to incorporate a Twitter follow button for all site authors on every post. Below is the structure of the Twitter button: <a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Foll ...

Increase the value of a number using jQuery

When using jquery to animate a random number increment from 0001 to 1000, the issue arises when reaching the number 0077. The final number displayed is 77 instead of 0077. Is there a solution to ensure the format remains as 0077? Your help is appreciated. ...

Using an HTML img tag without success, despite having the correct link

My HTML img tags are not working even though the link is correct. I have been attempting to resolve this issue for quite some time by researching multiple websites and trying various solutions, but unfortunately, nothing seems to be working. All I want i ...

I am a newcomer to HTML and I am eager to display the selected options from a <select> element

Can someone help me display the selected licorice flavor, chocolate color, and topping? For example: "Green apple, white, christmas sprinkles." I'm not sure if assigning a numeric value to each option is necessary. I did it on a whim. Please suggest ...

The font size of the textarea dynamically adjusts based on the size of the screen

Some strange behavior is occurring in the Android default browser when I set the width and height of a textarea to 100%. The font size of the textarea seems to increase based on the screen size, and even though I attempted to alert the font-size using jQue ...

Having trouble loading JSON api data in React?

Recently, I've delved into the world of React and was assigned a task to fetch data from a JSON API and showcase it on the screen. Initially, everything went smoothly while practicing with this URL - https://jsonplaceholder.typicode.com/users. Howeve ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

Troubleshooting problems with the width of CSS grid underlines within a scrolling container

There seems to be an issue with the underlining border in this example not extending across the entire width of the container. Any suggestions on how to resolve this would be greatly appreciated. .outer-container { width: 200px; overflow: auto; } . ...