Child with position absolute has full parent height - css

Trying to recreate this layout:
The problem is the white box on top of the black. I don't know the height of the black box. How can I create the white box on top of the others?
This is my code:
https://codepen.io/olefrankjensen/pen/oQyZpX?editors=1100
HTML
<div class="container">
<div class="formbox">[login form markup here...]</div>
<div class="box">
<h1>Don't have an account</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus dicta cumque harum.</p>
<button>SIGN UP</button>
</div>
<div class="box">
<h1>Have an account</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus dicta cumque harum.</p>
<button>LOGIN</button>
</div>
</div>
CSS
// reset
h1, h2, h3, h4, h5, h6, p, button, div {
margin: 0;
padding: 0;
line-height: 1;
}
// font
#import url('https://fonts.googleapis.com/css?family=Roboto');
// colors
$white: rgba(255,255,255,.8);
:root {
font-size: 16px;
}
body {
background-image: url(
https://picsum.photos/1200/800?);
display: flex;
justify-content: center;
align-items: center;
font-family: 'Roboto', sans-serif;
color: $white;
}
.container {
width: 900px;
margin: 50px auto;
background: rgba(0,0,0,.5);
position: relative;
display: flex;
}
.box {
padding: 5rem 3rem;
float: left;
h1, p {
margin-bottom: 1rem;
}
button {
padding: 1rem 2rem;
background: transparent;
border: 2px solid $white;
border-radius: 6px;
color: $white;
text-align: center;
font-size: 1rem;
cursor: pointer;
text-transform: uppercase;
}
}
.formbox {
width: 500px;
background: white;
margin-left: -50px;
margin-top: -25px;
box-shadow: 0 0 50px black;
display: block;
position: absolute;
}
I have to absolute position the .formbox to not make it "interfere" with the other flex items in the flex container .container. But as a consequence it has no height.
How can I give the Login overlay full height of it's parent?

When position is absolute, top, bottom, right and left properties work relative to the first parent with position: relative.
So, you don't really need the margin-top: -25px or margin-left: -50px, you can just add top: -25px; bottom: -25px; left: -25px to .formbox and you are done. If you need to move it to the right, just add right: XXpx and remove left (or set a positive value like left: 50%).
https://codepen.io/anon/pen/gQKWNq?editors=1100

I could go with adding to formbox class a height of 100% + 50px (margin -25px what you have took x 2). You can do this with calc function: https://developer.mozilla.org/en-US/docs/Web/CSS/calc
.formbox {
width: 500px;
background: white;
margin-left: -50px;
margin-top: -25px;
box-shadow: 0 0 50px black;
display: block;
position: absolute;
height: calc(100% + 50px);
}

Related

Why is my media-query switching my mobile-first design?

I designed a website mobile-first and used a media query to make two columns sit next to each other once the screen expands. Instead it made a grid on my mobile version and I have whitespace on the bottom of my tablet version
If I switch the min to max it just goes to my mobile-first design ignores the media query CSS Grid altogether (as expected) for the tablet and desktop version
#media (min-width: 600px) {
.authentic {
display: grid;
grid-template-columns: repeat(2, 50%);
grid-template-areas: 'bowl content';
height: 100%;
margin: 0px;
}
.right-col {
grid-area: content;
padding: 0 10%;
text-align: left;
align-self: center;
}
}
Here's the HTML if it helps
<section class="authentic">
<div class="right-col">
<h2>Authentic. Awesome.</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque aliquam id ullam vel quia voluptatem nobis nisi error nihil saepe!</p>
</div>
<img src="images/dumpling.jpeg" alt="Dumplings in a bowl" />
</section>
Here is the full HTML and CSS code for full reference:
HTML
<!doctype html>
<!-- Fun project to help me learn responsive layout design -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Yummy Eats</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="hero-bg">
<section class="top">
<header>
yummy.eats
</header>
<h1><span>The Healthy </span> Eating Experience</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia eius impedit porro similique officiis
eligendi, asperiores maxime est praesentium libero.</p>
<div class="form-container">
<form action="">
<div class="form-left">
<label for="city">Enter your city:</label>
<input type="text" name="city" id="city">
<p>Example: "San Diego"</p>
</div>
<input type="button" value="Find Food Now">
</form>
</div>
</section>
</div>
<section class="authentic">
<div class="right-col">
<h2> Authentic. Awesome.</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque aliquam id ullam vel quia voluptatem
nobis nisi error nihil saepe!</p>
</div>
<img src="images/dumpling.jpeg" alt="Dumplings in a bowl" />
</section>
</body>
</html>
CSS
#import url('https://fonts.googleapis.com/css?family=Montserrat:400,700&display=swap');
/*Margin affects outside. Padding affects inside */
/*Custom Properties */
:root {
--leading: 2em;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.hero-bg {
background: #307D99 url('images/bg.jpg');
color: white;
text-align: center;
padding-bottom: 4em;
padding-top: 1em;
/* for margin and padding its good to use em */
}
header {
padding: 1em 5em 3em 10em;
}
.hero-bg a {
color: white;
text-decoration: none;
/* ^^this removes url underlining */
font-weight: bold;
font-size: 1.2rem;
/*for text we use rem */
}
section {
margin: 0 1em;
}
h1 {
font-size: 2.5rem;
margin: 2em 0 1.2em;
}
h1 span {
text-transform: uppercase;
display: block;
/* display:block puts things on their own line, nothing is on the right or left to it */
font-size: 1.4rem;
position: relative;
z-index: 1;
}
h1 span::before {
content: ' ';
position: absolute;
width: 3em;
background: #00BFFF;
height: .4em;
bottom: 0;
z-index: -1;
margin-left: -.3em;
}
.hero-bg p {
font-weight: bold;
margin: 0 1em 3em;
/* font-size: .759rem; */
}
.form-container {
background-color: white;
margin: 2em -1em 0;
padding: 2em;
}
label {
color: #2D7D98;
font-weight: bold;
display: block;
margin-bottom: 1em;
font-size: 1.2em;
}
input[type=text] {
border: 1px solid #707070;
width: 100%;
padding: 1em;
border-radius: .5em;
margin-top: 1.2em;
box-sizing: border-box;
}
.form-container p {
color: gray;
margin-bottom: 1.5em;
font-weight: normal;
font-size: .9em;
margin-top: .3em;
}
input[type=button] {
background-color: #F89104;
border: none;
width: 100%;
color: white;
font-weight: bold;
padding: 1em 0;
border-radius: .5em;
font-size: 1.3em;
cursor: pointer;
}
input[type=button]:hover {
background-color: #bb690a;
}
.authentic {
margin: 0;
}
.right-col {
text-align: center;
margin: 3em 1em;
}
h2 {
text-transform: uppercase;
position: relative;
}
h2::before {
content: ' ';
position: absolute;
width: 8em;
background: #00BFFF;
height: .4em;
bottom: 0;
z-index: -1;
margin-left: -.3em;
}
img {
width: 100%;
/* this is super important if you are using images to get your website to fit the entire page */
}
p {
line-height: var(--leading);
}
/* Media query put in place to change the width for the section (form) when the user enlarges the screen into desktop format */
#media (min-width: 730px) {
section {
margin: 0 4em;
}
.form-container {
margin: 2em 4em 0;
}
}
#media (min-width: 600px) {
.hero-bg {
text-align: left;
}
.hero-bg p {
margin: 0 0 3em;
}
.hero-bg section {
width: 65%;
}
.form-container {
margin: 2em 0 0;
padding: 2em;
border-radius: .5em;
box-shadow: 10px 10px 10px rgba(0, 0, 0, .3);
}
form {
display: flex;
}
.form-left {
width: 70%;
}
label {
margin: 0;
}
input[type="button"] {
height: fit-content;
font-size: 1.1em;
margin-left: 1em;
margin-top: 2.2em;
padding: .7em 0;
width: 30%;
}
/* CSS Grid Usage: put the text and bowl next to each other and then changed the order by putting the bowl on the left
since it came before content in grid-template-areas */
.hero-bg p {
margin-bottom: 0px;
}
.authentic {
display: grid;
grid-template-columns: repeat(2, 50%);
grid-template-areas: "bowl content";
height: 100%;
margin: 0px;
}
.right-col {
grid-area: content;
padding: 0 10%;
text-align: left;
align-self: center;
}
img {
grid-area: bowl;
}
h2 {
margin: 0;
}
}
/* A fadeIn effect for the top section */
.top {
animation: fadeIn 4s forwards;
}
#keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-2em);
}
to {
opacity: 1;
transform: translateY(-2em);
}
#keyframes overlay {
0% {
opacity: 0;
}
30% {
1;
}
70% {
1;
}
100% {
0;
}
}
}
Concerning the height / whitespace at the bottom, you should add
html, body {
height: 100%;
}
Otherwise the .authentic element`s 100% height has no height to refer to.
Concerning the other details, you have to add more details (i.e. code) to your question (i.e. the full relevant CSS and HTML code, not only the media query and part of the HTML)

Center H2 slightly above it's parent div?

I am trying to position a styled heading to be centered and to sit slightly above it's parent div. I assumed the heading would have to be absolutely positioned to be able to be shifted outside of the parent by adding a negative margin to the top. I can center it when it's position is relative, but I can not add a negative margin to bring it outside of the parent.
HTML
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="content">
<h2 class="content-box-title">A Heading</h2>
<div class="content-body">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam, culpa illum! Sint maiores numquam provident, consequatur voluptatibus, repudiandae, dolorum sed, itaque saepe magni fugit mollitia! Repellat dignissimos, quas esse itaque.</p>
</div>
</div>
</div>
</div>
</div>
CSS
.content {
position: relative;
background-color: #d5d5d5;
}
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin: -40px auto;
}
.content-body {
padding: 20px;
}
http://jsfiddle.net/LsohxL3m/
By making the title an inline-block, you can center it using text-align: center on the parent div. You don't need absolute positioning or auto margin then, so you can just use a negative top margin. You can use text-align left on the content to keep that left aligned:
.content {
background-color: #d5d5d5;
text-align: center;
}
.content-box-title {
display: inline-block;
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
}
.content-body {
padding: 20px;
text-align: left;
}
Here's a fiddle.
I adjusted that fiddle to include this css content:
.content h2 {
box-sizing: border-box;
margin: -60px auto 0;
width: 100%;
}
an it seemed to center the h2 and set it just a bit above the content div.
but after deeper investigation, it looks like you just need to as a width to your original tag, and the box-sizing rule to assist with the padding/margin on the element.
I appended this to your existing .content-box-title css and it worked fine:
width: 100%;
box-sizing: border-box;
Check now: http://jsfiddle.net/LsohxL3m/1/
I changed the styling of the title as follows:
.content-box-title {
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
margin-left: auto;
margin-right: auto;
}
You can use translate:
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
left:50%;
transform: translate(-50%, 0);
}
http://jsfiddle.net/LsohxL3m/6/
change
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin: -40px auto;
}
.content-body {
padding: 20px;
}
to
.content-box-title {
transform: translateY(-60px);
background-color: grey;
margin-top: 60px;
text-align: center;
padding: 10px;
}
.content-body {
padding: 0 20px 20px 20px;
}
Here is a sample

Link button full height of container Bootstrap

I'm having issues forcing a link to take full height of its parent container.
Here is a Fiddle of my code: http://jsfiddle.net/z2ua5g4j/
a > span.button-single {
font-size: 30px;
color: #fff;
display: block;
margin-bottom: 8px;
margin-left: 0px;
}
.box {
background-color: #f5f5f5;
border: 1px solid #dcdcdc;
height: 100%;
}
.box h1 {
color: #667477;
font-size: 24px;
margin-left: 20px;
}
.box p {
color: #b1b1b1;
font-size: 13px;
margin-left: 20px;
margin-bottom: 20px;
}
.box a.button {
width: 95px;
background-color: #b4b4b4;
margin-right:-1px;
color: #fff;
padding-top: 13px;
padding-bottom: 13px;
font-size: 15px;
line-height: 16px;
text-align: center;
float: right;
height: 100%;
display: block;
}
.box a.button:hover {
text-decoration: none;
background-color: #a7a7a7;
}
Basically, I want to make the gray button (on the right) take full height of the box container. As of writing, I've tried setting the link to display block and set its height to 100%, but with no avail.
Any ideas?
I changed box to 100px, and setup parent elements of the button to have 100% height
However, you still may want to take a look at http://webdesign.about.com/od/csstutorials/f/set-css-height-100-percent.htm to understand how height:100%; is different from width:100%;. I'm guessing you believe they work the same.
#import url("http://getbootstrap.com/dist/css/bootstrap.min.css");
a > span.button-single {
font-size: 30px;
color: #fff;
display: block;
margin-bottom: 8px;
margin-left: 0px;
}
.box {
background-color: #f5f5f5;
border: 1px solid #dcdcdc;
height: 100px;
}
.box h1 {
color: #667477;
font-size: 24px;
margin-left: 20px;
}
.box p {
color: #b1b1b1;
font-size: 13px;
margin-left: 20px;
margin-bottom: 20px;
}
.box a.button {
width: 95px;
background-color: #b4b4b4;
margin-right:-1px;
color: #fff;
padding-top: 13px;
padding-bottom: 13px;
font-size: 15px;
line-height: 16px;
text-align: center;
float: right;
height: 100%;
display: block;
}
.box a.button:hover {
text-decoration: none;
background-color: #a7a7a7;
}
.row{
height: 100%;
}
<div class="box">
<div class="row">
<div class="col-sm-10">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla bibendum purus ut pretium ultricies. Cras pulvinar tincidunt lorem, ut posuere risus mollis in. Fusce pharetra urna nec sagittis suscipit.</p>
</div>
<div class="col-sm-2" style="height:100%;">
<span class="button-single glyphicon glyphicon-plus-sign"></span> More<br/>Details
</div>
</div>
</div>
Just use the btn-block class instead of messing around with div heights, like this:
<div class="col-md-3"><button class="btn btn-block" >Show Details</button></div>

Vertically center a heading tag and a heading pseudo selector's content

Another vertical alignment question...
Given the following,
HTML:
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris. Vivamus imperdiet congue iaculis.</h2>
</div>
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris.</h2>
</div>
CSS:
.thing{
background: #eee;
margin: 0 auto 10px;
width: 625px
}
h2{
display: inline-block;
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
vertical-align: middle;
}
.thing:before{
background: red;
content: '';
display: inline-block;
height: 40px;
position: absolute;
vertical-align: middle;
width: 40px;
}
What do I need to do in order to have the text and the red box vertically centered? This should work for headings of any height.
Fiddle
You just need to remove the position:Absolute that breaks the inline-block behavior. Instead use some trick to fight the space between inline-block elements and be sure the two elements can be aside on the containers width. Try this:
.thing{
font-size:0; /*ADD*/
}
h2{
/*margin: 0 0 0 40px; REMOVE*/
width:585px; /*ADD*/
}
.thing:before{
/*position: absolute; REMOVE*/
}
Check this Demo Fiddle
I guess your question has two answers.
Here's solution one: (Make the red box equal the height of the .thing content.
.thing{
background: #eee;
margin: 0 auto 10px;
position: relative; // Make .thing:before relate it's position to this
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
}
.thing:before{
background: red;
content: '';
height: 100%; // Height will equal .thing height
position: absolute;
width: 40px;
}
Demo 1
Second solution: (Make text center vertically)
.thing{
background: #eee;
margin: 0 auto 10px;
min-height: 40px;
position: relative;
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.thing:before{
background: red;
content: '';
height: 40px;
left: 0;
position: absolute;
top: 0;
width: 40px;
}
Demo 2

Creating a hover div that matches the height and width of base div

So I have a base div and a hover div (will display:block in js on mouseover).
Anyways I'm having the hardest time making the height and width of my hover div match the height and width of my base div.
I think the problem is because of my responsive code, also in the base div the image is actually over another background image and width and height is set to 95% to create a border effect.
#user-options li img.role-photo {
width: 95%;
height: 95%;
padding-top: 5px;
}
I've uploaded my work to a test link here.
As you can see, over the 2nd "card" I have the hover div showing. The opaque white box with the 'Sign in with Facebook' button is suppose to cover up the image of the singer below. I can't seem to get it working right :(
Here is my jsfiddle
http://jsfiddle.net/leongaban/PWVBp/
HTML from current page:
<ul id="user-options">
<li>
<img class="role-photo" src="../images/registration_fan_tile.png" alt="Fan"/>
<h3>Fan</h3>
<p>Calling all tastemakers! Influence Hollywood, don't just consume it. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</li>
<li style="">
<div class="role-card-talent" style="position: relative;">
<img class="role-photo" src="../images/registration_talent_tile.png" alt="Talent"/>
<!-- Hover Controls -->
<div class="hover_controls" style="display:block;">
<img class="fb-signin" src="../images/registration_signin_fb.png">
<div class="learn_about_pages learnMoreLink" tag="" style="margin:10px">
<img src="../images/registration_info_icon.png">Learn More
</div>
</div><!-- hover_controls -->
</div><!-- role-card-talent -->
<h3>Talent</h3>
<p>Oh, Hai! Now you are on your way to being "connected." You're welcome. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</li>
CSS
#user-options {
margin:0px; padding:0px; float:left; width:100%; list-style-type:none;
}
#user-options li {
position: relative;
float:left;
width:20%;
margin: 1%;
text-align: center;
padding: 5px;
background: url("../../images/one_background.png") no-repeat;
background-size: 100% auto;
overflow: hidden;
}
#user-options li img.role-photo {
width: 95%;
height: 95%;
padding-top: 5px;
}
.hover_controls {
position: absolute;
margin: 0;
top: 5px;
left: -5px;
padding: 5px;
/*padding-top: 20px;*/
width: 100%;
height: auto;
/*min-height: */
text-align: center;
color: #409ad5;
font-size: 0.875em;
font-weight: bold;
background: rgba(255,255,255,.5);
background-size: 100%;
}
.hover_controls a {
color: #666;
font-size: 0.750em;
}
.hover_controls img.fb-signin {
width: 100%;
}
Set your height to 100% on the .hover_controls in your css. see jsfiddle
http://jsfiddle.net/cornelas/PWVBp/3/
.hover_controls {
position: absolute;
margin: 0;
top: 5px;
left: -5px;
padding: 5px;
/*padding-top: 20px;*/
width: 100%;
height: 100%; /** CHANGE TO 100% **/
/*min-height: */
text-align: center;
color: #409ad5;
font-size: 0.875em;
font-weight: bold;
background: rgba(255,255,255,.5);
background-size: 100%;
}
Simply add bottom: 5px; to your .hover_controls class, so it looks like:
.hover_controls {
position: absolute;
margin: 0;
top: 5px;
bottom: 5px;
left: -5px;
padding: 5px;
/*padding-top: 20px;*/
width: 100%;
height: auto;
/*min-height: */
text-align: center;
color: #409ad5;
font-size: 0.875em;
font-weight: bold;
background: rgba(255,255,255,.5);
background-size: 100%;
}
If you set top and bottom or left and right to absolute block inside relative block, it will stretch its height/width in a way that its bottom and top lines or left and right borders will be in x pixels from corresponding sides of parent relative div
Demo: http://jsfiddle.net/PWVBp/5/

Resources