简体   繁体   English

在 CSS 中,如何获得左侧固定宽度列,右侧表格使用其余宽度?

[英]In CSS, how do I get a left-side fixed-width column with a right-side table that uses the rest of the width?

So I've tried to work with the pure-CSS, never-use-tables-for-layout gospel, I really have.所以我尝试使用纯 CSS,从不使用表格布局的福音,我真的有。 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.我愿意付出一些努力来完成 CSS 中的事情,不要误会我的意思。 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.但是当使用布局表可以完成的一些最简单的事情似乎在 CSS 中完全不可能时,我不在乎概念纯度是否真的开始受到打击。

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.我对我浪费的时间感到愤怒,我对从 QuarkXpress 背景中出来的人给我提供无用的固定宽度设计感到愤怒,我对 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.我真的很想知道一个简单问题的答案,该问题将决定我是再次尝试纯 CSS 还是将其混为一谈并在我喜欢的时候使用布局表。 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?问题是:有没有什么办法使用纯CSS布局在左边有一列固定宽度的列,在它的右边放一个数据表,让数据表整齐地占据剩下的什么空间可用? 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?也就是说,通过具有宽度为 100% 且左侧单元格宽度固定的两个单元格布局表可以轻松实现相同的布局?

<div style="width: 200px;background-color: #FFFFCC; float: left;">
Left column
</div>

<div style="margin-left: 200px; background-color: #CCFFFF">
Right column
</div>

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).应该这样做(显然实现会根据它在页面中的位置而有所不同,但我认为这就是您正在寻找的概念)。

Is this what you're looking for? 是你要找的吗?

 body { margin: 0; padding: 0; border: 0; overflow: hidden; height: 100%; max-height: 100%; } #framecontent { position: absolute; top: 0; bottom: 0; left: 0; width: 200px; /*Width of frame div*/ height: 100%; overflow: hidden; /*Disable scrollbars. Set to "scroll" to enable*/ background: navy; color: white; } #maincontent { position: fixed; top: 0; left: 200px; /*Set left value to WidthOfFrameDiv*/ right: 0; bottom: 0; overflow: auto; background: #fff; } .innertube { margin: 15px; /*Margins for inner DIV inside each DIV (to provide padding)*/ } * html body { /*IE6 hack*/ padding: 0 0 0 200px; /*Set value to (0 0 0 WidthOfFrameDiv)*/ } * html #maincontent { /*IE6 hack*/ height: 100%; width: 100%; }
 <div id="framecontent"> <div class="innertube"> <h1>CSS Left Frame Layout</h1> <h3>Sample text here</h3> </div> </div> <div id="maincontent"> <div class="innertube"> <h1>Dynamic Drive CSS Library</h1> <p style="text-align: center">Credits: <a href="http://www.dynamicdrive.com/style/">Dynamic Drive CSS Library</a> </p> </div> </div>

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.我敢肯定,您对 CSS 的掌握比以前要好得多。 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.我个人发现 CSS 是需要大量练习才能精通的事物之一。我使用基于 CSS 的布局已经 5 年了,我每天仍在学习有趣的技巧。

I think this is what you're looking for:我认为这就是你要找的:

<table style='width: 100%;'>
  <tr>
    <td style='width: 200px;'></td>
    <td></td>
  </tr>
</table>

Thank me later.以后谢谢我。 =P =P

