Positioning child content outside of parent container - html

I'm trying to visually position a DIV (article header) outside of it's parent DIV (article) container. Is this possible?
My mark-up example is available here - http://codepen.io/calebo/pen/CGoyD

Try this:
.main {
background: tomato;
float: left;
width: 600px;
margin-top: 30px; /* above Layout adjustment */
/* removed overflow: hidden; */
/* use always clearfix method instead */
}
.article_header {
background: SteelBlue;
color: #fff;
position: absolute;
bottom: 100%;
margin-bottom: 10px; /* to neutralize padding of parent container */
left: -10px; /* to neutralize padding of parent container */
padding: 10px; /* to neutralize padding of parent container */
right: -330px; /* to neutralize padding of parent container */
}
.main > article{
position: relative;
}
.aside {
background: cyan;
float: right;
width: 300px;
margin-top: 30px; /* above Layout adjustment */
}
Working Codepen
To make adjustments, play with margin property.

You could do it like this:
.inner-wrap {position: relative; overflow: visible;}
.inner-wrap:after {content:""; display:table; clear:both;}
.article_header {position: absolute; width: 940px; left: 0; bottom: 100%;}
I've removed the overflow: hidden and replaced it with a clearfix method.

Some thing like this can be done
<div class="wrapper">
<h2>Desired effect</h2>
<div class="inner-wrap">
<div class="main">
<article>
<header class="article_header">article header - unknown height</header>
<section class="article_body">article body</section>
</article>
</div>
<div class="aside">aside</div>
</div>
</div>
CSS
body {
font-family: helvetica, arial, sans-serif;
}
.wrapper {
margin:0 auto;
width:80%;
}
h2 {
text-align: center;
}
.inner-wrap {
background: none repeat scroll 0 0 #CCCCCC;
margin: 41px auto 0;
position: relative;
width: 940px;
}
.inner-wrap > * {
padding: 10px;
}
.main {
background: tomato;
float: left;
width: 600px;
}
.article_header {
background: none repeat scroll 0 0 #4682B4;
color: #FFFFFF;
left: 0;
padding: 1%;
position: absolute;
top: -39px;
width: 98%;
}
.aside {
background: cyan;
float: right;
width: 300px;
}
jsfiddle : http://jsfiddle.net/w26yw/1/ i.e: http://jsfiddle.net/w26yw/1/show/

Here's the cleanest solution. It is basically the original #Mr_Green answer with a little fix.
.main > article {
position: relative;
}
.article_header {
background: SteelBlue;
color: #fff;
position: absolute;
bottom: 100%;
margin-bottom: 10px; /* to neutralize padding of parent container */
margin-left: -10px; /* to neutralize padding of parent container */
padding: 0px 10px; /* to neutralize padding of parent container */
width: 920px;
}
.inner-wrap {
background: #ccc;
width: 940px;
margin: 0 auto;
/* remove overflow: hidden */
}
Here's a working CodePen

Related

Div isn't stretching to 100% height of parent

