简体   繁体   English

在大屏幕上将n-div彼此相邻,在小屏幕上将n-div彼此相邻

[英]put n-divs next to each others on large screens and one below each others on small screens

I am struggling to achieve this efect ; 我正在努力实现这种效果;

I would like to put n-divs next to each others if the screen is big enough , and one below each other otherwise , and I would like those n-divs to be contained in one div ( the yellow container in my code ) and the title area (black in my code) + the yellow container in a wrapper that encapsulates everything ( green in my code ) 如果屏幕足够大,我想将n-div彼此相邻放置,否则将其彼此相邻放置,并且我希望将这些n-div包含在一个div中(我的代码中的黄色容器),并且标题区域(我的代码中为黑色)+包装所有内容的包装器中的黄色容器(我的代码中为绿色)

I started to write the code , but I am far from achieving the result. 我开始编写代码,但距离实现结果还差得很远。

Please , be nice to me , I am new to font-end developement and still in the learning process. 拜托,对我很好,我是字体开发的新手,仍在学习过程中。

Jsfiddle here --> https://jsfiddle.net/9atbtj0L/1/ Jsfiddle在这里-> https://jsfiddle.net/9atbtj0L/1/

I will appreciate any corrections and/or enhancements to my code. 我将对我的代码进行的任何更正和/或增强表示赞赏。

code here : 代码在这里:

html html

<div class="homepage-wrapper">
<div class="homepage-top-category-container">
    <div class="homepage-top-category-container-title">
        <span id="homepage-top-category-container-title">block title here</span>
    </div>
    <div class="homepage-top-category-container-list">
        <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catA">
            block A
        </div>
        <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catB">
            block B
        </div>
        <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catC">
            block C
        </div>
        <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catD">
            block D
        </div>
    </div>
</div>
    </div>
</div>  

css 的CSS

.homepage-wrapper{ /*This should contain the title of the grid + different blocks*/ 
    background-color: green;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto; 
    }
.homepage-top-category-container-title{
    background-color: black;
    margin-bottom: 10px;
    }
#homepage-top-category-container-title{
    color: orange;
    }
.homepage-top-category-container-list{ /*This should be the container*/
    display: flex;
    height: auto;
    margin-left: auto;
    margin-right: auto; 
    background-color: yellow;
    }
.homepage-top-category-container-item{
    display: block;
    float: none;
    width: auto;
    height:auto;
    border: solid 1px black;
    }

Thank you. 谢谢。

******************************EDIT*********************************** ******************************编辑******************* ****************

I've got some help fixing my code from very knowledgeable members of our community , so I have updated my code , alhough I noticed some others problems : 我从社区中知识渊博的成员那里获得了一些修复代码的帮助,因此尽管发现了其他一些问题,但我还是更新了代码:

1- block that have enough space to align on a same lign don't do so and go underneath. 1-块具有足够的空间以对齐同一块木质,请不要这样做并进入下方。

2- I would like to put 4 blocks per line with a left-margin only between them. 2-我想在每行之间放置4个块,并且在它们之间仅保留一个左边距。 The max-width for the wrapper is 1080px. 包装器的最大宽度为1080px。

4 divs of 255px + 3 left-margin of 20px and 0px on extremes ( right side of the first div and left side of the last div ). 255像素的4个div +极值(第一个div的右侧和最后一个div的左侧)的3个左边界,分别为20px和0px。

Edited code here : 在这里编辑代码:

html : html:

<div class="homepage-wrapper">
    <div class="homepage-top-category-container">
        <div class="homepage-top-category-container-title">
            <span id="homepage-top-category-container-title">block title here</span>
        </div>
        <div class="homepage-top-category-container-list">
            <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catA">
                <img src="https://s-media-cache-ak0.pinimg.com/originals/3f/b3/1c/3fb31c972e990cb214e55540dd9a2e2b.jpg" width="auto" height="auto">
            </div>
            <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catB">
                <img src="https://s-media-cache-ak0.pinimg.com/originals/3f/b3/1c/3fb31c972e990cb214e55540dd9a2e2b.jpg" width="auto" height="auto">
            </div>
            <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catC">
                <img src="https://s-media-cache-ak0.pinimg.com/originals/3f/b3/1c/3fb31c972e990cb214e55540dd9a2e2b.jpg" width="auto" height="auto">
            </div>
            <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catD">
                <img src="https://s-media-cache-ak0.pinimg.com/originals/3f/b3/1c/3fb31c972e990cb214e55540dd9a2e2b.jpg" width="auto" height="auto">
            </div>
        </div>
    </div>
</div>

and css : 和CSS:

.homepage-wrapper{ 
    background-color: green;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto; 
    }
.homepage-top-category-container-title{
    background-color: black;
    margin-bottom: 10px;
    }
#homepage-top-category-container-title{
    color: orange;
    }
