如何制作带有两行的水平滚动 CSS div?

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

How can I make css Scrollable horizontal div with two row?

问题

I want to make this UI (two rows)
每个元素都有自己的宽度并且可滚动

但我想要按照以下的顺序

如何使用CSS制作这个UI?

英文:

I want to make this UI (two rows)<br>
every elements has own width and scrollable<br>
如何制作带有两行的水平滚动 CSS div?

and I found some ways like this link<br>
https://codesandbox.io/s/flex-horizontal-scroll-with-wrap-max-2-rows-d7zj3m?file=/src/App.js:77-84

but I want to make the order is

如何制作带有两行的水平滚动 CSS div?
How can I make this ui with css?

答案1

得分: 2

"Overflow-x" is用于水平滚动 div,但如果没有"no wrap",将不起作用。

英文:

"Overflow-x" is to scroll div horizontally but won't work without "no wrap"

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

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

.container{
  background: lightblue;
  width: 30%;
  height:50px;
  overflow-x: scroll;
  white-space: nowrap; 
 
}
button{
  margin: 2px;
  height: 20px;
}

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;title&gt;HTML Tutorial&lt;/title&gt;

&lt;body&gt;

  &lt;div class=&quot;container&quot;&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;br&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
    &lt;button&gt;CSS&lt;/button&gt;
  &lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

but If you need to print odd and even numbers instead,

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;body&gt;

  &lt;style&gt;
    .container {
      background: lightblue;
      width: 100px;
      overflow-x: scroll;
      white-space: nowrap;
    }
    
    p {
      margin: 5px;
    }
  &lt;/style&gt;

  &lt;div class=container&gt;

    &lt;p class=&quot;special&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;first&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;second&quot;&gt;&lt;/p&gt;
    &lt;p class=&quot;special1&quot;&gt;&lt;/p&gt;

  &lt;/div&gt;

  &lt;script&gt;
    function print() {

      var i;
      var special = document.getElementsByClassName(&quot;special&quot;)[0];
      var write = document.getElementsByClassName(&quot;first&quot;)[0];
      var write1 = document.getElementsByClassName(&quot;second&quot;)[0];
      var special1 = document.getElementsByClassName(&quot;special1&quot;)[0];

      for (i = 1; i &lt;= 62; i++) {

        special.innerHTML += &#39; - &#39;;
      }

      for (i = 1; i &lt;= 60; i++) {
        if ((i % 2) == 0) {
          continue; //if num is odd, skip it. 
        }
        write.innerHTML += i + &#39; &#39;;
      }

      for (i = 2; i &lt;= 60; i++) {
        if ((i % 2) == 1) {
          continue; //if num is odd, skip it. 
        }
        write1.innerHTML += i + &#39; &#39;;
      }
      for (i = 1; i &lt;= 62; i++) {

        special1.innerHTML += &#39; - &#39;;
      }

    }

    print(); //invoke print function
  &lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 08:06:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276928.html
匿名

发表评论

匿名网友

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

确定