桌面视图上浮动元素的问题

huangapple go评论61阅读模式
英文:

Issue with floated elements on desktop view

问题

I am working on a layout that contains some mainbars and some sidebars, on mobile it is fine but there is an issue on desktop view.

在移动端一切正常,但在桌面视图中存在问题。

There are unnecessary spaces between the mainbars and between the sidebars on desktop view.

在桌面视图中主栏之间和侧边栏之间有不必要的间距。

Please let me know the proper way to arrange the elements using CSS.

请告诉我使用CSS来正确排列这些元素的方法。

See the attached images:
请查看附加的图片:

  • 桌面视图上浮动元素的问题
  • 桌面视图上浮动元素的问题
  • 桌面视图上浮动元素的问题

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.content {
    background-color: brown;
    box-sizing: border-box;
    color: white;
    font-size: 2em;
    max-width: 800px;
    margin: 0 auto;
}
.main {
    background-color: green;
    width: 70%;
    float: left;
    box-sizing: border-box;
    border: 2px solid white;
    margin-bottom: 8px;
    clear: both;
}
.sidebar {
    background-color: blue;
    width: 30%;
    float: right;
    box-sizing: border-box;
    border: 2px solid white;
    margin-bottom: 8px;
    clear: both;
}
.clear{
    clear: both;
}
@media only screen and (max-width: 500px) {
    .main, .sidebar {
        width: 100%;
        clear: both;
    }
}

<!-- language: lang-html -->

<head>
    <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
</head>
<div class="content">
    <div class="sidebar">
        1-Sidebar
    </div>
    <div class="sidebar">
        2-Sidebar
    </div>
    <div class="main">
        3-Main
    </div>
    <div class="sidebar">
        4-Sidebar
    </div>
    <div class="main">
        5-Main
    </div>
    <div class="main">
        6-Main
    </div>
    <div class="sidebar">
        7-Sidebar
    </div>
    <div class="clear"></div>
</div>

<!-- end snippet -->

Update for @A Haworth reply:
请查看附加的图片。
桌面视图上浮动元素的问题

英文:

I am working on a layout that contain some mainbars and some sidebars, on mobile it is fine but there is issue on desktop view.

There are unnecessary spaces between the mainbars and between the sidebars on desktop view.

Please let me know the proper way to arrange the elements using css.

See the attached images桌面视图上浮动元素的问题桌面视图上浮动元素的问题桌面视图上浮动元素的问题

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

        .content {
            background-color: brown;
            box-sizing: border-box;
            color: white;
            font-size: 2em;
            max-width: 800px;
            margin: 0 auto;
        }
        .main {
            background-color: green;
            width: 70%;
            float: left;
            box-sizing: border-box;
            border: 2px solid white;
            margin-bottom: 8px;
            clear: both;
        }
        .sidebar {
            background-color: blue;
            width: 30%;
            float: right;
            box-sizing: border-box;
            border: 2px solid white;
            margin-bottom: 8px;
            clear: both;
        }
        .clear{
            clear: both;
        }
        @media only screen and (max-width: 500px) {
            .main, .sidebar {
                width: 100%;
                clear: both;
            }
        }

<!-- language: lang-html -->

&lt;head&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;initial-scale = 1.0,maximum-scale = 1.0&quot; /&gt;
&lt;/head&gt;
&lt;div class=&quot;content&quot;&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
        1-Sidebar
    &lt;/div&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
        2-Sidebar
    &lt;/div&gt;
    &lt;div class=&quot;main&quot;&gt;
        3-Main
    &lt;/div&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
        4-Sidebar
    &lt;/div&gt;
    &lt;div class=&quot;main&quot;&gt;
        5-Main
    &lt;/div&gt;
    &lt;div class=&quot;main&quot;&gt;
        6-Main
    &lt;/div&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
        7-Sidebar
    &lt;/div&gt;
    &lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Update for @A Haworth reply:
Please see the attached image.
桌面视图上浮动元素的问题

答案1

得分: 2

CSS浮动在某些情况下很有用,例如,当您希望文本围绕一个元素浮动时,但在给定情况下可能会导致问题。

CSS网格可以在更宽的视口设置中提供帮助。

更新:显示所需布局的图片已更改,原始答案不再适用。所以这里有一个稍微不同的方案。

您可以修改HTML,使所有主要元素都在一个父div内,而所有侧边栏元素都在另一个父div下面。

然后,我们在更宽的视口上使用网格来将两个(主要和侧边栏)定位在宽度的70%和30%处,新的设置意味着这两列是相互独立的 - 只是两个div的子元素在垂直方向上一个接一个地排列。

对于较窄的视口,我们将内容元素设置为单列网格。我们强制每个元素进入我们想要的网格行。似乎没有明显的算法来决定这一点,因此每个元素都设置了一个CSS变量(在了解确切要将哪个元素放在较窄的设置中的位置时,您将不得不设置这个变量),然后用它来通知网格应该位于哪个位置。

