Centering a variable-width grid after resizing a page

Here is an image of what I want to do in CSS: an image .

Any ideas?

+3
source share
5 answers

Original problem

Centering an element that does not have an explicit width

This prevents the possibility of using the usual margin: 0 auto; solution margin: 0 auto; .

A big problem

There is no unused space to center the wrapper in the container

An element is called compressed if its width is determined by the width of its contents. If the content is resized, the width of the element also changes. This is the default behavior for elements that are inline, inline blocks, or float.

When the contents in a wrapped shrink parent wrapping to the next line, the parent object ceases to be compressed; now it takes up the full width of its container. Technically, the parent is centered as it completely fills the container, but its contents are not centered. And if the parent does not have a noticeable bg image or wallpaper, the parent will also not be centered.

This also applies to some more advanced features like CSS3 flexbox, which seems to prevent this from being an option.

Decision

Media Inquiries

Turns out there is a simple CSS solution. The main problem is that one mock grid of this type cannot support a wide range of screen sizes and still center the content. But with CSS3 multimedia queries , many grid layouts can be created with the appropriate selection based on screen size.

For example, a layout grid is created with 1 column, as well as one with two columns and one with three columns, etc. for the number of columns that may be required. Whichever grid matches the current screen size, the one that was used. Layout grids can be identical, with the exception of a fixed width for each of them, a width that is large enough to match the number of columns. Since each grid has a fixed width, it becomes trivial to center them horizontally using margin: 0 auto; .

In other words, instead of trying to get a single layout to handle the entire range of browser sizes, create a lot of layouts and choose one that can easily handle the current browser size.

+2
source

Put all the divs in another div and make auto margin on both sides. See This Comprehensive Example: Problem with centering a div using css

0
source

Just set the CSS wrapper property as

 #wrapper{ margin: 0px auto; } 

The above code automatically aligns the center when resizing the browser window. This is the process of adding reaction to your page.

0
source

You want the div you want to center to have a width, and you need to do an automatic job. Here is an example:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Example</title> <style type="text/css"> body{ margin: 0px; padding: 0px; } #wrapper{ width: 800px; min-height: 200px; margin: auto; } .block{ width: 100px; height: 100px; float: left; } </style> </head> <body> <div id="wrapper"> <div class="block"></div> <div class="block"></div> <div class="block"></div> <div class="block"></div> </div> </body> </html> 
0
source

If you are too lazy to implement Media Queries (and for multiple scenarios to account for), I implemented a simple solution. Put the images in an unordered list (where li {display: inline-block}) and then (text-align: center) wrap. Adjust your fields, etc., to make it beautiful.

 <style> #wrapper { text-align: center; } #wrapper li { display: inline-block; } </style> <div id="wrapper"> <ul> <li><img src=""></img></li> <li><img src=""></img></li> <li><img src=""></img></li> <li><img src=""></img></li> <li><img src=""></img></li> </ul> </div> 
0
source

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


All Articles