I'm designing my CSS layout, but can't get the div to stretch to 100% of the height of the parent.
I have a menu bar that takes up the top 13.714vh of the screen. Then I have a main div that I want to take up the remainder of the screen height which I did with height: 100%. bottom-container takes up the bottom 38.2% of the vertical space available in main, and I want speech-bubble to take up the remaining 61.8% of the vertical space in main.
For some reason though, there's a huge white container in the middle of the screen, and speech-bubble isn't taking up the remaining space because of it. Can anyone help me figure out what's going on?
Is there a problem with my HTML or did I make an error in the CSS?
Here's the code pen:
https://codepen.io/TheNomadicAspie/pen/NWjKwxE
body {
margin: 0;
}
.menu-bar {
height: 13.714vh;
width: 100vw;
background: darkblue;
top: 0%;
}
.main {
background: black;
grid-template-rows: 61.8% 100%;
height: 100%;
width: 100%;
padding-left: 1.5%;
padding-right: 1.5%;
padding-top: 1.5%;
padding-right: 1.5%;
}
.speech-bubble {
grid-row: 1;
position: relative;
background: orange;
height: 97%;
width: 97%;
border-radius: 4em;
}
.speech-bubble:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
border: 4em solid transparent;
border-top-color: white;
border-bottom: 0;
margin-left: -4em;
margin-bottom: -4em;
}
.email-container {
visibility: hidden;
}
.question-text {
visibility: hidden;
}
.bottom-container {
grid-row: 2;
position: fixed;
background: green;
height: 38.2%;
width: 100vw;
bottom: 0%;
left: 0%;
}
<div id="menu_bar" , class="menu-bar"></div>
<div id="main" , class="main">
<div id="speech_bubble" , class="speech-bubble">
<div id="email_container" class="email-container">
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
<button id="submit_email_btn" class="btn">Submit</button>
</div>
<div id="question_text" class="question-text">Question</div>
</div>
</div>
<div id="bottom_container" , class="bottom-container">
</div>
</div>
</div>
Do you want anything like this? screenshot.
If so, making your .menu-bar as position: relative and modifying your .main class styles as follows will work:
.main {
position: absolute;
background: black;
left: 0;
right: 0;
height: 50%;
}
Also, you may add margin: auto in your speech-bubble class to align it to center.
Your main tag is not taking full height as your html and body tags are not taking the full height.
Always remember that block elements can stretch maximum to their's parent's height, hence you need to give html and body tag height of 100%.
I have added the additional css below.
html, body { height: 100%;}
I think you want thing like this
* {
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
height: 100vh;
}
.menu-bar {
height: 13.714vh;
background-color: tomato;
color: #fff
}
.main {
background: black;
padding: 1.5%;
flex: 1
}
.speech-bubble {
background-color: orange;
border-radius: 4em;
height: 95%;
position: relative;
display: flex;
flex-direction: column;
}
.speech-bubble:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
border: 4em solid transparent;
border-top-color: white;
border-bottom: 0;
margin-left: -4em;
margin-bottom: -4em;
}
.email-container {
align-items: center;
justify-content: center;
flex: 1;
display: flex;
}
.question-text {
height: 50px;
position: relative;
text-align: center
}
.bottom-container {
height: 70px;
background-color: lightseagreen;
}

Element width when transform: translate()

I'm absolute positioning a ul list (but could be any element) at the bottom center of a container.
When window or container is resized, the list shrinks as if it was some kind of margin or padding or max-width applied somewhere. See this fiddle.
Desired effect
I need the list to keep an auto width (depending on its content) and only shrinks when it takes more than 100% of the parent's width.
I've noticed this behavior only happens when transform: translateX(-50%).
EDIT
The wrapper div contains other elements. List acts as a navigation menu or toolbox.
HTML
<div id="wrapper">
<p>Whatever other content</p>
<ul id="list">
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
</ul>
</div>
CSS
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
#wrapper {
position: relative;
height: 100%;
width: 100%;
background: #fff;
}
#list {
list-style: none;
position: absolute;
padding: 2px;
bottom: 0;
left: 50%;
transform: translateX(-50%);
font-size: 0;
background: #555;
}
#list li {
display: inline-block;
margin: 1px;
}
#list span {
display: block;
height: 40px;
width: 40px;
background: #222;
}
You can use flexbox to replace the old absolute way of positioning.
#wrapper {
background: #fff;
height: 100%;
width: 100%;
position: relative;
display: flex;
}
#list {
margin: 0;
padding: 0;
list-style: none;
align-self: flex-end;
margin: 0 auto;
border: 1px solid #222;
}
I updated the fiddle http://jsfiddle.net/uLj5raoq/

Make div height extend to another div

