RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 853134
Accepted
Alex
Alex
Asked:2020-07-11 18:53:12 +0000 UTC2020-07-11 18:53:12 +0000 UTC 2020-07-11 18:53:12 +0000 UTC

如何修复“浮动:左”下的样式?

  • 772

如何正确设置样式,使块 v1 和 v2 在小屏幕上始终处于水平线上?就像图片中一样: 在此处输入图像描述

同时,以免在大屏幕上打破这种对齐方式: 在此处输入图像描述


在下面的代码中,在一个小屏幕上,它只能在很短的时间间隔内正常工作,否则它会像这样中断:

在此处输入图像描述


<style>
@media screen and (min-width: 428px) { /*Большой экран*/
    #aaa {float: left; width: 230px;}
    #vs1 {display : none;}
    #bbb {float: left; width: 170px;}
    #vs2 {display : block; clear: left;}
    #ccc {clear: left;}
}

@media screen and (max-width: 428px) { /*Маленький экран*/
    #aaa {width: 400px;}
    #vs1 {display: block;}
    #vs2 {display: none;}
    #bbb {width: 400px;}
}
</style>
</head>  
<body>

<div id="ab" style="">

       <div id="aaa" style="height: 200px; background-color: lightgreen; opacity: 0.3;">A</div>
       <div id="vs1">
               <div id="v1" style="float: left; width: 300px; height: 40px; background-color: orange; opacity: 0.3;">v1</div>
               <div id="v2" style="float: left; width: 100px; height: 40px; background-color: khaki; opacity: 0.3;">v2</div>
       </div>
       <div id="bbb" style="height: 200px; background-color: lightblue; opacity: 0.3;">B</div>

</div>       
<div id="vs2">
               <div id="v3" style="float: left; width: 300px; height: 40px; background-color: brown; opacity: 0.3;">v3</div>
               <div id="v4" style="float: left; width: 100px; height: 40px; background-color: yellow; opacity: 0.3;">v4</div>
</div>


<div id="ccc" style="width: 400px; height: 22px; background-color: red;">C</div>

</body>
css3
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    sirWill
    2020-07-11T19:50:04Z2020-07-11T19:50:04Z

    如果我们谈论响应式布局,那么块不应该有任何绝对宽度——这会在调整窗口大小时不断破坏布局。当然,如果需要针对特定​​设备/窗口调整输出,那么这些情况应该单独分析。

    #vs1在您的示例中,输出中断是因为容器在小屏幕上没有高度。最初,它等于0,因为嵌套在其中的所有块都带有float:left;,因此块 B 离开它们。

    使用flex. 适用于所有屏幕宽度,同时保持块的纵横比。如果您需要限制最小宽度 - 将 min-width 添加到必要的块中,您会很高兴;)

    body {
      margin: 0;
    }
    #v1, #v2, #v3, #v4 {
      height: 40px;
      opacity: 0.3;
    }
    #v1, #v3 {
      width: 75%;
    }
    #v2, #v4 {
      width: 25%;
    }
    #aaa, #bbb {
      height: 200px;
      opacity: 0.3;
    }
    #ccc {
      width: 100%;
    }
    
    @media screen and (min-width: 428px) {
      /*Большой экран*/
      #ab {
        display: flex;
        width: 100%;
      }
      #aaa {
        width: 57.5%;
      }
      #vs1 {
        display: none;
      }
      #bbb {
        width: 42.5%;
      }
      #vs2 {
        display: flex;
      }
    }
    
    @media screen and (max-width: 428px) {
      /*Маленький экран*/
      #aaa {
        width: 100%;
      }
      #vs1 {
        display: flex;
      }
      #vs2 {
        display: none;
      }
      #bbb {
        width: 100%;
      }
    }
    <div id="ab" style="">
      <div id="aaa" style="background-color: lightgreen;">A</div>
      <div id="vs1">
        <div id="v1" style="background-color: orange;">v1</div>
        <div id="v2" style="background-color: khaki;">v2</div>
      </div>
      <div id="bbb" style="background-color: lightblue;">B</div>
    </div>
    
    <div id="vs2">
      <div id="v3" style="background-color: brown;">v3</div>
      <div id="v4" style="background-color: yellow;">v4</div>
    </div>
    
    <div id="ccc" style="height: 22px; background-color: red;">C</div>

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5