Create a collage of images in CSS - html

I'm working on a project and I got to a place where I decided to realized a collage.
I have done the collage already and it is here: Fiddle
But I would like to do the same thing without using the property float on the elements of div1.
If possible, I would like to use only the properties right and left on the element of div1 without reducing the size of the elements of that div.
Sorry I'm not a native English speaker, I'm trying my best.

Hopefully, you would like to see something like this:
body {
width: 380px;
margin: 0 auto;
}
.div1{
text-align:justify;
}
.div1:after {
content: "";
display: inline-block;
width: 100%;
}
.div1 span img{
width:45%;
}
<div class="div1">
<span><img src="https://picsum.photos/189/324?random=1" alt="rd1"/></span>
<span><img src="https://picsum.photos/189/324?random=2" alt="rd2"/></span>
</div>
<div class="div2">
<img src="https://picsum.photos/380/325?random=3" alt="rd3"/>
</div>
My comment: I have set an 'img' width value to 45%, but you could set it to any value which will be equal or less than 50%. By the way, I suggest you to use tag 'span' instead of 'p' because 'span' ,in my opinion, is more suitable for this purpose. If you will use my approach you will be able to add any number of images to parent 'div' tag. For example, if you will add any number of images and their total width will be equal or less than 100%, these images are will be placed evenly on row.

This is what you asked for. But I recommend not to go with this approach. Extending it will be a nightmare. If image sizes change then it will be impossible to fix with with just CSS.
body {
width: 380px;
margin: 0 auto;
}
.div1{
position:relative;
height:324px;
}
.div1 p {
margin-bottom: 0;
margin-top: 0;
width:50%;
position:absolute;
}
.div1 p:first-child {
left:0px;
}
.div1 p:last-child {
left:50%;
}
<div class="div1">
<p><img src="http://lorempixel.com/189/324/" alt=""/></p>
<p><img src="http://lorempixel.com/189/324/" alt=""/></p>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>

you can use display: flex; like that you dont have to use float
body {
/* width: 520px;*/
margin: 0 auto;
}
/*.div1 p {
margin-bottom: 0;
margin-top: 0;
}*/
.div1 {
display:flex;
justify-content:center;
}
.div2{
display:flex;
justify-content:center;
}
<div class="div1">
<img src="http://lorempixel.com/189/324/" alt=""/>
<img src="http://lorempixel.com/189/324/" alt=""/>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>
you mean something like this?
.div1 {
left: 0;
right: 0;
text-align: center;
margin: auto;
}
.div2 {
left: 0;
right: 0;
text-align: center;
margin: auto;
}
<div class="div1">
<img src="http://lorempixel.com/189/324/" alt=""/>
<img src="http://lorempixel.com/189/324/" alt=""/>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>

Related

Two images side by side and responsive

I'm trying to put two images side by side and make them responsive. The problem now is, that the second image wraps first and then reacts to the size of the browser.
I want them to stay on the same line (level) and change their size automatically and wrap at a certain point (this part isn't the problem)....
The html:
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="item2.jpg" alt="bag" />
</div>
<div class="itemwrapper">
<img src="item3.jpg" alt="pen" />
</div>
</div>
</div>
The css:
#wrapper {
max-width: 1050px;
margin: 60px auto 60px auto;
background-color: #DDD
}
.itemwrapper {
display: inline;
}
img {
max-width: 100%;
height: auto;
}
use display table to set it side by side and keep it side by side and responsive.
display: table; with table-layout: fixed; will create a fluid layout for child elements with display: table-cell;
this will not only keep them the same width but also keep the containers the same height.
vertical-align: top; will keep them aligned to the top alternatively you can change the vertical position to middle and bottom plus some others.
Any questions just fire away.
#wrapper {
max-width: 1050px;
margin: 60px auto 60px auto;
background-color: #DDD
}
#outer {
display: table;
width: 100%;
table-layout: fixed;
}
.itemwrapper {
display: table-cell;
vertical-align: top;
width: 100%;
}
img {
max-width: 100%;
height: auto;
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="item2.jpg" alt="bag" />
</div>
<div class="itemwrapper">
<img src="item3.jpg" alt="pen" />
</div>
</div>
</div>
if image are same size or same ratio, you may use flex , width and min-width to set a break point:
#outer {
width:70%;/* demo*/
margin:auto;/* demo*/
display:flex;
flex-wrap:wrap;
}
#outer>div {flex:1;}
#outer>div>img {
width:100%;
min-width:200px;/* demo*/
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="http://lorempixel.com/200/100" alt="bag" />
</div>
<div class="itemwrapper">
<img src="http://lorempixel.com/400/200" alt="pen" />
</div>
</div>
</div>
remove or reset to your needs the rules commented with demo.
Thanks for the help, but I'm doing it with a different solution now, whicha friend suggested:
#outer {
position: relative;
width: 50%;
height: 0;
margin: 30px auto 0 auto;
padding-top: 25%;
background-color: #999;
}
.itemwrapper {
position: absolute;
width: 50%;
height: 100%;
top: 0;
}
.item2 {
left: 50%;
}
#outer img {
height: 100%;
max-width: 100%;
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="http://lorempixel.com/200/100" alt="bag" />
</div>
<div class="itemwrapper item2">
<img src="http://lorempixel.com/400/200" alt="pen" />
</div>
</div>
</div>
This evokes another problem though. The images arent filling the itemwrappers. I think i need to write some js for this :S.

CSS sizing div in div

I'm trying to make a div that contains 3 div, and each div contain an img.
But I want to make the first div (top) have a height of 10% of the screen size, the second (middle) 60%, and third (bottom) 30%. And actually, they don't resize, and they have their full size.
My code is actually :
#droite
{
display:block;
float:left;
margin-left:81%;
width:19%;
height:100%;
}
.logo
{
top-margin:0%;
height:10%;
width:100%;
}
.DroiteMilieu
{
top-margin:10%;
width:100%;
max-height:60%;
height:100%;
}
.basDePage
{
top-margin:80%;
height:30%;
width:100%;
}
img
{
position:absolute;
}
(#droite is actually the big division, in which logo is the first div, droiteMilieu the second and basDePage the third one. (I will rename them soon, but I want it to work first /:) )
And the html :
<div id="droite">
<div class="logo">
<img src="logo.png" style="max-width:100%; max-height:100%; position:relative;"</img>
</div>
<div class="DroiteMilieu">
<img src="droite.PNG" style="max-width:100%; max-height:100%; position:relative;"</img>
</div>
<div class="basDePage">
<img src="minimap.png" style="max-width:100%; max-height:100%; position:relative;" </img>
</div>
</div>
I'm actually still bad with html/css, and I don't understand where my error is located. :/
Can someone help me?
EDIT [Problem Solve] :
A way to make it works is :
CSS :
html, body {
margin: 0;
height: 100%;
width:100%;
}
#droite {
display:block;
float:left;
margin-left:81%;
width:19%;
height:100%;
}
.logo {
height: 10%;
background: red;
}
.DroiteMilieu {
height: 60%;
background: blue;
}
.basDePage {
height: 30%;
background: green;
}
#droite img {
max-width: 100%;
max-height: 100%;
position: relative;
}
And HTML :
<div id="droite">
<div class="logo">
<img src="logo.png" style="width:100%; height:100%;">
</div>
<div class="DroiteMilieu">
<img src="droite.PNG" style="width:100%; height:100%;">
</div>
<div class="basDePage">
<img src="minimap.png" style="width:100%; height:100%;">
</div>
</div>
Here is a solution for you.
You don't need margin top and width, as they default to 100% width and stack on top.
To make the floated div to have 100% height, the body needs too, so I added that.
html, body {
margin: 0;
height: 100%;
}
#droite {
display:block;
float:left;
margin-left:81%;
width:19%;
height:100%;
}
.logo {
height: 10%;
background: red;
}
.DroiteMilieu {
height: 60%;
background: blue;
}
.basDePage {
height: 30%;
background: green;
}
#droite img {
max-width: 100%;
max-height: 100%;
position: relative;
}
<div id="droite">
<div class="logo">
<img src="logo.png">
</div>
<div class="DroiteMilieu">
<img src="droite.PNG">
</div>
<div class="basDePage">
<img src="minimap.png">
</div>
</div>
You made a few mistake, first of all, top-margin is not a valid property. You have to use margin-top.
Secondly, If you add height: 100%; you're asking your div to take 100% of the space of its parent (in this case, is your div taking 100% of the screen height?).
To answer your question, the way to do it, if your project only have to support CSS3, i'd suggest you to use the property: vh or viewport height.
Here is a little tutorial on it:
https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/
You can use it this way:
.droite{
height: 10vh; /* If you need 10% of the viewport height, then you write it this way */
}
In other words, 1vh = 1% of the viewport height.
You are setting height: 100% on .DroiteMilieu which inherits from droite and makes it 100%. That might push the others out.
Remove the max-height: 60% on .DroiteMilieu and set height: 60%; see what you get.
plus top-margin: is not a valid css attribute
.DroiteMilieu
{
top-margin:10%; <---- bad css
width:100%;
height: 60%; <----
}

Align elements in a figure

I've got a problem with my HW. I cant align 2 elements on the left side https://jsfiddle.net/tkjxLfjy/ This is the code and i tried things like float:left but didn't work... So can you help me to put the meter and the text under the picture (the black sqare)?
According to w3school:
Elements after a floating element will flow around it. To avoid this,
use the clear property.
Add Clear:both to the div. The image has float:left so the next elements sit behind that.
Jsfiddle
figure div
{
clear: both;
}
You can remove float:left and add display: block to the image
Basically display: block reserve the whole line for the element, so that no other element set beside it, unless it's positioned.
Here is a fiddle
You should try like like this-
.clr{
clear:both
}
body {
font-family: serif;
height:100%;
width: 100%;
}
#container {
width: 650px;
border-radius: 10px;
height: 280px;
background-color: pink;
}
.header {
text-align: center;
position:relative;
top: 15px;
}
/* Figure one */
figure{
float: left;
}
img {
width: 150px;
height: 150px;
background: black;
}
meter {
width: 90px;
}
.meter-col{
float: left;
}
<div id="container">
<div class="header">
<h2>Profile</h2>
</div>
<figure>
<figcaption>User: Kent</figcaption>
<img src="avatar.png" />
</figure>
<div class="meter-col">
<div>Profile completed: 60%</div>
<meter value="60" min="0" max="100">2 out of 10</meter>
</div>
<div class="clr"></div>
</div>
May it will helps you.
<div id="container">
<div class="header">
<h2>Profile</h2>
</div>
<figure>
<figcaption>User: Kent</figcaption>
<img src="avatar.png" />
<div>
Profile completed: 60%
<meter value="60" min="0" max="100">2 out of 10</meter>
</div>
</figure>
</div>
No need of other changing 'cause the tag has a default "block" behavior.
I changed a few things around. I also updated a few things to HTML5 (preferred). I changed everything to display block and changed the div that all of that lives in to float: left. JS fiddle link below.
https://jsfiddle.net/tkjxLfjy/6/
HTML:
<body>
<div id="container">
<header>
<h2>Profile</h2>
</header>
<figure>
<figcaption>User: Kent</figcaption>
<img src="avatar.png" />
<label for="meter">Profile completed: 60%</label>
<meter name="meter" value="60" min="0" max="100">2 out of 10</meter>
</figure>
</div>
</body>
CSS:
body {
font-family: serif;
height:100%;
width: 100%;
}
#container {
width: 650px;
border-radius: 10px;
height: 280px;
background-color: pink;
float: left;
}
header {
text-align: center;
position:relative;
top: 15px;
}
/* Figure one */
img {
width: 150px;
height: 150px;
background: black;
display: block;
}
meter {
float: left;
width: 90px;
}

Scale image bug

My html:
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/ef/1e/45/ef1e450945a5a7ff0c4b7776810d4f90.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/0e/3b/85/0e3b858ffcfdbfa02b562c3dc7e3b5e1.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/df/c7/88/dfc7889e5dd99ad0c45834b4e4675389.jpg" alt="my img" />
</div>
My css:
.wrapper {
margin: 0;
padding: 0;
}
.scale-img {
transform: scale(0.5);
transform-origin: top left;
}
In my page I have several imgs, and each of the img's size is different, I don't want to set size in css for each img, and I just want to scale each img to half of its original size. But the wrapper div stay the img its original size, I don't know where go wrong?
Try this... For better understanding, I have used a dummy image.
.wrapper {
margin: 0;
padding: 0;
border: 1px solid #000;
position:absolute;
}
.scale-img{
width:100px;
height:100px;
position: relative;
}
<div class="wrapper">
<img class="scale-img" src="http://www.gettyimages.ca/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg" alt="my img" />
</div>
The reason transform doesn't affect the parent is because it "modifies the coordinate space of the CSS visual formatting model".
So in effect, it's sort of like doing this:
#container {
height:200px;
width:200px;
border:1px solid;
position:relative;
}
#container span {
background:red;
position:absolute;
top:50px;
left:50px;
right:50px;
bottom:50px;
}
<div id="container">
<span></span>
</div>
In an attempt to fix your issue, you'll more than likely need a bit of JavaScript:
var objScaleIMGs = document.querySelectorAll('img.scale-img') // Grab all the images that need scaling.
for (var i = 0; i < objScaleIMGs.length; i++) { // Loop through all the images, setting their dimensions half of what they currently are.
var h = objScaleIMGs[i].height;
var w = objScaleIMGs[i].width;
objScaleIMGs[i].height = h / 2;
objScaleIMGs[i].width = w / 2;
}
.wrapper {
margin: 0;
padding: 0;
border: 1px solid;
float: left;
}
.scale-img {
float: left;
}
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/ef/1e/45/ef1e450945a5a7ff0c4b7776810d4f90.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/0e/3b/85/0e3b858ffcfdbfa02b562c3dc7e3b5e1.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/df/c7/88/dfc7889e5dd99ad0c45834b4e4675389.jpg" alt="my img" />
</div>
You should transform the wrapper instead if you want to change the div's size with the change in img original size. The reason being that scale is used for img and not for wrapper div.
.wrapper {
margin: 0;
padding: 0;
height: 960px;
width: 540px;
}
.scale-img {
height:100%;
width:100%
}
You should refer: CSS Transform with element resizing
The problem I noticed is that when element scales, browser change its pixels ratio, not pixels amount. Element is smaller but it doesn't change its actual pixel size in DOM. Because of that I don't think that CSS-only solution exist.

Unexpected element behavior with css image centering

I've been trying to get this image centered in the page for a while, and for some reason margin-left: auto; margin-right: auto; weren't doing anything. So in the spirit of wildly trying everything in sight, I stumbled on the following surprisingly correct result. My question is, why on earth does setting the width to 25% work? I would have expected 100%, or 50% at least.
This fiddle shows some other widths, which apparently behave in a nonlinear fashion: http://jsfiddle.net/mo85kkvv/
(Bonus question: is there a super-obvious way to use the margin-left/right properties instead that I'm missing?)
HTML:
<body>
<div id="bcontainer">
<img src="banner.png" alt="banner" />
</div>
</body>
CSS:
body {
margin: 0;
}
#bcontainer {
width: 25%; /* why 25%?? */
height: 50px;
display: table-cell;
text-align: center;
}
I don't know a lot about HTML but I think that the proper way to define the class container is:
.container {
height: auto;
display: block;
margin:auto;
}
This is more generic. You can use the element inspector, and see how the layers change.
Is what your after ? http://jsfiddle.net/mo85kkvv/4/
HTML
<body>
<div class="container" id="one">
<img src="http://www.clker.com/cliparts/6/J/D/n/z/V/gold-scroll-banner.svg" alt="banner" />
</div><br>
<div class="container" id="two">
<img src="http://www.clker.com/cliparts/6/J/D/n/z/V/gold-scroll-banner.svg" alt="banner" />
</div><br>
<div class="container" id="three">
<img src="http://www.clker.com/cliparts/6/J/D/n/z/V/gold-scroll-banner.svg" alt="banner" />
</div><br>
<div class="container" id="four">
<img src="http://www.clker.com/cliparts/6/J/D/n/z/V/gold-scroll-banner.svg" alt="banner" />
</div>
</body>
CSS
body {
}
.container img{
width:100%;
display: inline;
text-align: center;
margin: 0 auto;
}
#one {
width: 25%;
margin: 0 auto;
}
#two {
width: 50%;
margin: 0 auto;
}
#three {
width: 75%;
margin: 0 auto;
}
#four {
width: 100%;
margin: 0 auto;
}
It all depends what you want. Do you want the wrapper to be centered with the image floating in the center, or do you want the wrapper (in this case .container) to shrink around the image and be the one that floats in the center? I have updated your fiddle with simple examples of a few options.
http://jsfiddle.net/mo85kkvv/6/