How to save the correct image header grid with text content of various sizes

I have a problem here, if the title is too long, if it goes beyond the limits of one line, this causes the following elements to not float correctly. I would like to have this, so that regardless of the contents of the header, the next line will fit the space. I saw another site to achieve something very similar.

I can’t decide what else I need.

body { padding: 0; margin: 0 auto; } .container:after, .group:before, .group:after, .clearfix:before, .clearfix:after, .row:before, .row:after { content: '\0020'; display: block; overflow: hidden; visibility: hidden; width: 0; height: 0; } .container:after, .row:after, .clearfix:after, .group:after { clear: both; } .row, .clearfix, .group { zoom: 1; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } body, html { height: 100%; } body { color: #000; background: #FFF } .column { float: left; display: block } .constraint { padding-right: 2%; padding-left: 2%; margin-right: auto; margin-left: auto; width: 100% } .col-4 { width: 33.33333333% } .columns { padding-left: 25px; padding-right: 25px } .row { max-width: 100%; margin: 0px auto; width: auto } .picture { height: 0; position: relative; background-size: cover; background-position: center center; background-repeat: no-repeat } .thumb, .thumb-title, .thumb-description { margin-bottom: 1rem } .picture.portrait { padding-bottom: 65% } section#projects .picture.portrait { padding-bottom: 75%; background-position: inherit } .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; overflow: hidden } 
  <div class="constraint"> <section id="projects"> <!-- Row --> <div class="row"> <!-- Project previews --> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </div> </section> </div> 

CSS tricks say:

The overflow method is based on setting the CSS property of the overflow of the parent element. If this property is set to auto or hidden in the parent element, the parent will expand to contain floats

Considering this, I set the div.project-thumbnail container, but without changes:

 .project-thumbnail { overflow: auto } 

jsFiddle


EDIT

As follows from the comment, setting the height of the inscription div div.thumb will allow it to swim correctly. A fixed height trims content that becomes too large for it. Adding overflow: auto to this div will just add scrolling to it, this is not what I want.


EDIT

This site here has a similar system. Elements with a width float, there is no line separator, but if the content within its div goes beyond several lines, the lower part of the div will be adjusted, carefully aligning the next line.

+5
source share
5 answers

First decision

This is what you are looking for, you only need to add one rule to your css,

 .row article:nth-child(3n+1){ clear: left; } 

 .row article:nth-child(3n+1){ clear: left; } body { padding: 0; margin: 0 auto; } .container:after, .group:before, .group:after, .clearfix:before, .clearfix:after, .row:before, .row:after { content: '\0020'; display: block; overflow: hidden; visibility: hidden; width: 0; height: 0; } .container:after, .row:after, .clearfix:after, .group:after { clear: both; } .row, .clearfix, .group { zoom: 1; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } body, html { height: 100%; } body { color: #000; background: #FFF } .column { float: left; display: block } .constraint { padding-right: 2%; padding-left: 2%; margin-right: auto; margin-left: auto; width: 100% } .col-4 { width: 33.33333333% } .columns { padding-left: 25px; padding-right: 25px } .row { max-width: 100%; margin: 0px auto; width: auto } .picture { height: 0; position: relative; background-size: cover; background-position: center center; background-repeat: no-repeat } .thumb, .thumb-title, .thumb-description { margin-bottom: 1rem } .picture.portrait { padding-bottom: 65% } section#projects .picture.portrait { padding-bottom: 75%; background-position: inherit } .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; overflow: hidden } 
 <div class="constraint"> <section id="projects"> <!-- Row --> <div class="row"> <!-- Project previews --> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </div> </section> </div> 

The second solution: display the inline block

Easily scalable fluid

I prefer this solution using display:inline-block , which is simple and now works fine for IE. The only drop is the spaces between the markup (which I chose to remove them here).

I just took the liberty of changing my code so that you can find which changes.

 body { padding: 0; margin: 0 auto; } .container:after, .group:before, .group:after, .clearfix:before, .clearfix:after, .row:before, .row:after { content: '\0020'; display: block; overflow: hidden; visibility: hidden; width: 0; height: 0; } .container:after, .row:after, .clearfix:after, .group:after { clear: both; } .row, .clearfix, .group { zoom: 1; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } body, html { height: 100%; } body { color: #000; background: #FFF } .column { display: inline-block; vertical-align: top; } .constraint { padding-right: 2%; padding-left: 2%; margin-right: auto; margin-left: auto; width: 100% } .col-4 { width: 33.33333333% } .columns { padding-left: 25px; padding-right: 25px } .row { max-width: 100%; margin: 0px auto; width: auto } .picture { height: 0; position: relative; background-size: cover; background-position: center center; background-repeat: no-repeat } .thumb, .thumb-title, .thumb-description { margin-bottom: 1rem } .picture.portrait { padding-bottom: 65% } section#projects .picture.portrait { padding-bottom: 75%; background-position: inherit } .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; overflow: hidden } 
 <div class="constraint"> <section id="projects"> <!-- Row --> <div class="row"> <!-- Project previews --> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article><article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article><article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article><article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article><article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article><article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </div> </section> </div> 

Third solution: grid layout

Very cool powerful modern CSS function, you can get more information here

 body { padding: 0; margin: 0 auto; } body, html { height: 100%; } body { color: #000; background: #FFF } .constraint { /*! padding-right: 2%; */ /*! padding-left: 2%; */ margin-right: auto; margin-left: auto; width: 100%; } .col-4 { /*! width: 33.33333333% */ } .columns { padding-left: 25px; padding-right: 25px; } .row { max-width: 100%; margin: 0px auto; width: auto; display: grid; grid-template-columns: 1fr 1fr 1fr; } .picture { height: 0; position: relative; background-size: cover; background-position: center center; background-repeat: no-repeat } .thumb, .thumb-title, .thumb-description { margin-bottom: 1rem } .picture.portrait { padding-bottom: 65% } section#projects .picture.portrait { padding-bottom: 75%; background-position: inherit } .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; } 
 <div class="constraint"> <section id="projects"> <!-- Row --> <div class="row"> <!-- Project previews --> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </div> </section> </div> 
+2
source

updated thanks to @ user10089632 for comments

To get the layout you want to use with float , try adding a clear property to every fourth .column .

Example

 .column:nth-child(3n + 4) { clear: both; } 

This will target every third .column starting on the 4th .column (you don't need to apply the rule to the first div in your example)

You can configure nth-child(n) if you need to change your grid to 4 x 4 or something else.

Instead, you can apply this code to article , depending on the rest of your site.

fiddle

 body { padding: 0; margin: 0 auto; } .container:after, .group:before, .group:after, .clearfix:before, .clearfix:after, .row:before, .row:after { content: '\0020'; display: block; overflow: hidden; visibility: hidden; width: 0; height: 0; } .container:after, .row:after, .clearfix:after, .group:after { clear: both; } .row, .clearfix, .group { zoom: 1; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } body, html { height: 100%; } body { color: #000; background: #FFF } .column { float: left; display: block } .constraint { padding-right: 2%; padding-left: 2%; margin-right: auto; margin-left: auto; width: 100% } .col-4 { width: 33.33333333% } .columns { padding-left: 25px; padding-right: 25px } .row { max-width: 100%; margin: 0px auto; width: auto } .picture { height: 0; position: relative; background-size: cover; background-position: center center; background-repeat: no-repeat } .thumb, .thumb-title, .thumb-description { margin-bottom: 1rem } .picture.portrait { padding-bottom: 65% } section#projects .picture.portrait { padding-bottom: 75%; background-position: inherit } .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; overflow: hidden } .column:nth-child(4) { clear: both; } 
  <div class="constraint"> <section id="projects"> <!-- Row --> <div class="row"> <!-- Project previews --> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article class="project-thumbnail col-4 columns column"> <div class="picture portrait" style="background-image:url('http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg'); background-position: 50%"> </div> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </div> </section> </div> 
+3
source

All this seems like too much code.

I simplified it with Flexbox (as shown below).

Here is what I did:

For .center, I added a class called the "center" to the div that contains the mesh container. It mainly centers your section.

For a section A section is a container of all grid elements. I added display, flexibility, flexibility and width. You do not need to declare all of them (except for display), as some of them are default values ​​that are delivered using display: flex. I recommend that you test all of this on the console.

For an article you need to declare the width and margin here. I declare 4 columns. The following is an example calculation using two columns:

Since my margin is 1.66%, my total area is 6.64%. How did I come up with this number? The first mesh element has an edge to the right and left. The second grid element has an edge to the right and left. So 1.66% X 4

Then I subtract 6.64 from 100, which gives me 93.36. I divide it into two parts because I have two columns. Thus, each column width will be 46.68%. * /

If you need to add padding, you will need to declare the following:

 article { box-sizing:border-box; } 

This will crash your columns.

CSS Tricks has a great tutorial on using flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

 .center { display: flex; flex-direction: column; align-items: center; } section { display:flex; flex-direction:row; flex-wrap:wrap; width:80%; } article { background: grey; width:21.68%; margin: 1.66%; } img { max-width:100%; } 
 <div class="center"> <section> <!-- Project previews --> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview with some content that makes it extend onto another line.</p> </div> </article> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> <article> <img src="http://images.all-free-download.com/images/graphiclarge/hd_sky_picture_05_hd_pictures_166299.jpg" alt="image"> <div class="thumb"> <h3 class="thumb-title">Box One</h3> <p class="thumb-description">Item preview</p> </div> </article> </section> </div> 
+2
source

Overflow has nothing to do with this. Your divs differ in height ... To display them correctly, set the height of the description container.

+1
source

you must specify a static height .project-thumbnail

 .project-thumbnail { margin-bottom: 80px; margin-bottom: 3rem; overflow: hidden; height: 365px; } 

enter image description here

enter image description here

0
source

Source: https://habr.com/ru/post/1273831/


All Articles