Aligning text with CSS issue

Viewed 15

I'm working on a website for someone and just can't figure out the right way to align the "Pervious Case Study" text so that it's centered. I provided a screenshot for a visual reference -

enter image description here

div.sticky {
  position: -webkit-sticky;
  position: fixed;
  display: inline-block;
  white-space: nowrap;
  left: 0;
  top: 30%;
  bottom: 50%;
  color: white;
  text-align: center;
  background-color: #45256e;
  padding: 60px;
  font-size: 20px;
  width: 130px;
  height: 10px;
}
1 Answers

There are many ways. Assume html looks like this.

HTML

<div class="block">
  <div class="sticky">
    Previous Case Study
  </div>
</div>
  1. using position absolute and translate
div.block {
  position: relative;
  width: 300px;
  height: 300px;
}

div.block > div.sticky {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
}
  1. Using flexbox
div.block {
  width: 300px;
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
}
Related