Center an Image vertically and horizontally using CSS - javascript

How do I vertically and horizontally center an image when I do not know the size of it? I asked this question and someone suggested using a table. This isn't the first time I heard a table can do it but I tried without luck.
Searching SO only got me results when I do know the size of the image. How do I do this with a table?
NOTE: JavaScript/jQuery is not preferred but if there's a solution with it I'm open to it.

Pretty easy, this is the format of all my images/containers:
<div class="photo"><img /></div>
<style type="text/css">
div.photo { height: 100px; line-height: 100px;text-align:center; }
div.photo img { vertical-align:middle;}
</style>

The CSS Method
You can set an explicit height and line-height on your container to center an image:
<div id="container">
<img src="http://placekitten.com/200/200" />
</div>
<style>
#container { height: 600px; line-height: 600px; text-align: center }
#container img { vertical-align: middle }
</style>
See it in action: http://jsfiddle.net/jonathansampson/qN3nm/
The HTML/Table Method
The table method follows. It's merely utilizing the valign (vertical-align) property:
<table>
<tbody>
<tr>
<td align="center" valign="middle">
<img src="someHeight.jpg" />
</td>
</tr>
</tbody>
</table>
A jQuery Plugin
Since you tagged this question "jQuery," I'll provide a reference to the jQuery Center Plugin that also achieves vertical/horizontal centering by using CSS positioning and dynamic reading of an elements dimensions: http://plugins.jquery.com/project/elementcenter

With a table:
<table height="400">
<tbody>
<tr>
<td valign="middle"><img /></td>
</tr>
</tbody>
</table>
The 400 is just something I picked. You will need to define a height on table so it is taller than your image.
A jquery solution would be good if you wanted to try and use divs and junk, but if you don't care you don't care. You also have to rely on JS being turned on.
HTML:
<div id="imgContainer" style="position:relative;">
<img style="position:absolute;" />
</div>
JS:
$('#imgContainer > img').each(function(){
//get img dimensions
var h = $(this).height();
var w = $(this).width();
//get div dimensions
var div_h =$('#imgContainer').height();
var div_w =$('#imgContainer').width();
//set img position
this.style.top = Math.round((div_h - h) / 2) + 'px';
this.style.left = '50%';
this.style.marginLeft = Math.round(w/2) + 'px';
});

DON'T USE TABLES. Terrible practice unless your using tabular data.
The best way to do this is with the following code.
<html>
<head>
<title></title>
<style media="screen">
.centered {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;*/
}
</style>
</head>
<body>
<img class="centered" src="" width="100" height="100" alt="Centered Image"/>
</body>
This will work as long as it is not inside any elements without static positioning. All containing elements must be static positioning which is the default anyway.

Using CSS there is no easy way to vertically align an image center. Though to align it center horizontally you can use the following
<img src="randomimage.jpg" style="margin: 0px auto;" />
I would not reccommend a table for laying out an image as it is not really good practice anymore. Tables should only be used for tabular data.

There is some bad way to do it. Just display this image as block with absolute positioning (parent element must have "position: relative"). So you can play with margin-left and margin-top with negative values ~= a half of image sizes (respectively width and height)

If you don't mind losing IE compatibility (IE7 and older don't support this at all), you can use some CSS to simulate tables, without ever using one:
<div style="display: table; height: 500px; width: 500px;">
<img src="pic.jpg" style="display: table-cell; vertical-align:middle; margin: 0 auto; text-align: center">
</div>
Just pick appropriate height/width for the containing <div>.

If you don't mind losing the img-tag, you can use background-image to center an image in a container block.
markup:
<div class="imagebox" style="background-image: url(theimage.png);"></div>
style:
.imagebox
{
width: 500px;
height: 500px;
border: solid 1px black;
background-repeat: no-repeat;
background-position: 50% 50%;
}

Basically, you should be able to create a table in HTML, and the styling for the td tag should set the text-align attribute to center and the vertical-align attribute to middle. And, you can mess with other attributes, like borders, padding, etc...

I end up doing the below. Tested with firefox, chrome, IE8 and opera. All good.
table.NAME
{
background: green; //test color
text-align: center;
vertical-align:middle;
}
table.NAME tr td
{
width: 150px;
height: 150px;
}
html
<table class="NAME"><tr>
<td><img src="dfgdfgfdgf.gif" alt="dfgdfgfdgf.gif"/></td>
<td><img src="dfgdfgfdgf.gif" alt="dfgdfgfdgf.gif"/></td>
...

Related

I have a long table in a div, when I scroll the table, I want to see it moves inertially HTML