here is my code: http://jsfiddle.net/fxWg7/4041/
I want to make the left sidebar extend down to the footer dynamically. The footer is a sticky footer which means it will stay down there no matter how long the main content is.
I want the left sidebar to extend down to the footer no matter the height of the main content.
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 50px;
/* bottom = footer height */
}
.container {
height: auto;
overflow: hidden;
}
.left {
width: 180px;
float: left;
background: #aafed6;
}
.right {
float: none;
/* not needed, just for clarification */
background: #e8f6fe;
/* the next props are meant to keep this block independent from the other floated one */
width: auto;
overflow: hidden;
}
footer {
width: 100%;
background-color: #000;
color: #fff;
padding: 1em;
position: absolute;
left: 0;
bottom: 0;
width: 100%;
}
<div class="container">
<div class="left">
left content fixed width
<br>
<br> I want this to extend to footer!
</div>
<div class="right">
right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
</div>
</div>
<footer>
This is my footer.
</footer>
UPDATED:
The following css code will do it:
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.container {
height: 100%;
display: flex;
flex: 1;
}
.left {
width: 180px;
background: #aafed6;
}
.right {
width: auto;
background: #e8f6fe;
}
footer {
width: 100%;
background-color: #000;
color: #fff;
padding: 1em;
position: absolute;
left: 0;
bottom: 0;
width: 100%;
}
View jsfiddle - http://jsfiddle.net/jalenconner/be71229w/1/
This solution utilizes CSS Flexbox, which you can learn more about here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
html {
position: relative;
height: 100%;
}
body {
height: 100%
}
.container {
/* Full Height - height of footer*/
height: calc(100% - 50px);
display: flex;
flex-direction: row;
}
.left {
width: 20%;
float: left;
background: #aafed6;
height: 100%;
}
.right {
background: #e8f6fe;
width: 80%;
}
footer {
width: 100%;
background-color: #000;
color: #fff;
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 50px;
}
.footer-text {
padding: 1em;
}
Just a little tweak to the body tag fixes it
*,
*:after,
*:before {
-moz-box-sizing:border-box;
box-sizing:border-box;
}
html {
position: relative;
height: 100%;
}
body {
height: 100%;
margin: 0;
}
.container {
height: inherit;
overflow: hidden;
}
.left {
width: 180px;
float: left;
background: #aafed6;
height: calc(100% - 50px);/**deduct heght of footer**/
}
.right {
float: none;
/* not needed, just for clarification */
background: #e8f6fe;
/* the next props are meant to keep this block independent from the other
floated one */
width: auto;
overflow: hidden;
}
footer {
width: 100%;
background-color: #000;
color: #fff;
padding: 1em;
position: absolute;
left: 0;
bottom: 0;
width: 100%;
}
<body>
<div class="container">
<div class="left">
left content fixed width
<br>
<br> I want this to extend to footer!
</div>
<div class="right">
right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
<br>
<br> right content flexible width
</div>
</div>
<footer>
This is my footer.
</footer>
</body>
html {
position: relative;
}
body {
margin-bottom: 40px;
}
html, body {
height: 100%;
}
.container {
overflow: auto;
display: flex;
min-height: 100%;
}
.left {
width: 180px;
background: #aafed6;
}
.right {
background: #e8f6fe;
width: auto;
overflow: hidden;
}
footer {
width: 100%;
background-color: #000;
color: #fff;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height: 40px;
}
Use above CSS code, you will get desired output with fixed footer and content with 100% height.

Footer above bottom of page, instead of being at the bottom of the page content

I am trying to make the footer stay at the bottom of the page, NOT the bottom of the screen (fixed) but at the bottom of the entire page, so you can only see it after scrolling to bottom. However, for some reason it stays above the bottom, and I can't seem to find the reason...
FIDDLE:
https://jsfiddle.net/okfudezn/
Image:
HTML (the div has no wrappers etc):
<div class="footer">
<a>REGISTERED NAMES AND TRADEMARKS ARE THE PROPERTY OF THEIR RESPECTIVE OWNERS - Copyright © 2017 All rights reserved</a>
</div>
CSS:
.footer {
background-color: #4b4c46;
height: 55px;
line-height: 55px;
width: 100%;
text-align: center;
color: #e1dac5;
font-size: 14px;
}
Just change replace you content div height to auto
updated fiddle
.content {
position: relative;
width: 650px;
height: auto;
background-color: #e6e6e6;
border: 1px solid #bcbcbc;
margin: 0 auto;
margin-bottom: 80px;
top: -100px;
}
I would try with:
.footer {
position: absolute;
bottom: 0;
}
Change this css
.content {
background-color: #e6e6e6;
border: 1px solid #bcbcbc;
/*height: 650px;*/ /*Remove this*/
margin: 0 auto 30px;/*Change this*/
overflow: hidden;/*Add this*/
position: relative;
/*top: -100px;*//*Remove this*/
width: 650px;
}
.grid {
width: 600px;
/*height: 1000px;*/ /*Remove this*/
margin: 0 auto;
padding-top: 30px;
}
https://jsfiddle.net/okfudezn/
Here you go!
html, body {
margin:0;
padding:0;
height: 100%;
}
#container {
position: relative;
height: auto;
min-height: calc(100% - 54px);
padding-top: 54px; /* Header & Footer */
}
#header {
position: fixed;
top: 0;
width: 100%;
height: 54px;
background: red;
}
#content {
background: orange;
height: 100%;
}
#footer {
position: absolut;
bottom: 0;
width: 100%;
height: 54px;
background: yellow;
}
.simulateContent {
height: 1000px;
}
<div id="container">
<div id="header">
HEADER
</div>
<div id="content">
CONTENT START
<div class="simulateContent"></div>
CONTENT END
</div>
<div id="footer">
FOOTER
</div>
</div>

