两栏布局固宽改成百分比如何实现?

请问各位大佬,如何不使用flex来实现下列flex的效果?

// css flex
#wrap {
    display: flex;
    flex-direction: row;
}
.content {
    flex: 1;
}
.menu {
    flex: 50%;
}

// html
<div id="wrap">
    <div class="content">content</div>
    <div class="menu">menu</div>
</div>

或者换种思路问,网上的两栏布局(两栏布局,一栏固宽,一栏自适应),改成一栏百分比宽度,一栏自适应,如何实现?

===================

我在描述的清楚一些吧,需要实现如下一个布局:

  1. 左右两列布局;
  2. 左栏自适应,右栏50%;
  3. 右栏不存在的时候,左栏可以自适应,自动填充剩余宽度
  4. 不能使用flex布局;
阅读 4.3k
8 个回答

content{
Flex:1 1 auto
}
menu{
flex:0 0 50%
}

display:table可以实现,具体代码搜索一下

calc函数 加 左右浮动

子元素一个定位,一个正常就行

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width: 60%;
            height: 400px;
            border: 1px solid red;
            margin: 100px auto;
            padding-left: 300px;
            position: relative;
        }
        .box1{
            position: absolute;
            left: 0;
            top: 0;
            width: 300px;
            height: 400px;
            background: gray;
        }

    </style>
</head>

<body>
    <div class="box">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</body>

</html>

利用浮动来布局,以下为代码:

    #wrap{
        position: relative;
    }
    .content{
        background-color: #ff0000;
        width: 100%;
        float: left;
    }
    .menu{
        width: 50%;
        margin-left: -50%;
        float: right;
        position: relative;
        background-color: #00ffff;
    }
    
    

点击此处查看示例

新手上路,请多包涵
  <div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>


  <style>
    div {
      height: 100px;
      outline: 1px solid red;
      transform: scale(-1);
      position: relative;
    }
    .left {
      float: left;
      width: 50%;
    }
    .right {
      overflow: hidden;
    }
  </style>

因为并没有提到具体的兼容要求,所以这里使用了 E + F 的相邻选择符方式来“判断”是否有侧边栏,如果没有侧边栏,那么就把内容一栏的 padding 给干掉。

<div class="box">
    <div class="side"></div>
    <div class="main">
        <div class="main_box"></div>
    </div>
</div>
.main {
  float:left;
    width: 100%;
    height: 300px;
    background-color: #d8d8d8;
    box-sizing: border-box;
}
.side {
    float: right;
    width: 50%;
    height: 320px;
    margin-left: -50%;
    background-color: #dd320f;
    opacity: .5;
}
.side + .main {
    padding-right: 50%;
}


/* 为了看里面的内容是怎么样的,加一个模块 */
.main_box {
    width: 98%;
    height: 100px;
    background-color: #33ff33;
}

大概的效果就是这样:
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题