Text inside a div to align both vertically and horizontally

dasher121

I have a <div> and a <p> inside the div containing some text.

Horizontal alignment is achievable using text-align:center property but I am not able to middle it vertically.

I ve checked other related solutions, but they are specific to that particular problem and not suited for any div size.

I need a solution that could possibly be suited for any div of varying dimensions (in my case it is 100% both width and height).

Here is my Fiddle

Weafs.py

You can use top: calc(50% - 1em) on p to center it vertically.

p {
    position: relative;
    top: calc(50% - 1em);
}

Fiddle


Solution for Multiline text:

The idea is to get the height of the text, divide it by 2 and use it in calc(50% - ****) when the page loads or the window is resized. Then find the rule for the p tag and modify the top property.

Fiddle

var p = document.getElementsByTagName('p')[0];

function doMath() {
  var ss = document.styleSheets;
  for (k = 0; k < ss.length; k++) {
    var rules = ss[k];
    for (l = 0; l < rules.cssRules.length; l++) {
      var r = rules.cssRules[l];
      if (r.selectorText == "p") {
        r.style.top = 'calc(50% - ' + String(parseInt(getComputedStyle(p).height.slice(0, -2)) / 2) + 'px)'
      }
    }
  }
}

doMath();
window.onresize = doMath;
html,
body {
  height: 100%;
  width: 100%;
  margin: 0 auto;
}
#dvTxt {
  background-color: lightblue;
  height: 100%;
  width: 100%;
  text-align: center;
  vertical-align: middle;
}
p {
  position: relative;
  top: calc(50% - 7.5px);
}
<div id="dvTxt">
  <p>This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered
    vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want
    it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is
    my text. I want it to be centered vertically This is my text. I want it to be centered vertically This is my text. I want it to be centered vertically</p>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spinner text center align both vertically and horizontally

From Dev

How to align SPAN inside a DIV vertically and horizontally

From Dev

Align image inside DIV horizontally and vertically

From Dev

How to align SPAN inside a DIV vertically and horizontally

From Dev

Centrer align <p> vertically and horizontally inside a div

From Dev

Center align image inside div horizontally and vertically

From Dev

Can't align vertically text inside a div which is aligned horizontally with others

From Dev

how to center a div in a td cell and a link inside of it ?both vertically and horizontally?

From Dev

Vertically align a text with an image inside a div

From Dev

Vertically Align Image and Text Inside a Div

From Dev

Align text vertically inside div with CSS

From Dev

Center contents of div horizontally/vertically and align text to image

From Dev

Vertically and horizontally align an image in a div

From Dev

horizontally and vertically align of text with PIL

From Dev

How to align both vertically and horizontally in CSS?

From Dev

How to vertically align both text and image middle of div?

From Dev

How to vertically align both image and text side by side in a div

From Dev

How to vertically align both text and image middle of div?

From Java

CSS center text (horizontally and vertically) inside a div block

From Dev

Vertically align div inside a div

From Dev

Vertically align div inside a div

From Dev

Centering div both vertically and horizontally inside of 100% width relatively positioned div

From Dev

Vertically align text inside a div with percentage height and width?

From Dev

Center DIV container both horizontally and vertically

From Dev

How to align text horizontally & vertically in UITextView?

From Dev

How to align text horizontally & vertically in UITextView?

From Dev

Vertically align a img inside a div?

From Dev

Vertically align a img inside a div?

From Dev

Vertically align <p> inside a <div>

Related Related

  1. 1

    Spinner text center align both vertically and horizontally

  2. 2

    How to align SPAN inside a DIV vertically and horizontally

  3. 3

    Align image inside DIV horizontally and vertically

  4. 4

    How to align SPAN inside a DIV vertically and horizontally

  5. 5

    Centrer align <p> vertically and horizontally inside a div

  6. 6

    Center align image inside div horizontally and vertically

  7. 7

    Can't align vertically text inside a div which is aligned horizontally with others

  8. 8

    how to center a div in a td cell and a link inside of it ?both vertically and horizontally?

  9. 9

    Vertically align a text with an image inside a div

  10. 10

    Vertically Align Image and Text Inside a Div

  11. 11

    Align text vertically inside div with CSS

  12. 12

    Center contents of div horizontally/vertically and align text to image

  13. 13

    Vertically and horizontally align an image in a div

  14. 14

    horizontally and vertically align of text with PIL

  15. 15

    How to align both vertically and horizontally in CSS?

  16. 16

    How to vertically align both text and image middle of div?

  17. 17

    How to vertically align both image and text side by side in a div

  18. 18

    How to vertically align both text and image middle of div?

  19. 19

    CSS center text (horizontally and vertically) inside a div block

  20. 20

    Vertically align div inside a div

  21. 21

    Vertically align div inside a div

  22. 22

    Centering div both vertically and horizontally inside of 100% width relatively positioned div

  23. 23

    Vertically align text inside a div with percentage height and width?

  24. 24

    Center DIV container both horizontally and vertically

  25. 25

    How to align text horizontally & vertically in UITextView?

  26. 26

    How to align text horizontally & vertically in UITextView?

  27. 27

    Vertically align a img inside a div?

  28. 28

    Vertically align a img inside a div?

  29. 29

    Vertically align <p> inside a <div>

HotTag

Archive