简体   繁体   English

在Div中垂直居中

[英]Centering a table vertically in a Div

I did some research and found that the only way to vertically center a table inside a div(where the table does not span the full height, the height varies with varying content) is with javascript/jquery: 我做了一些研究,发现将一个表垂直居中在div内的唯一方法(表不会跨越整个高度,高度随着内容的变化而变化)是javascript / jquery:

<script>
   var tableMarginTop = Math.round( (testHeight - tableHeight) / 2 );
   $('table').css('margin-top', tableMarginTop)
</script>

Now my code looks like this: CSS: 现在我的代码看起来像这样:CSS:

.rightDiv{
    width: 300px
    height: 380px;
    background: url(http://myimage.com) no-repeat;
}

.rightDiv table{
    margin: auto; /*For centering horizontally*/
}

HTML: HTML:

<div class="rightDiv">
  <table width="80%">
    <tr><td></td></tr>
  </table>
</div>

My question: How to I implement that code for this situation? 我的问题:如何为这种情况实现该代码? Not sure how to call the specific div class and table class in the JS function for the relevant div and table? 不知道如何在相关的div和表的JS函数中调用特定的div类和表类?

Thank You 谢谢

Hoping I have understood your question correctly - how about this example? 希望我能正确理解你的问题 - 这个例子怎么样? http://jsfiddle.net/9Zg8a/1/ http://jsfiddle.net/9Zg8a/1/

<div style="height:200px; vertical-align:middle; display:table-cell; border:green 1px solid">
  <table style="border:red 1px solid">
    <tr>
      <td>test text
      </td>
    </tr>
  </table>
</div>​

This answer is for the question: 这个答案是针对这个问题的:

My question: How to I implement that code for this situation? 我的问题:如何为这种情况实现该代码? Not sure how to call the specific div class and table class in the JS function for the relevant div and table? 不知道如何在相关的div和表的JS函数中调用特定的div类和表类?

".rightDiv" and ".rightDiv table" at your sample offers nothing! 您的样本中的“.rightDiv”和“.rightDiv表”不提供任何内容! Make it simpler. 让它更简单。

CSS CSS

#rightDiv{
    width: 300px
    height: 380px;
    background: url(http://myimage.com) no-repeat;
}

#rightDivTable{
    margin: auto; /*For centering horizontally*/
}

HTML HTML

<div id="rightDiv">
  <table id="rightDivTable" width="80%">
    <tr><td></td></tr>
  </table>
</div>

UPDATE: added missing quotes and requested code 更新:添加了缺少的引号和请求的代码

This way you will use $('#rightDiv') and $('#rightDivTable') in jquery for your elements. 这样你就可以在jquery中为元素使用$('#rightDiv')和$('#rightDivTable')。

JS JS

var 
    testHeight = $('#rightDiv').innerHeight(),
    tableHeight = $('#rightDivTable').outerHeight(),    
    tableMarginTop = Math.round( (testHeight - tableHeight) / 2 );
    $('#rightDivTable').css('margin-top', tableMarginTop);

This way you can center a table in a div. 通过这种方式,您可以将表放在div中。 By setting margin-left and margin-right to auto you can center pretty much every object. 通过将margin-left和margin-right设置为auto,您可以将每个对象居中。

<div style="300px; height: 380px">
  <table style="margin-left:auto; margin-right:auto">
    <tr>
      <td>
        ...
      </td>
    </tr>
  </table>
</div> 

how about this- 这个怎么样-

<div class="rightDiv">
  <center><table width="80%">
    <tr><td></td></tr>
  </table></center>
</div>

center is not recommended for use but what is the problem in using it. 不推荐使用center ,但使用它有什么问题。

Update- 最新情况:

i can't assign it center without dimensions. 没有尺寸我不能指定它的中心。

There may be other ways of centering vertically, but if you want to stick with script, here's one way of doing it - jsfiddle here : 可能有其他垂直居中的方式,但如果你想坚持使用脚本,这里有一种方法 - jsfiddle here

var testHeight = $('.rightDiv').innerHeight();
var tableHeight = $('.rightDiv table').outerHeight();

var tableMarginTop = Math.round( (testHeight - tableHeight) / 2 );
$('table').css('margin-top', tableMarginTop)   

JQuery lets you use CSS-style selectors for referencing elements, so you can use your existing classes and refer to them in the same way that the CSS does. JQuery允许您使用CSS样式选择器来引用元素,因此您可以使用现有类并以与CSS相同的方式引用它们。 Or you can assign IDs and use $('#idgoeshere') instead - and perhaps update the CSS also to use id-based selectors. 或者你可以分配ID并使用$('#idgoeshere') - 也许更新CSS也使用基于id的选择器。

Using IDs can be faster, since JQuery can internally optimize the selector query to use document.getElementById. 使用ID可以更快,因为JQuery可以在内部优化选择器查询以使用document.getElementById。 (One common benefit to using class-based selectors is that you can operate on a set of matching elements all in one go - though this doesn't work in your specific case if the tables have differing heights.) (使用基于类的选择器的一个共同好处是,您可以一次性操作一组匹配元素 - 尽管如果表具有不同的高度,这在您的特定情况下不起作用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM