How can I have a transparent overlay and center with flex box? - html

I have a block element with a background image. I am attempting to:
Add a transparent gradient overlay on top of a background image
Center another block element using flexbox,
I have already achieved both of these independently of each other, see the CodePen as well as the images and code below.
Using the transparent overlay (with the child block HTML removed):
Centering the child block (with the overlay CSS removed):
However when I try and combine the effects, the flexbox stops working and the overlay is missing around the child block
Here is my HTML
<div class="container">
<p>title goes here</p>
</div>
Here is my CSS:
.container {
background: url("http://msue.anr.msu.edu/uploads/images/forest.jpg");
height: 400px;
width: 100%;
box-shadow: inset 0px -4px 14px 0px rgba(50, 50, 50, 0.71);
background-size: 100% auto;
z-index: 0;
position: relative;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.container p {
display: block;
color: red;
text-align: center;
font-size: 24pt;
background-color: blue;
z-index: 2;
}
.container:after {
content: '';
background: linear-gradient(135deg, #dc4225, #292484);
width: 100%;
height: 100%;
opacity: 0.5;
top: 0;
left: 0;
display: block;
z-index: 1;
position: relative;
}
How can I get the semitransparent overlay overlay and the flexbox happening at the same time?

For .container:after, change position: relative to position: absolute.
Live, working example:
.container {
background: url("http://msue.anr.msu.edu/uploads/images/forest.jpg");
height: 400px;
width: 100%;
box-shadow: inset 0px -4px 14px 0px rgba(50, 50, 50, 0.71);
background-size: 100% auto;
z-index: 0;
position: relative;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.container p {
display: block;
color: red;
text-align: center;
font-size: 24pt;
background-color: blue;
z-index: 2;
}
.container:after {
content: '';
background: linear-gradient(135deg, #dc4225, #292484);
width: 100%;
height: 100%;
opacity: 0.5;
top: 0;
left: 0;
display: block;
z-index: 1;
position: absolute;
}
<div class="container">
<p>title goes here</p>
</div>
Codepen Version: http://codepen.io/anon/pen/gpqxoo

In case folks need to set position of the container to absolute like I do:
.container {
#include inline-flex;
#include flex-direction(column);
#include align-items(center);
#include justify-content(center);
background-position: center center;
background-repeat: no-repeat;
background-size: contain;
font-size: 16px;
height: 100%;
width: 100%;
position: absolute;
white-space: normal;
z-index: 1;
}
.container::after {
content: ' ';
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(0,0,0,.8);
z-index: -1;
}
The importance pieces here are rgba, and z-index. Background image of the container is set dynamically in code, but that isn't relevant here.

Related

how to perfectly center an icon image inside a <div>

I had an icon img and was trying to center it inside a card div but is somehow not working. I already tried text-align: center; and display:flex; justify-content: center; and both did not help. I attached my code below. Any help is appreciated. Thanks in advance.
* {
box-sizing: border-box;
}
.card {
background-color: #fff;
border-radius: 4px;
max-width: 0px;
height: 0px;
position: absolute;
padding: 20px;
color: #444;
cursor: pointer;
top: 3%;
right: 0.5%;
}
.card:before {
content: '';
display: block;
position: absolute;
background-color: #ccc;
left: 20px;
right: 20px;
bottom: 0;
top: 50%;
z-index: -1;
box-shadow: 0 0 40px lighten(#000, 60%);
transition: box-shadow .2s ease-in-out;
}
.card.level-1:hover:before {
box-shadow: 0 0 80px lighten(#000, 60%);
}
.card level-1 img {
display: flex;
justify-content: center;
}
<div class="card level-1">
<img src="https://img.icons8.com/windows/32/000000/microphone--v2.png" />
</div>
You should put flex on the container of the image like this:
.card {
display: flex;
justify-content: center;
align-items: center;
}

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

Trying to get the images to fit in box and Boxes to be responsive

I am trying to get this code to work I am about 90% of the way there, but am having difficulty scaling and making flexbox responsive.
What I want to know is:
1) When I change the width to auto on line 20 (also 44, and 68) why does the image disappear? I want image to fit in box and the text overlay to be over image.
2) How do I get the image box to be responsive? When in Mobile I want them stacked and then to flex as they get to desktop so they are in row. So column to row. Why is that not working?
Any help to get images to fit in box and the text overlay to cover image and for it to flex and be responsive would be helpful. I have been stuck on this for quite a while and not sure why this is not working.
Code Pen link
HTML
<div class="opener-wrapper">
<div id="opener1" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lorem Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
<div id="opener2" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lore Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
<div id="opener3" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lorem Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
</div>
CSS
.opener-wrapper {
display: flex;
flex-wrap: wrap;
}
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener1::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener2::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener2::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener3::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener3::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
.flex-center {
/* display: flex;
flex-direction: column;
justify-content: center;
align-content: center; */
}
.opener-msg {
color: #fff;
text-shadow: #343a40 2px 2px;
min-width: 100%;
min-height: 12em;
position: relative;
margin: 3% 0;
text-transform: uppercase;
}
.opener-msg::before {
content: "";
display: block;
position: absolute;
margin-left: 0;
min-width: 100%;
min-height: 12em;
z-index: -1;
opacity: 0.4;
background-color: #343a40;
}
.opener-title,
.opener-subtitle {
width: 100%;
display: block;
text-align: center;
}
.opener-title {
margin: 3% 0;
}
1) When I change the width to auto on line 20 (also 44, and 68) why does the image disappear? I want image to fit in box and the text overlay to be over image.
This box is set to position: absolute. Absolutely positioned elements are sized relative to their parent container (or next relatively positioned parent). They require a width and height.
What if you set the background-image on #opener1, and set the color overlay on #opener1::after?
2) How do I get the image box to be responsive? When in Mobile I want them stacked and then to flex as they get to desktop so they are in row. So column to row. Why is that not working?
You need to set the flex-direction to row on the flex container. There is currently no flex-direction set on .opener-wrapper. This will allow the boxes to flow horizontally. Also, you have min-width set to 100%. This will ensure that your boxes take up 100% of the width -- which is not what you want. Remove this line, and add max-width: 33.33%;.
You may want to take a look at Chris Coyier's A Complete Guide to Flexbox if you haven't yet!
1) width: auto; sets the width of the element to the browser default width. In this case, the browser initializes the :before pseudo-element with a width of 0px which can be seen in the computed styles in this imgur image
2) You will need #media rules to resize the elements depending on how the page is being viewed. Currently all of your openers have a width and min-width of 100%. This makes them use 100% of the space that they occupy. If you want elements to display on the same row, you have to divide that 100% by how many elements you want to display on each row. For the case of these three elements, I did the following.
.opener {
width: 33%;
height: 100%;
min-width: 33%;
min-height: 100%;
}
To follow this up, I added a #media section to the CSS to change the width of the elements to 100% when the display is smaller than 600px.
#media only screen and (max-width: 600px) {
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
width: 100%;
min-width: 100%;
}
#opener2::before {
width: 100%;
min-width: 100%;
}
#opener3::before {
width: 100%;
min-width: 100%;
}
}
All together, your CSS should be similar to this:
.opener-wrapper {
display: flex;
flex-wrap: wrap;
}
.opener {
width: 33%;
height: 100%;
min-width: 33%;
min-height: 100%;
}
#opener1::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener1::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener2::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener2::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener3::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener3::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
.flex-center {
/* display: flex;
flex-direction: column;
justify-content: center;
align-content: center; */
}
.opener-msg {
color: #fff;
text-shadow: #343a40 2px 2px;
min-width: 100%;
min-height: 12em;
position: relative;
margin: 3% 0;
text-transform: uppercase;
}
.opener-msg::before {
content: "";
display: block;
position: absolute;
margin-left: 0;
min-width: 100%;
min-height: 12em;
z-index: -1;
opacity: 0.4;
background-color: #343a40;
}
.opener-title,
.opener-subtitle {
width: 100%;
display: block;
text-align: center;
}
.opener-title {
margin: 3% 0;
}
#media only screen and (max-width: 600px) {
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
width: 100%;
min-width: 100%;
}
#opener2::before {
width: 100%;
min-width: 100%;
}
#opener3::before {
width: 100%;
min-width: 100%;
}
}

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;
}