How to center a

element inside a

container?

前端 未结 7 850
旧巷少年郎
旧巷少年郎 2020-12-13 23:34

I want my

element to be at the center of a container

, as in perfectly centered -- the top, bottom, left and right margins spl
7条回答
  •  执笔经年
    2020-12-14 00:08

    You dont need absolute positioning Use

    p {
     text-align: center;
    line-height: 100px;
    
    }
    

    And adjust at will...

    If text exceeds width and goes more than one line

    In that case the adjust you can do is to include the display property in your rules as follows;

    (I added a background for a better view of the example)

    div
    {
      width:300px;
      height:100px;  
      display: table; 
      background:#ccddcc;  
    }
    
    
    p {
      text-align:center; 
      vertical-align: middle;
      display: table-cell;   
    }
    

    Play with it in this JBin

    enter image description here

提交回复
热议问题