.homepage-top-category-container-list{
    display: flex;
    flex-wrap:wrap;
    width: auto;
    height: auto;
    background-color: yellow; 
    }
.homepage-top-category-container-list > div {
        margin-left: 20px;
         margin-bottom: 20px;
    }
.homepage-top-category-container-item{
    display: block;
    float: none;
    width: auto;
    height:auto;
    border: solid 1px black;

}

and JSfiddle here ---> https://jsfiddle.net/mz2u6rzg/ 和JSfiddle在这里---> https://jsfiddle.net/mz2u6rzg/

I have added an image to better identify blocks. 我添加了图像以更好地识别块。 I will appreciate any corrections and enhancements from our community. 我会感谢我们社区的任何纠正和改进。

Thanks for your help. 谢谢你的帮助。

I think that you're looking for flex-wrap:wrap . 我认为您正在寻找flex-wrap:wrap

According to the MDN reference : 根据MDN参考

The CSS flex-wrap property specifies whether flex items are forced into a single line or can be wrapped onto multiple lines. CSS flex-wrap属性指定将弹性项目强制插入单行还是可以包装到多行。

 .homepage-wrapper{ /*This should contain the title of the grid + different blocks*/ background-color: green; max-width: 1080px; margin-left: auto; margin-right: auto; } .homepage-top-category-container-title{ background-color: black; margin-bottom: 10px; } #homepage-top-category-container-title{ color: orange; } .homepage-top-category-container-list{ /*This should be the container*/ display: flex; flex-wrap:wrap; height: auto; margin-left: auto; margin-right: auto; background-color: yellow; } .homepage-top-category-container-item{ display: block; float: none; width: auto; height:auto; border: solid 1px black; } 
 <div class="homepage-wrapper"> <div class="homepage-top-category-container"> <div class="homepage-top-category-container-title"> <span id="homepage-top-category-container-title">block title here</span> </div> <div class="homepage-top-category-container-list"> <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catA"> block A </div> <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catB"> block B </div> <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catC"> block C </div> <div class="homepage-top-category-container-item" id="homepage-top-category-container-item|catD"> block D </div> </div> </div> </div> 

You can achieve this using media query.I have used 400px as the break point you can use as per your choice.Here is a working JSfiddle link https://jsfiddle.net/0f5y8q8b/ 您可以使用媒体查询来实现这一点。我使用了400px作为您可以选择的断点。这里是一个有效的JSfiddle链接https://jsfiddle.net/0f5y8q8b/

@media only screen and (min-width: 400px) {
    .homepage-top-category-container-item{
        width: 100%
    }
    .homepage-top-category-container-list{
      display:block
    }

} }

you can use the css media tag to check for screen size but it might not be compatible with old browser 您可以使用css媒体标签检查屏幕尺寸,但可能与旧版浏览器不兼容

@media only screen and (max-width: 1023px) {
}

@media only screen and (min-width: 1024px) {
}

see CSS media queries for screen sizes 请参阅CSS媒体查询以获取屏幕尺寸

i'Ve updated your fiddle with this code AFTER the 'normal' styling so it will over write the 'default' display for your class 我在“正常”样式之后用此代码更新了您的小提琴,因此它将覆盖您班级的“默认”显示

@media only screen and (max-width: 300px) {
  .homepage-top-category-container-list{ /*This should be the container*/
  display: block;
  }
}

https://jsfiddle.net/9atbtj0L/1/ https://jsfiddle.net/9atbtj0L/1/

if you use the zoom in/out of your browser you will see it in action. 如果您使用浏览器的放大/缩小,则会看到它的实际效果。

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 小屏幕上的 insertBefore div 和大屏幕上的 insertAfter - insertBefore div on small screens and insertAfter on large screens 在小屏幕上显示行中的 div - Display divs in the row on small screens 大屏幕多视图,小屏幕单视图 - Multiple views for large screens, single view for small screens ReactJS - 获得两个 div 两个互相跟随滚动 - ReactJS - Get two divs two follow each others scrolling CSS 将两个 div 并排放置 - CSS put two divs next to each other jQuery:需要逻辑方面的帮助。 不同的div,每个div都有自己的触发器。 一次只能打开一个,关闭其他所有 - Jquery: Need help with the logic. Different divs, each w/ their own trigger. Only one can be open at a time, close all others divs旁边​​的每个,但页面上出现一个 - Divs next to each but one appearing off the page 有没有办法阻止绝对定位的div在小屏幕上消失? - Is there a way to stop absolute positioned divs from disappearing on small screens? 在小屏幕上固定定位的html divs有什么好的解决方案? - What is a good solution to fixed positioned html divs on small screens? Bootstrap Foundation:在大屏幕上,网站将两个容器彼此隔开放置 - Bootstrap Foundation: On large screens the website positions two containers besides each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM