img scale to viewport inside div - html

I'm building an image lightbox with a close button. The Image should scale to it's default size until it hits a maximum of 90% page width/height.
This works fine with smaller images than the maximum, anything above will overflow the container div.
Here is my codepen example:
https://codepen.io/gempir/pen/eMYmyx
How do I force the image to scale?
Setting
img {
max-height: 100%;
max-width: 100%;
}
Does not help because the parent div has no fixed width. Any idea how to handle this situation correctly, besides using JS to calculate on the fly.
.image-overlay {
display: flex;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.25);
text-align: center;
user-select: none;
justify-content: center;
align-items: center;
}
.image-overlay .image-container {
display: inline-block;
position: relative;
max-width: 90% !important;
max-height: 90% !important;
background: blue;
}
.image-overlay .image-container .close {
position: absolute;
top: 5px;
right: 5px;
color: white;
background: rgba(0, 0, 0, 0.5);
padding: 5px;
}
.image-overlay .image-container img {
display: block;
max-height: 100%;
max-width: 100%;
}
<div class="image-overlay">
<div class="image-container">
<img src="https://picsum.photos/g/1800/1800">
<div class="close">close</div>
</div>
</div>

Use viewport units instead:
.image-overlay {
display: flex;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .25);
text-align: center;
user-select: none;
justify-content: center;
align-items: center;
}
.image-overlay .image-container {
display: inline-block;
position: relative;
max-width: 90% !important;
max-height: 90% !important;
background: blue;
}
.image-overlay .image-container .close {
position: absolute;
top: 5px;
right: 5px;
color: white;
background: rgba(0, 0, 0, 0.5);
padding: 5px;
}
.image-overlay .image-container img {
display: block;
max-height: 90vh;
max-width: 90vw;
}
<div class="image-overlay">
<div class="image-container">
<img src="https://picsum.photos/g/1800/1800">
<div class="close">close</div>
</div>
</div>
This will restrict your image based on the actual viewport available, instead of the container, your image is in.
Codepen example

You can use vmin and vmax to deal with this if you want to change behaviour depending on the viewport.
.image-overlay {
display: flex;
position: fixed;
left: 0;
top: 0;
height: 100%;
width: 100%;
background: rgba(0,0,0, .25);
text-align: center;
user-select: none;
justify-content: center;
align-items: center;
}
.image-container {
display: block;
position: relative;
background: blue;
}
.close {
position: absolute;
top: 5px;
right: 5px;
color: white;
background: rgba(0,0,0,0.5);
padding: 5px;
}
img {
display: block;
max-width: 90vmin;
max-height: 90vmin;
}
<div class="image-overlay">
<div class="image-container">
<img src="https://picsum.photos/g/1800/1800">
<div class="close">close</div>
</div>
</div>
Explanation:
vmin = 1%of min viewport size (width or height, equals to vh or vw depending on which one is the smaller)
vmax = the opposite of vmin.
what can vmin and vmax do?
you can use vh and vw (or % depending on the situation) to resize images, but sometimes they look too small on smartphones, so you can use:
.images{
max-width: 90vmin;
max-height: 90vmin;
}
#media(max-width:600px){
.images{
max-width: 90vmax;
max-height: 90vmax;
}
}
they'll fit well on computer viewports (or even smart TV etc) and will be a bit bigger on smartphones.

Related

set parent height based on his child image height

I have a div that with an image in it
#slideshow {
line-height: 1;
position: relative;
/*height: 125px;*/
/*width: 100vw;*/
border-radius: 12px;
overflow: hidden;
margin-bottom: 5px;
margin-top: 10px;
}
#slideshow img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
margin: 0 auto;
overflow: hidden;
opacity: 0;
font-size: 15vw;
text-align: center;
color: rgba(0, 0, 0, 0.2);
display: flex;
justify-content: center;
align-items: center;
border-radius: 12px;
}
<div id="slideshow">
<img alt="c" class="rectangle__image" src="/assets/media/ads-images/shad_NiOyJ1U.png">
</div>
I don't want to manually set the height of the image's parent; the height of the parent container should be contingent on the height of the image, but when I write the page this way, the image fails to display.
You need to remove position: absolute; from the img since absolute positioning removes the element from document's flow. Also you had set opacity to 0.
#slideshow {
line-height: 1;
position: relative;
/*height: 125px;*/
/*width: 100vw;*/
border-radius: 12px;
overflow: hidden;
margin-bottom: 5px;
margin-top: 10px;
}
#slideshow img {
width: 100%;
height: 100%;
margin: 0 auto;
overflow: hidden;
opacity: 1;
font-size: 15vw;
text-align: center;
color: rgba(0, 0, 0, 0.2);
display: flex;
justify-content: center;
align-items: center;
border-radius: 12px;
}
<div id="slideshow">
<img alt="c" class="rectangle__image" src="https://source.unsplash.com/random">
</div>
Pen Link - https://codepen.io/techysharnav/pen/RwpByxW

Div does not display when toggled when styled

