在 CSS 中,如何获得左侧固定宽度列和使用其余宽度的右侧表格?
因此,我尝试使用纯 CSS、永远不要使用表格进行布局的福音,我确实做到了。 但在经历了难以置信的痛苦和痛苦之后,我几乎准备放弃了。 我愿意付出一些努力来完成 CSS 中的事情,请不要误会我的意思。 但是,当布局表可以完成的一些最简单的事情在 CSS 中完全不可能时,我不在乎概念的纯粹性是否真的开始受到打击。
现在,我可能看起来很生气,确实如此; 我对我浪费的时间感到愤怒,我对来自 QuarkXpress 背景的人们向我提供无用的固定宽度设计感到愤怒,我对 CSS 未能兑现的承诺感到愤怒。 但我并不是想引发争论;我只是想引起争论。 我真的很想知道一个简单问题的答案,这个问题将决定我是否再次尝试纯 CSS 的东西,或者将其集中起来并在我愿意的时候使用布局表。 因为我不想回到布局表,认为这件事是不可能的,如果它实际上不是的话。
问题是这样的:是否有任何方法使用纯CSS布局在左边有一列,它是固定宽度的,在它的右边放置一个数据表,并有数据桌子整齐地占据了剩余的可用空间? 也就是说,通过使用宽度为 100% 且左侧单元具有固定宽度的两个单元布局表,可以轻松实现相同的布局吗?
So I've tried to work with the pure-CSS, never-use-tables-for-layout gospel, I really have. But after an incredible amount of pain and anguish, I'm just about ready to give up. I'm willing to go to some effort to accomplish things in CSS, don't get me wrong. But when it seems like some of the most asininely simple things that can be done with layout tables are utterly impossible in CSS, I don't care if conceptual purity really starts to take a beating.
Now, it probably seems like I'm angry, and I am; I'm angry about my wasted time, I'm angry about people coming out of QuarkXpress backgrounds handing me useless fixed-width designs, I'm angry about the failed promise of CSS. But I'm not trying to start an argument; I really do want to know the answer to one simple question that will determine whether I give the pure-CSS thing another try or lump it and use layout tables whenever I feel like it. Because I'd hate to go back to layout tables thinking that this thing is impossible if it's actually not.
The question is this: is there any way using pure-CSS layout to have a column on the left, which is fixed-width, and to the right of it place a data table, and have the data table neatly take up the remainder of whatever space is available? That is, the same layout which is easily achievable by having a two-cell layout table with width 100% and a fixed width on the left cell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这应该可以做到(显然,实现会根据它在页面中的位置而有所不同,但我认为这就是您正在寻找的概念)。
That should do it (obviously implementation will vary based on where it is in the page, but I think that's the concept you're looking for).
这就是你的吗?正在寻找?
我感受到你的痛苦,但尽量不要将其视为浪费时间。 我确信您对 CSS 的掌握比以前更好了。 继续使用它,您就会开始看到它的优点。 我个人发现 CSS 是需要大量练习才能精通的东西之一。我使用基于 CSS 的布局已有 5 年了,而且每天仍在学习有趣的技巧。
Is this what you're looking for?
I feel your pain, but try not to look at it as time wasted. I'm sure you have a much better grasp of CSS than you previously did. Keep working with it and you'll start seeing the advantages. I personally have found CSS to be one of those things that takes a lot of practice to become proficient in. I've been using CSS based layouts for 5 years and I still learning interesting tricks everyday.
我想这就是您要找的:
稍后谢谢我。 =P
(我显然是在开玩笑......有点......)
I think this is what you're looking for:
Thank me later. =P
(I'm obviously joking.... kind of...)
为了使这个问题保持最新状态,这里有 5 种方法可以让您同时使用 CSS2 和 CSS3 来实现相同的目标。
示例 1:浮点数和浮点数 利润
这是多年来的做法:简单而有效。
示例 2:CSS calc();
calc()
可以在 IE9 及以上版本上使用,尽管某些版本的 Android 浏览器的支持有点不稳定:http://caniuse.com/#feat=calc示例 3:CSS 显示为表格
示例 4:CSS3 Flexbox
Flexbox 在跨浏览器中具有令人惊讶的良好支持: http://caniuse.com/#search=flex
示例 5:CSS3 网格
CSS 网格使 CSS 中的复杂布局变得非常直观和简单。
http://caniuse.com/#search=grid
这是一个包含所有 5 种技术的 Codepen:
2 列(1 固定,1 弯曲)5 种不同的方式!
To keep this question up-to-date, here are 5 ways you can achieve the same thing using both CSS2 and CSS3.
Example 1: Floats & Margin
This is the way it's been done for years: Simple and effective.
Example 2: CSS calc();
calc()
works from IE9 upwards although support on some versions of android's browser is a little flakey: http://caniuse.com/#feat=calcExample 3: CSS Display as Table
Example 4: CSS3 Flexbox
Flexbox has surprisingly good support across browsers: http://caniuse.com/#search=flex
Example 5: CSS3 Grid
CSS Grid makes complicated layouts in CSS really intuitive and easy.
http://caniuse.com/#search=grid
Here's a codepen that features all 5 techniques:
2 Columns (1 Fixed, 1 Flexed) 5 different ways!
首先,我推荐 Eric Meyer 的 CSS 书籍以及网络上的 CSS 参考:分开列表。 我在工作中广泛使用 CSS,并且我认为我已经非常擅长使用它了。
综上所述:做有效的事情。 你刚才所经历的痛苦我也经历过。 最后,我认为仅仅为了说我没有使用表格而牺牲我的设计是不值得的。
请记住:付钱给你不是为了做 CSS - 你是为了编写工作软件而付钱的。
First, I'd recommend Eric Meyer's CSS books as well as the CSS reference on the web: A List Apart. I use CSS extensively in my work and I think that I have gotten pretty good with it.
With all of that being said: do what works. I have been through exactly the pain that you've just experienced. In the end, I figured that it wasn't worth compromising my design just to be able to say that I hadn't used tables.
Remember: you aren't paid to do CSS - you are paid to write working software.
像这样的事情:
当然,您应该将样式放在单独的样式表中而不是内联。 左侧的单个固定宽度列相当简单,但有时我确实会看到其他布局,这些布局可以使用表格轻松完成,但据我所知,使用 CSS 非常困难。
Something like this:
Of course, you should probably put the styles in a separate stylesheet rather than inline. And a single fixed-width column on the left is fairly simple, but occasionally I do see other layouts which can be done easily with tables but are, as far as I know, fiendishly difficult with CSS.
您可能想尝试这些:
http://www.alistapart.com/stories/practicalcss/
http://www.w3.org/2002/03/csslayout-howto< /a>
原因如下:
http://en.wikipedia.org/wiki/Tableless_web_design
< a href="http://davespicks.com/essays/notables.html" rel="nofollow noreferrer">http://davespicks.com/essays/notables.html
更多操作方法:
You might want to try these:
http://www.alistapart.com/stories/practicalcss/
http://www.w3.org/2002/03/csslayout-howto
Here's why:
http://en.wikipedia.org/wiki/Tableless_web_design
http://davespicks.com/essays/notables.html
More HowTOs:
我喜欢 CSS 仍然需要一整页代码来重复使用表格的几行。
在经历了 CSS 战争之后,我得出的结论是“纯粹”是情人眼里出西施,并且已经转向更多的“让我们只使用有效的”方法。
使用 CSS 的好处是:让东西看起来更漂亮。 尽可能使用 DIV 和 SPAN。 但是,如果您发现自己花了一天的时间试图让所有不同的浏览器风格之间的内容保持一致,那么就在那里放一张桌子并继续前进。 别担心,与大多数人的想法相反,小狗实际上不会死。
I love how CSS still takes a full page of code to duplicate a couple lines of using a table.
After fighting the CSS war, I've come to the conclusion that "pure" is in the eye of the beholder and have moved to more of a "let's just use what works" approach.
Use CSS on what it's good for: making things look pretty. Use DIV's and SPAN's when you can. But if you find yourself spending a day trying to get things to line up right across all the different browser flavors, then slap a table in there and move on. Don't worry, contrary to what most people seem to think, a puppy will in fact not die.
几乎可以肯定,有一个答案涉及将 display:table 和 display:table-cell 应用到相关元素。 这就是说……不。
There is almost certainly an answer that involves applying display:table and display:table-cell to the elements in question. Which is to say... no.