英文:

CSS float is useful when you want e.g. text to float around an element but it leads to problems in the given situation.

CSS grid can help instead for the wider viewport settings.

UPDATE: the picture showing the required layout has changed and the original answer here no longer applies. So here's a slightly different scheme.

What you can do is alter your HTML so all the main elements come within one parent div and all the sidebar elements under another.

Then we use grid on the wider viewport to position the two (main and sidebar) at 70% and 30% of the width respectively and the new setup means the two columns are independent of each other - it's just the two divs having their children one after the other vertically.

For the narrower viewport we make the content element be a single column grid. We force each element to go into the grid row we want.There appears not to be an obvious algorithm for deciding this so each of the elements sets a CSS variable (which you will have to set when you know exactly which element is to go where on the narrower setting) which is then used to inform the grid which position it should be in.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;initial-scale = 1.0,maximum-scale = 1.0&quot; /&gt;
  &lt;style&gt;
    .content {
      background-color: brown;
      box-sizing: border-box;
      color: white;
      font-size: 2em;
      width: min(800px, 100vw);
      margin: 0 auto;
      display: grid;
      grid-template-columns: 7fr 3fr;
    }
    
    .main&gt;* {
      background-color: green;
      box-sizing: border-box;
      border: 2px solid white;
      margin-bottom: 8px;
    }
    
    .sidebar&gt;* {
      background-color: blue;
      box-sizing: border-box;
      border: 2px solid white;
      margin-bottom: 8px;
    }
    
    @media only screen and (max-width: 500px) {
      .sidebar&gt;*,
      .main&gt;* {
        grid-row: var(--r);
      }
      .content {
        grid-template-columns: 1fr;
      }
      .main,
      .sidebar {
        display: contents;
      }
    }
  &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div class=&quot;content&quot;&gt;
    &lt;div class=&quot;main&quot;&gt;
      &lt;div style=&quot;--r: 3;&quot;&gt;
        3-Mains&lt;br&gt;more lines&lt;br&gt;lines..
      &lt;/div&gt;
      &lt;div style=&quot;--r: 5;&quot;&gt;
        5-Main
      &lt;/div&gt;
      &lt;div style=&quot;--r: 6;&quot;&gt;
        6-Main&lt;br&gt;more lines&lt;br&gt;lines...
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
      &lt;div style=&quot;--r: 1;&quot;&gt;
        1-Sidebar
      &lt;/div&gt;
      &lt;div style=&quot;--r: 2;&quot;&gt;
        2-Sidebar
      &lt;/div&gt;
      &lt;div style=&quot;--r: 4;&quot;&gt;
        4-Sidebar
      &lt;/div&gt;
      &lt;div style=&quot;--r: 7;&quot;&gt;
        7-Sidebar
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

ORIGINAL ANSWER:

The layout for narrower viewports does not on the face of it have a definite logic so it seems best to leave the HTML as it is as then the narrow viewport layout is just the natural layout.

For the wider viewports, the trick here is to define a grid with 70%/30% width split and also to set the flow to be dense. This means the system will fill up from the top each time if there is space. So for example in this case the main which is number 3 will go into column 1 row 1 as there is space for it (I've added a bit of extra text to prove that the system adjusts to the content).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.content {
  background-color: brown;
  box-sizing: border-box;
  color: white;
  font-size: 2em;
  width: min(800px, 100vw);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 7fr 3fr;
  grid-auto-flow: dense;
  row-gap: 8px;
}

.main {
  background-color: green;
  box-sizing: border-box;
  border: 2px solid white;
  ;
  grid-column: 1;
}

.sidebar {
  background-color: blue;
  box-sizing: border-box;
  border: 2px solid white;
  grid-column: 2;
}

@media only screen and (max-width: 500px) {
  .main,
  .sidebar {
    width: 100%;
  }
  .content {
    display: block;
  }
}

<!-- language: lang-html -->

&lt;head&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;initial-scale = 1.0,maximum-scale = 1.0&quot; /&gt;
&lt;/head&gt;
&lt;div class=&quot;content&quot;&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    1-Sidebar
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    2-Sidebar
  &lt;/div&gt;
  &lt;div class=&quot;main&quot;&gt;
    3-Main
    &lt;p&gt;with some more text over more than one line&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    4-Sidebar
  &lt;/div&gt;
  &lt;div class=&quot;main&quot;&gt;
    5-Main
  &lt;/div&gt;
  &lt;div class=&quot;main&quot;&gt;
    6-Main
  &lt;/div&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    7-Sidebar
  &lt;/div&gt;
  &lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Note: I have inferred that as you set a max width to .content you want the whole viewport width to be used if it is narrower than 800px. Obviously change the width setting if something else is required.

huangapple
  • 本文由 发表于 2023年7月13日 13:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定