Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Center text vertically inside a div

Center text vertically inside a div

Scheduled Pinned Locked Moved Web Development
question
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I have the following code:

    		Tuesday
    		2023-03-15
    

    Why this code doesn't center the text at middle on vertical direction ?

    _ L J 3 Replies Last reply
    0
    • _ _Flaviu

      I have the following code:

      		Tuesday
      		2023-03-15
      

      Why this code doesn't center the text at middle on vertical direction ?

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #2

      The parent div would include several divs, where the text will be aligned separately.

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        The parent div would include several divs, where the text will be aligned separately.

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        I have tried:

        			Tuesday
        			2023-03-15
        

        No work.

        1 Reply Last reply
        0
        • _ _Flaviu

          I have the following code:

          		Tuesday
          		2023-03-15
          

          Why this code doesn't center the text at middle on vertical direction ?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I'm not a CSS expert but take a look at the example here: CSS Layout - Horizontal & Vertical Align[^]; it shows how to centre a div element.

          _ 1 Reply Last reply
          0
          • L Lost User

            I'm not a CSS expert but take a look at the example here: CSS Layout - Horizontal & Vertical Align[^]; it shows how to centre a div element.

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            Yes, I know, but the trick is that the second div has no vertical correct alignment, and I don't realise why:

            Horizontal and Vertical alignment
            
            
            .container {
            	height: 200px;
            	width: 400px;
            	border: 2px dashed #4b2869;
            	display: table-cell;
            	text-align: center;
            	vertical-align: middle;
            	position: relative;
            }
            .container1 {
            	height: 200px;
            	width: 400px;
            	border: 2px dashed #4b2869;
            	display: table-cell;
            	text-align: center;
            	vertical-align: bottom;
            	position: absolute;
            }
            

            container

            container1

            L 1 Reply Last reply
            0
            • _ _Flaviu

              Yes, I know, but the trick is that the second div has no vertical correct alignment, and I don't realise why:

              Horizontal and Vertical alignment
              
              
              .container {
              	height: 200px;
              	width: 400px;
              	border: 2px dashed #4b2869;
              	display: table-cell;
              	text-align: center;
              	vertical-align: middle;
              	position: relative;
              }
              .container1 {
              	height: 200px;
              	width: 400px;
              	border: 2px dashed #4b2869;
              	display: table-cell;
              	text-align: center;
              	vertical-align: bottom;
              	position: absolute;
              }
              

              container

              container1

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Sorry, I have tried a number of different styles and combinations but cannot get it to do what you want. But as I mentioned, I am not a CSS expert.

              _ 1 Reply Last reply
              0
              • L Lost User

                Sorry, I have tried a number of different styles and combinations but cannot get it to do what you want. But as I mentioned, I am not a CSS expert.

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #7

                Thank you Richard a lot. As you see, the vertical alignment of the second div is not correct, and I don't know why, I am not a web developer at all.

                L 1 Reply Last reply
                0
                • _ _Flaviu

                  Thank you Richard a lot. As you see, the vertical alignment of the second div is not correct, and I don't know why, I am not a web developer at all.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I'm afraid I only dabble so my knowledge is limited. You might try opening the same question in QA to see if it gets a better response.

                  1 Reply Last reply
                  0
                  • _ _Flaviu

                    I have the following code:

                    		Tuesday
                    		2023-03-15
                    

                    Why this code doesn't center the text at middle on vertical direction ?

                    J Offline
                    J Offline
                    Jeremy Falcon
                    wrote on last edited by
                    #9

                    Just a heads up, floats are not needed here at all. Anyway, the vertical align property is for inline containers. A div by default is a block level element so it's ignored. To see what I mean check out the examples on [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align) and [W3Schools](https://www.w3schools.com/cssref/tryit.php?filename=trycss\_vertical-align). With that being said, there are much, much better ways to do this. If you want to vertically center without having to use explicit heights, the recommended way these days is to just use flexbox. There are a ton of other techniques like negative margins, line height, etc. too but flexbox should be something to master anyway. So, no time like the present.

                    Tuesday 2023-03-20
                    

                    _Be aware that inline styles should be avoided. Only using them here to match your example._

                    Jeremy Falcon

                    _ 1 Reply Last reply
                    0
                    • J Jeremy Falcon

                      Just a heads up, floats are not needed here at all. Anyway, the vertical align property is for inline containers. A div by default is a block level element so it's ignored. To see what I mean check out the examples on [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align) and [W3Schools](https://www.w3schools.com/cssref/tryit.php?filename=trycss\_vertical-align). With that being said, there are much, much better ways to do this. If you want to vertically center without having to use explicit heights, the recommended way these days is to just use flexbox. There are a ton of other techniques like negative margins, line height, etc. too but flexbox should be something to master anyway. So, no time like the present.

                      Tuesday 2023-03-20
                      

                      _Be aware that inline styles should be avoided. Only using them here to match your example._

                      Jeremy Falcon

                      _ Offline
                      _ Offline
                      _Flaviu
                      wrote on last edited by
                      #10

                      It works now, thanks a lot !

                      J 1 Reply Last reply
                      0
                      • _ _Flaviu

                        It works now, thanks a lot !

                        J Offline
                        J Offline
                        Jeremy Falcon
                        wrote on last edited by
                        #11

                        You're very welcome.

                        Jeremy Falcon

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups