简体   繁体   English

使响应式画廊HTML / CSS

[英]Making responsive gallery HTML/CSS

I'm trying to create a responsive gallery using HTML/CSS like in here: 我正在尝试使用HTML / CSS创建一个响应式画廊,如下所示: 在砌体中编号的绿色div框

Thats my HTML and CSS: 那就是我的HTML和CSS:

<div id="flow" class="container-fluid clear">
    <div id="photographies">
        {% for photo in photographies %}
            <div class="flow-photo {% if loop.index0 is odd %}odd{% endif %}">
                <a href="{{ path('photo', { id: photo.getId }) }}"><img id="{{ photo.getId }}" src="{{ photo.getImage }}"></a>
                <div class="info">
                    <div class="description">
                        <h1>{{ photo.getTitle }}</h1>
                    </div>
                </div>
            </div>
        {% endfor %}
    </div>
</div>
#flow {background:#f1f1f1;padding: 0.5% 1%;}
.flow-photo {width:49.5%;float:left;margin:0.5% 0;}
.odd {margin-left:1%}
.flow-photo>a>img {width:100%;height:auto;border-radius:2px 2px 0 0;}
.info {background:#fff;border-radius:0 0 2px 2px;text-align:left;padding:7px}
.info h1 {margin:0;font-weight:700;font-size:11px;}

With this current code I'm getting next grid 使用当前代码,我将获得下一个网格 在砌体中编号的绿色div框

The third div is not in the right place, because the first one is longer then second, so after second div there is a chaos. 第三分区的位置不正确,因为第一个分区的长度比第二个分区的长,因此第二个分区之后会出现混乱。

If I add after every second div clear:both , I get this: 如果我每隔div后添加clear:both ,就会得到:

在砌体中编号的绿色div框

There are gaps, because some of divs are longer than others. 存在差距,因为某些div比其他更长。 That's not what I want. 那不是我想要的

What should I do to get responsive gallery like in my first picture? 我应该怎么做才能像第一张照片一样获得响应式画廊?

You can create masonry layout with flexbox. 您可以使用flexbox创建砌体布局。 Here is the example: 这是示例:

<div class="masonry-layout">
  <div class="masonry-layout__panel">
    <div class="masonry-layout__panel-content">
      <-- CONTENT HERE -->
    </div>
  </div>
  <div class="masonry-layout__panel">
    <div class="masonry-layout__panel-content">
      <-- CONTENT HERE -->
    </div>
  </div>
  <div class="masonry-layout__panel">
    <div class="masonry-layout__panel-content">
       <-- CONTENT HERE -->
    </div>
  </div>
  <-- FOLLOWING CONTENT PANELS -->
</div>

And here is the css, column-count is how much columns you will have, in your case that will be 2. 这是css,column-count是您将拥有的列数,在您的情况下为2。

.masonry-layout {
  column-count: 2;
  column-gap: 0;
}
.masonry-layout__panel {
  break-inside: avoid;
  padding: 5px;
}
.masonry-layout__panel-content {
  padding: 10px;
  border-radius: 10px;
}

@media screen and (max-width: 600px) {
   .masonry-layout {
     column-count: 1;
     column-gap: 0;
   }
}

@media screen and (min-width: 768px) and (max-width: 1024px) {
   .masonry-layout {
     column-count: 2;
     column-gap: 0;
   }
}

Please see below code example. 请参见下面的代码示例。 This contains text but you can replace this with images. 它包含文本,但是您可以将其替换为图像。

CSS and HTML code CSS和HTML代码

 *, *:before, *:after { box-sizing: border-box !important; } article { -moz-column-width: 13em; -webkit-column-width: 13em; -moz-column-gap: 1em; -webkit-column-gap: 1em; } section { display: inline-block; margin: 0.25rem; padding: 1rem; width: 100%; background: #efefef; } p { margin: 1rem 0; } /* styles for background color, etc; not necessary for this thing to work */ body { padding: 1em; font-family: "Garamond", serif; } h1 { font-size: 3rem; font-weight: 800; } body { line-height: 1.25; } p { text-align: left; } 
 <h1>Pure CSS Masonry</h1> <p>By using CSS3 columns, we can easily create a Masonry.js-like layout where random-height blocks fit together.</p> <article> <section> <p>Lorem ipsum dolor sit amet, consectetur.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error aliquid reprehenderit expedita odio beatae est.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis quaerat suscipit ad.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem nihil alias amet dolores fuga totam sequi a cupiditate ipsa voluptas id facilis nobis.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem ut debitis dolorum earum expedita eveniet voluptatem quibusdam facere eos numquam commodi ad iusto laboriosam rerum aliquam.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat architecto quis tenetur fugiat veniam iste molestiae fuga labore!</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit accusamus tempore at porro officia rerum est impedit ea ipsa tenetur. Labore libero hic error sunt laborum expedita.</p> </section> <section> <p>Lorem ipsum dolor sit.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima asperiores eveniet vero velit eligendi aliquid in.</p> </section> <section> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus dolorem maxime minima animi cum.</p> </section> </article> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM