Mosaic of images HTML/CSS

后端 未结 10 1259
情书的邮戳
情书的邮戳 2020-11-29 03:53

I want to make an image layout with portrait images inside a div with a fixed aspect ratio of 3:2. The size of images is 327

相关标签:
10条回答
  • 2020-11-29 04:00

    wrap your image with a div. set width and height for the div according to ratio. Give only height for image. If you want the image to take only space they need use display:inline also

    0 讨论(0)
  • 2020-11-29 04:02

    Absolute positioning doesn't seem to be the best choice if you want to dynamically keep the same positions and ratio.

    Native HTML flow is usually the way to go. Absolute positioning is like vitamin tablets. You use it when you need it, but it's not your main food. ;)

    What I would do is :

    1. simply position the container as wanted (centered for example) and size its width with percentage of the window/section it's in.

    2. Then you put your ._pictures1-xx divs inside it, and size the pics' width using percentage of the container. Height will keep ratio automatically (*)

    3. I would then make the ._pictures1-xx divs display "inline-block" and float "left". Then a little div with clear"both" after the last pic and close the container.

    (*) reminder : the width or height default value is "auto", meaning any size that keeps the image ratio when the other is a px/% value. The horizontal margins natively become dynamic when your pics' height is defined, in order to keep your pics' ratios. If you define width and leave height auto, then the height is dynamic in order to keep the pics' ratio, and margins don't change.

    I hope this was helpful.

    0 讨论(0)
  • 2020-11-29 04:06

    If you set one dimension or the other, but not both, the images should resize fluidly. Try setting just the width to a percentage unit.

    0 讨论(0)
  • 2020-11-29 04:08

    Try something like this

    Styling

    body{
        background: black;
        width:80%;
        margin:5em auto;
        display:block;
    }
    .wrapper{
        background:#FFF;
        float:left;
    }   
    
    .container{
        height:476px;
        width:192px;
        display:inline-block;
        float:left;
    }
    .small{
        height:188px;
        width:125px;
        display:block;
        margin:0 auto;
        background:#333;
    }
    .medium{
        background:#666;
        width:192px;
        height:288px;
    }
    .large{
        height:476px;
        width:200px;
        background:#999;
        display:inline-block;
        float:left;
    }
    

    This is HTML

    <div class="wrapper">
        <div class="container">
            <div class="small">
                <div class="small_inner">
                </div>
            </div>        
            <div class="medium">
                <div class="medium_inner">
                </div>
            </div>
        </div>
        <div class="large">
            <div class="large_inner">
            </div>
        </div>
        <div class="container">
            <div class="medium">
                <div class="medium_inner">
                </div>
            </div>
            <div class="small">
                <div class="small_inner">
                </div>
            </div>  
        </div>
        <div class="large">
            <div class="large_inner">
            </div>
        </div>
        <div class="container">
            <div class="small">
                <div class="small_inner">
                </div>
            </div>        
            <div class="medium">
    
                <div class="medium_inner">
                </div>
            </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-29 04:09

    aspectRatioResizeImg
    This is a jQuery plugin that allows resizing of an image preserving its aspect ratio, fitting a container. Optionally the container can be resized to match the image aspect ratio.
    https://github.com/stereoactivo/jquery.resize-image-aspect-ratio

    0 讨论(0)
  • 2020-11-29 04:15

    To make a proper answer, I am first going to clarify the requirements :

    1. images all have the same aspect ratio : 3/2
    2. images shouldn't be cropped
    3. no space between images
    4. make a mosaic of images

    You can have thousands of possibilities to display your images. I am going to make a simple layout that should show you the way to build your own.

    Here is a FANCY FIDDLE of what you can achieve and here is what it looks like :

    Mosaic of images in html/css - example layout

    Code :

    body, html {
        width:100%;
        margin:0;
        padding:0;
    }
    #wrap {
        width:984px;
        height:492px;
    }
    .big_col, .medium_col, .small_col{
        height:492px;
        float:left;
    }
    img {
        display:block;
        margin:0;
        padding:0;
        border:none;
        float:left;
    }
    .big_col {
        width:328px;
    }
    .medium_col{
        width:164px;
    }
    .small_col{
        width:82px;
    }
    .big_img img {
        width:328px;
        height:492px
    }
    .medium_img img {
        width:164px;
        height:246px;
    }
    .small_img img {
        width:82px;
        height:123px;
    }
    <div id="wrap">
        <div class="big_col">
            <div class="small_img">
                <img src="https://picsum.photos/id/241/328/492" alt="" />
                <img src="https://picsum.photos/id/147/328/492" alt="" />
                <img src="https://picsum.photos/id/258/328/492" alt="" />
                <img src="https://picsum.photos/id/237/328/492" alt="" />
            </div>
            <div class="medium_img">
                <img src="https://picsum.photos/id/356/328/492" alt="" />
                <img src="https://picsum.photos/id/254/328/492" alt="" />
            </div>
            <div class="small_img">
                <img src="https://picsum.photos/id/156/328/492" alt="" />
                <img src="https://picsum.photos/id/175/328/492" alt="" />
                <img src="https://picsum.photos/id/132/328/492" alt="" />
                <img src="https://picsum.photos/id/197/328/492" alt="" />
            </div>
        </div>
        <div class="big_col">
            <img src="https://picsum.photos/328/492" alt="" />
        </div>
        <div class="small_col small_img">
            <img src="https://picsum.photos/id/210/328/492" alt="" />
            <img src="https://picsum.photos/id/152/328/492" alt="" />
            <img src="https://picsum.photos/id/142/328/492" alt="" />
            <img src="https://picsum.photos/id/189/328/492" alt="" />
        </div>
        <div class="medium_col medium_img">
                <img src="https://picsum.photos/id/254/328/492" alt="" />
                <img src="https://picsum.photos/id/111/328/492" alt="" />
        </div>
        <div class="small_col small_img">
                <img src="https://picsum.photos/id/198/328/492" alt="" />
                <img src="https://picsum.photos/id/201/328/492" alt="" />
                <img src="https://picsum.photos/id/286/328/492" alt="" />
                <img src="https://picsum.photos/id/145/328/492" alt="" />
        </div>
    </div>


    First step : think, calculate and think again

    First : To make it simple let's say your images can have 3 sizes (I changed the image size by 1 px to make calculations easier) :

    1. big : 328*492px
    2. medium, 1/2 of big : 164*246px
    3. small, 1/4 of big : 82*123px

    Second : As your images are all portraits and your container has the same height as the big image, you will have to work with 492px heigh columns that can have 3 widths :

    1. big : 328px wide, they can display all size images
    2. medium : 328/2 = 164px wide, they can display medium and small images
    3. small : 327/4 = 82px wide, they can only display small images

    Third : How many columns and what image sizes? This is up to you, you will have to make a choice according to the total width of your container and the number of images you want to display.

    But in your fiddle, the container (._pictures1) has a 935px width which will be impossible to achieve with the column widths chosen just before.

    935/82 = 11.4024...
    

    The closest you can get is 82*12 = 984px wide container.

    You will either have to change the width of the container either change the sizes of images and columns to fit your initial width.


    Or (spoiler) you can work with percentages, this can be an alternative especialy if you need your layout to be responsive but this can become complicated and I am trying to make things simple.

    As I am sure you are curious and want to check it out, here is an example layout in a

    Responsive mosaic of image fiddle

    Code snippet :

    body, html {
        width:100%;
        margin:0;
        padding:0;
    }
    #wrap {
        width:100%;
    }
    .big_col, .medium_col, .small_col{
        float:left;
    }
    img {
        height:auto;
        display:block;
        margin:0;
        padding:0;
        border:none;
        float:left;
    }
    .big_col {
        width:25%;
    }
    .medium_col{
        width:12.5%;
    }
    .small_col{
        width:6.25%;
    }
    
    .small_col img{
        width:100%;
    }
    .medium_col>img {
        width:100%;
    }
    .medium_col .small_img img {
        width:50%;
    }
    .big_col .small_img img {
        width:25%;
    }
    .big_col .medium_img img {
        width:50%;
    }
    .big_col img {
        width:100%;
    }
    <div id="wrap">
        <div class="big_col">
            <div class="small_img">
                <img src="https://picsum.photos/id/241/328/492" alt="" />
                <img src="https://picsum.photos/id/147/328/492" alt="" />
                <img src="https://picsum.photos/id/258/328/492" alt="" />
                <img src="https://picsum.photos/id/237/328/492" alt="" />
            </div>
            <div class="medium_img">
                <img src="https://picsum.photos/id/356/328/492" alt="" />
                <img src="https://picsum.photos/id/254/328/492" alt="" />
            </div>
            <div class="small_img">
                <img src="https://picsum.photos/id/156/328/492" alt="" />
                <img src="https://picsum.photos/id/175/328/492" alt="" />
                <img src="https://picsum.photos/id/132/328/492" alt="" />
                <img src="https://picsum.photos/id/197/328/492" alt="" />
            </div>
        </div>
        <div class="big_col">
            <img src="https://picsum.photos/328/492" alt="" />
        </div>
        <div class="small_col small_img">
            <img src="https://picsum.photos/id/210/328/492" alt="" />
            <img src="https://picsum.photos/id/152/328/492" alt="" />
            <img src="https://picsum.photos/id/142/328/492" alt="" />
            <img src="https://picsum.photos/id/189/328/492" alt="" />
        </div>
        <div class="medium_col medium_img">
                <img src="https://picsum.photos/id/254/328/492" alt="" />
                <img src="https://picsum.photos/id/111/328/492" alt="" />
        </div>
        <div class="small_col small_img">
                <img src="https://picsum.photos/id/198/328/492" alt="" />
                <img src="https://picsum.photos/id/201/328/492" alt="" />
                <img src="https://picsum.photos/id/145/328/492" alt="" />
                <img src="https://picsum.photos/id/198/328/492" alt="" />
        </div>
    </div>


    Next step : design the layout

    Use a pen, photoshop, or any other tool that suits you, if you are realy good you can even use your brain to mentaly represent the layout you want.

    I designed the image you can see at the bigininng of the answer.

    As I said before there are many layout posibilities (number of columns and sizes of images in those columns) so for the example I chose 2 big columns 1 medium and 2 small ones

    328*2+164+82*2 = 984px ( = width of container so it can fit!)
    

    Last step : start coding!

    You can see the result in this

    FIDDLE

    This layout is based on floats so we need to define in the width, height of container, columns, images so they can all fit next to each other nicely (as we have already thought about that with calculation and design, it's easy).

    body, html {
        width:100%;
        margin:0;
        padding:0;
    }
    #wrap {
        width:984px;
        height:492px;
    }
    .big_col, .medium_col, .small_col{
        height:492px;
        float:left;
    }
    img {
        display:block;
        margin:0;
        padding:0;
        border:none;
        float:left;
    }
    .big_col {
        width:328px;
    }
    .medium_col{
        width:164px;
    }
    .small_col{
        width:82px;
    }
    .big_img img {
        width:328px;
        height:492px
    }
    .medium_img img {
        width:164px;
        height:246px;
    }
    .small_img img {
        width:82px;
        height:123px;
    }
    <div id="wrap">
        <div class="big_col">
            <div class="small_img">
                <img src="https://picsum.photos/id/241/328/492" alt="" />
                <img src="https://picsum.photos/id/147/328/492" alt="" />
                <img src="https://picsum.photos/id/258/328/492" alt="" />
                <img src="https://picsum.photos/id/237/328/492" alt="" />
            </div>
            <div class="medium_img">
                <img src="https://picsum.photos/id/356/328/492" alt="" />
                <img src="https://picsum.photos/id/254/328/492" alt="" />
            </div>
            <div class="small_img">
                <img src="https://picsum.photos/id/156/328/492" alt="" />
                <img src="https://picsum.photos/id/175/328/492" alt="" />
                <img src="https://picsum.photos/id/132/328/492" alt="" />
                <img src="https://picsum.photos/id/197/328/492" alt="" />
            </div>
        </div>
        <div class="big_col">
            <img src="https://picsum.photos/328/492" alt="" />
        </div>
        <div class="small_col small_img">
            <img src="https://picsum.photos/id/210/328/492" alt="" />
            <img src="https://picsum.photos/id/152/328/492" alt="" />
            <img src="https://picsum.photos/id/142/328/492" alt="" />
            <img src="https://picsum.photos/id/189/328/492" alt="" />
        </div>
        <div class="medium_col medium_img">
                <img src="https://picsum.photos/id/254/328/492" alt="" />
                <img src="https://picsum.photos/id/111/328/492" alt="" />
        </div>
        <div class="small_col small_img">
                <img src="https://picsum.photos/id/198/328/492" alt="" />
                <img src="https://picsum.photos/id/201/328/492" alt="" />
                <img src="https://picsum.photos/id/145/328/492" alt="" />
                <img src="https://picsum.photos/id/198/328/492" alt="" />
        </div>
    </div>

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