Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I vertically align text in a paragraph?

Tags:

html

css

I would like to know to align the text in a p element to be vertically centered.

Here are my styles:

p.event_desc {
     font: bold 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
     line-height: 14px;
     height: 35px;
     margin: 0px;
}
<p class="event_desc">Lorem ipsum.</p>
like image 393
Sowmya Avatar asked Jun 15 '12 13:06

Sowmya


People also ask

How do I align a paragraph vertically?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How do I align a paragraph vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.


4 Answers

Try these styles:

p.event_desc {    font: bold 12px "Helvetica Neue", Helvetica, Arial, sans-serif;    line-height: 14px;    height:75px;    margin: 0px;    display: table-cell;    vertical-align: middle;    padding: 10px;    border: 1px solid #f00;  }
<p class="event_desc">lorem ipsum</p>
like image 184
Dipak Avatar answered Sep 20 '22 15:09

Dipak


You can use line-height for that. Just set it up to the exact height of your p tag.

p.event_desc {   line-height:35px; } 
like image 31
Andres Ilich Avatar answered Sep 18 '22 15:09

Andres Ilich


If the answers that involve tables or setting line-height don't work for you, you can experiment with wrapping the p element in a div and style its positioning relative to the height of the parent div.

.parent-div{
  width: 100px;
  height: 100px;
  background-color: blue;
}

.text-div{
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

p.event_desc{
  font: bold 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: white;
  text-align: center;
}
<div class="parent-div">
  <div class="text-div">
   <p class="event_desc">
    MY TEXT
   </p>
  </div>
</div>
like image 45
Edgar Chávez Avatar answered Sep 19 '22 15:09

Edgar Chávez


So personally I'm not sure of the best-method way, but one thing I have found works well for vertical alignment is using Flex, as you can justify it's content!

Let's say you have the following HTML and CSS:

.paragraph { 
      font-weight: light;
      color: gray;
      min-height: 6rem;
      background: lightblue;
  }
<h1 class="heading"> Nice to meet you! </h1>
<p class="paragraph"> This is a paragraph </p>

We end up with a paragraph that isn't vertically centered, now if we use a Flex Column and apply the min height + BG to that we get the following:

.myflexbox {
    min-height: 6rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: lightblue;
}

.paragraph { 
      font-weight: light;
      color: gray;
  }
<h1 class="heading"> Nice to meet you! </h1>
<div class="myflexbox">
    <p class="paragraph"> This is a paragraph </p>
</div>

However, in some situations you can't just wrap the P tag in a div so easily, well using Flexbox on the P tag is perfectly fine even if it's not the nicest practice.

.myflexparagraph {
    min-height: 6rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: lightblue;
}

.paragraph { 
      font-weight: light;
      color: gray;
  }
<h1 class="heading"> Nice to meet you! </h1>
<p class="paragraph myflexparagraph"> This is a paragraph </p>

I have no clue if this is good or bad but if this helps only one person somewhere that's still one more then naught!

like image 34
Justin L Avatar answered Sep 21 '22 15:09

Justin L