如何在不使用表格或 JavaScript 的情况下实现以下结构?白色边框代表 div 的边缘,与问题无关。

Structure 1

中间区域的大小会有所不同,但它将具有精确的像素值,并且整个结构应根据这些值进行缩放。为了简化它,我需要一种方法来将“100% - n px”宽度设置为顶部中间和底部中间的 div。

我希望有一个干净的跨浏览器解决方案,但如果不可能,CSS hacks 也可以。

这是一个奖金。我一直在努力研究另一种结构,最终使用了表格或 JavaScript。它略有不同,但引入了新问题。我主要在基于 jQuery 的窗口系统中使用它,但我想将布局保留在脚本之外,并且只控制一个元素(中间的元素)的大小。

Structure 2

有帮助吗?

解决方案

可以使用嵌套元素和填充以获得工具栏上的左,右边缘。一个div元素的默认宽度是auto,这意味着它使用可用的宽度。然后,可以添加填充到元素,它仍然可用宽度内保持。

下面是一个例子,可以使用用于使图像作为左和右圆角,并且在它们之间重复的中央图像。

在HTML:

<div class="Header">
   <div>
      <div>This is the dynamic center area</div>
   </div>
</div>

在CSS:

.Header {
   background: url(left.gif) no-repeat;
   padding-left: 30px;
}
.Header div {
   background: url(right.gif) top right no-repeat;
   padding-right: 30px;
}
.Header div div {
   background: url(center.gif) repeat-x;
   padding: 0;
   height: 30px;
}

其他提示

新的方式我只是偶然发现了: CSS calc()

.calculated-width {
    width: -webkit-calc(100% - 100px);
    width:    -moz-calc(100% - 100px);
    width:         calc(100% - 100px);
}​

来源:的 CSS宽度100%减去100px的

您可以使用 Flexbox的 布局。您需要设置 flex: 1 需要有动态的宽度或元素高度为 flex-direction: row and column分别

<强>动态宽度:

<强> HTML

<div class="container">
  <div class="fixed-width">
    1
  </div>
  <div class="flexible-width">
    2
  </div>
  <div class="fixed-width">
    3
  </div>
</div>

<强> CSS

.container {
  display: flex;
}
.fixed-width {
  width: 200px; /* Fixed width or flex-basis: 200px */
}
.flexible-width {
  flex: 1; /* Stretch to occupy remaining width i.e. flex-grow: 1 and flex-shrink: 1*/
}

<强>输出:

.container {
  display: flex;
  width: 100%;
  color: #fff;
  font-family: Roboto;
}
.fixed-width {
  background: #9BCB3C;
  width: 200px; /* Fixed width */
  text-align: center;
}
.flexible-width {
  background: #88BEF5;
  flex: 1; /* Stretch to occupy remaining width */
  text-align: center;
}
<div class="container">
  <div class="fixed-width">
    1
  </div>
  <div class="flexible-width">
    2
  </div>
  <div class="fixed-width">
    3
  </div>

</div>


<强>动态高度:

<强> HTML

<div class="container">
  <div class="fixed-height">
    1
  </div>
  <div class="flexible-height">
    2
  </div>
  <div class="fixed-height">
    3
  </div>
</div>

<强> CSS

.container {
  display: flex;
}
.fixed-height {
  height: 200px; /* Fixed height or flex-basis: 200px */
}
.flexible-height {
  flex: 1; /* Stretch to occupy remaining height i.e. flex-grow: 1 and flex-shrink: 1*/
}

<强>输出:

.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  color: #fff;
  font-family: Roboto;
}
.fixed-height {
  background: #9BCB3C;
  height: 50px; /* Fixed height or flex-basis: 100px */
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.flexible-height {
  background: #88BEF5;
  flex: 1; /* Stretch to occupy remaining width */
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<div class="container">
  <div class="fixed-height">
    1
  </div>
  <div class="flexible-height">
    2
  </div>
  <div class="fixed-height">
    3
  </div>

</div>

在通常的方式是通过Guffa,嵌套元素概述做。这是一个有点伤感无需添加额外的标记来获得你需要的挂钩,但在实践中一个包装DIV在这里或那里是不会伤害任何人。

如果你必须这样做没有额外的元素(例如,当你没有页面标记的控制),可以使用的盒大小,其中有相当不错的,但不完全或简单的浏览器的支持。可能不必依靠脚本虽然更多的乐趣。

也许我很愚蠢,但是 table 不是这里显而易见的解决方案吗?

<div class="parent">
    <div class="fixed">
    <div class="stretchToFit">
</div>

.parent{ display: table; width 100%; }
.fixed { display: table-cell; width: 150px; }
.stretchToFit{ display: table-cell; vertical-align: top}

我在 chrome 中发现的另一种方法甚至更简单,但天哪,这是一个黑客!

.fixed{ 
   float: left
}
.stretchToFit{
   display: table-cell;
   width: 1%;
}

仅此一项就应该水平填充该行的其余部分,就像表格单元格一样。然而,当它超过其父级的 100% 时,您会遇到一些奇怪的问题,将宽度设置为百分比值可以修复它。

我们可以很容易使用挠性盒实现这一点。

如果我们有像头,MiddleContainer和页脚三个要素。我们想给一些固定的高度,页眉和页脚。然后我们可以写出这样的:

有关阵营/ RN(默认值是“显示器”为柔性和“flexDirection”作为柱),在网络的CSS我们必须指定体容器或容器含有这些作为显示:“柔性”,挠曲方向: '列' 象下面这样:

    container-containing-these-elements: {
     display: flex,
     flex-direction: column
    }
    header: {
     height: 40,
    },
    middle-container: {
     flex: 1, // this will take the rest of the space available.
    },
    footer: {
     height: 100,
    }

如果你的包裹格为100%,你用填充的像素数量,那么如果填充#需要是动态的,你可以很容易地使用jQuery来修改你的加厚量当你的事件触发。

我有一个类似的问题,其中我想跨对左侧的一个图像在屏幕的顶部的横幅和到屏幕的边缘右侧的重复图像。我结束了解决它像这样:

CSS:

.banner_left {
position: absolute;
top: 0px;
left: 0px;
width: 131px;
height: 150px;
background-image: url("left_image.jpg");
background-repeat: no-repeat;
}

.banner_right {
position: absolute;
top: 0px;
left: 131px;
right: 0px;
height: 150px;
background-image: url("right_repeating_image.jpg");
background-repeat: repeat-x;
background-position: top left;
}

的关键是正确的标签。基本上,我指定我希望它从131px从右侧重复从左边为0px。

在某些情况下,您可以利用边距设置来有效指定“100% 宽度减去 N 像素”。请参阅已接受的答案 这个问题.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top