I have a long table(the width is very large), when I scroll that table from left to right, I want it moves inertially. The current version of mine is just when I use my finger to scroll it, it moves but when I hold up my finger it stopped. What should I do? Here is a part of my HTML code for that table inside the div.
<div class="welcomeProgram">
<p class=programTitle>Project</p>
<div class="outer">
<div class="inner">
<table>
<tr>
<td><img src="img/ast_youxue.png"/><p>Project1</p></td>
<td><img src="img/canada_youxue2.png"/><p>project2</p></td>
<td><img src="img/usa_youxue2.png"/><p>project3</p></td>
<td><img src="img/uk_youxue2.png"/><p>project4</p></td>
</tr>
</table>
</div>
</div>
</div>
And there is my css code:
table {
table-layout: fixed;
width: 100%;
*margin-left: -100px;/*ie7*/
}
td, th {
vertical-align: top;
padding:2px;
width:330px;
}
.outer {
position:relative;
margin-top: 2%;
}
.inner {
overflow-x:scroll;
overflow-y:visible;
width:97%;
margin-left:0%;
-webkit-overflow-scrolling: touch;
}
.inner img{
width: 100%;
}
.inner p{
font-size: 20px;
margin-top: 4%;
margin-left: 1%;
}
I'm not sure what your asking. "inertially" isn't a word in the dictionary. Inertia is, but it's the absence of motion AKA a body that stays at rest. See: http://www.dictionary.com/browse/inertia
It sounds like the table is already doing what it needs to do, when the user's cursor or finger releases the table... it stops.
If you want it to keep moving, you'd need to use a library like jQuery to animate it. HTML + CSS won't do that by themselves. You could start the scroll onKeyDown & then onMouseMove. It'd be similar to programming a drag & drop operation, using jQuery. Once you've setup 2 flags to track the onKeyDown & onMouseMove, where both flags are set to true, then you could use the onKeyUp event to run the animation. You could use .scrollLeft() to allow it to continue to keep moving. See: https://api.jquery.com/scrollleft/
Would you please clarify, what you're looking for the table to do?
Edit: I tried to add this to the comments, but it wouldn't fit into it.
See if this helps...
<html>
<head>
<style>
.wrap {
overflow-y: scroll;
}
table {
width: 10000px;
}
</style>
</head>
<body>
<div class="wrap"></div>
</body>
<footer>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script>
$(function() {
// Generate Table
var table = '<table border="1"><tr>';
for (var i=0; i<1000; i++) {
table += '<td>Hi</td>';
}
table += '</tr></table>';
$('.wrap').html(table);
// Run Animation
$('.wrap').on('scroll', function(){
$(this).animate({
scrollLeft: "+=50"
}, 800);
});
});
</script>
</footer>
</html>

How to resize a real time flot chart?

Is there a js library or a way to resize my real time flot chart when I resize the browser? My chart is like this example. The difference between the example and my chart is that it is put in a table.
EDIT:
I have found that jquery.flot.resize.js exists and does what I want. But does influence the library functionality the position of the flot in the table?
You can use relative units to resize element relative to window size one of them is %. Set width and height of element in % it will resize this element according to %age size of parent. Example
Resize window to see results!
<div style="
position: absolute;
width: 50%;
height: 50%;
background: red;">
Resizeable DIV
</div>
Demonstration given here https://jsfiddle.net/7rtguhuv/
I post the solution to my problem here. Maybe someone will need it.
I used jquery.flot.resize.js and the next code:
<table cellpadding="0" cellspacing="0" class="floatedTable">
<tr>
<td class="auto-style15">
<div id="content">
<div class="demo-container" style="position:absolute; width:75%; height:80%; top: 57px; left: 64px;">
<div id="placeholder" class="demo-placeholder" style="position:absolute; width:95%; height:95%; top: 17px; left: 24px;"></div>
</div>
</div>
</td>
</tr>
</table>
where class="floatedTable" is:
.floatedTable {
width: 100%;
height:100%;
}

bigvideo.js - stack DIVs over and under bigvideo container

So I've started playing around with bigvideo.js (which is built on top of video.js) and it works fine for the basic usage of having a fixed background video over the whole screen. I have also managed to show it inside of a div.
My problem though, is that I can't seem to stack other DIVs with other content over or under the bigvideo.js container div, and I can't seem to figure out how to solve this.
My HTML:
<div style="float: left; width: 100%; height: 300px;">
<h1>hi there</h1>
</div>
<div style="float: left; width: 100%; height: 500px;" id="intro-video-container">
</div>
JS firing up bigvideo:
$(function() {
var BV = new $.BigVideo({container: $('#intro-video-container'),useFlashForFirefox:false});
BV.init();
BV.show('intro.mp4',{ambient:true});
});
So the video container div ALWAYS gets stuck up to the left top of the body, no matter if I try to force it down with margin-top, or place divs before it, etc.
Any ideas?
Update, here is an illustration of what I kind of what to achieve:
Try to use container div (so called wrapping) in your page where you will place the desired content (as on the plugin's example page):
CSS
.box {
background:#444; background:rgba(0,0,0,.6);
padding:20px;
border-radius:5px;
margin-bottom:20px;
}
.main {
position:relative;
margin:50px 50px 440px 220px;
min-width:300px;
-webkit-transition-duration:0.6s;-moz-transition-duration:0.6s;-ms-transition-duration:0.6s;-o-transition-duration:0.6s;transition-duration:0.6s;
}
.dimmed {
color: #ccc;
}
#big-video-wrap {
height: 100%;
left: 0;
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
}
HTML
<div id="big-video-wrap"></div>
<div class="main">
<div id="overview" class="box">
<h1>BigVideo<span class="dimmed"><small>.</small>js</span></h1>
<h2>Simple Ambient Video Example</h2>
</div>
</div>
JavaScript
$(function() {
var BV = new $.BigVideo({container: $('#big-video-wrap'),useFlashForFirefox:false});
BV.init();
BV.show('intro.mp4',{ambient:true});
});
EDIT:
Now, it is more clear what you are trying to achieve, the simplest solution is to include an iframe on place of the div, which points to your full-screen video page.
I.e. create page video.html with all initializations and plug-in includes, then use it as source of your iframe on main page. Your iframe can be styled to match the desired dimensions (for example 100% width and 300px height).