Hello so I am trying to make a div appear when an element has been toggled. The function and events definitely work because if I remove the styling then the element appears but when I add it, it has disappeared and I actually have no idea why. It disappears when I add position:absolute but even when I put the height and width to 100% and it doesn't follow. Here is my Sandbox.
Here is my css:
* {
box-sizing: border-box;
}
html,
body,
#root {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
user-select: none;
background: lightblue;
}
.Main {
height: 100%;
width: 100%;
position: abosolute;
}
.Contain {
border-radius: 50%;
box-shadow: 50px rgba(0, 0, 0, 0.05);
position: absolute;
height: 50px;
width: 50px;
padding-left: 20px;
margin-bottom: 20px;
z-index: 998;
cursor: pointer;
}
.Contain p {
color: white;
}
.Item {
width: 100%;
height: 100%;
background: white;
border-radius: 5px 30px 30px 5px;
will-change: transform, opacity;
position: absolute;
display: inline-block;
bottom: 0.1%;
box-shadow: 50px rgba(0, 0, 0, 0.05);
}
.Item p {
color: black;
}
.Buttons {
top: 20%;
left: 25%;
z-index: 998;
position: absolute;
}
.Overlay {
background-color: #282c34;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
box-shadow: 0px 10px 10px -5px rgba(0, 0, 0, 0.05);
z-index: 999;
position: absolute;
}
I would post the JSX but they're in different pages and it is probably easier to see the sandbox
Thank you!
So I figured out that what was causing issues with my style was my <a.div> around my overlay component that reduced the styling. So I made the styling for the div to be position: 'sticky' and top:0 and width and height to 100%.
Any children of another div would be styled in relative to fit the parent div so the parent div must be styled accordingly

How to style a blurred, centered and responsive box for a login form with full size background?

I have tried to build a login form on a HTML page (Angular) that has a full size, centered background image and the form is placed in a div with blurred background, that is centered in the x- and y-axis of the browser window.
That is how far I came: https://codepen.io/surfermicha/pen/ZwpxBa
<div class="login-background-door2">
<div class="aero-background centered">
<h3>Here will be a login form later</h3>
</div>
</div>
Unfortunately i have some issues with that:
The centered box isn't exactly in the center
It's not responsive. The div is to small at small devices. I want 10px margin left and right, but a max-width 500px on bigger screens.
Could anyone help edit the codepen for a working responsive solution
You can set media queries by your needs, like I set into 567px because after 567px view of your center block, don't look nice so I set into 567px.
body, html {
font-family: "roboto", monospace;
color: #EEE;
padding: 0;
width: 100%;
margin: 0;
height: 100%;
overflow-x: hidden;
}
.aero-background::before {
content: '';
background: url("http://placekitten.com/2400/2000") center no-repeat;
filter: blur(6px);
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: -1;
pointer-events: none;
}
.aero-background {
border-radius: 5px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.4);
border: 1px solid rgba(255, 255, 255, 0.1);
color: white;
align-items: center;
justify-content: center;
text-shadow: 0 0 10px black;
}
.centered {
max-width: 500px;
min-height: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.login-background-door2 {
background: url("http://placekitten.com/2400/2000") center no-repeat;
height: 100%;
width: 100%;
overflow: hidden;
}
#media screen and (max-width: 567px) {
.centered {
width: 250px;
}
}
<div class="login-background-door2">
<div class="aero-background centered">
<h3>Here will be a login form later</h3>
</div>
</div>
https://codepen.io/anon/pen/MLjXgW
Centered with flex, no media queries
body, html {
font-family: "roboto", monospace;
color: #EEE;
padding: 0;
width: 100vw;
margin: 0;
height: 100vh;
overflow-x: hidden;
}
$login-background-image: "http://placekitten.com/2400/2000";
.aero-background::before{
content: '';
background: url($login-background-image) center no-repeat;
filter: blur(6px);
position: absolute;
left:0; top:0; right:0; bottom:0;
z-index: -1;
pointer-events: none;
}
.aero-background {
border-radius:5px;
box-shadow: 0 0 15px rgba(black, .4);
border:1px solid rgba(white,.1);
color: white;
align-items: center;
justify-content: center;
text-shadow:0 0 10px black;
max-width:500px;
min-height: 300px;
margin-left: 10px;
margin-right: 10px;
display: flex;
text-align: center;
padding: 10px;
}
.login-background-door2 {
background: url($login-background-image) center no-repeat;
height: 100%;
width: 100%;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
}
<div class="login-background-door2">
<div class="aero-background">
<h3>Here will be a login form later</h3>
</div>
</div>
My first time answering so I apologize if missed something

Overlap image with another image

