How to center a

element inside a

container?

前端 未结 7 851
旧巷少年郎
旧巷少年郎 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:04

    Centered and middled content ?

    Do it this way :

    <table style="width:100%">
        <tr>
            <td valign="middle" align="center">Table once ruled centering</td>
        </tr>
    </table>
    

    I fiddled it here

    Ha, let me guess .. you want DIVs ..

    just make your first outter DIV behave like a table-cell then style it with vertical align:middle;

    <div>
        <p>I want this paragraph to be at the center, but I can't.</p>
    </div>
    
    div {
        width:500px;
        height:100px;
        background-color:aqua;
        text-align:center;
        /*  there it is */
        display:table-cell;
        vertical-align:middle;
    }
    

    jsfiddle.net/9Mk64/

    0 讨论(0)
  • 2020-12-14 00:05

    You only need to add text-align: center to your <div>

    In your case also remove both styles that you added to your <p>.

    Check out the demo here: http://jsfiddle.net/76uGE/3/

    Good Luck

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 00:08

    on the p element, add 3 styling rules.

    .myCenteredPElement{
        margin-left:  auto;
        margin-right: auto;
        text-align: center;
    }
    
    0 讨论(0)
  • 2020-12-14 00:09

    To get left/right centering, then applying text-align: center to the div and margin: auto to the p.

    For vertical positioning you should make sure you understand the different ways of doing so, this is a commonly asked problem: Vertical alignment of elements in a div

    0 讨论(0)
  • 2020-12-14 00:09

    ♣you should do these steps :

    1. the mother Element should be positioned(for EXP you can give it position:relative;)
    2. the child Element should have positioned "Absolute" and values should set like this: top:0;buttom:0;right:0;left:0; (to be middle vertically)
    3. for the child Element you should set "margin : auto" (to be middle vertically)
    4. the child and mother Element should have "height"and"width" value
    5. for mother Element => text-align:center (to be middle horizontally)

    ♣♣simply here is the summery of those 5 steps:

    .mother_Element {
        position : relative;
        height : 20%;
        width : 5%;
        text-align : center
        }
    .child_Element {
        height : 1.2 em;
        width : 5%;
        margin : auto;
        position : absolute;
        top:0;
        bottom:0;
        left:0;
        right:0;
        }
    
    0 讨论(0)
提交回复
热议问题