i want to set child div width to be 80% of parent div. how to do
<div id='container'>
<div id="toolbar" class="ui-widget-header ui-corner-all" style="padding:3px; vertical-align: middle; white-space:nowrap; overflow: hidden;">
<button id="BtnPreviousMonth">Previous Month</button>
<button id="BtnNextMonth">Next Month</button>
</div>
<div id='subcontainer'>
<div id="mycal" style="position:absolute;"></div>
</div>
</div>
when i set mycal width and height in %. it has no effect. any suggestions ?
Set width:80% , you can set width in percentage(%) , The percentage is calculated with respect to the width of the generated box's containing block.
HTML:
<div id="container">
<div id="child">
</div>
</div>
CSS:
#container{
position:relative;
width:500px;
height:100px;
}
#child{
width:80%;
}
Fiddle Demo
You must have to provide with to parent div then after you can give width in % to child div.
In your snippet mycal is absolutely positioned. That means that its width and height in % will be relative to the nearest non-static positioned parent (position: relative, absolute, fixed) or to the body in case there is no such. If you want to give mycal 80% width of subcontainer you can make
#subcontainer {
position:relative;
}
but, since subcontainer does not have a defined height and it's child is absolutely positioned (and therefore doesn't affect parent's height) - subcontainer's height is 0. Therefore 20% of 0 is 0. To solve this issue, you can either specify height of subcontainer or populate it with static positioned content.
Here is an example: http://jsbin.com/iYOQAKiS/1/edit
<div id='container'>
<div id="toolbar" class="ui-widget-header ui-corner-all" style="padding:3px; vertical-align: middle; white-space:nowrap; overflow: hidden;">
<button id="BtnPreviousMonth">Previous Month</button>
<button id="BtnNextMonth">Next Month</button>
</div>
<div id="subcontainer" style="position:relative;">Some static content
<div id="mycal" style="position:absolute;width:80%;height:20%">
MyCal content
</div>
</div>
</div>
Since you want to set width 80% to the child mycal, set width to its parent subcontainer
#subcontainer{
width:1000px;
heigth:1000px
}
#mycal{
width:80%;
height:20%;
}
When you set a percentage width and height, it has an effect.
I guess, the problem in your case is, that it is just not visible, set some background-color to see it
#mycal {
width: 80%;
height: 50%;
background-color: plum;
}
See JSFiddle
Although, this might not be what you want. Since you've set position: absolute, the containing box is not subcontainer, but the body element.
Related
I have this following code:
<div class="parent">
<ul class="menu">
<li>this</li>
<li>width</li>
<li>is</li>
<li>dynamic.</li>
</ul>
<div class="something">
<span>so is this</span>
<table>because of this table.</table>
</div>
<div class="fill">
<span>and so is this. but this guy needs to fill the remaining width.</span>
</div>
</div>
Image
These 3 items - ul and 2 divs - are aligned side by side, and as you can see, they have dynamic widths. I have to make these 3 items fit inside div.parent, which width is fixed at 1200px.
Currently, I'm using 'float: left;' to align these 3 items side-by-side, and I can use 'display: inline-block;' if necessary [works perfectly]. But I've tried to use some tricks with 'display: table;' for the parent and 'display: table-cell;' for these 3 items, without success.
I need to fill this remaining space on the black div, which is the 'div.fill'. Any ideas?
EDIT2: http://jsfiddle.net/cAs9t/
Demo
Just add
div.fill { float: none; overflow: hidden; }
Where float: none removes the floating (you can also avoid applying float: left to .fill), and overflow: hidden (or anything different than visible) prevent floating elements from overlapping .fill.
Other ways:
You could use display: table-cell and display: table, but you couldn't specify which element should grow to fill all remaining space.
But if you want full control and want to distribute remaining spaces in a more complex way, you should use Flexboxes.
I made the parent display:table and the 3 children as display:table-cell. Now all 3 children fill the width of the parent and you don't need to float any of them. One advantage of this method is that you can utilize vertical-align and also avoid wrapping of blocks when the parent is shorter than the content. In a way, you get all the goodness of a table.
Υou can set the width of the first 2 children and leave the last without specifying width so that it will fill the parent container.
See this demo.
Use display:table for the container and display:table-row for its direct children.
Set height:0 for the divs with variable height and height:auto for the div that should fill the remaining space.
If the container needs a fixed height, whether in pixels or %, you can add a single div with {overflow-y:auto; height:100%} to the horizontal filler and add content within there.
<div style="height:300px; border:1px solid red;padding:2px;">
<div style="display:table; height:100%; width:100%;border: 1px solid blue;">
<div style="display: table-row; height:0; padding:2px; background-color:yellow;">
I take as much space as needed
</div>
<div style="display: table-row; height:auto; padding:2px; background-color:green;">
<div style="height:100%; overflow: auto;">
<div style="height: 500px">My parent will fill the remaining space</div>
</div>
</div>
<div style="display: table-row; height:0; padding:2px; background-color:yellow;">
<p>I take as much space as needed as well</p>
</div>
</div>
</div>
Flexbox solution
Note: Add flex vendor prefixes if required by your supported browsers.
.parent {
width: 1200px;
display: flex;
}
.menu {
margin: 0;
}
.menu li {
display: inline-block;
}
.fill {
flex-grow: 1;
}
<div class="parent">
<ul class="menu" style="background: #6cf">
<li>this</li>
<li>width</li>
<li>is</li>
<li>dynamic.</li>
</ul>
<div class="something" style="background: #fd6">
<span>so is this</span>
<table>because of this table.</table>
</div>
<div class="fill" style="background: #5c5">
<span>and so is this. but this guy needs to fill the remaining width.</span>
</div>
</div>
I am trying to contain a div's borders within its parent div, and I would like the overflow text from the child div to automatically put a scroll-bar on the child div. I have tried everything that I can think of, but I do not know of a way to do that which I am trying to do. Could someone please offer me some advice on how to do this as efficiently as possible?
My parent div has a percentage-defined height though
This should not be a problem, as long as parents has an height that has a valid value.You can set a height or a max-height width a percentage value.
max-height, will let it grow untill it matches the max value.
http://jsfiddle.net/E2Mfa/
For instance this style sheet:
html, body, .childContainer1 {
height:100%;
background:#edf;
}
body, div, p {
margin:0;
}
.parentContainer {
height:25%;
background:#fed;
}
.childContainer1 {
overflow:auto;
}
.childContainer2 {
max-height:100%;
background:#def;
overflow:auto;
}
If you remove height from html or body, it doesnt work anymore.
When you give percentage height, it calculates it from its parent height.
If no height found in CSS parent, then there is no value to calculate from.
max-height returns no values avalaible to calculate a percentage height for the childs
The structure used here :
<div class="parentContainer">
<div class="childContainer1">
...
</div>
</div>
<div class="parentContainer">
<div class="childContainer2">
...
</div>
</div>
<div class="parentContainer">
<div class="childContainer1">
...
</div>
</div>
<div class="parentContainer">
<div class="childContainer2">
...
</div>
</div>
Does your parent div have an absolute size? If it does you could so something like this:
<div style="width:100px;height:100px;">
<div style="position:absolute;overflow:auto;border:solid black 1px;">My Content</div>
</div>
Check this (not sure wether you want something like this),
<div class="outer-div">
<div class="inner-div">Test content Test content Test content Test content Test content Test content Test content Test content Test content Test content</div>
</div>
.outer-div {
width :200px;
height :100px;
border: 1px solid gray;
}
.inner-div {
width :50%;
height :75px;
border: 1px solid black;
overflow: auto;
}
Demo Fiddle
I was asked a question in an interview that "what is the difference between the css height:100% and height:auto?"
Can any one explain?
height: 100% gives the element 100% height of its parent container.
height: auto means the element height will depend upon the height of its children.
Consider these examples:
height: 100%
<div style="height: 50px">
<div id="innerDiv" style="height: 100%">
</div>
</div>
#innerDiv is going to have height: 50px
height: auto
<div style="height: 50px">
<div id="innerDiv" style="height: auto">
<div id="evenInner" style="height: 10px">
</div>
</div>
</div>
#innerDiv is going to have height: 10px
A height of 100% for is, presumably, the height of your browser's inner window, because that is the height of its parent, the page. An auto height will be the minimum height of necessary to contain .
height:100% works if the parent container has a specified height property else, it won't work
The default is height: auto in browser, but height: X% Defines the height in percentage of the containing block.
I have following kind of pattern. How to apply a css changes for first and second childDiv class to 50% to the parent div
How do I set 50%, 50% to the child div?
<div class="parentDiv">
<div class="childDiv"> // 50% width
</div>
<div class="childDiv"> // 50% width
</div>
</div>
.childDiv{
display:inline-block;
width:50%;
}
Example
Important notes:
don't leave whitespaces between the divs
You might as well use floats instead of display:inline-block;
If the elements don't align in the example, you browser does not support box-sizing, just omit the border then (it was for illustration purposes only).
There's a bit of a trick here, of which you need to be aware. If you put any whitespace between the closing of the first div and the opening of the second, your 50% won't work because of the space being displayed in the browser.
There are a couple ways to do this. If you are targetting only modern browsers (IE9+, FF, Chrome, Safari), you can use inline-block:
<style>
.childDiv {
display: inline-block;
width: 50%;
}
</style>
<div class="parentDiv">
<div class="childDiv"> // 50% width
</div><div class="childDiv"> // 50% width
</div>
</div>
However, IE7 doesn't support inline-block, so you can go to the "old-school" method, using floats:
<style>
.childDiv {
float: left;
width: 50%;
}
</style>
<div class="parentDiv">
<div class="childDiv"> // 50% width
</div><div class="childDiv"> // 50% width
</div>
<div style="clear: both"></div>
</div>
If you want to ensure both columns are exactly the same width and still have a small gap between them, use different styles of floats. Note this method doesn't require that you eliminate whitespace in your markup between divs, as long as the width you use is less than 50%:
<style>
.childDiv {
width: 49.5%;
}
.left { float: left; }
.right{ float: right; }
</style>
<div class="parentDiv">
<div class="childDiv left"> // 49.5% width
</div>
<div class="childDiv right"> // 49.5% width
</div>
<div style="clear: both"></div>
</div>
set parent width to something first.
.parentDiv
{
width: //insert width of the parentDIV
}
And then afterwards set the childDiv width.
Is it possible to have a fixed width div floated alongside a variable width div?
I would like to have a 80px image sit alongside a div that stretches to 100% of the remining width using dynamic content - the html would be a repeated version of below -
for example -
<div class="mediaImg">
<img src="#"/>
</div>
<div class="textArea">
blah blah blah
</div>
Thanks
Paul
you have to write like
#divleft
{
background-color:red;
width:80px;
height:80px;
float:left;
}
#divright
{
background-color:blue;
overflow:hidden;
height:80px;
}
In this example of you give padding & margin to #divright. there is not effect in the layout structure.
check this http://jsfiddle.net/sandeep/u2sQD/1/
Just apply float and an width on your fixed div:
div.fixed{
float: left;
width: 70px;
padding: 0 5px;
}
Live example: http://jsfiddle.net/nvgBm/
Yes. I would put them inside a container type div with position:absolute then the two div's having relative positioning with float left.
<div class='container' width='100%'>
<div class='imgDiv'>
<img src='...'></img>
</div>
<div class='content'>
...content goes here
</div>
</div>
Then the css...
.container{
position:absolute
width:100%
height:100%
}
.imgDiv{
position:relative
float:left
width:80px
}
.content{
position:relative
float:left
}