First image is going as a background with its multiply blend mode and its color (100% x 1172px).
At the very bottom of this container is a block (50% x 520px) with same background, but without blending mode.
What exactly I'm trying to reach.
-- The idea is to keep these two image as a single one at least to >= 920px width of breakpoint.
HTML&CSS markup
.main-container {
display: flex;
height: 1172px;
width: 100%;
position: relative;
}
.main__fluid {
display: flex;
flex: 1;
}
.main__fluid--image {
background: url(https://i.imgur.com/eRnGawp.jpg);
background-size: cover;
background-repeat: no-repeat;
position: relative;
}
.main__inner-block {
display: flex;
align-items: flex-end;
position: relative;
height: 520px;
width: 50%;
margin-top: auto;
margin-left: auto;
margin-right: auto;
margin-bottom: 3rem;
border-radius: 10px;
box-shadow: 1px 2px 4px 0px rgba(0, 0, 0, 0.28);
z-index: 9;
}
.main__inner-block--image-mask {
overflow: hidden;
}
.image {
position: absolute;
width: 100%;
height: 100%;
left: 0;
bottom: 0;
background: url(https://i.imgur.com/Piu55zF.jpg);
background-repeat: no-repeat;
background-position: top -36rem center;
max-width: 100%:
}
.main__navigation {
height: 80px;
width: 100%;
background-color: #5ec8c3;
position: relative;
}
.footer {
background-color: #1f5c71;
height: 91px;
position: relative;
top: -5rem;
}
<div class="main-container">
<div class="main__fluid main__fluid--image">
<div class="main__inner-block main__inner-block--image-mask">
<div class="image"></div>
</div>
</div>
</div>
Currently have this one:
jsfiddle
jsfiddle (UPD)
UPD: found somewhat partial solution
I will assign parent for big image (which is with blending mode)
and child for smaller image (which is without blending mode)
Actually I just deleted property background-size from parent or/and add background-size: auto auto to its style, the idea is to, of course, prevent resizing on Y axis. Then did tweaks for child to fit it relative parent through background-position and the last one, aligned them centered with background-position: center. Updated jsfiddle with these changes.
Try this css block. It will align both images into same position.
.main-container {
display: flex;
height: 1172px;
width: 100%;
position: relative;
}
.main__fluid {
display: flex;
flex: 1;
}
.main__fluid--image {
background: url(https://i.imgur.com/Piu55zF.jpg);
background-size: cover;
position: relative;
background-color: #51c8c4;
background-blend-mode: multiply;
background-repeat: no-repeat;
background-position: top -27rem center;
}
.main__inner-block {
position: relative;
height: 45%;
width: 80%;
margin-top: 20.5%;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
box-shadow: 1px 2px 1px 0px rgba(0, 0, 0, 0.28)
}
.main__inner-block--image-mask {
overflow: hidden;
}
.image {
position: relative;
width: 100%;
height: 100%;
background: url(https://i.imgur.com/Piu55zF.jpg);
background-repeat: no-repeat;
background-position: top -45rem center;
}

CSS image resizing while keeping proportional

I'm trying to create a UI element that remains centered and the image scales to the size of the container. I have additional caption and text over the image. But the image seems to scale proportionally over only some of the range when I adjust the height or width. Preferably using only CSS and HTML. The top-level div does need to be absolutely positioned. I'm indifferent to the use of flexbox. That was just one tact I've tried.
Here it is as a codepen.
And the full code is below, though it is easier to play with it in the codepen as you can adjust the results pane easily. (open up the console so you can also adjust height.)
.block {
/* this needs to remain an absolute positioned block with size and location expressed in percent */
position: absolute;
height: 80%;
width: 80%;
top: 10%;
left: 10%;
background-color: #777777;
/* Don't care if using flexbox */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
text-align: center;
}
.imagecontain {
position: relative;
padding: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
}
.image {
max-height: inherit;
max-width: calc(100% - 8px);
padding: 0 !important;
border: 4px solid #123456 !important;
}
.button {
border-color: #b2b2b2;
background-color: #f8f8f8;
color: #444;
position: relative;
display: inline-block;
margin: 0;
padding: 0 12px;
min-width: 52px;
min-height: 47px;
border-width: 1px;
border-style: solid;
border-radius: 2px;
vertical-align: top;
text-align: center;
text-overflow: ellipsis;
font-size: 16px;
line-height: 42px;
cursor: pointer;
box-shadow: 0px 0px 3px #888888 !important;
}
.overimage {
vertical-align: bottom;
position: absolute;
bottom: 10%;
left: 50%;
max-width: 80%;
min-width: 60%;
padding: 5px;
opacity: .7;
transform: translateX(-50%);
background-color: black;
color: white;
border-radius: 10px;
z-index: 2;
}
.name {
text-align: bottom;
background-color: white;
padding: 5px;
border: 1px solid black;
}
<div class="block">
<div class="imagecontain">
<div class="overimage">this is a test of the emergency broadcast</div>
<img class="button image" src="https://scontent-dfw1-1.xx.fbcdn.net/hphotos-xfa1/v/t1.0-9/582520_10151526852434301_1015227082_n.jpg?oh=6537667094d5a160b8fbab0728dc2f5a&oe=57971FCB">
</div>
<div class="name">Mountains</div>
</div>
Use the background-size properties in CSS. Change your image from an tag to the background of the div. The specific property you're looking for is probably
background-size: cover;
http://www.w3schools.com/cssref/css3_pr_background-size.asp
Add:
.imagecontain {
max-width: 100%;
width: 100%
}
Remove:
.block {
height: 80%;
}