How to align iframe always in the center

前端 未结 6 1530
臣服心动
臣服心动 2021-01-02 12:58

I have an iframe surrounded by div element and I am simply trying to position it always in the center. here is my jsfiddle effort : jsfiddle

and tr

6条回答
  •  孤独总比滥情好
    2021-01-02 13:18

    center iframe

    one solution is:

    css:

    div {
      text-align:center;
      width:100%;
    }
    iframe{
      width: 200px;
    }
    

    fiddle: http://jsfiddle.net/h9gTm/

    edit: vertical align added

    css:

    div {
        text-align: center;
        width: 100%;
        vertical-align: middle;
        height: 100%;
        display: table-cell;
    }
    .iframe{
      width: 200px;
    }
    div,
    body,
    html {
        height: 100%;
        width: 100%;
    }
    body{
        display: table;
    }
    

    fiddle: http://jsfiddle.net/h9gTm/1/

    Edit: FLEX solution

    using display: flex on the

    div {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    

    fiddle: http://jsfiddle.net/h9gTm/867/

提交回复
热议问题