I'm trying to make a box width 100% when its position absolute ?
This below image is what I am trying to make
https://i.imgur.com/qMaT361.gif
<div class="box1">
<div class="box2">
float
</div>
</div>
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:absolute; top:10px; right:0; left:0; width:100%; height:50px; background:red; }
Demo : https://jsfiddle.net/otkg9nfh/8/
Please help~
When a div is set to absolute position the div takes the boundry set by the parent div (relative position). If you wan to make the div 100% of the browser screen you will have to make the div position fixed insted of absolute
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:fixed; top:10px; left:0; right:0; width:100%; height:50px; background:red; }
<div class="box1">
<div class="box2">
float
</div>
</div>
.
you can use viewport width width:100vw
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:relative; width: 100vw; height:50px; background:red; }
<div class="box1">
<div class="box2">
float
</div>
</div>
.box{position: relative;}
.box1 { width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:absolute; top:10px; left:35.55%; right:0; width:100%; height:50px; background:red;}
<div class="box">
<div class="box1">
<div class="box2">
float
</div>
</div>
</div>
If you want div to start from left with some space using absolute then do like this.
You can do it 2 way
One is by using position: fixed. But you will lost the relative positioning based it's parent. Because when you apply fixed it goes out regular document flow and it consider the body it's parent.
Another way by using calc() if you want to keep the position; absolute.
.box2 { position:absolute; top:10px; right:0; left:calc(250px - 50vw); width:100vw; height:50px; background:red;}
Here left:calc(250px - 50vw); used to make center the element. 250px half size of parent container and 50vw is ensure half size of window vertical width.
It's still not clear from your question what you want the element to be "100%" of, but assuming it's the parent of the parent (the document body in this case), you can simply remove position:relative; from .box1.
https://developer.mozilla.org/en-US/docs/Web/CSS/position#Types_of_positioning
An absolutely positioned element is an element whose computed position value is absolute or fixed. The top, right, bottom, and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor to which the element is relatively positioned.) If the element has margins, they are added to the offset.
.box1 {
width: 500px;
height: 100px;
margin: 0 auto;
overflow: visible;
background: #f1f1f1;
}
.box2 {
position: absolute;
top: 10px;
left: 0;
right: 0;
width: 100%;
height: 50px;
background: red;
}
<div class="box1">
<div class="box2">
float
</div>
</div>
As Shahil M suggested, you can also remove box2 from inside of box1. Ultimately you need to either change your markup or you css. As is often the case when things don't behave, you might want to re-examine why you've structured things like this to begin. Typically when you apply absolute positioning you are expecting the parent to act as the containing block.
Related
Trying to add a tooltip to a div with position:absolute and overflow hidden.
Plunker: http://plnkr.co/edit/qH5nHheCoyOmvRKLVx2o?p=preview
<div class="container">
<div class="inner">
Why am I not overlapping?
</div>
</div>
.container{
height:300px;
width:300px;
position: absolute;
overflow: hidden;
z-index: 1;
background-color:black;
color: blue;
}
.inner{
height:500px;
width:500px;
top:100px;
left:50px;
background-color:yellow;
position:absolute;
text-align: right;
z-index:10;
}
I had a similar problem in the past and couldn't find a solution except maybe for hacks. I ended with adding the inner div to an element higher in the DOM tree and positioning using Javascript.
Here is the thing, there is a div .page which is absolutely positioned at tha page. Inside, there is a .container div and within the container there are the .contents.
The .page has certain heigth, so, contents would be scrolled inside the .page. In this situation I want a .stuck div to stick to the top of the .page. ( I am sure I made grammatical mistakes above!)
Anyway, the fiddel:
http://jsfiddle.net/YBAvb/
update: I want the .stuck to be fixed at the top of .page regardless of the scroll on the .page.
this is the layout:
<div class="page">
<div class="container">
<div class="content"></div>
<div class="stuck"></div>
</div>
</div>
and my (not working) css:
.page{
position: absolute;
top:50px;
left:200px;
width:200px;
height:300px;
overflow:auto;
}
.container{
position:relative;
}
.stuck{
position: absolute;
top:0;
right:0;
left:0;
height:50px;
background:blue;
}
.content{
height:700px;
background:gray;
}
I want the blue .stuck div always be there at the top of the .page div. Any help?
update:
note: a quick trick might be to make the .stuck be positions:fixed and the same position and width of the .page, but that is not my answere since the coordinates of the .page might change with JS any time.
You can add .page and .stuck to a common parent element and overlay the one over the other.
http://jsfiddle.net/YBAvb/1/
.page_holder{
position: absolute;
top:50px;
left:200px;
width:200px;
height:300px;
}
.page {
position: absolute;
top:0;
left:0;
width:200px;
height:300px;
overflow:auto;
z-index: 1;
}
.container {
position:relative;
}
.stuck {
position: absolute;
top:0;
right:0;
left:0;
height:50px;
background:blue;
margin-right: 18px;
z-index: 2;
}
.content {
height:700px;
background:gray;
}
<div class="page_holder">
<div class="stuck"></div>
<div class="page">
<div class="container">
<div class="content"></div>
</div>
</div>
</div>
since the coordinates of the .page might change with JS any time.
...so you already use JS! :)
Using this exact HTML markup is not possible
For such cases we always have Javascript and jQuery: JSBIN DEMO
$('.stuck').each(function(){
var $that = $(this),
$page = $that.closest('.page');
$page.scroll(function(){
$that.css({top: this.scrollTop});
});
});
I have layout comprising of a 100% width header, 2 column content divs (30-70% width) and a 70% width footer (visible only in the bottom of right div).
My HTML mark up is like:
<section id="mySection" >
<header id="headerTop">
</header>
<div id="wrapperLeft">
</div>
<div id="wrapperRight">
</div>
<footer id="footerRight">
</footer>
</section>
My CSS is
#mySection
{
margin:0 auto;
padding:0;
text-align:center;
vertical-align:middle;
overflow:hidden;
}
#headerTop
{
position:absolute;
top:0;
left:0;
height:40px;
width:100%;
overflow:hidden;
}
#wrapperLeft
{
position:absolute;
top:40px;
left:0;
width:30%;
bottom:0;
overflow:auto;
}
#wrapperRight
{
position:absolute;
top:40px;
left:30%;
width:70%
bottom:30px;
overflow:auto;
}
#footerRight
{
position:absolute;
left:30%;
bottom:0;
width:70%;
overflow:hidden;
}
I would like to know if I can design this better such that if i hide the left or right div, the other div is displayed at 100%. I think i can change the CSS dynamically via javascript and adjust the left and width values for the other div, but it is getting messy and would like to avoid it if possible.
Ideally would love to call show or hide on the div and the other div automatically adjusts itself to 100% width.
I have no control over the height of the content in either div and would want the browser to display scrollbar when the content height exceeds the window.
Thanks in advance for your help.
I would add a wrapper to the divs so you can float then instead of positioning then absolutely. This way you can make at least one div 100% wide. For instance the right div. If you want both divs to be dynamic in size you will have to use jquery. For instance adding classes if you want to keep the jquery to a minimal.
example HTML:
<div id="header"></div>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="footer"></div>
example CSS :
#main{
position:relative;
overflo:hidden // This will make the container grow with the children
width:960px;
}
#left{
width:200px;
float:left;
height:100%;
}
#right{
float:left;
width:100%;
height:100%;
}
Example of CSS with additional classto toggle divs
#main.only-left #left{
width:100%;
}
#main.only-left #right{display:none;}
I think I know what you're talking about. I've created a little example here. Basically set 30% on the sidecolumn, and display: block; on the main column. Click on the body anywhere to toggle the side column to show how the main column adapts... is this going in the right direction?
Codepen sketch
HTML
<div class='wrapper'>
<header>Header</header>
<section>
<aside>Sidebar</aside>
<article>Main article</article>
</section>
<footer>Footer</footer>
</div>
CSS
body {
margin: 0;
padding: 0;
}
section {
overflow: hidden;
position: relative;
}
header {
background: crimson;
height: 100px;
width: 100%;
}
aside {
background: #efefef;
float: left;
height: 300px;
width: 30%;
}
aside.hide { display: none; } /** For demo purposes **/
article {
background: #ccc;
display: block;
height: 300px;
}
footer {
background: crimson;
float: right;
height: 100px;
width: 70%;
}
jQuery (just for hideToggle example)
$('html').on('click', function(){
$('aside').toggleClass('hide');
});
UPDATE: Here's an example with a little assitance from jQuery for class toggling. Could probably be generalized more... http://codepen.io/kunalbhat/pen/kuAcg
I read through the other 100% CSS solutions but most of them just don't work for my project unfortunately (such as using position:fixed, etc).
Basically I have a logo and a content window. The logo is 100px in height and 100% in width, and I want the content window to be covering the whole remaining area. I also want it to expand automatically, if the content window exceeds the windows height (e.g. using the min-height property).
Can you point me towards the right way to do this?
My problem is that the content window height always seems to be referencing the original size and not the size of the parent container, hence there will be scrollbars, even when the content is short.
Below my (shortened) code:
CSS:
html, body
{
height: 100%;
min-height: 100%;
margin: 0;
padding: 0;
background-color:#000000;
}
#wrapper
{
height: 100%;
min-height: 100%;
}
#header
{
height:100px;
}
#content {
background-color:#eee;
min-height:100%;
}
HTML:
<body>
<div id="wrapper">
<div id="header"></div>
<div id="content">
content..
</div>
</div>
Thanks!
HTML:
<div class="wrapper">
<div class="header"></div>
<div class="content"></div>
</div>
CSS:
.wrapper{
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.header{
background:red;
height:100px;
}
.content{
background:blue;
position:absolute;
top:100px;
left:0;
right:0;
bottom:0;
}
fiddle: http://jsfiddle.net/rVRFF/
note: .header height and .content top values must match.
I'm trying to design a 2 column layout using divs. My left column size is fixed to 150 px. But right column is not fixed size, so I want it to extend to right boundary of the browser. I used width:auto and width:100% values in right column but they didn't work.
CSS :
html {
height:100%; width:100%
}
body {
color: #000040;
text-align: left;
padding:0;
margin:0;
height:100%;
width:100%
}
#header {
position:relative;
float:left;
background-color: #000053;
width: 100%;
height: 76px
}
#wrapper {
position:relative;
overflow:hidden;
width:100%;
height: 100%;
margin:0px auto;
padding:0;
background-color:Aqua
}
#container {
clear:left;
float:left;
overflow:hidden;
width:100%;
height:100%
}
#left_column {
position: relative;
float: left;
background-color:Fuchsia;
width: 150px;
overflow:hidden;
height:100%
}
#right_column {
position: relative;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:auto }
HTML
<html>
<body>
<div id="wrapper">
<div id="header">
HEADER
</div>
<div id="container">
<div id="left_column">
LEFT COLUMN
</div>
<div id="right_column">
RIGHT COLUMN
</div>
</div>
</div>
</body>
</html>
I would remove all position statements and only put a float:left on the left column, not the right nor the container. Give the right column a margin-left:150px and it should work fine.
Except for the left-floated column, you can also remove the width:100% statements from the rest; when they're not floated, they'll be 100% wide automatically.
The overflow:hidden is only needed on the wrapper; at least if you are using it to have the div grow in height to accommodate the floats inside it.
change for the div right_column the position from relative to fixed, and width from auto to 100%. Also add left:150px;
With these changes you css for right_column will look like the following:
#right_column {
position: fixed;
left:150px;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:100%; }
you can check it here http://jsbin.com/ejetu3