Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to align rotated text on the side of a card?

I’m trying to create a playing card with a piece of rotated text on the side, but I’m having a hard time figuring out the CSS necessary to align the rotated text correctly.

This is how I would like the text to be aligned:

example of aligned text

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But in my current code snippet, I can’t get the text to behave like that. I’ve tried changing the transform-origin, text-align, and top, left, and right properties in hopes of getting it to align, but I still can’t figure it out. Any help would be greatly appreciated.

.group {
  display: flex;
  gap: 0.5rem;
}

.card {
  aspect-ratio: 0.675;
  width: 10rem;
  height: auto;
  border: 1px solid grey;
  border-radius: 6px;
  margin: 0.5rem;
  padding: 0.5rem;
}

.inner-div {
  display: flex;
  height: 100%;
}

.relative {
  position: relative;
}

.rotated-text {
  transform: rotate(-90deg);
  white-space: nowrap;
  position: absolute;
  margin: 0;
}

.red-card {
  flex-grow: 1;
  background: red;
  border-radius: 6px;
  margin-left: 1.5rem;
}
<div class='group'>
  <div class="card">
    <div class="inner-div">
      <div class="relative">
        <p class='rotated-text'>Text</p>
      </div>
      <div class='red-card'>
      </div>
    </div>
  </div>
  <div class="card">
    <div class="inner-div">
      <div class="relative">
        <p class='rotated-text'>Another piece of text</p>
      </div>
      <div class='red-card'>
      </div>
    </div>
  </div>
</div>

>Solution :

Use

transform: translate(-100%) rotate(-90deg);
transform-origin: right top;
.group {
  display: flex;
  gap: 0.5rem;
}

.card {
  aspect-ratio: 0.675;
  width: 10rem;
  height: auto;
  border: 1px solid grey;
  border-radius: 6px;
  margin: 0.5rem;
  padding: 0.5rem;
}

.inner-div {
  display: flex;
  height: 100%;
}

.relative {
  position: relative;
}

.rotated-text {
  white-space: nowrap;
  position: absolute;
  margin: 0;
  transform: translate(-100%) rotate(-90deg);
  transform-origin: right top;
}

.red-card {
  flex-grow: 1;
  background: red;
  border-radius: 6px;
  margin-left: 1.5rem;
}
<div class='group'>
  <div class="card">
    <div class="inner-div">
      <div class="relative">
        <p class='rotated-text'>Text</p>
      </div>
      <div class='red-card'>
      </div>
    </div>
  </div>
  <div class="card">
    <div class="inner-div">
      <div class="relative">
        <p class='rotated-text'>Another piece of text</p>
      </div>
      <div class='red-card'>
      </div>
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading