Jump to content

  • Free consultations and support
  • Live chatClick Here for Live Chat
  • Call ico 1888-906-1888
    Phone support: Open

    Ready for your call :)

    Our business hours:

    Mon — Fri, 2am — 8pm (EST)

    US & EU support teams

    Phone support: Closed

    We are back in: 1h 20m

    Our business hours:

    Mon — Fri, 2am — 8pm (EST)

    US & EU support teams


Vertical Centering Text in DIV


  • Please log in to reply
&nsbp;

#1 mika

mika

    Junior Member

  • Designer
  • 7 posts

Posted 01 May 2013 - 11:52 AM

To center vertically some text inside a div do the following:

1. Create a CSS class for the div that contains the text and named for example "centered"

<div class="centered">some text</div>

2. Then in your stylesheet add the class "centered" and simply set the line-height of the text to be tha same as the height of the div.

.centered{height:40px; line-height:40px;}

* Keep in mind that this works only if the height of the div is fixed.

#2 iAcceso

iAcceso

    Junior Member

  • Designer
  • 5 posts

Posted 08 May 2013 - 04:49 PM

Thanks for the tips!
I am learning CSS and I've been searching for a simple way to do this for a while :D

#3 mika

mika

    Junior Member

  • Designer
  • 7 posts

Posted 08 May 2013 - 05:17 PM

happy to know:)

#4 HappyGD

HappyGD

    Moderator

  • Designer
  • 2731 posts

Posted 09 May 2013 - 08:16 PM

I'm not an expert with coding but wouldn't "text-align: center;" do the same thing?

edit: ah sorry, I just noticed the instructions was for vertically centering text in a div :)

Edited by HappyGD, 09 May 2013 - 08:23 PM.


#5 mika

mika

    Junior Member

  • Designer
  • 7 posts

Posted 09 May 2013 - 08:49 PM

no problem :)

#6 Uduria

Uduria

    Junior Member

  • Designer
  • 4 posts

Posted 13 July 2013 - 11:33 AM

Thanks so much, could you please tell me what "float" do exactly with left: 50%? do they affect each other directly?

#7 spidergems

spidergems

    Junior Member

  • Designer
  • 7 posts

Posted 30 April 2018 - 05:05 AM

With the flexbox layout module in CSS3, it has become a lot easier to center items vertically in a container. We no longer have to apply the fixed height.

 

Here is an example: 

<section>
  <h3>Hello World!</h3>
</section>
section {
  display:flex;
  height: 100vh;
  align items: center;
}

If you apply this style, the child items of the section will be vertically centered; whatever be the height of container.  

 

 

Here is another method.

<section>
  <h3>Hello World!</h3>
</section>
section {
  height: 100vh;
  position: relative;
}

h3 {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}


This is another method of vertically centering elements in a container. 

 

 

I prefer the first method because the second method required positioning properties which I don't like to use.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users