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>
Related
I've got one wrapping div and two divs with different height inside them. How can I make sure that the wrapping div is always at least the height of the bigger div inside it because for some reason I see that when in the .leftMenu div there is a lot of info (both divs are dynamically filled with info) - the wrapping div stays as the height of .centerBody! (I've put on purpose the styles of divs inline so its quicker)
My code:
<div id="importPartUpdate">
<div class="leftMenu" style="position: relative;margin-top: 10px;width: 15%;clear: both;display: inline-lock;float: left;" />
<div class="centerBody" style="margin-top: 10px;margin-left: 0px; display: inline-block; position: reative;width: 83%;padding: 5px;"/>
</div>
Clear your div floats using clear:both CSS property.
<div id="importPartUpdate">
<div class="leftMenu" style="position: relative;margin-top: 10px;width: 15%;clear: both;display: inline-lock;float: left;" />
<div class="centerBody" style="margin-top: 10px;margin-left: 0px; display: inline-block; position: reative;width: 83%;padding: 5px;"/>
<div style="clear:both"></div>
</div>
FIDDLE DEMO
You can use display:flex;. Check this link: http://css-tricks.com/snippets/css/a-guide-to-flexbox/
Here's the working fiddle: http://jsfiddle.net/NhKb6/2/
I'm losing my mind trying to get this simple thing working - I have a div that's 80px high - inside of which I have other divs that should take up the height of the 80px div, and just fill in the space, essentially splitting the parent div up into sections. Right now the inner divs are getting pushed all over the place and I don't know why.
The boxes should all be hugging each other above the black horizontal line.
JSFiddle: http://jsfiddle.net/Mk7L4/
HTML:
<div class="post-panel">
<div class="upvote-area">
<p>20</p>
</div>
<div class="title-area"> <a class="post-title" href="#post.Url"><strong>#post.Title</strong></a>
</div>
<div class="response-count-area"> <span>0 replies</span>
<p>Active: Now</p>
</div>
<div class="view-count-area"> <span>0 views</span>
</div>
</div>
In the css, I have the height of the container element set to 80px, and each of the inner divs set to display: inline-block, and height: 100%.
What am I doing wrong?
Add vertical-align: top;
.post-panel div {
height: 100%;
display: inline-block;
vertical-align: top;
}
;)
How do I form a div to have the same dimensions as those of the content housed by it , and not any larger ?
The div that I currently has a larger width than that of the three divs that i have placed inside it.How can i get it to fit the inner div's size?
Here is the link to what i have now :: http://jsfiddle.net/jairajdesai/Fd5hQ/
HTML :
<div id="initial_options">
<div class="option_button" id="option_1">
<big>1</big> <span class="supplementary_text">text goes here</span>
</div>
<div class="option_button" id="option_2">
<big>2</2></big> <span class="supplementary_text">text goes here</span>
</div>
<div class="option_button" id="option_3">
<big>3</big> <span class="supplementary_text">text goes here</span>
</div>
</div>
CSS :
#initial_options {
position:relative;
top:18%;
left:0;
z-index:10;
background-color:#eee;
}
.option_button{
width:20%;
border-radius:none;
}
#option_1{
background-color:#013adf;
}
#option_2{
background-color:#f7fe2e;
}
#option_3{
background-color:#ff0000;
}
Inline element like <span> wraps around the content and more. Either you can use this or you can set the display property as inline.
div {
display: inline;
}
However setting to display as inline will not give you any option to add top/bottom margin and padding. If you want to add that too, you should use inline-block as the property.
div {
display: inline-block;
}
use display:inline-block; without paddings, margins and floats
I am trying to place 7 divs side by side but with a bit of uniqueness.
You can take a look at what I have done so far through the link HERE and view page source.
I want the Center div's width to fill the space between the Left Middle and Right Middle div irrespective of how far one drags the browser form to the left or right. At the moment the center div has white spaces left and right of it.
Can anyone help me out please?
You can achieve it with <table>. If you are pretending to use div-based structure, then you can simulate divs behaviour by using display:table etc...
here is HTML:
<div style="display:table;width:100%;">
<div style="display:table-row">
<div style="display:table-cell;width:100px;background:blue;">Left Fixed</div>
<div style="display:table-cell;width:auto;background:green;">Left Stretch</div>
<div style="display:table-cell;width:120px;background:yellow;">Left Middle</div>
<div style="display:table-cell;width:auto;background:#999;">Center</div>
<div style="display:table-cell;width:120px;background:yellow;">Right Middle</div>
<div style="display:table-cell;width:auto;background:green;">Right Stretch</div>
<div style="display:table-cell;width:100px;background:blue;">Right Fixed</div>
</div>
</div>
Here is a demo: demo link
Try with display: inline-block and white-space: nowrap.
Demo
Example:
HTML
<div class="parent">
<div class="child">first</div>
<div class="child2">first2</div>
<div class="child3">first3</div>
<div class="child4">first4</div>
<div class="child5">first5</div>
<div class="child6">first6</div>
<div class="child7">first7</div>
</div>
CSS
.parent{
margin:0 auto;
background:red;
font-size:0;
white-space:nowrap;
}
.child, .child1, .child2, .child3, .child4, .child5, .child6, .child7{
display:inline-block;
vertical-align:top;
width:100px;
padding:20px;
font-size:12px;
}
.child{
background:green;
}
.child2{
background:rgba(0,0,0,0.4);
}
.child3{
background:rgba(0,0,0,0.6);
}
.child4{
background:rgba(0,0,0,0.1);
}
.child5{
background:rgba(0,0,0,0.3);
}
.child6{
background:rgba(45,234,0,0.9);
}
.child7{
background:rgba(232,0,222,0.9);
}
LIve demo
Your left div has a width of 45%; your right div similarly. But the middle div has a width of 8%, so there's 2% left over.
If you make the centre div have a width of 10%, the gaps disappear.
<div style="position: relative;">
<div style="margin-left: auto; margin-right: auto; width: 10%; margin-top: 0px; background-color: #999">
Center</div>
</div>
since you had the two divs width's add up to 90% and the center div as 8%, fix this and the center this fills up the center
You can achieve this without any problem using HTML <table>. Or if you want to have it table-less, by using only div-based structure, then you can simulate table's behavior with display as table, table-row, table-cell in your CSS
Here is a Live Demo.
http://twitter.github.com/bootstrap/scaffolding.html
I tried like all combinations:
<div class="row">
<div class="span7 offset5"> box </div>
</div>
or
<div class="container">
<div class="row">
<div class="span7 offset5"> box </div>
</div>
</div>
changed span and offset numbers...
But I cant get a simple box perfectly centered on a page :(
I just want a 6-column-wide box centered...
edit:
did it with
<div class="container">
<div class="row" id="login-container">
<div class="span8 offset2">
box
</div>
</div>
</div>
But the box is too wide, is there any way I can do it with span7 ?
span7 offset2 gives extra padding to the left span7 offset3 extra padding to the right...
Bootstrap's spans are floated to the left. All it takes to center them is override this behavior. I do this by adding this to my stylesheet:
.center {
float: none;
margin-left: auto;
margin-right: auto;
}
If you have this class defined, just add it to the span and you're good to go.
<div class="span7 center"> box </div>
Note that this custom center class must be defined after the bootstrap css. You could use !important but that isn't recommended.
besides shrinking the div itself to the size you want, by reducing span size like so... class="span6 offset3", class="span4 offset4", etc... something as simple as style="text-align: center" on the div could have the effect you're looking for
you can't use span7 with any set offset and get the span centered on the page (Because total spans = 12)
Bootstrap3 has the .center-block class that you can use. It is defined as
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
Documentation here.
If you want to go full-bootstrap (and not the auto left/right way) you need a pattern that will fit within 12 columns e.g. 2 blanks, 8 content, 2 blanks. That's what this setup will do.
It only covers the -md- variants, I tend to snap it to full size for small by adding col-xs-12
<div class="container">
<div class="col-md-8 col-md-offset-2">
box
</div>
</div>
Sounds like you just wanted to center align a single container.
The bootstrap framework might be overcomplicating that one example, you could have just had a standalone div with your own styling, something like:
<div class="login-container">
<!-- Your Login Form -->
</div>
and style:
.login-container {
margin: 0 auto;
width: 400px; /* Whatever exact width you are looking for (not bound by preset bootstrap widths) */
}
That should work fine if you are nested somewhere within a bootstrap .container div.
add the class centercontents
/** Center the contents of the element **/
.centercontents {
text-align: center !important;
}
#ZuhaibAli code kind of work for me but I changed it a little bit:
I created a new class in css
.center {
float: none;
margin-left: auto;
margin-right: auto;
}
then the div become
<div class="center col-md-6"></div>
I added col-md-6 for the width of the div itself which in this situation meant the div is half the size, there are 1 -12 col md in bootstrap.
Follow this guidance https://getbootstrap.com/docs/3.3/css/
Use .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
wrap the div in a parent div with class row then add style margin:0 auto; to the div
<div class="row">
<div style="margin: 0 auto;">center</div>
</div>