简体   繁体   English

使用CSS进行3列布局,左/右可变大小,中间流体

[英]Using CSS for 3 column layout, left/right variable size, mid fluid

I need 3 column layout, first and 3rd column sizes are variable because there will be image or some variable length text(or another image) but i need middle to fill the rest space with background image, something like this if it would work like i imagine : 我需要3列布局,第一列和第3列是可变的,因为会有图像或一些可变长度的文本(或另一个图像),但我需要中间用背景图像填充剩余空间,如果它会像我一样工作想象一下

HTML: HTML:

<div class="left-vp">
   <img src="~/Content/images/vp1.png" />
</div>
<div class="mid-vp">

</div>
<div class="right-vp">
<p>
    //some text here or another img
</p>
</div>

CSS CSS

 .left-vp {
    float: left;
 }

 .mid-vp {
    height: 2px;
    background: #FFFFFF url("images/dot.png") repeat-x;
    width: 100%;
 }
 .right-vp {
    float: right;
 }

Is something like this possible with CSS? CSS可以这样吗?

If you have control of the markup, and don't mind making changes, you can use table block styles to accomplish this. 如果您可以控制标记,并且不介意进行更改,则可以使用表块样式来完成此操作。 It's the only way I know of which will handle all scenarios and resizing. 这是我知道哪种方式可以处理所有场景和调整大小的唯一方法。

HTML HTML

<div class="container">
  <div>
    <div class="col col1">
      <div class="nowrap">Column 1</div>
    </div>
    <div class="col col2 fill center">
      <div class="nowrap">Column 2</div>
    </div>
    <div class="col col3">
      <div class="nowrap">Column 3</div>
    </div>
  </div>
</div>

CSS CSS

.container { width: 100%; }
.container { display: table; }
.container > div { display: table-row; }
.container > div > div { display: table-cell; }
.container > div > div { padding: .5em; }

.container .nowrap { white-space: nowrap; }
.container .fill { width: 100%; }
.container .center { text-align: center; }

.col1 { background: red; }
.col2 { background: blue; }
.col3 { background: green; }

In action: http://jsfiddle.net/Vxc3n/1/ 在行动: http//jsfiddle.net/Vxc3n/1/

A few things to keep in mind: 要记住以下几点:

  1. If your first and 3rd columns contain text, you will need to wrap them in a DIV which has the white-space: no-wrap CSS style 如果第一列和第三列包含文本,则需要将它们包装在具有空白区域的DIV中:无包装CSS样式
  2. If you have more than 1 fill column, ensure the width total = 100% (eg, 2 columns, use 50%) 如果您有超过1个填充列,请确保宽度总计= 100%(例如,2列,使用50%)
  3. You won't be able to shrink the columns beyond the minimum required width 您将无法将列缩小到超出所需的最小宽度

HTML HTML

<div id="container">
   <div id="left"></div>
   <div id="right"></div>
   <div id="center"></div>
</div>

CSS CSS

#container{width:100%;}
#left{float:left;width:100px; height: 100px; background-color: gray;}
#right{float:right;width:100px; height: 100px; background-color: green;}
#center{margin:0 auto;width:100%; height:100px;  background-color: blue;}

in action -> http://jsfiddle.net/5xfR9/39/ 在行动 - > http://jsfiddle.net/5xfR9/39/

I'm not sure what your actual requirements are for that central column but if it's just to contain a background as in the question could you not move the background styles to the container itself? 我不确定你对该中心列的实际要求是什么,但如果它只是包含问题中的背景,你可以不将背景样式移动到容器本身吗?

As an expansion on Eriks' jsfiddle: http://jsfiddle.net/5xfR9/46/ 作为Eriks的jsfiddle的扩展: http//jsfiddle.net/5xfR9/46/

HTML HTML

<div id="container" class="clearfix">
  <div id="left">some text</div>
  <div id="right">some text</div>
</div>

CSS CSS

#container{ width:100%; background-color: blue; }
#left{ float:left; height: 100px; background-color: red; }
#right{ float:right; height: 100px; background-color: green; }


.clearfix:before,
.clearfix:after {
    content: " ";
    display: table;
}
.clearfix:after {
    clear: both;
}

I've added a clearfix class to make sure the container actually contains the columns so that the background can show through (this is the clearfix class from a version of HTML5 Boilerplate). 我添加了一个clearfix类,以确保容器实际包含列,以便后台可以显示(这是HTML5 Boilerplate版本中的clearfix类)。

You just need to play around with min-width and max-width properties until you get what you want. 你只需要使用最小宽度和最大宽度属性,直到你得到你想要的。 And it seems to work easiest when you give the columns a max-width as a percentage of the body or a wrap. 当你给列的最大宽度作为主体或包裹的百分比时,它似乎最容易工作。

Here is a working example i put together: 这是我放在一起的一个工作示例:

http://jsfiddle.net/76Ep3/1/ http://jsfiddle.net/76Ep3/1/

HTML HTML

<div id="wrap">
    <div id="left">LEFT content...</div>
     <div id="center">CENTER content...</div>      
    <div id="right">Right content</div>
</div>

CSS CSS

 *{margin:0;padding:0;}

body, html{
    height:100%;
}

#wrap {
    min-width:390px;
    height:100%;
}

#left{
    float:left;
    min-width:100px;
    max-width:37%;
    margin:0 auto;
    background-color:blue;
    height:100%;
}

#center {
    float:left;
    min-width:100px;
    max-width:20%;
    background-color:red;    
    height:100%;
}

#right {
    float:left;
    min-width:100px;
    max-width:37%;
    background-color:yellow;
    height:100%;
}

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

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