htmlcsscenteringtruncate

How can I maintain truncation with an ellipsis while also having vertical text centered?


I am trying to display content where book-spine is visually centered inside of shelf. It is supposed to look like a book's spine. When book-spine is too long, it should be truncated with an ellipsis.

How can I get book-spine centered and truncated inside of shelf?

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: 'Cambria', serif;
}

.wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.container {
  width: 80%;
  margin: auto;
}

@media (max-width: 768px) {
  .container {
    width: 90%;
  }
}

@media (max-width: 480px) {
  .container {
    width: 95%;
  }
}

.shelf {
  display: flex;
  justify-content: space-between;
  margin: auto;
  max-width: 500px;
}

.book-spineFlex {
  width: 45px;
  height: 150px;
  margin-right: 1px;
  background-color: #a0522d;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  writing-mode: vertical-lr;
  transform: rotate(180deg);
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  border-radius: 3px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.book-spineBlock {
  width: 45px;
  height: 150px;
  margin-right: 1px;
  background-color: #a0522d;
  color: white;
  display: block;
  align-items: center;
  justify-content: center;
  writing-mode: vertical-lr;
  transform: rotate(180deg);
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  border-radius: 3px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
<div class="wrapper">
  <div class="container">
    <div class="shelf">
      <div class="book-spineFlex">Sample title 1 w/ truncation</div>
      <div class="book-spineBlock">Sample title 1 w/truncation</div>
    </div>
  </div>
</div>

In addition to the above, I tried using flexbox to no avail (centered but no truncation). Implementing position: relative and position: absolute brought back the truncation but removed the centering.


Solution

  • Swap out align-items for align-content.

    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
      font-family: 'Cambria', serif;
    }
    
    .wrapper {
      display: flex;
      flex-direction: column;
      min-height: 100vh;
    }
    
    .container {
      width: 80%;
      margin: auto;
    }
    
    @media (max-width: 768px) {
      .container {
        width: 90%;
      }
    }
    
    @media (max-width: 480px) {
      .container {
        width: 95%;
      }
    }
    
    .shelf {
      display: flex;
      justify-content: space-between;
      margin: auto;
      max-width: 500px;
    }
    
    .book-spineFlex {
      width: 45px;
      height: 150px;
      margin-right: 1px;
      background-color: #a0522d;
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      writing-mode: vertical-lr;
      transform: rotate(180deg);
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      border-radius: 3px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    
    .book-spineBlock {
      width: 45px;
      height: 150px;
      margin-right: 1px;
      background-color: #a0522d;
      color: white;
      display: block;
      align-content: center;
      justify-content: center;
      writing-mode: vertical-lr;
      transform: rotate(180deg);
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      border-radius: 3px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    <div class="wrapper">
      <div class="container">
        <div class="shelf">
          <div class="book-spineFlex">Sample title 1 w/ truncation</div>
          <div class="book-spineBlock">Sample title 1 w/truncation</div>
        </div>
      </div>
    </div>