(I'm obviously joking.... kind of...) (我显然是在开玩笑......有点......)

To keep this question up-to-date, here are 5 ways you can achieve the same thing using both CSS2 and CSS3.为了使这个问题保持最新,这里有 5 种方法可以使用 CSS2 和 CSS3 实现相同的目标。


Example 1: Floats & Margin示例 1:浮动和保证金

This is the way it's been done for years: Simple and effective.这就是多年来的做法:简单而有效。

 #example1 .fixedColumn { width: 180px; float: left; background: #FCC; padding: 10px; } #example1 .flexibleColumn { margin-left: 200px; background: #CFF; padding: 10px; }
 <div id="example1"> <div class="fixedColumn"> Fixed Column </div> <div class="flexibleColumn"> Flexible Column </div> </div>


Example 2: CSS calc();示例 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=calc calc()从 IE9 开始工作,尽管对某些版本的 android 浏览器的支持有点不稳定: http ://caniuse.com/#feat=calc

 #example2.calc { overflow: hidden; } #example2.calc .fixedColumn { width: 180px; float: left; background: #FCC; padding: 10px; } #example2.calc .flexibleColumn { width: calc(100% - 220px); /*200px for the fixed column and 20 for the left and right padding */ float: left; background: #CFF; padding: 10px; }
 <div id="example2" class="calc"> <div class="fixedColumn"> Fixed Column </div> <div class="flexibleColumn"> Flexible Column </div> </div>


Example 3: CSS Display as Table示例 3:CSS 显示为表格

 #example3.table { display: table; width: 100%; } #example3.table .fixedColumn { width: 180px; display: table-cell; background: #FCC; padding: 10px; } #example3.table .flexibleColumn { display: table-cell; background: #CFF; padding: 10px; }
 <div id="example3" class="table"> <div class="fixedColumn"> Fixed Column </div> <div class="flexibleColumn"> Flexible Column </div> </div>


Example 4: CSS3 Flexbox示例 4:CSS3 弹性盒

Flexbox has surprisingly good support across browsers: http://caniuse.com/#search=flex Flexbox 对跨浏览器的支持出奇地好: http ://caniuse.com/#search=flex

 #example4.flex { display: flex; } #example4.flex .fixedColumn { width: 180px; background: #FCC; padding: 10px; } #example4.flex .flexibleColumn { flex: 1 100%; flex-basis: auto; background: #CFF; padding: 10px; }
 <div id="example4" class="flex"> <div class="fixedColumn"> Fixed Column </div> <div class="flexibleColumn"> Flexible Column </div> </div>


Example 5: CSS3 Grid示例 5:CSS3 网格

CSS Grid makes complicated layouts in CSS really intuitive and easy. CSS Grid 使复杂的 CSS 布局变得非常直观和简单。

http://caniuse.com/#search=grid http://caniuse.com/#search=grid

 #example5.grid { display: grid; grid-template-columns: 200px 1fr; } #example5.grid .fixedColumn { grid-column: 1; background: #FCC; padding: 10px; } #example5.grid .flexibleColumn { grid-column: 2; background: #CFF; padding: 10px; }
 <div id="example5" class="grid"> <div class="fixedColumn"> Fixed Column </div> <div class="flexibleColumn"> Flexible Column </div> </div>

Here's a codepen that features all 5 techniques:这是一个具有所有 5 种技术的代码笔:

2 Columns (1 Fixed, 1 Flexed) 5 different ways! 2 列(1 固定,1 弯曲) 5 种不同的方式!

First, I'd recommend Eric Meyer's CSS books as well as the CSS reference on the web: A List Apart . :首先,我建议Eric Meyer的CSS书,以及在网络上CSS参考相隔名单 I use CSS extensively in my work and I think that I have gotten pretty good with it.我在我的工作中广泛使用 CSS,我认为我已经很好地使用了它。

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:像这样的东西:

<div style="position: relative; width: 100%">
    <div style="position: absolute; left: 0; top: 0; width: 200px">
        left column
    </div>
    <div style="position: absolute; left: 200px; top: 0">
         other stuff
    </div>
</div>

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.左边的一个固定宽度的列相当简单,但偶尔我会看到其他布局,这些布局可以用表格轻松完成,但据我所知,使用 CSS 非常困难。

You might want to try these:你可能想试试这些:

http://www.alistapart.com/stories/practicalcss/ http://www.alistapart.com/stories/practicalcss/

http://www.w3.org/2002/03/csslayout-howto http://www.w3.org/2002/03/csslayout-howto

Here's why:原因如下:

http://en.wikipedia.org/wiki/Tableless_web_design http://en.wikipedia.org/wiki/Tableless_web_design

http://davespicks.com/essays/notables.html http://davespicks.com/essays/notables.html

More HowTOs:更多操作方法:

    div.row {
      clear: both;
      padding-top: 10px;
    }

    div.row span.label {
      float: left;
      width: 100px;
      text-align: right;
    }

    div.row span.formw {
      float: right;
      width: 335px;
      text-align: left;
    }


    <div style="width: 350px; background-color: #cc9;
      border: 1px dotted #333; padding: 5px;
      margin: 0px auto";>
      <form>
        <div class="row">
          <span class="label">Name:</span><span
            class="formw"><input type="text" size="25" /></span>
        </div>
        <div class="row">
          <span class="label">Age:</span><span
            class="formw"><input type="text" size="25" /></span>
        </div>
        <div class="row">
          <span class="label">Shoe size:</span><span
            class="formw"><input type="text" size="25" /></span>
        </div>
        <div class="row">
          <span class="label">Comments:</span><span
              class="formw">
            <textarea cols="25" rows="8">
            Go ahead - write something...
            </textarea>
          </span>
        </div>
        <div class="spacer">
          &nbsp;
        </div>
      </form>
    </div>

I love how CSS still takes a full page of code to duplicate a couple lines of using a table.我喜欢 CSS 仍然需要一整页代码来复制几行使用表格。

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.在与 CSS 之战打完之后,我得出的结论是“纯粹的”在旁观者的眼中,并且更多地转向“让我们使用有效的方法”。

Use CSS on what it's good for: making things look pretty.将 CSS 用于它的好处:使事情看起来很漂亮。 Use DIV's and SPAN's when you can.尽可能使用 DIV 和 SPAN。 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.别担心,与大多数人似乎认为的相反,小狗实际上不会死。

As Sparr says, there's CSS that specifically meets this requirement, because it's not really possible to do it any other way without various, possibly unacceptable compromises, and that's display:table , display:table-cell , etc. 正如Sparr所说,有专门满足这一要求的CSS,因为如果没有各种可能不可接受的妥协,并且display:tabledisplay:table-cell等,它实际上不可能以任何其他方式实现。

Unfortunately Internet Explorer prior to Internet Explorer 8 doesn't support these display modes, so the problem is not really with CSS but with a failure of browsers (actually Internet Explorer, the others are fine) to support CSS adequately. 不幸的是,Internet Explorer 8之前的Internet Explorer不支持这些显示模式,因此问题不在于CSS,而是因为浏览器(实际上是Internet Explorer,其他都很好)无法充分支持CSS。

The good news is that this is changing, and in due course, we will have a proper CSS solution. 好消息是,这种情况正在发生变化,并且在适当的时候,我们将拥有一个合适的CSS解决方案。 In the meantime, pick one of the compromises in the other answers. 与此同时,在其他答案中选择一个妥协方案。

There is almost certainly an answer that involves applying display:table and display:table-cell to the elements in question.几乎可以肯定有一个答案涉及将 display:table 和 display:table-cell 应用于相关元素。 Which is to say... no.也就是说……不。

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

相关问题 CSS左侧和右侧 - CSS Left-side and Right-side Bootstrap 4导航,从移动折叠中排除的左侧切换和右侧按钮? - Bootstrap 4 navigation, left-side toggle with right-side button excluded from mobile collapse? 在左侧弯曲一个大羽毛图标,在右侧弯曲标题和描述 - Flex one big feather icon on left-side, title and description on right-side 如何修复工作台宽度的左侧并使右侧填充剩余宽度 - How to get fix left side of table width and have the right side fill remaining width 如何创建两个div元素,左侧具有固定的宽度,右侧具有动态宽度 - How to create two div elements, the left side has a fixed width and the right side has a dynamic width 三个不同宽度的div并排,左右固定 - Three divs with different width side by side, those left and right fixed 如何创建中心固定其余部分的左侧和右侧固定宽度元素? - How can I create left & right side fixed width elements with a center that fills the remainder? 如何使表的列填充未被固定宽度列占用的宽度? - How do I make a table's columns fill the width not occupied by fixed-width columns? 如何在表格的左侧和右侧添加阴影 - how do i add shadow on left and right side of column of table css 2列左固定仅按图像宽度,右列占用容器的其余宽度 - css 2 column left fixed-only-by-image-width, right col to take up rest of container width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM