Contain A <div> Within Another <div> - css

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

Related

Bootstrap - maintaining column widths using position:fixed?

I am trying to create a "sticky" navigation on my page where a div gets position:fixed; once the user has scrolled to the element. The functionality is working as expected, but the widths of the two columns that are supposed to stick to the top change once the sticky class is added. I tried adding width:100%; to the CSS of the sticky element, but then the element expands beyond the container.
How can I make sure the column widths stay where they should be when position:fixed; is added?
HMTL:
<div class="container">
<div class="padding"></div>
<div class="anchor"></div>
<div class="row sticky">
<div class="col-sm-6">
Testing
</div>
<div class="col-sm-6">
Testing
</div>
</div>
<div class="padding2"></div>
</div>
CSS:
.padding {
height:250px;
}
.padding2 {
height:1000px;
}
.sticky {
background:#000;
height:50px;
line-height:50px;
color:#fff;
font-weight:bold;
}
.sticky.stick {
position:fixed;
top:0;
z-index:10000;
}
JS:
function stickyDiv() {
var top = $(window).scrollTop();
var divTop = $('.anchor').offset().top;
if (top > divTop) {
$('.sticky').addClass('stick');
} else {
$('.sticky').removeClass('stick');
}
}
$(window).scroll(function(){
stickyDiv();
});
stickyDiv();
JSFiddle
Thanks!
Fixed position is relative to body, so it will count the 100% width from body width. If using javascript is ok, you can set the sticky width by getting the container width. Check the Updated Fiddle
You can add a 100% width wrapper around the container and have that stick instead.
<div class="wrap">
<div class="container sticky">
<div class="row">
...
</div>
</div>
</div>
updated fiddle:
https://jsfiddle.net/mileandra/omeegfc7/
add:
width:inherit;
to your .sticky.stick
JSFiddle
like Nanang Mahdaen El-Agun said, a fixed position relates the width to the body. With width:inherit; it will use the width of the .container class
reference: Set width of a "Position: fixed" div relative to parent div
I was able to get it to work by setting your container to "container-fluid" then adding the width: 100%; to your .stick class.
In Bootstrap 4 it's sufficient to add a <div class="sticky-top">...</div> around your row div: https://getbootstrap.com/docs/4.4/utilities/position/
<div class="sticky-top">
<div class="row">
<div class="col-sm-6">
Testing
</div>
<div class="col-sm-6">
Testing
</div>
</div>
</div>

How to style flex parent to wrap all children

I am working on a grid layout using css flex styling and want a total css solution, if possible, I have the means to fix it with javascript.
When a row exceeds the viewport width, it displays the scrollbar,
but when you scroll, the styling of the row element remains the size of the viewport,
it does not seem to "wrap" all of its children.
see : fiddle
Try scrolling, you will see the yellow row (.sk_row) class does not appear around all its children.
A solution would be fine, but I would like to know why the parent does not visually contain all children. I think I may be missing some key concept about flexboxes...
Duplicate of fiddle code...
<body>
<div id='pg_wrap'>
<div id='frm0'>
<div class='sk_scrl'>
<div class='sk_row'>
<div class='itm_val'>row 1</div>
<div class='itm_val'>1</div>
<div class='itm_val'>2</div>
<div class='itm_val'>3</div>
<div class='itm_val'>4</div>
<div class='itm_val'>5</div>
<div class='itm_val'>6</div>
<div class='itm_val'>7</div>
<div class='itm_val'>8</div>
</div>
<div class='sk_row'>
<div class='itm_val'>row 2</div>
<div class='itm_val'>1</div>
<div class='itm_val'>2</div>
<div class='itm_val'>3</div>
<div class='itm_val'>4</div>
<div class='itm_val'>5</div>
<div class='itm_val'>6</div>
<div class='itm_val'>7</div>
<div class='itm_val'>8</div>
</div>
</div>
</div>
</div>
#frm0{ width:420px;height:200px}
.sk_scrl{ overflow:auto;display:flex;flex-flow:column;align-content:stretch}
.sk_row{
display:flex;
justify-content:flex-start;
align-items:center;
background:#ff0;border:2px #f00 solid;
height:50px}
.itm_val{
display:flex;
border:1px #000 solid;background:#666;
flex:0 0 100px; height:30px; margin:0 5px;
align-items:center;justify-content:center}
Note : this is not the same as question
That op wants to change child behaviour, I want the parent to change.
It's not working the way you want because .sk_row inherits the width, in this case from #frm0:
#frm0 { width: 420px; }
With the class .sk_scrl you can't see it very well, because it's set to:
.sk_scrl { overflow: auto; }
If you use your browsers developer tools (assuming you have any), you'll see that the elements wrapped around your .itm_val divs are all 420 pixel wide. The reason the .itm_val divs are all visible outside of their container, is because they are "overflowing" out of their containing div.
Here's an example for how the width-inheriting-thing works:
<div class="container">
<div class="element"></div>
</div>
If you set the the width of .container to 50%, it will use up half of the available width within the window. If, however, you want .element to take up the full width of the window, you will have to adjust the width like this:
.element {
width: 200%;
}
If it were set to 100%, it would only be as wide as .container.
Here's a fiddle: http://jsfiddle.net/Niffler/n8hmpv13/

how to set child div width 80% to parent div

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.

CSS 100% divs side by side, Horizontal flow

I am trying to do some step by step (4 steps in total) checkout. I thought about having a kind of a horizontal slider setup.
I created a container with a width of 400% containing 4 containers. These containers should be side by side and fill 100% of the Window. This is kind of a horizontal slider but i want to use it as a form.
[[[Container 1][Container 2][Container 3][Container 4]]]
My html:
<div class="container fullWidth">
<div id="checkoutContainer">
<div class="checkout" id="cart">Cart</div>
<div class="checkout" id="contact">Contact</div>
<div class="checkout" id="address">Addresse</div>
<div class="checkout" id="overview">Übersicht</div>
<div class="checkout" id="thankyou">Danke</div>
</div>
</div>
My CSS:
.container.fullWidth {
width : 100%;
overflow-x: hidden;
}
div#checkoutContainer {
background-image: url(../img/background.jpg);
background-repeat : repeat-x;
width : 500%;
height : 657px;
}
div#checkoutContainer div.checkout {
float : left;
width : 900px;
height : 657px;
}
The question is now: Can i somehow make the single divs fill the width of the page without using javascript (I know i can detect the page width and set the width)? Setting div.checkout { width : 100% }does not work as they will stack on each other then.
Check out my approach, divs will fill the container so no need to 'fine tune', plus it is responsive and not dependent on the number of divs
HTML
<div id="checkoutContainer">
<div class="checkout" id="cart">Cart</div>
<div class="checkout" id="contact">Contact</div>
<div class="checkout" id="address">Addresse</div>
<div class="checkout" id="overview">Übersicht</div>
<div class="checkout" id="thankyou">Danke</div>
</div>
CSS
#checkoutContainer {
overflow: hidden;
white-space: nowrap;
width: 100%;
}
.checkout {
display:inline-block;
width : 100%;
height : 200px;
vertical-align: top;
}
See working example
Quick Example:
http://jsfiddle.net/DTTnB/
You can fine-tune this how you want
I took off overflow-x: hidden so you can see that they lie horizontally
.
you were on the right track
Key differences:
I made each of the containers only a proportion of their wrapping container:
.checkout:
width : 20%;
Their wrapping container I made it wide enough to accommodate all containers so that each container would take up at least the page width.
checkoutContainer:
width : 1000%;
you can fine tune this

how to make 2 fixed-width sidebar div and 1 flexible-width main div?

basically my html code looks like:
<div id="leftbar"></div>
<div id="content"></div>
<div id="rightbar"></div>
how do i code it with css so that the all 3 divs will be side by side and leftbar and rightbar have a fixed width while content will be flexible to fill out the webbrowser.
Try this: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
How about this one: http://www.glish.com/css/7.asp
Float the leftbar left, and give content a margin-left value equal to (or greater than) the width of the leftbar. Float the rightbar right, and give content a margin-right value equal to (or greater than) the width of rightbar.
.nav1 { width:200px; float:left; }
.nav2 { width:200px; float:right; }
.content { margin:0 210px; }
.clear { clear:both; }
--
<div id="wrapper">
<div class="nav1">Main Nav Items</div>
<div class="nav2">Other Nav Items</div>
<div class="content">Content goes here</div>
<div class="clear"></div>
</div>
Now css has flexbox model.
You should read the specification to get the flexible layout of webpage
http://www.w3.org/TR/css3-flexbox/

Resources