Skip to content

Latest commit

 

History

History
111 lines (90 loc) · 1.6 KB

centering-text.md

File metadata and controls

111 lines (90 loc) · 1.6 KB

Centering-text-Text

There are multiple ways to center text horizontally / vertically within a container. We will look at some of the most common ways to do so.

HTML

div class="container">
  <p>
    Text
  </p>
</div>

CSS

.container {
  width: 100px;
  height: 100px;
  border: 2px solid red;
}

Alt text

Flexbox

CSS

.container {
  margin-top: 100px;
  margin-left: 100px;
  width: 100px;
  height: 100px;
  border: 2px solid red;
  display: flex;
  align-items: center;
  justify-content: center;
}

Alt text

Grid

.container {
  margin-top: 100px;
  margin-left: 100px;
  width: 100px;
  height: 100px;
  border: 2px solid red;
  display: grid;
  align-items: center;
}

or

.container {
  margin-top: 100px;
  margin-left: 100px;
  width: 100px;
  height: 100px;
  border: 2px solid red;
  display: grid;
  place-items: center;
}

Alt text

Addition: Fixing the max. number of rows

HTML

<div class="container">
  <div class="text-container">
    <p> 
          This is a very long text that should not exceed the red box.
    </p>
  </div>
</div>

CSS

.container {
  width: 100px;
  height: 100px;
  border: 2px solid red;
  display: grid;
  align-items: center;
}

.text-container {
  margin: 5px;
  border: 1px solid grey;
  max-height: 95px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}

Alt text