CSS Re-Centering elements on wrap

后端 未结 4 460
太阳男子
太阳男子 2021-01-18 01:32

I thought this would be simple but it\'s proving to be a bit of a headache. I\'m trying to get a grid of images to re-center when the user resizes the browser and causes one

相关标签:
4条回答
  • 2021-01-18 01:38

    Please add float:left to class .project-thumbnail for the browser breakpoints specific to different browser / screen/device sizes.

    .project-thumbnail{
        float:left;
        border:2px solid black;
        min-width: 269px;
        max-width: 269px;
    }
    

    Add margin: 0, auto; to following class.

    .child-wrapper
    {
        margin: 0, auto;
    }
    

    Going through the PHP code I understood that the DIV with the class name .project-thumbnail gets repeated through WHILE loop iterations.

    0 讨论(0)
  • 2021-01-18 01:40

    Have you tried:

    .child-wrapper{margin:0 auto;}
    

    So it stays centered? It usually works.

    0 讨论(0)
  • 2021-01-18 01:54

    I found a very similar question with two functional answers. One uses JS and the other uses placeholder elements. Neither are very pretty, but both appear to work around the inline-block whitespace wrap problem here.

    Shrink-wrap and center a container for inline-block elements

    0 讨论(0)
  • 2021-01-18 02:02

    This is the best solution I can think of with CSS only, the magic part is the @media queries. Obviously you'll have to do the math to fit your case.

    JsFiddle Demo

    body {
        margin: 0;
    }
    .parent-wrapper {
        margin: auto;
        width: 500px;
        padding: 5px 0;
        font-size: 0;
    }
    .child-wrapper {
        display: inline-block;
        width: 100px;
        font-size: 16px;
    }
    .child-wrapper img {
        max-width: 100%;
        height: auto;
        padding: 5px;
        vertical-align: top;
        box-sizing: border-box;
    }
    @media screen and (max-width: 499px) {
        .parent-wrapper { width: 400px; }
    }
    @media screen and (max-width: 399px) {
        .parent-wrapper { width: 300px; }
    }
    @media screen and (max-width: 299px) {
        .parent-wrapper { width: 200px; }
    }
    @media screen and (max-width: 199px) {
        .parent-wrapper { width: 100px; }
    }
    <div class="parent-wrapper">
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
        <div class="child-wrapper">
            <img src="//dummyimage.com/100" />
        </div>
    </div>

    0 讨论(0)
提交回复
热议问题