How to position a div on the bottom of a parent td?

I need to place a <div> with fixed height (100px) and full width (100% of the parent <td>) within a <td> on the bottom. The <td> could be higher than the browsers viewport height as the content of the other <td>s are probably huge.
I already tried some solutions like this (link), which is actually placing the div at the bottom of the browsers viewport.
Edit:
Here's a snippet of what is NOT working (according to the link above):
td {
position: relative;
}
div {
position: absolute;
bottom: 0;
}
Is there any option to fix a <div> to the total bottom of a <td> using PHP, HTML, CSS or JavaScript (jQuery also)?
Edit 3:
Another problem occuring, when I use the solution as showed above is, that if I assign the div the property "position: absolute;" the "width: 100%;" relates to the viewport width, not the td width.
Edit 4:
The actual code of my page:
html:
<tr>
<td id="content">
</td>
<td class="sidebar">
<div class="internal">Notice</div>
</td>
</tr>
css:
#content{
height: 1000px;
}
.sidebar{
width: 10%;
min-width: 200px;
position: relative;
}
div.internal{
position: absolute;
bottom:0;
width:100%;
height: 100px;
}
jsFiddle: Source
Here's a working example
Use this to place the div at the bottom
td{
position: absolute;
}
div{
position: absolute;
bottom: 0px;
}
UPDATE
This is an example with your code working link
It work's for me in Chrome and IE. The Red section is your div. Is this the layout you want?
UPDATE 2
If you want to use a table layout you can try doing that: table layout
UPDATE 3: working only with tables
If the previous solution didn't work for you I'm guessing your code isn't modular enough. If you want to use tables, you might want to use only tables. Add another table inside the requested cell like this: table inside the cell . As much as I'm against it, I still think it's better than using JS to solve your problem. It will be easier to maintain in the future.
You need using something like:
<table>
<tr>
<td class="sidebar">
<div class="internal">Notice</div>
</td>
</tr>
</table>
CSS:
.sidebar{
width: 10%;
min-width: 200px;
height: 1000px;
background-color: green;
}
div.internal{
position: absolute;
height: 100px;
background-color: yellow;
}
Javascript:
$(document).ready(function () {
var $div = $('div.internal');
var $td = $div.closest('td');
$div.width($td.width() + 2);
$div.css('top', ($td.height() - $div.height() + 12) + 'px');
});
http://jsfiddle.net/Z58ZW/5/
try adding
div.myClass{
position: absolute;
bottom:0;
width:100%;
}
to the div.
Example with the div positioned only on td bottom.
JSFiddle

Center div inside 100% width container with dynamic width left and right floats

I want to have 3 divs aligned inside a container div, like this:
[[LEFT] [CENTER] [RIGHT]]
Container div is 100% wide (no fixed width), and center div should remain in center after resizing the container.
Left and Right DIV have no fixed width and need to expand/contract with the container. Center DIV does have a fixed width.
I have this:
<div style="width: 100%">
<div style="float: left; height: 50px;"></div>
<div style="float: right; height: 50px;"></div>
<div style="margin: 0 auto; height: 50px; width: 500px;"></div>
</div>
Problem is, the left and right do not show because there is no set width
Any suggestions?
You can't do that with pure CSS. You need to use JavaScript. In the example below Middle div is fixed at 400px while remaining space is be split between left and right divs. With jQuery you can do
function calc() {
var ww = $(window).width();
var rem = ww - $('.div2').width();
$('.div1, .div3').css('width', rem / 2);
}
calc();
$(window).resize(calc);
Check working example at http://jsfiddle.net/M5Ghx/3/
Another option, if you wanted to avoid using javascript, would be to give the center div an absolute position and create two divs to use as buffers within the left and right divs:
<div style="width: 100%; text-align:center">
<div style="width:50%; height: 50px; float:left">
<div style="width:250px; height: 50px; float:right"></div>
</div>
<div style="margin-right:auto; margin-left:auto; position:absolute; left:0; right:0; width: 500px;height:50px;"></div>
<div style="width:50%; height: 50px; float:right">
<div style="width:250px; height: 50px; float:left"></div>
</div>
</div>
If you only care about Mozilla and WebKit, then you should look in to using the Flexible Box Model:
http://hacks.mozilla.org/2010/04/the-css-3-flexible-box-model/
That will solve all your centering issues in pure CSS. Just be sure to read the docs and play around with the different options so that you understand how it works.

Categories

Resources