【问题标题】:CSS simple responsive designCSS 简单的响应式设计
【发布时间】:2014-01-30 03:31:27
【问题描述】:

我正在尝试构建 3 个 div。 “left” “Center” 和 Right” 都向左浮动。“left”和“Right” div 的宽度是固定的(如侧边栏)。“center” div(内容)是 auto 或 %。 当我调整浏览器的大小时,在(例如中心 div 60%)的情况下,右侧 div 被推到较低的位置。如果我使用(例如 center div auto),我会得到“left” div,“center” div 会占用所有剩余空间和下面的“right” div。

我需要的是,“左”和“右” div 固定尺寸 250 像素。 “中心”填充了“左右” div 之间的剩余空间。并且在调整大小时,只有“中心 div”的宽度会改变以适应。

(注意:当然我使用媒体查询)这些东西对我有帮助,从 750px 到 950px。)

<style type="text/css">
#left {
float:left;
height: 200px;
width: 250px;
border: medium dashed #03F;
}

#center {
float:left;
height: 200px;
width: 60%;
border: medium solid #C00;
}

#right {
float:left;
height: 200px;
width: 250px;
border: medium dashed #0C6;
}
</style>


<div id="left">
</div>

<div id="center">
</div>

<div id="right">
</div>

【问题讨论】:

  • 如果不使用 CSS3 媒体查询来适应每个屏幕尺寸,您最好使用流畅的布局。即所有列的基于百分比的大小,否则在小型设备上,您的中心列将被压扁。
  • 你在使用媒体查询吗?

标签: css html


【解决方案1】:

检查这个:http://jsfiddle.net/87enE/

创建一个wrapper 并使用display:table;。然后对子元素使用dislay: table-cell:

HTML:

<div id="wrapper">

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

</div>

CSS:

#wrapper{
    width: 100%;
    display: table;
}

#left {
    display: table-cell;    
    height: 200px;
    width: 250px;
    border: medium dashed #03F;
}

#center {
    display: table-cell;
    height: 200px;
    border: medium solid #C00;
}

#right {
    display: table-cell;
    height: 200px;
    width: 250px;
    border: medium dashed #0C6;
}

对于响应式关注,最好使用percentage 而不是fixed 宽度:

小提琴:http://jsfiddle.net/87enE/3/

【讨论】:

  • 我看不出这适用于移动设备,中心栏会被压扁。
  • @luke2012 我同意你的看法,但这就是我们所要求的。
  • @tsterker 这似乎太错误了,最好从该人那里获取更多信息,而不是仅仅为他们提供不充分的修复。当有人说响应式时,我确定他们希望它在较小的设备上工作,但不会。
  • @luke2012 抱歉,我错过了响应式标题。我刚读了他写的关于fixed左右div的内容,这与标题确实矛盾。
  • @luke2012 由于 OP 没有进一步指定除灵活中心之外的任何所需响应行为,因此提供假设任意用例的答案是错误的。所以从这个意义上说,答案是相当精确的。尽管我同意指出这种方法的问题是好的,但我认为你甚至不能在移动设备上有效地使用 3 列设计。
【解决方案2】:

您应该将所有div 放在table tr td 下,并将first 和last td 设置为固定大小(以% 为单位)并将center 设置为100%。

试试这个:

<style type="text/css">
#left {
float:left;
height: 200px;
width: 100%;
border: medium dashed #03F;
}

#center {
float:left;
height: 200px;
width: 100%;
border: medium solid #C00;
}

#right {
float:left;
height: 200px;
width: 100%;
border: medium dashed #0C6;
}
</style>


<table style="width:100%;">
<tr>
<td style="width:20%;">
<div id="left">
</div>
</td>
<td style="width:60%;">
<div id="center">
</div>
</td>
<td style="width:20%;">
<div id="right">
</div>
</td>
</table>

这是demo

注意:如果您希望保持响应,您需要在 % 而不是 px 中提供所有宽度。您还可以获取有关媒体查询的更多详细信息here

它应该对你有帮助!

谢谢。

【讨论】:

    【解决方案3】:

    在这种情况下,我相信 FlexBox 能最好地处理这种情况。 #center 的宽度会改变以占用剩余空间。

    <style>
    #container {
      display:flex;
      flex-flow: row nowrap;
    }
    #left {
      height: 200px;
      width: 250px;
      order:0;
      border: medium dashed #03F;
    }
    #center {
      flex:1;
      order:1;
      height: 200px;
      border: medium solid #C00;
    }
    #right {
      height: 200px;
      width: 250px;
      order:2;
      border: medium dashed #0C6;
    }
    </style>
    

    【讨论】:

      【解决方案4】:

      你可以试试这个:

      Fiddle Here:

      <div id="wrapper" class="clearfix">
      
          <div class="div" id="left">
          </div>
          <div class="div" id="center">
          </div>
          <div class="div" id="right">
          </div>
      
      </div>
      

      祝你好运..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多