html - How to implement a gallery in CSS -
i have 4 images aligned side side. except third image below second image. both aligned right of first image. fourth image aligned right of second , third image (which stacked on top of each other).
see picture: http://www.gliffy.com/go/publish/9802269
how implement in css?
i have following code:
<div class="examples"> <div class="example"><img src="examples/1.jpg" /></div> <div class="example"><img src="examples/2.jpg" /></div> <div class="example"><img src="examples/3.jpg" /></div> <div class="example"><img src="examples/4.jpg" /></div> </div> i want automatically. no hacks using html.
i got them aligned side side, can't figure out how align second , third image second image on top of third image.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .examples { position: relative; display: block; box-sizing: border-box; height: 100vh; width: 100%; margin: 0; padding: 0; } .example { position: absolute; background-size: cover; background-repeat: no-repeat; width: 33.33%; cursor: pointer; margin: 0; padding: 0; box-sizing: border-box; background-position: 50% 50%; } .example:nth-child(1) { top: 0; left: 0; height: 100%; background-image: url(examples/1.jpg); } .example:nth-child(2) { top: 0; left: 33.33%; height: 50%; background-image: url('examples/2.jpg'); } .example:nth-child(3) { top: 50%; left: 33.33%; height: 50%; background-image: url('examples/3.jpg'); } .example:nth-child(4) { top: 0; left: 66.66%; height: 100%; background-image: url('examples/4.jpg'); } </style> </head> <body> <div class="examples"> <div class="example"></div> <div class="example"></div> <div class="example"></div> <div class="example"></div> </div> </body> </html>
here's pure css solution. 1 caveat, you'll run pixel rounding @ sizes, causing images off pixel or 2 @ viewport sizes.
.examples { position: relative; display: block; box-sizing: border-box; } .example { width: 25%; height: auto; position: absolute; margin: 0; padding: 0; display: inline-block; box-sizing: border-box; } .example img { width: 100%; height: auto; display: block; margin: 0; padding: 0; } .example:nth-child(1) { position: relative; } .example:nth-child(3) { margin-top: 12.475%; margin-bottom:-50%; } .example:nth-child(4) { left: 50%; } <div class="examples"> <div class="example"><img src="http://placehold.it/400x400&text=image1" /></div> <div class="example"><img src="http://placehold.it/400x200&text=image2" /></div> <div class="example"><img src="http://placehold.it/400x200&text=image3" /></div> <div class="example"><img src="http://placehold.it/400x400&text=image4" /></div> </div> and working jsfiddle
Comments
Post a Comment