Search code examples
cssiframeresponsive-designcenter

How to center an iframe, responsive


I have this small part of html code, and like to the iframe just the half size, but centered of the page:

Here is the page: https://www.outdoorequipped.com/active/contacts

<div class="row">
<div class="map-container">
<iframe src="https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2s!4v1451982933747!6m8!1m7!1sIT5z6S82FJMAAAQZdjRuFQ!2m2!1d34.238620415813!2d-77.94889641093141!3f55.18!4f0!5f0.7820865974627469" height="250" width="600"></iframe>
</div>
</div>

and her my css:

.map-container {
  position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px;
    height: 0;
    overflow: hidden;
}

/* 16x9 Aspect Ratio */
.map-container-16x9 {
  padding-bottom: 56.25%;
}

/* 4x3 Aspect Ratio */
.map-container-4x3 {
  padding-bottom: 75%;
}

.map-container iframe {
  position: absolute;
  top:0;
  left: 0;
  width: 100%;
  height: 100%;
}

Any idea how to do it correct?


Solution

  • Would this be what you're trying to do ?

    .map-container iframe {
        position: absolute;
        top: 25%;
        left: 25%;
        width: 50%;
        height: 50%;
    }
    

    Or this ? (without absolute positioning)

    .map-container iframe {
        width: 50%;
        display: block;      
        margin: auto;
    }
    

    It depends if you want to center it vertically as well or not.