I want to create a div that's wider than its parent, and i found many solutions.
Almost all of them say something that looks like this:
position:absolute;
left:0;
right:0;
(for example: How to expand child <div> with 100% of body width?)
This is indeed a solution, but there is only one little problem.
situation:
(jsfiddle)
<style>
.parent
{
width:70%;
padding: 1%;
}
.fullwidth
{
position:absolute;
left:0;
right:0;
}
</style>
<div class="parent">
<div>
<h1>
This is a normal div.
This text is visible
</h1>
</div>
<div class="fullwidth">
<h1>
This is a full width div.
</h1>
</div>
<div class="normal-div">
<h1>
This is a normal div
This text is hiding behind fullwidth div
</h1>
</div>
</div>
In this example, the second normal div is hiding behind the fullwidth div, because the fullwidth is absolute positioned.
So, how can you do this without having the divs hide behind the fullwidth div?
You need to make two changes to the "normal div":
Position relative (the default is static)
Set z-index below that of the absolute positioned div
And one change to the absolute div (set its z-index below the "normal" div).
http://jsfiddle.net/gx4p2red/3/
.fullwidth
{
position:absolute;
left:0;
right:0;
/*Testing only*/
background-color: green;
z-index: 0;
}
/*Testing only*/
.normal-div
{
background-color:red;
position: relative;
z-index: 1;
}
Related
I am trying to fix a div with a 50vw width. However, when I fix the div, 50vw acts as if it's 100vw.
In the example below, to get the effect I want, I have to make the target 25vw instead of 50vw. 100vw is wider than the screen.
Here is the jsfiddle. the blue .target container should be half the width of the yellow container.
<div class="main">
.
<div class="content">
<div class="content-wrapper">
<div class="target-containers">
<div class="target">. </div>
</div>
</div>
</div>
</div>
CSS
html, body {
margin: 0;
width:100%;
height: 100%;
}
body {
background-color:gray;
}
.main {
background-color: yellow;
min-height:100vw;
position:relative;
margin-left: 25%;
margin-right:25%;
}
.content-wrappers {
position:relative;
}
.target-containers {
position:relative;
}
.target {
min-width:50%;
width:50%;
position:fixed;
float:left;
background-color:blue;
}
Please read about position: fixed.
It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none
Who is the initial containing block? (EDIT to clearify this comment)
Please read about identifying the containing block
Note: The containing block in which the root element () resides is a rectangle called the initial containing block. It has the dimensions of the viewport (for continuous media) or the page area (for paged media).
Please read about position: sticky
A stickily positioned box is positioned similarly to a relatively positioned box, but the offset is computed with reference to the nearest ancestor with a scrolling box, or the viewport if no ancestor has a scrolling box.
You could change the position value to sticky like this
.main {
background-color: yellow;
max-height:100vh;
min-height: 100vh;
margin-left: 25%;
margin-right:25%;
overflow-y: auto
}
.target{
min-width:50%;
width:50%;
position:sticky;
background-color:blue;
top: 0;
}
Fiddle Example
If you make an element position:fixed it will break it out of the document flow.
If you remove position: fixed it works as expected as it is inheriting from the parent element.
https://jsfiddle.net/k460abmv/
I have this a parent div which has a fixed position and also a child div which has a fixed position (can be changed) and has text inside. This child text is centered from the parent div.
I want to create a behavior by changing some CSS where when the parent div "height" is decreased, the child div with the text stays the same position and won't show if the parent div doesn't cover it.
The below snippet shows my current layout.
#parent {
position:fixed;
background:red;
width:100vw;
height:100vh;
}
#child {
color:black;
font-family:Arial;
position:fixed;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
<div id="parent">
<div id="child">
<h1>
TEST
</h1>
</div>
</div>
What I wish to happen by changing some CSS is like this:
http://prntscr.com/bjin61
Say when the magic value is around 50%, it should only show the area which is highlight in a yellow border WITH THE TEXT CUTTING OUT (this is important).
You need to explicitely set the child position in absolute units, instead of % of parent. If you set position to fixed, child will become completely separate from parent, so it needs to be absolute. Finally, hide overflowing elements for parent.
#parent {
position:fixed;
background:red;
width:100vw;
height:47vh;
overflow: hidden;
}
#child {
background: yellow;
color:black;
font-family:Arial;
position:absolute;
top: 50vh;
left: 50vw;
transform: translate(-50%,-50%);
}
<div id="parent">
<div id="child">
<h1>
TEST
</h1>
</div>
</div>
I have a "fixed" DIV on the very top of my page:
<div id="banner-wrapper">
<div id="banner"></div>
</div>
with the following CSS:
#banner-wrapper {
width:300px;
height:500px;
}
#banner {
width:300px;
height:500px;
position:fixed;
top:0;
left:0;
background:orange;
}
This "fixed" DIV is followed by a "content-wrapper" DIV:
<div id="content-wrapper">
<div id="content-left">
content left
</div>
<div id="content-right">
content right or sidebar
</div>
</div>
with the following CSS:
#content-wrapper {
width:300px;
background:red;
position:absolute;
top:500px;
bottom:0;
}
#content-left {
width:150px;
float:left;
}
#content-right {
width:150px;
float:right;
}
The issue I'm having is that the "content-wrapper" DIV does not fully cover the "fixed" DIV. The top of the "content-wrapper" covers the "fixed" DIV and the bottom of "content-wrapper" becomes transparent, showing the "fixed" DIV beneath.
I was able to solve the problem by giving the "body" a height in CSS. However, I do not want to give the "body" a height as I do not know the true hight of the content and would like it to remain flexible. I've also have tried inserting
<div style="clear:both;"></div>
before the closing tags but it does not force the "content-wrapper" down.
Here is an example of the issue on JSFiddle.
As you can see, the "red" box does not reach the "blue" box even though it is set to absolute, bottom 0. From what I can tell it reaches the bottom if it does not contain any DIVs inside of it. But once I add the "content-x" DIVs, it no longer reaches the bottom of the page.
Thank you for any help.
You could relatively position the element #content-wrapper rather than absolutely positioning it. Then you can omit the top/bottom positioning and it will behave as expected.
The reason it wasn't working in the first place was because you were giving the absolutely positioned element a height of 100%. Therefore it will have the same height is the window, which is not what you wanted.
Updated Example
Change the following:
#content-wrapper {
width: 300px;
height: 100%;
background: red;
position: absolute;
top: 500px;
bottom: 0;
}
to:
#content-wrapper {
width: 300px;
background: red;
position: relative;
}
I'd prefer to do this using only CSS.
I have a relative element which contains an absolute element. I want the relative element to be sized based on how big the absolute element is, so in other words it should wrap neatly around it. To illustrate, in this fiddle, "footer" is positioned underneath "header-wrapper", but it overlaps its contents because "header-wrapper" is ignoring its absolute contents: http://jsfiddle.net/cxmjdL78/1/
HTML
<div class="wrapper">
<div class="header-wrapper">
<div class="header-1">HEADER HEADER HEADER</div>
<div class="header-2">HEADER HEADER<br>HEADER HEADER</div>
</div>
<div class="footer">this text should go below the header</div>
</div>
CSS
.wrapper {
position:relative;
width:300px;
height:300px;
}
.header-wrapper {
position:relative;
}
.header-1 {
position:absolute;
background:#232323;
width:100%;
height:auto;
opacity:0.4;
}
.header-2 {
position:absolute;
background:#323232;
width:100%;
height:auto;
opacity:0.4;
}
.footer {
position:relative;
background:#26d452;
opacity:0.4;
}
When you use position:absolute on an element, you "take it out of the flow". You can't get a size based on a absolutely positioned element as far as I know. You have to set the height on your header-wrapper, or use javascript to achieve the effect.
Why do you have to use position absolute?
For a CSS only solution, the only thing I can think of is to add a height to your header-wrapper. If the content in your div is static, this will solve your problem, but if it's dynamic, you're going to be forced into some kind of JavaScript solution.
Please look at the following example
http://jsfiddle.net/GANeX/90/
I want to show my green colored div outside the wrapper. I cannot change the positioning of the wrapper div as inner-wrapper also holds some other content which may come out when we change the position:relative to position:static.
How can I do this?
You can wrap the things you don't want to overflow in an element with a class called dont-overflow. Set the width of that class to the current .wrapper width, and then remove the overflow from the parent and add it to that class:
CSS dont-overflow class:
.dont-overflow
{
overflow-x: hidden;
overflow-y: scroll;
width: 200px;
height: 200px;
}
HTML:
<div class='wrapper'>
<div class='inner-wrapper'>
<div class='content-wrapper'></div>
<div class='dont-overflow'>
<div class='content-wrapper'></div>
<div class='content-wrapper'></div>
<div class='content-wrapper'></div>
</div>
</div>
</div>
http://jsfiddle.net/GANeX/97/
I think you need to use z-index.
.inner-wrapper{
position:absolute;
left:10px;
right:10px;
top:5px;
bottom:5px;
z-index: 999;
background:yellow;}