【问题标题】:Make full-width without setting `width` [duplicate]在不设置`width`的情况下制作全角[重复]
【发布时间】:2016-12-31 13:15:42
【问题描述】:

我将模拟我需要实现的目标。

例如,我希望 #2 占据整个空间,剩下的 100% - 180px.. 如何实现?

附言似乎flexboxcalc 更受设备支持 - http://css3clickchart.com/#flexbox

【问题讨论】:

  • 这个堆栈溢出question 可能有帮助!
  • display: table 父级和display: table-cell 子级,flexbox#2 {position: absolute; left: 0; top: 0; padding-left: 180px;
  • 您可以使用 flex 来为您解决问题。您可以使用 jQuery 来计算该空间并将其放入您的 CSS 中。我不知道 css calc 是否可以解决空间
  • This@user3202670 提供的链接中的答案可能特别感兴趣
  • @justinas,不,这很混乱。将查看此处发布的其他解决方案。

标签: css


【解决方案1】:

您可以使用如下所示的 flexbox 模型。添加flex: auto; 将允许正确的内容使用剩余宽度。

* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
#parent {
  display: flex;
  height: 100%;
}
#left {
  width: 180px;
  background-color: hotpink;
}
#right {
  flex: auto;
  background-color: dodgerblue;
}
<div id="parent">
  <div id="left"></div>
  <div id="right"></div>
</div>

【讨论】:

  • 请发布有关flex的浏览器支持信息
  • 似乎flexboxcalc 更受设备支持
【解决方案2】:

使用 css calc

这里有一个例子。这可能会有所帮助:

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

【讨论】:

  • 请发布有关calc的浏览器支持信息
  • 查看linklink
【解决方案3】:

您可以使用float: leftoverflow: hidden

aside {
  float: left;
  width: 30%;
  background: beige;
}
article {
  overflow: hidden;
  background: brown;
  color: white;
}
<aside>
  sidebar
</aside>
<article>
  content
</article>

【讨论】:

  • overflow:hidden 不是个好办法
【解决方案4】:

有很多方法可以做到这一点。下面是一种简单的方法。

第一种方式:简单的内联块

.container {
  width: 100%;
  height: 600px;
  background-color: #f2f2f2;  
  }

.sidebar {
  background-color: red;
  width: 180px;
  height: 600px;
  float: left;
}
.main-content {
  display: inline-block; 
  background-color: green;  
}
<div class="container">
  <div class="main-content">Main Content</div>
  <div class="sidebar">Sidebar</div>
</div>

不过警告:在这种情况下,.main-content 只会占用它需要的空间,实际上不会是全宽。所以如果你想给它设置背景,你应该把背景设置为.container。

第二种方式:使用计算宽度

.container {
  width: 100%;
  height: 600px;
  background-color: #f2f2f2;
  position: relative;
  }

.sidebar {
  background-color: red;
  width: 180px;
  height: 600px;
  float: left;
}
.main-content {
  float: right;
  background-color: green;  
  width: calc(100% - 180px);
  height: 600px;
}
<div class="container">
  <div class="main-content">Main Content</div>
  <div class="sidebar">Sidebar</div>
</div>

第三种方式:使用 Flex

.container {
  width: 100%;
  height: 600px;
  background-color: #f2f2f2;
  display: flex;
  }

.sidebar {
  background-color: red;
  width: 180px;
  height: 600px;
}
.main-content {
  background-color: green;
  flex: auto;
}
<div class="container">
  <div class="sidebar">Sidebar</div>
  <div class="main-content">Main Content</div>
</div>

Flexbox 可能是最好的解决方案,但据说旧浏览器不支持它。

第 4 种方法是伪造表格的老式方法:

.container {
  width: 100%;
  height: 600px;
  background-color: #f2f2f2; 
  display: table;
  }

.sidebar {
  background-color: red;
  width: 180px;
  display: table-cell;
}
.main-content {
  display: table-cell;
  background-color: green;  
}
<div class="container">
  <div class="sidebar">Sidebar</div>  
  <div class="main-content">Main Content</div>
</div>

【讨论】:

  • 为什么投反对票?
  • 不要惊讶,您可能会在 StkOvfl 上进入 -~~~ 却没有意识到为什么......阅读常见问题解答。不过我投了赞成票。
  • 谢谢。我在 SO 上回答的第一天。 :)
  • 这个答案不起作用。为主要内容添加背景,您会看到。
  • @Andrej 祝你好运......但是,是的,再次查看您的答案(我已更新),当您单击 RUN SNIPPET 时,您将看到您的代码不起作用。
猜你喜欢
  • 2017-04-22
  • 2021-12-10
  • 2020-01-04
  • 2019-11-26
  • 1970-01-01
  • 2016-01-26
  • 2010-12-26
  • 2012-01-30
  • 2019-09-16
相关资源
最近更新 更多