I am dealing with a fixed width parent div that is being used in multiple pages. This div has many(3-5) child div elements inside it. For different pages, the number of child div elements vary, i.e. for page 1, the parent div has 3 child divs, for page 2, the parent div has 5 child divs etc.
How can I set the width of these child elements so that they fully occupy the width of parent div in every page?
P.S. The constraint is to not use SASS/Less and Bootstrap.
Basically you are asking for a table where the width of every cell should be the same in proportion of the fixed width of the parent.
It is possible in CSS if you know the maximum possible number of children (if not you will always use all the space but the width of each cell will change based on the content)
When you know the maximum number if children, you add that property to the class .height: max-width:Npx where N is the width of the parent divided by the number of children
.container {
border:1px solid #000;
border-radius:5px;
width:500px;
display:table;
}
.child{
display:table-cell;
border-right:1px solid #000;
}
.child:last-of-type {
border:none;
}
.height{
min-height:100px;
}
<div class="container height">
<div class="child height">Child 1</div>
<div class="child height">Child 2</div>
<div class="child height">Child 3</div>
<div class="child height">Child 4</div>
<div class="child height">Child 5</div>
</div>
You might want to try flex
.parent {
width: 100%;
background-color: blue;
display: flex;
}
.child {
height: 20px;
background-color: red;
margin: 10px;
display: flex;
width: 100%;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Related
I have a parent div, that at least contains one child. In some cases it will contain 2 children and in the last case it will contain 3 children.
I would like to achieve that in all three cases the children expand to the 100% width.
Case1
Here childrenA should be width:100%
<div class="parent">
<div class="childrenA"></div>
</div>
Case2
Here childrenA should be width:50% and childrenB should be width:50%
<div class="parent">
<div class="childrenA"></div>
<div class="childrenB"></div>
</div>
Case3
Here childrenA should be width:33%, childrenB should be width:33% and childrenC should be width:33%
<div class="parent">
<div class="childrenA"></div>
<div class="childrenB"></div>
<div class="childrenC"></div>
</div>
Yeah width can be set automatically with the help of flex box assuming the height of the child elements is specified;
on Parent div
.parent {
display:flex;
}
assuming the height of children divs to be 100px,we can do
.childrenA{
background-color:violet;
height:100px;
flex:1;
}
.childrenB{
background-color:grey;
height:100px;
flex:1;
}
.childrenC{
background-color:green;
height:100px;
flex:1;
}
flex:1 is a shorthand for
flex-grow : 1; ➜ The div will grow in same proportion as the window-size
flex-shrink : 1; ➜ The div will shrink in same proportion as the window-size
flex-basis : 0; ➜ The div does not have a starting value as such and will
take up screen as per the screen size available for
e.g:- if 3 divs are in the wrapper then each div will take 33%.
.childrenA {
background-color: violet;
height: 100px;
flex: 1;
}
.childrenB {
background-color: grey;
height: 100px;
flex: 1;
}
.childrenC {
background-color: green;
height: 100px;
flex: 1;
}
.parent {
display: flex;
}
<div class="parent">
<div class="childrenA"></div>
</div>
<br>
<div class="parent">
<div class="childrenA"></div>
<div class="childrenB"></div>
</div>
<br>
<div class="parent">
<div class="childrenA"></div>
<div class="childrenB"></div>
<div class="childrenC"></div>
</div>
So there might be an easier way to explain this problem but this is how I know:
This basically is a simple dropdown menu inside a dropdown menu. I know how this dropdown works but the real problem here is width of .
<div id="nav2">
Categories
<div id="dropcontents">
<div id="sub-nav">
Mobile
<div id="sub-dropcontents">
Hardware
Software
</div>
</div>
Windows
News
Articles
</div>
</div>
Now the question is if I give 50% width to "dropcontents" then it takes like the half the whole website width. SO isn't it supposed to take 50% of "nav2" as it is inside that div? And I don't want to use pixel here. And I noted that "sub-dropcontents" take 50% width of "dropcontents" which I assume is correct.
Here's the pictorial representation:
The problem is the position value:
If the parent and the children are not positioned, 50% width for the children means 50% width of the parent
If the children is position:absolute; 50% of width means 50% of the first parent that is positioned; if there is not any parent it'll refer the percentage to the whole document.
To fix that just put position:something; in the div that the percentage must refer to.
For a better explanation see this DEMO.
.parent {
width: 500px;
height: 200px;
background-color: red;
margin-bottom:10px;
}
.child {
width: 50%;
height: 200px;
background-color: blue;
}
.absolute {
position:absolute;
}
.relative {
position:relative;
}
Parent-> not positioned and Child -> not positioned
<div class="parent">
<div class="child">
</div>
</div>
Parent-> not positioned and Child -> absolute
<div class="parent">
<div class="child absolute">
</div>
</div>
Parent-> relative and Child -> absolute
<div class="parent relative">
<div class="child absolute">
</div>
</div>
Parent-> absolute and Child -> absolute
<div class="parent absolute">
<div class="child absolute">
</div>
</div>
it(any element) takes the percentage width of its parent element.
Note nav2 is a block element and it will take out the entire width of of its parent (in this case the body)
See this snippet
#nav2{
border:solid red;
}
#dropcontents{
border:solid;
width:50%;
}
<div id="nav2">
Categories
<div id="dropcontents">
<div id="sub-nav">
Mobile
<div id="sub-dropcontents">
Hardware
Software
</div>
</div>
Windows
News
Articles
</div>
</div>
If you set the width of nav to to 50% of its parent width, you will notice that the dropContents div will adjust to 50% of nav2
See snippet below
#nav2 {
border: solid red;
width: 50%
}
#dropcontents {
border: solid;
width: 50%;
}
<div id="nav2">
Categories
<div id="dropcontents">
<div id="sub-nav">
Mobile
<div id="sub-dropcontents">
Hardware
Software
</div>
</div>
Windows
News
Articles
</div>
</div>
I'm struggling with Bootstrap rows and columns in a SharePoint web site. The problem is that I can't and don't want to change the styling that originates from SharePoint, but still be able to use the Bootstrap grid in a part of the page.
I've tried to illustrate the problem without Bootstrap and SharePoint. Here's the JSFiddle:
https://jsfiddle.net/knLjyhe4/
Below is a complete illustration of my example. The problem is that once I use a row to separate element B from C, D and E, the height of side element A affects the first row's height, which I don't want. I want element C to appear immediately below element B. The second example is how it looks before I add the div.row elements.
Below is the HTML and CSS for the isolated example. I had hoped that I could style the div.main element somehow so that the float of A doesn't affect the float of B-E at all. But I can't figure it out.
Please note that I'm sure there are several solutions if I start to change the HTML and styles (like using position), but I really just want to know if there is a way in CSS where the div.main element gets "its own" floating area, without being affected by the A element's float.
<style>
section {
width: 600px;
margin: auto;
}
.block {
float: left;
margin: 10px;
background-color: #339;
color: #fff;
width: 140px;
padding: 10px;
}
.side {
width: 200px;
height: 100px;
}
.main {
margin-left: 240px;
}
.row:after {
display: table;
content: ' ';
clear: both;
}
</style>
<section>
<div class="side block">This is element A in problematic example. I want element C immediately below element B, regardless of the height of this element</div>
<div class="main">
<div class="row">
<div class="block">This is element B</div>
</div>
<div class="row">
<div class="block">This is element C</div>
<div class="block">This is element D</div>
<div class="block">This is element E</div>
</div>
</div>
</section>
<section>
<div class="side block">This is element A when it works but without rows</div>
<div class="main">
<div class="block">This is element B</div>
<div class="block">This is element C</div>
<div class="block">This is element D</div>
<div class="block">This is element E</div>
<div class="block">This is element F</div>
<div class="block">This is element G</div>
<div class="block">This is element H</div>
<div class="block">This is element I</div>
</div>
</section>
Seems to be working if you change your CSS for .main to this (display: table-row;):
.main {
margin-left: 240px;
display: table-row;
}
Updated JSFiddle here
UPDATE 1
Changed table to table-row since it did not work in IE10.
UPDATE 2
For future reference, the final solution used in SharePoint / O365 looked something like this:
HTML (.container is a bootstrap container)
<div id="DeltaPlaceHolderMain">
<div class="container">
<div class="inner-container">
<!--Your content here-->
</div>
</div>
</div>
CSS
.container .inner-container {
display: inline-block;
width: 100%;
}
The .main needs to be float:left and it needs to have less px to width.
Try defines
.side {width:30%; float:left;}
.main{width:70%; float:left; margin-left:0; }
Don't forget to clean the margin-left of .main
The clear: both property on the row:after pseudoclass is causing your second row to jump down below the left-floated side element.
In bootstrap you should use classname col-md-4 on your side element, classname col-md-8 on your main element, and remove the float: left property from your side element. This will give you 2 columns, one for side which is 4 grids wide and one for main which is 8 grids wide. Your rows should function as you expect once the float is gone.
<style>
section {
width: 600px;
margin: auto;
}
.block {
background-color: #339;
color: #fff;
padding: 10px;
}
</style>
<section class="row">
<div class="block col-md-4">This is element A</div>
<div class="col-md-8">
<div class="row">
<div class="block col-md-6">This is element B</div>
</div>
<div class="row">
<div class="block col-md-6">This is element C</div>
<div class="block col-md-6">This is element D</div>
<div class="block col-md-6">This is element E</div>
</div>
</div>
</section>
In general, with bootstrap you don't want to float things. Also, instead of setting element widths explicitly, it is better to use the .col- classes to fit them into the bootstrap grid system.
I have a div with an specific height. i want this: i put some nested div's inside this div an tell them use specific percentage of height of the parent div. for example: div1=10%, div2=50% and div3=40%. Im talking about height.
Im using bootstrap and i can control location of parts of a row via col-*, But i want this for height of a parent div. How i can achieve this via Bootstrap?
<div id="parent" stele="height:500px;">
<div class="child">text 1</div>
<div class="child">text 2</div>
<div class="child">text 3</div>
</div>
Bootstrap grid system will make it easy for you to make responsive columns, because that is a cumbersome part to handle yourself for differing screen-sizes without horizontal scrolling. For height, you can rely on plain CSS styles, because vertical scrolling is not a problem.
Whatever your use-case be, just remember that percent dimensions are always relative to an element's parent. So if you want to give an element a height of 10% you need to consider the question: 10% of what?.
Following snippet will hopefully make it clear to you.
Snippet:
.parent { height: 120px; border: 1px solid gray; }
.parent div:nth-child(1) { height: 20%; background-color: #f00; }
.parent div:nth-child(2) { height: 50%; background-color: #00f; }
.parent div:nth-child(3) { height: 30%; background-color: #0f0; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="parent col-xs-10 col-xs-offset-1">
<div class="child">text 1</div>
<div class="child">text 2</div>
<div class="child">text 3</div>
</div>
</div>
</div>
I have a parent div of height 100 px but full width (default).
Inside it I have a lot of 20px by 20px divs.
When the child divs are about to reach the bottom, I want the next ones to start wrapping.
That is, they should start displaying alongside to the right.
In other words I don't want scrolling to happen. But wrapping.
<div id="parent" style="height:100px">
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
<div style="height:20px;width:20px"></div>
............
</div>
You can use CSS3 columns - http://jsfiddle.net/spbqh/
#parent {
-webkit-column-count: 3;
-moz-column-count: 3;
column-count: 3;
background: #eee;
}
div > div {
height: 20px;
width: 20px;
background: beige;
}
You could try using CSS3 columns. Unfortunately, they only work on Firefox and Chrome for now, I believe, so it doesn't work in IE.
http://jsfiddle.net/Lf5Ma/
<div id="parent">
<div id="test">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<!-- Snip -->
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
...and the CSS:
#parent{
position:relative;
height:110px;
width:100%;
border:1px solid red;
}
#test{
height:100%;
-webkit-column-width:25px;
-moz-column-width:25px;
column-width:25px;
-webkit-column-gap:0px;
-moz-column-gap:0px;
column-gap:0px;
}
.child{
width:20px;
height:20px;
border:1px solid #000;
}
EDIT:
I found a javascript-based column script that could work. It looks like you just add the javascript after your stylesheets, and you can go ahead and use the new column properties without having to make any modifications. I've verified it works in IE 9, at the very least.