100% height in strict mode

I have a site which looks good in quirks mode:
the navbar is always shown
the content has a minimum height
the content is streched when the page is enlarged: content height = document height - navbar height - footer height
Now, I'd like to change to the strict mode by adding <!DOCTYPE html>. I tried a lot of things but I didn't get the same behaviour. The content isn't streched anymore.
I've put the code in this fiddle: http://jsfiddle.net/5e1yvLxj/
In the fiddle the strict mode is activated.
index.html
<html lang="en">
<head>
<link href="main.css" rel="stylesheet"/>
</head>
<body>
<div id="nav">
</div>
<div id="messages">
<div id="chat-box">
</div>
</div>
<div id="footer">
</div>
</body>
main.css
html {
min-height: 100%;
position: relative;
}
body {
background-color: #004269;
margin-top: 112px; /* nav */
margin-bottom: 190px; /* footer */
}
#nav {
height: 100px;
position: absolute;
top: 0;
width: 100%;
background-color: #227733;
}
#messages {
position: relative;
margin: 0;
position: relative;
min-height: 200px;
padding: 10px;
background-color: white;
padding: 15px; }
#chat-box {
position: relative;
min-height: inherit;
padding-top: 0px;
height: 100%;
}
#footer {
padding-top: 20px;
background-color: #227733;
position: absolute;
bottom: 0;
width: 100%;
height: 150px;
}
Just use one of those sticky footer techniques:
document.getElementById("messages").onclick = function() {
var p = document.createElement("p");
p.innerHTML = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
this.appendChild(p);
}
html, body {
margin: 0;
height: 100%;
}
#nav {
height: 100px;
background-color: #227733;
position: relative; /* force higher z-index than next divs */
}
#footer {
height: 150px;
background-color: #227733;
}
#messages {
min-height: 100%;
margin-top: -100px; /* match header height */
margin-bottom: -150px; /* match footer height */
background-color: #EEEEEE;
}
#messages:before {
content: "";
display: block;
height: 100px; /* match header height */
}
#messages:after {
content: "";
display: block;
height: 150px; /* match footer height */
}
<div id="nav">#nav</div>
<div id="messages">#messages (click to add content)</div>
<div id="footer">#footer</div>
You use too much position: relative and absolute. This can be useful in some cases but not here. Try the following for a change.
You can also use the calc to calculate the pixels using the percentage that the static nav and footer heights.
html {
height: 100%;
}
body {
background-color: #004269;
/* nav */
/* footer */
height: 100%;
}
#nav {
height: 100px;
width: 100%;
background-color: #227733;
margin-top:0px;
}
#messages
{
margin: 0;
background-color: white;
padding: 15px;
height: calc(100% - 100px - 150px);
overflow: hidden;
}
#chat-box {
min-height: inherit;
padding-top: 0px;
margin: 0;
background-color: white;
padding: 15px;
}
#footer {
padding-top: 20px;
background-color: #227733;
margin-bottom: 0px;
width: 100%;
height: 150px;
}
Here's a small JavaScript that'll do it without you're having to modify anything else you already have there:
window.onload = function() {
var nav = parseInt(document.defaultView.getComputedStyle(document.getElementById('nav'), null).height),
footer = parseInt(document.defaultView.getComputedStyle(document.getElementById('footer')).height);
document.getElementById('messages').setAttribute('style', 'height: ' + (window.innerHeight - nav - footer) + 'px;');
}
JSFiddle: http://jsfiddle.net/7kobcy8o/
I verified it working in IE11 and Chrome 39.