简体   繁体   English

我怎样才能使左流动的立柱和浮动的右立柱垂直居中?

[英]How can I vertically center a fluid left column and a floated right column?

In my project, there are two columns. 在我的项目中,有两列。 The left contains an image that is, at maximum, 100% of the left column's width with an auto height. 左侧包含的图像最大为左侧列宽度的100%,并且具有自动高度。 The right varies in width while the left one fills up the remaining space. 右侧的宽度变化,而左侧的宽度则填充剩余空间。 This uses the technique described in JackJoe's answer to a previous question. 这使用了JackJoe对上一个问题的答案中描述的技术。

What I want to do now is vertically center the column with the smaller height. 我现在想做的是将列的垂直居中位置并减小高度。 There are times when the right column or the left column is shorter, depending upon the width of the window. 有时右栏或左栏较短,具体取决于窗口的宽度。 I have tried using 我尝试使用

{
  position: relative;
  top: 50%;
  transform: translateY(-50%); //with more prefixes
}

However, for some reason the top: 50% part does not work at all. 但是,出于某种原因, top: 50%部分根本无法工作。 Here is my current code, without the vertical centering attempt (this works as expected): 这是我当前的代码, 没有垂直居中尝试(这按预期工作):

 $(document).ready(function() { $("#right").click(function() { $(this).toggleClass("wide"); }); }); 
 #container { width: 100%; border: 1px solid red; height: auto; overflow: hidden; } #right { float: right; border: 1px solid green; width: 100px; transition: 1s width; } #left { border: 1px solid green; width: auto; overflow: hidden; } #left img { max-width: 100%; height: auto; } #right.wide { width: 300px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="container"> <div id="right"> Click Me </div> <div id="left"> <img src="http://placehold.it/440x120"/> </div> </div> 

EDIT: I forgot to specify -- the width of the right column needs to depend upon the widths of it's contents, just like the width of an inline-block element element would with no set width. 编辑:我忘了指定-右列的宽度需要取决于其内容的宽度,就像没有设置宽度的内联块元素元素的宽度一样。 I just used exact widths in the demo to show the fluidity of the columns. 我只是在演示中使用了精确的宽度来显示列的流动性。

You could use CSS tables to get the desired result. 您可以使用CSS表获得所需的结果。 Apply display: table-cell to the left and right elements and vertical-align: middle to vertically center the content. display: table-cell应用于左右元素, vertical-align: middle将内容vertical-align: middle至垂直居中。

This is a pretty robust approach and works in most modern browsers. 这是一种非常健壮的方法,可在大多数现代浏览器中使用。

The major advantage is that you don't need any JavaScript/jQuery to adjust the layout. 主要优点是您不需要任何JavaScript / jQuery即可调整布局。

Update: If you have optional elements that show/hide on the right, the vertical centering will still work. 更新:如果您的可选元素在右侧显示/隐藏,则垂直居中仍然有效。

 $(document).ready(function() { $("#right").click(function() { $(this).toggleClass("wide"); }); }); 
 #container { width: 100%; border: 1px solid red; height: auto; display: table; } #right { display: table-cell; vertical-align: middle; border: 1px solid green; width: 200px; transition: 1s width; } #left { display: table-cell; vertical-align: middle; border: 1px solid green; width: auto; } #left img { width: 100%; height: auto; display: block; } #right.wide { width: 300px; } #right p.extra { display: none; } #right.wide p.extra { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="left"> <img src="http://placehold.it/440x120" /> </div> <div id="right"><p><b>Click Me</b></p> <p class="extra">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer facilisis velit ut neque tempor quis cursus tortor suscipit. Curabitur rutrum magna vitae arcu pharetra eget cursus ante accumsan. Nunc commodo malesuada adipiscing. Pellentesque consequat laoreet sagittis. Sed sit amet erat augue. Morbi consectetur, elit quis iaculis cursus, mauris nulla hendrerit augue, ut faucibus elit sapien vitae justo. In a ipsum malesuada nulla rutrum luctus. Donec a enim sapien. Sed ultrices ligula ac neque vulputate luctus. Suspendisse pretium pretium felis, in aliquet risus fringilla at. Nunc cursus sagittis commodo.</p> </div> </div> 

You could use display: table-cell to achieve the vertical alignment. 您可以使用display:table-cell来实现垂直对齐。 This uses however different HTML (and CSS). 但是,这使用了不同的HTML(和CSS)。

#container {
 width: 100%;
 border: 1px solid red;
 height: auto;
 overflow: hidden;
    display: table
}

#right {
 border: 1px solid green;
 width: 100px;
 transition: 1s width;
    display: table-cell;
    vertical-align: middle;
}

#left {
 border: 1px solid green;
 width: auto;
 overflow: hidden;
    display: table-cell;
}

Here's a fiddle: http://jsfiddle.net/woj3s8La/ 这是一个小提琴: http : //jsfiddle.net/woj3s8La/

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

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