How to make the connectors of my organization chart responsive?

I am creating an organization chart. Everything looks fine except the connectors look misalign. In responsive, they are more misaligned depending upon the content in each box. Is there a way to keep the connectors in sync on mobile and desktop? Thanks in advance.
Here is my code –
https://codepen.io/Shawnny-Tandon/pen/ExMJQMG

:root {
  --level-1: #8dccad;
}

.orgchart {
  margin: 0 auto;
  overflow: auto;
  height: 610px;
  width: 722px;
}

* {
  padding: 0;
  margin: 0;
}

ol {
  list-style: none;
}

.rectangle {
  position: relative;
  padding: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
  text-align: center;
  border: solid black 1px;
  font-size: 12px !important;
  transition: background-color 0.3s ease;
  /* Add a smooth transition effect */
  /* Add a default background color */
  background-color: #0071bc;
}


/* Add hover effect */

.rectangle:hover {
  background-color: #205493;
  /* Change the background color on hover */
}


/* LEVEL-1 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-1 {
  width: 50%;
  margin: 0 auto 40px;
  background: var(--level-1);
  text-align: center;
  color: white;
}


/* LEVEL-2 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-2-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-2-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: 105%;
  width: 2px;
  height: calc(99.5%);
  background: #a1b8e1;
}

.level-2-wrapper li+li {
  margin-top: 20px;
}

.level-2 {
  font-weight: normal;
  color: #fff;
}

.level-2::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 109%;
  transform: translate(-100%, -50%);
  width: 17px;
  height: 2px;
  background: #a1b8e1;
}

.oc {
  display: flex;
}


/* LEVEL-2 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-sub-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-sub-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: -10px;
  width: 2px;
  height: 23%;
  background: #a1b8e1;
}

.level-sub-wrapper li+li {
  margin-top: 20px;
}

.level-sub {
  font-weight: normal;
  background: #0071bc;
  color: #fff;
}

.level-sub::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-100%, -50%);
  width: 14px;
  height: 2px;
  background: #a1b8e1;
}

.level-sub-wrapper::after {
  content: "";
  position: absolute;
  top: 24.2%;
  left: 80px;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 21px;
  background: #a1b8e1;
}

.level-sub2 {
  font-weight: normal;
  color: #fff;
}

.orgchart a:visited {
  color: white;
  text-decoration: underline;
}

.orgchart a {
  color: #105183;
  text-decoration: none;
  font-size: 12px !important;
}


/* level-3 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-3-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-3-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: 105%;
  width: 2px;
  height: calc(74%);
  background: #a1b8e1;
}

.level-3-wrapper li+li {
  margin-top: 20px;
}

.level-3 {
  font-weight: normal;
  color: #fff;
}

.level-3::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 109%;
  transform: translate(-100%, -50%);
  width: 17px;
  height: 2px;
  background: #a1b8e1;
}
<div class="rxbodyfield">
  <div class="orgchart">
    <h2 class="level-1 rectangle">
    <a href="">CEO<br />Test1</a></h2>
    <div class="oc">
      <ol class="level-2-wrapper">
        <li>
          <div class="level-2 rectangle"><a href=""><strong>Manager1</strong><br />
Test ABCD</a></div>
        </li>
        <li>
          <div class="level-2 rectangle"><strong>Manager2</strong>
            <a href=""><br />
            </a>Test</div>
        </li>
        <li>
          <div class="level-2 rectangle"><strong>Manager3</strong><br /> Test
          </div>
        </li>
        <li>
          <div class="level-2 rectangle"><strong>Manager4</strong><br /> Senior Chief Counsel<br>fdffdg<br>dfddffdfdffd</div>
        </li>
      </ol>
      <ol class="level-3-wrapper">
        <li>
          <div class="level-3 rectangle"><strong>Manager5</strong><br /> Senior Deputy Director</div>
        </li>
        <li>
          <div class="level-3 rectangle"><strong>Manager 6</strong><br /> Blah blah blah blah</div>
        </li>
        <li>
          <div class="level-3 rectangle"><strong>Manager7</strong><br /> Test
          </div>
        </li>
      </ol>
      <ol class="level-sub-wrapper">
        <li>
          <div class="level-sub rectangle"><strong>Manager8</strong><br /> Senior Staff</div>
        </li>
        <li>
          <div class="level-sub2 rectangle" style=" margin-left: 20px;"><strong>Manager9</strong><br /> Executive Director</div>
        </li>
      </ol>
    </div>
  </div>
</div>

Because you used an explicit width value in your orgchart class. Try to set this property to the max-width or max-inline-size and give your some of your elements max-width to avoid to covering the whole line. Also add the flex-wrap: wrap; declaration to wrap your elements. Try the following code for a quick demo:

:root {
  --level-1: #8dccad;
}

.orgchart {
  margin: 0 auto;
  overflow: auto;
  height: 610px;
  /* width: 722px; */

  /* Add this declaration to make your container element responsive */
  max-width: 722px;
}

* {
  padding: 0;
  margin: 0;
}

ol {
  list-style: none;
}

.rectangle {
  position: relative;
  padding: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
  text-align: center;
  border: solid black 1px;
  font-size: 12px !important;
  transition: background-color 0.3s ease;
  /* Add a smooth transition effect */
  /* Add a default background color */
  background-color: #0071bc;
}


/* Add hover effect */

.rectangle:hover {
  background-color: #205493;
  /* Change the background color on hover */
}


/* LEVEL-1 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-1 {
  width: 50%;
  margin: 0 auto 40px;
  background: var(--level-1);
  text-align: center;
  color: white;
}


/* LEVEL-2 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-2-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-2-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: 105%;
  width: 2px;
  height: calc(99.5%);
  background: #a1b8e1;
}

.level-2-wrapper li+li {
  margin-top: 20px;
}

.level-2 {
  font-weight: normal;
  color: #fff;
}

.level-2::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 109%;
  transform: translate(-100%, -50%);
  width: 17px;
  height: 2px;
  background: #a1b8e1;
}

.oc {
  display: flex;

  /* Add this declation to wrap the flex items */
  flex-wrap: wrap;
}


/* LEVEL-2 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-sub-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-sub-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: -10px;
  width: 2px;
  height: 23%;
  background: #a1b8e1;
}

.level-sub-wrapper li+li {
  margin-top: 20px;
}

.level-sub {
  font-weight: normal;
  background: #0071bc;
  color: #fff;

  /* Add this declaration to avoid your element from covering the whole line */
  max-width: 190px;
}

.level-sub::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-100%, -50%);
  width: 14px;
  height: 2px;
  background: #a1b8e1;
}

.level-sub-wrapper::after {
  content: "";
  position: absolute;
  top: 24.2%;
  left: 80px;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 21px;
  background: #a1b8e1;
}

.level-sub2 {
  font-weight: normal;
  color: #fff;
}

.orgchart a:visited {
  color: white;
  text-decoration: underline;
}

.orgchart a {
  color: #105183;
  text-decoration: none;
  font-size: 12px !important;
}


/* level-3 STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.level-3-wrapper {
  flex: 1;
  padding: 5px;
  margin: 0 20px;
  position: relative;
}

.level-3-wrapper::before {
  content: "";
  position: absolute;
  top: -40px;
  left: 105%;
  width: 2px;
  height: calc(74%);
  background: #a1b8e1;
}

.level-3-wrapper li+li {
  margin-top: 20px;
}

.level-3 {
  font-weight: normal;
  color: #fff;
}

.level-3::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 109%;
  transform: translate(-100%, -50%);
  width: 17px;
  height: 2px;
  background: #a1b8e1;
}

Leave a Comment