简体   繁体   English

研究创建结合CSS中百分比和静态(例如像素)值的网格

[英]Research on creating grids that combine percentage and static (e.g. pixel) values in CSS

I just want to make a research that concerns responsive web design. 我只是想进行一项涉及响应式网页设计的研究。 Don't treat this question like a problem that must be solved. 不要将这个问题视为必须解决的问题。 It's just an experiment :) 这只是一个实验:)

Sometimes we need to combine percentage and fixed values for dimension calculation especially when it comes to create some responsive layouts. 有时我们需要将百分比和固定值结合起来进行维度计算,尤其是在创建一些响应式布局时。 As far as I'm concerned I've found four ways to achieve the desired effect in pure CSS. 就我而言,我发现了四种在纯CSS中实现预期效果的方法。

Problem 问题

Let's have a quick look on the problem - we need to create a three column layout stretched to the entire width of page where one of the column has a constant width and each remaining column fills out half of the available space. 让我们快速浏览一下这个问题 - 我们需要创建一个三列布局,扩展到整个页面宽度,其中一列具有恒定宽度,每个剩余列填充可用空间的一半。

<main>
  <section>
    <article class="first">
      I fill out half of the available space!
    </article>
    <article class="second">
      I fill out half of the available space!
      <strong>Be aware that contents of article and aside may be changed!</strong>
    </article>
    <aside>
      I'm 50px width!
    </aside>
  </section>
</main>

We have to achieve following layout without modifying HTML structure, contents of <article> and <aside> may be changed . 我们必须在修改HTML结构的情况下实现以下布局, 可以改变 <article><aside>内容。 Only pure CSS solutions will be accepted. 只接受纯CSS解决方案。

例

Solution 1 - Cross-browser fixed layout table 解决方案1 ​​ - 跨浏览器的固定布局表

Example: FIDDLE 示例: FIDDLE

The width of each column in default table is calculated automatically and depends on the content of cells. 默认表中每列的宽度是自动计算的,取决于单元格的内容。 In order to resolve the problem we need to force the size of the column, so this solution uses table that has table-layout property set to fixed . 为了解决这个问题,我们需要强制列的大小,因此该解决方案使用将table-layout属性设置为fixed It allows to set the width of any column. 它允许设置任何列的宽度。

It is probably the most supported solution ( read more ). 它可能是最受支持的解决方案( 了解更多 )。

Solution 2 - Making use of calc() function 解决方案2 - 使用calc()函数

Example: FIDDLE 示例: FIDDLE

The calc() function enables us to combine percentage and fixed values, for example: calc()函数使我们能够组合百分比和固定值,例如:

article {
  width: calc(100% - 50px);
}

Unfortunately this is not cross browser solution ( read more ) and it is recommended to use fallbacks ( read more ). 不幸的是,这不是跨浏览器解决方案( 阅读更多 ),建议使用回退( 阅读更多 )。

Solution 3 - Flexible flexbox 解决方案3 - 灵活的flexbox

Example: FIDDLE 示例: FIDDLE

This is probably the future of responsive web design. 这可能是响应式网页设计的未来。 I've implemented desired layout in an instant. 我已经实现了所需的布局。 Flexbox offers a lot of interesting features ( read more ). Flexbox提供了许多有趣的功能( 阅读更多 )。

You can read here about the compatibility. 你可以在这里阅读兼容性。

Solution 4 - Margin manipulation and absolute positioning 解决方案4 - 保证金操纵和绝对定位

Example: FIDDLE 示例: FIDDLE

This is another well supported solution. 这是另一个很好的支持解决方案 Absolute position is applied to the static aside element, section has appropriate right margin, 50% width is added for both article elements. 绝对位置应用于静态aside元素, section具有适当的右边距,为两个article元素添加50%宽度。

Summary 摘要

That's a very common problem in responsive world and I am very curious if there is any other ideas how to resolve it in pure CSS. 这在响应世界中是一个非常普遍的问题,如果有任何其他想法如何在纯CSS中解决它,我很好奇。 Any thoughts on that will be appreciated. 对此的任何想法将不胜感激。

Footnotes 脚注

Try to shrink fiddle's preview pane to the minimal width - in my opinion good, worthy tables still behaves most predictably ;) 尝试将小提琴的预览窗格缩小到最小宽度 - 在我看来,好的,有价值的表仍然表现得最可预测;)

Regards. 问候。

( Edit: this one is similar (/simplified) to the OP's Solution 1 and AFAIK he covered all of the most popular solutions out in the wild. To recap, this one is a neat way to make it cross-browser compatible) 编辑:这个与OP的解决方案1和AFAIK类似(/简化),他涵盖了所有最流行的解决方案。回顾一下,这个是一个使其跨浏览器兼容的简洁方法)

jsBin demo jsBin演示

...would be to simply mimic the way table does it, ......就是简单地模仿table的方式,
and prevent stretches using table-layout: fixed; 并使用table-layout: fixed;防止延伸table-layout: fixed; :

article, aside {
  display:table-cell;
}
aside {
  width: 50px;  
}
section {
  display:table;
  table-layout: fixed;
  width:100%;
}

See also: Two column template with static and responsive width 另请参见: 具有静态和响应宽度的两列模板

NOTE! 注意! Don't forget you can nest elements inside the two-column version or other versions to create different variants. 不要忘记您可以在两列版本或其他版本中嵌套元素以创建不同的变体。

I broke your conditions slightly by putting the two articles into a container element '.left' but this is the technique I usually use. 我将这两篇文章放入容器元素'.left'中稍微打破了你的条件,但这是我经常使用的技术。 Give the aside a fixed width and give the responsive content a padding-right of the same amount so that they don't overlap. 将一定宽度放在一边,并为响应内容提供相同数量的填充权限,以使它们不重叠。

http://jsfiddle.net/John_C/fhvp3pod/ http://jsfiddle.net/John_C/fhvp3pod/

.left{
    padding-right:50px;
}
aside{
    position:absolute;
    top:0;
    right:0;
    width:50px;
}

The main disadvantage of this method is that the aside is outside the main rendering tree so, if the aside is longer than the main content, it won't push down the page following elements. 这种方法的主要缺点是旁边在主渲染树之外,因此,如果旁边比主要内容长,它将不会按下页面后面的元素。

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

相关问题 在JavaScript中,如何从像素值(例如2em或20px)获取单位(例如em或px)? - In javascript, how to get the unit (e.g. em or px) from a pixel value (e.g. 2em or 20px)? div不能使用外部CSS文件设置样式(例如背景) - div can not be styled (e.g. background) with external css file 具有JavaScript和CSS的动画图像(例如非GIF)? - Animated image (e.g. non-GIF) with JavaScript and CSS? @Media Breaks CSS 较小时(例如屏幕) - @Media Breaks CSS When Smaller (e.g. screen) 打开IE并注入变量值(例如ScriptEngineMinorVersion) - Open IE and inject variable values (e.g. ScriptEngineMinorVersion) 如何使用jquery-ui每次调整相同像素值(例如“ 10px”)的大小? - How to resize every time for the same pixel value e.g.(“10px”) with jquery-ui? 使用百分比值创建CSS动画关键帧 - Creating CSS animation keyframes with percentage values 创建按钮的更优雅的方式,例如使用单个 HTML 元素 - More elegant way of creating button e.g. using single HTML element CSS / JS中导航菜单中的箭头(例如playframework.org) - Arrow in nav menus in CSS/JS (e.g. playframework.org) 当页面具有响应式设计时(例如,使用Bootstrap)减小CSS的大小 - Reduce size of CSS when page has responsive design (e.g. using Bootstrap)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM