I have an iframe and a div inside a container. The two of them need to be vertically centered. After reading a few posts on tables to center, I gave it a try but to no avail. The iframe continues to stick to the top left border even though I have the iframe 'display' property set to 'table-cell' & 'vertical-align' to 'middle'.
The HTML code:
<!-- the container div -->
<div id="iframe_r_container">
<!-- iframe -->
<iframe id="iframing" src="mannequin.html" frameborder="0" ></iframe>
<!--div--> <div id="right_container">
<div id="user_credit">
<h1>Username</h1><br />
has <span id="credits">20,000</span> credits.
</div>
<div> <button id="template_button"><img src="images/Coutallure-WebApp/template_button.png" /><span>Templates</span></button> </div>
</div>
And here is the CSS:
/* START OF IFRAME_R_CONTAINER */
#iframe_r_container {
position: absolute;
display: table;
top: 48px;
bottom: 38px;
width: 960px;
}
/* START OF IFRAME */
#iframing {
display: table-cell;
width: 640px;
height: 480px;
vertical-align: middle;
}
/* END OF IFRAME */
/* START OF RIGHT CONTAINER */
#right_container {
display: table-cell;
vertical-align: middle;
width: 113px;
margin: 20px;
}
I have been stuck at this for half a day today so any help would be immensely appreciated.
If you don't mind using another technique than table-cell centering, you can try something like this :
#iframe_r_container {
position: absolute;
top: 48px;
bottom: 38px;
width: 960px;
}
/* START OF IFRAME */
#iframing {
position: relative;
top: 50%;
margin-top: -240px;
width: 640px;
height: 480px;
float: left;
}
/* END OF IFRAME */
/* START OF RIGHT CONTAINER */
#right_container {
position: relative;
top: 50%;
height: 113px;
margin-top: -57px;
margin-left: 670px;
width: 113px;
}
It works here on my FF/mac but you'd have to test it on other browser.
To center #right_container, you'd have to give it a heigh (here 113px) and set the negative margin-top accordingly.
Also, you may want to give a min-height: 640px to #iframe_r_container to avoid the iframe overflowing outside of its container.
I am not sure what are you trying to achieve, but just from reading your post - you cannot try centering element itself with some align property, this must be property of its parent element. You should try that margin, i think this is the right property to work with.
Adding "height" to your containers should do it. Just adding it to your iframe container worked for me in FF on my Mac.
#iframe_r_container {
position: absolute;
top: 48px;
bottom: 38px;
width: 960px;
height:480px;
}
(Note: Internet Explorer 8 (and higher) supports the property values "inline-table", "run-in", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-row", "table-row-group", and "inherit" only if a !DOCTYPE is specified.)
try these settings:
/* START OF IFRAME_R_CONTAINER */
#iframe_r_container {
position: absolute;
top: 48px;
bottom: 38px;
width: 960px;
}
/* START OF IFRAME */
#iframing {
width: 640px;
height: 480px;
margin: 0 auto;
}
/* END OF IFRAME */
/* START OF RIGHT CONTAINER */
#right_container {
vertical-align: middle;
width: 113px;
margin: 0 auto;
}
The attribute margin: 0 auto; will (hopefully) center your iframe and other div inside the container. Not tested but give it a try.
Related
I loosely followed a basic CSS lightbox template from w3 schools for modal lightboxes. I haven't gotten to the Javascript yet, but just laying out the look and style of the lightbox.
I have a fixed position div element serving as the grey background, and inside that the "lightbox-content" div to hold an iframe of a Vimeo link. I followed a workaround to make the iframe responsive by containing it inside another div and adjusting the styling. I want the max-width of my iframe to be 1280px, but up to 100% width on anything smaller.
Everything works great on smaller screens, the iframe and contained link fills the width, stays vertically centered, and scales with the page. However, when I go above the 1280, the iframe moves in all sorts of weird ways. I'd like to have it so when the page width goes about 1280 or so,the iframe just stays at a fixed size in the center of the screen at 1280px wide.
I tried using an #media query to change some of the CSS rules, but I'm getting so lost in the position after several hours of trying. I think what's throwing me is having so many div with different types of positioning inside each other, and also not clearly understanding how to properly clear CSS rules within a media query.
Is there anything obvious I've done wrong that I could fix to help resolve the issue? It's hard to see the effect in the tiny result window, so if there's a way to make it full-screen in the browser, hopefully you can see what I'm talking about.
.lightbox {
display: block;
position: fixed;
left: 0;
top: 0;
overflow: auto;
padding-top: 0px;
z-index: 1;
background: rgba(0, 0, 0, .85);
width: 100%;
height: 100%;
}
.lightbox-content {
position: absolute;
width: 100%;
height: auto;
max-width: 1280px;
max-height: 720px;
top: 45%;
left: 50%;
right: 50%;
margin-top: -25%;
margin-left: -50%;
}
.responsive-container {
position: relative;
overflow: hidden;
padding-bottom: 56.25%;
}
.responsive-iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
#media only screen and (min-width: 1299px) {
.lightbox {
display: block;
position: fixed;
left: 0;
top: 0;
overflow: hidden;
padding-top: auto;
z-index: 1;
width: 100%;
height: 100%;
}
.lightbox-content {
position: relative;
width: 1280px;
height: 720px;
max-width: none;
max-height: none;
top: 0;
left: 0;
right: 0;
margin-top: auto;
margin-left: auto;
}
.responsive-container {
position: relative;
overflow: hidden;
padding-bottom: 56.25%;
}
.responsive-iframe {
position: absolute;
width: 100%;
height: 100%;
border: 0;
}
}
<div id="myLightbox" class="lightbox">
<div class="lightbox-content">
<div class="responsive-container">
<iframe class="responsive-iframe" id="lightbox-window" name="lightbox-window" src="https://player.vimeo.com/video/261201719" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
</div>
</div>
I want to wrap a laptop image around a youtube video. I allready got a solution for that. Now i want to make everything responsive for smaller devices. Any solutions for that ?
body {
padding-top: 70px;
/* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
}
#tv_container {
background: url('img/video.jpg') no-repeat top left transparent;
width: 1440px; /* Adjust TV image width */
height: 810px; /* Adjust TV image height */
position: relative;
}
#tv_container iframe {
position: absolute;
}
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe {
position: relative;
top: 78px;
left: 252px;
width: 65%;
height: 67%;
}
<div class="container">
<div id="tv_container">
<div class="videoWrapper">
<iframe width="760" height="315" src="https://www.youtube.com/embed/qidS1nnK0Ps" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
You should change the #tv_container styles to use a variable width:
#tv_container {
background: url('img/video.jpg') no-repeat top left transparent;
width: 80%;
height: auto;
position: relative;
}
I also used an auto height for the container so it changes according to its width.
I have seen some web design lessons that always start with a css like this:
body,html {
display: block;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
I'm trying to figure out what's the point of declaring attributes like width, height or display for body and html that are, if I'm not wrong, by default in browsers.
I thought it would be to prevent and undefined return or similar when accessing the css with js, but the result is the same when the attributes are defined in the css than when left to default:
console.log($("BODY").css('width')); // Always returns the width of the body
I also thought it could be to start the inheritance in cascade elements, but a div inside the body inherits the value just the same.
Anybody knows a solid reason for this approach? any browser / device issue I have missed? future compatibility? plain pedantry?
I'm kind of curious about it.
I found a good reason to define the html and body width and height to 100%. Say you want to vertically align a relative positioned div, you need to put it into an absolute positioned container:
html,
body {
margin: 0;
padding: 0;
}
#container {
position: absolute;
width: 100%;
height: 100%;
}
#main {
background: lightgrey;
position: relative;
top: 50%;
transform: translateY(-50%);
width: 100px;
height: 100px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
<div id="container">
<div id="main">
<h1>MY DIV</h1>
</div>
</div>
But, setting the body width and height to 100% you get an absolute positioned container that covers the whole window:
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#main {
background: lightgrey;
position: relative;
top: 50%;
transform: translateY(-50%);
width: 100px;
height: 100px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
<div id="main">
<h1>MY DIV</h1>
</div>
You get the same result, but it saves you a div element.
So, I've seen tons of questions about this, but I would like a personal example. I'm rather new to programming, so I may be a little stupid...
Anyway, I have two <div>s, one with id bg and the other with class player.
This is what it looks like:
The red box is the player, and the large image is the bg.
I need the player to start in the center of the bg.
The bg is 640px X 640px.
This is the code I have so far in my CSS file:
#bg {
width: 640px;
height: 640px;
top: 0;
left: 0;
}
.player {
position:relative;
background-color:#FF0000;
width: 32px;
height: 32px;
top: 0px;
left: 0px;
}
Try changing your stylesheet to:
#bg {
width: 640px;
height: 640px;
top: 0;
left: 0;
position: relative;
}
.player {
position: absolute;
background-color: #FF0000;
width: 32px;
height: 32px;
top: 320px;
left: 320px;
z-index: 1;
}
And your HTML should look like this:
<div id="bg">
<!-- your bd code here -->
<div class="player"></div>
</div>
position: relative is relative to where the object would be placed normally. In your example, it would normally come below the first div, so that's where it will stay. (In other words position: relative used with a positioning of 0 won't move the objet anywhere.)
You could add top: -320px; left: 320px. That would position it it the space of the first div. But maxksbd19's answer is probably the better solution for your ultimate goal.
I try and avoid absolute positioning as it does not adapt to the container size and a change to the container requires you to go through your css and change all the absolute values.
I would do the following
CSS:
#bg {
overflow: auto; /* stops the .player from from moving #bg down */
width: 640px;
height: 640px;
background-color: blue;
text-align: center; /* center child div in IE */
}
.player {
background-color: White;
width: 32px;
height: 32px;
margin: 0 auto; /* center div in parent for non IE browsers */
margin-top: 304px; /* 50% from top minus div size */
}
HTML:
<div id="bg">
<div class="player"></div>
</div>
Now you only have to keep track of the top margin of the child container.
i would like to center a popup div how can i do that browser friendly????
this is the original a bit to the left.
<div class="popLayerWrapper">
<div id="addBookmarksLayer" class="popLayer" style="left:260px; padding:30px;">
<div class="closeLayer" >close</div>
<div class="layerContent"></div>
</div>
</div>
Not sure I understand which part you want to center, but assuming the whole thing:
.popLayerWrapper {
position: absolute;
width: 40%; /* could be anything */
left: 50%;
margin-left: -20%; /* half of width */
height: 40%; /* again, could be anything */
top: 50%;
margin-top: -20%; /* half of height */
}
I use this code to center an element in the center of the window viewport:
html {
display: table;
table-layout: fixed;
width: 100%;
height: 100%;
}
body {
display: table-cell;
width: 100%;
height: 100%;
vertical-align: middle;
margin:0;
}
#center {
margin: auto;
}
You will find a full example at this link (Bredele CSS bundle). I think it should work for your popup.
Olivier