简体   繁体   English

n列随机高度portlet的响应式Web设计

[英]responsive web design for n columns of random height portlets

Briefly: 简要地:

How do I lay out N columns of random height portlets all in CSS so that if the browser is resized, the number of is columns reduced (using @media (min-width:)) and the portlets still sit nicely together on the page with no gaps. 如何在CSS中布置N列高度随机的portlet,以便调整浏览器的大小,减少列的数量(使用@media(min-width :)),而portlet仍然可以很好地放在页面上没有差距。

This is similar to Float multiple fixed-width / varible-height boxes into 2 columns but more general. 这类似于将固定宽度/可变高度的多个浮动框分成2列,但更为通用。

Detail: 详情:

I've built a web application (PHP / Zend Framework) with a "dashboard" page made of a series of portlets. 我已经构建了一个Web应用程序(PHP / Zend Framework),其中包含由一系列portlet组成的“仪表板”页面。 The portlets can be arranged in 1, 2, 3 or 4 equal width columns (user selectable) in a fluid layout. 这些portlet可以以流体布局的方式排列在1、2、3或4个等宽列(用户可选)中。 When the user resizes the browser window, the columns expand to fill the available width and the portlets also expand horizontally. 当用户调整浏览器窗口的大小时,列会扩展以填充可用宽度,并且portlet也会水平扩展。 The vertical height of each portlet is defined by its content. 每个portlet的垂直高度由其内容定义。 Some are only 1-2 lines, others can be 30-40+ lines of text / tables / image etc. 有些只有1-2行,其他则可以是30-40 +行文本/表格/图像等。

I want to turn this into a "responsive design" so that the user doesn't have to select the number of columns. 我想将其转变为“响应式设计”,以便用户不必选择列数。 On a small screen (eg iPhone) only one column will display. 在小屏幕(例如iPhone)上,只会显示一列。 On a wide screen they might have 4 or 5 columns. 在宽屏上,它们可能有4或5列。 If the browser window is resized, the number of columns will adjust up or down to allow portlets to stay approx 300-400 pixels wide. 如果调整浏览器窗口的大小,则列数将向上或向下调整,以使portlet保持大约300-400像素宽。

I think I can do this with a bit of jQuery and some server side support (PHP), but would prefer to do it all in CSS if possible (no / minimal javascript). 我想我可以用一些jQuery和一些服务器端支持(PHP)来做到这一点,但如果可能的话,宁愿在CSS中全部做到(没有/最少的javascript)。

Use a combination of media queries and text alignment to achieve this: 结合使用媒体查询和文本对齐来实现此目的:

In its natural state, 'text-align: justify' will not work unless the contents of the line are long enough to cause a line-break. 在其自然状态下,除非该行的内容足够长而导致换行,否则'text-align:justify'将不起作用。 Otherwise, the text remains left aligned. 否则,文本将保持左对齐。 We can solve this problem by giving 100% width to an invisible inline element at the end of the line. 我们可以通过为行尾的不可见内联元素提供100%的宽度来解决此问题。

Because 'text-align: justify' is designed to work on individual inline words, it works on any inline element—and more importantly any inline-block element. 因为'text-align:justify'被设计为可用于单个内联词,所以它可用于任何内联元素,更重要的是可用于任何内联块元素。

To account for any and all possible numbers of elements on the last row, the number of “placeholder” elements you will need to add is equal to the maximum number of elements per row, minus 2. These elements should be inserted at the end of your grid (before the “break” element if you are not using a pseudo-element to break the row) and then left alone. 要考虑最后一行中所有元素的所有可能数量,您需要添加的“占位符”元素的数量等于每行中元素的最大数量减去2。这些元素应插入到网格(如果不使用伪元素来破坏行,则在“ break”元素之前),然后单独放置。 Since they do not occupy any vertical space, the “placeholder” elements won't affect the layout if the last row is full or if your site is responsive and the number of elements per row changes. 由于它们不占用任何垂直空间,因此如果最后一行已满或网站响应并且每行元素数发生变化,则“占位符”元素将不会影响布局。 As long as you have enough placeholders for the widest view, you'll be fine. 只要您有足够的占位符可以看到最大的范围,就可以了。

Obviously, this has some semantic implications—as there is no way to create any of these placeholders using pseudo-elements. 显然,这具有语义上的含义-因为无法使用伪元素创建任何这些占位符。 On a grid where the last row will always have the maximum number of elements, we don't need to use placeholders at all (just a break), but in most CMS situations they are necessary, and should be hard-coded into your HTML. 在最后一行将始终具有最大数量的元素的网格上,我们根本不需要使用占位符(只是休息一下),但是在大多数CMS情况下,它们是必需的,并且应硬编码到HTML中。

By simply applying 'text-align: justify' to the container, and giving the child elements 'display: inline-block' and a percentage width, you'll never have to deal with horizontal margins ever again! 通过简单地将'text-align:justify'应用于容器,并为子元素赋予'display:inline-block'和一个百分比宽度,您将再也不必处理水平边距了! (Oh and did I mention, when using this trick, you'll also never need to use float on your elements ever again, so you can wave goodbye to those ignominious clearfixes and clear divs too!) (哦,我提过,使用此技巧时,您也将不再需要在元素上再次使用float,因此您也可以向那些卑鄙的clearfix和div告别!)

We should be aware that when using 'display: inline-block', our elements will be at the mercy of various typographic CSS properties, including font-size, line-height, vertical-align and word-spacing. 我们应该意识到,当使用'display:inline-block'时,我们的元素将受到各种印刷CSS属性的支配,包括字体大小,行高,垂直对齐和字距。 These properties will have a visible affect on your layout's whitespace, but can be easily adjusted or removed as needed. 这些属性将对布局的空白产生明显影响,但可以根据需要轻松调整或删除。 In 99% of cases, setting 'font-size: 0.1px;' 在99%的情况下,设置“ font-size:0.1px;” to the container and 'vertical-align: top' to the child elements should do the trick. 到容器并在子元素上使用“ vertical-align:top”应该可以解决问题。

References 参考文献

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

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