skip to Main Content

I want to stack these two elements on top of each other. I wanted the text to align in the center of the popup container, but I can’t seem to do it with a rotated element.

.popup {
  background-color: #f48277;
  width: 70px;
  height: 200px;
  position: fixed;
  right: 0;
  bottom: 30vh;
  border-radius: 70px;
}

.popup-text {
  transform: rotate(-90deg);
  color: white;
  font-size: 26px;
  font-weight: bold;
  position: fixed;
  right: 0;
  bottom: 43vh;
}

a {
  text-decoration: none;
}

@media screen and (max-width: 600px) {
  .popup {
    width: 60px;
    height: 170px;
  }
  .popup-text {
    color: white;
    font-size: 16px;
    font-weight: bold;
  }
}
<div class="popup">

</div>
<a target="_blank" href="https://google.com">
  <p class="popup-text"> Save 20% |</p>
</a>

3

Answers


  1. I’d suggest putting everything inside the same container, so your HTML looks like this:

    <div class="popup">
      <a target="_blank" href="https://google.com">
        <p class="popup-text"> Save 20% |</p>
      </a>
    </div>
    

    Aligning two different elements can get messy very quickly when they don’t share the same parent.

    You can use writing-mode: vertical-rl to rotate the text. Additionally you can use transform: rotate(180deg) to flip the text as well:

    .popup-text {
       writing-mode: vertical-rl;
       transform: rotate(180deg);
    }
    

    You can then use display: flex on the container to center the text both horizontally and vertically:

    .popup {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    /* simple CSS reset */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .popup {
      background-color: #f48277;
      width: 70px;
      height: 200px;
      position: fixed;
      right: 0;
      bottom: 30vh;
      border-radius: 70px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .popup-text {
      color: white;
      font-size: 26px;
      font-weight: bold;
      writing-mode: vertical-rl;
      transform: rotate(180deg);
    }
    
    a {
      text-decoration: none;
    }
    
    @media screen and (max-width: 600px) {
      .popup {
        width: 60px;
        height: 170px;
      }
      .popup-text {
        color: white;
        font-size: 16px;
        font-weight: bold;
      }
    }
    <div class="popup">
      <a target="_blank" href="https://google.com">
        <p class="popup-text"> Save 20% |</p>
      </a>
    </div>
    Login or Signup to reply.
  2. The problem was because you needed to put text inside a parent div and rotate the parent div not the text itself.

    That’s why text wasn’t stable inside the div.

    * {
      margin: 0;
      padding: 0;
    }
    
    .popup {
      text-align: center;
      padding-top: 20px;
      background-color: salmon;
      width: 180px;
      height: 50px;
      position: fixed;
      right: -50px;
      bottom: 32vh;
      border-radius: 50px;
      overflow: hidden;
      transform: rotate(-90deg);
    }
    
    .popup-text {
      color: white;
      font-size: 26px;
      font-weight: bold;
    }
    
    a {
      color: white;
      text-decoration: none;
    }
    
    @media screen and (max-width: 600px) {
      .popup {
        padding-top: 9px;
        width: 130px;
        height: 30px;
      }
      .popup-text {
        color: white;
        font-size: 16px;
        font-weight: bold;
      }
    }
    <div class="popup-text popup">
      <a target="_blank" href="https://google.com">Save 20% |</a>
    </div>
    Login or Signup to reply.
  3. Just going to throw in my two cents: if it were me, I’d make the banner horizontal, put everything in it like normal, and then rotate the whole deal 90 degrees:

    .popup {
      background-color: #f48277;
      width: 200px;
      height: 70px;
      position: fixed;
      right: 70px;
      top: 0;
      border-radius: 70px;
      transform-origin: top right;
      transform: rotate(-90deg);
      
      display: flex;
      justify-content: center;
      align-items: center;
      
    }
    
    .popup-text {
      color: white;
      font-size: 26px;
      font-weight: bold;
    }
    
    a {
      text-decoration: none;
    }
    
    @media screen and (max-width: 600px) {
      .popup {
        width: 170px;
        height: 60px;
        right: 60px;
      }
      .popup-text {
        color: white;
        font-size: 16px;
        font-weight: bold;
      }
    }
    <div class="popup">
      <a target="_blank" href="https://google.com">
        <p class="popup-text"> Save 20%</p>
      </a>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search