I'm trying to make a table with the display:table-cell and display:table css properties. It looks like this:
My problem is that the orange table cell needs to be just as big as its inner content. Sometimes the green boxes are 100px and sometimes they are 200px. The .left should auto-grow. I would like a CSS solution for this problem. Is that possible?
It should look like this:
Here's the JSFiddle. And here's the code:
Html:
<div class="wrapper">
<div class="left">
<div class="tree">A
<br/>- B
<br/>- C
<br/>
</div>
<div class="filter">F
<br/>F2
<br/>
</div>
</div>
<div class="content">A A A
<br/>B B B
<br/>C C C
<br/>
</div>
<div class="right">S
<br/>S2
<br/>
</div>
</div>
CSS:
div {
margin:2px;
padding:2px;
}
.wrapper {
width:700px;
border:solid 1px red;
display:table;
table-layout:fixed;
}
.right {
display:table-cell;
border:solid 1px blue;
width:100px;
}
.left {
display:table-cell;
border:dashed 1px orange;
}
.content {
display:table-cell;
}
.tree, .filter {
display:block;
width:100px;
border:solid 1px green;
}
The .left doesn't play nice :(
Edit2
Place width: 100px on your left class and remove table-layout:fixed; from wrapper class
FIDDLE
EDIT
In your markup remove whitespaces in .left class when it is empty like so:
<div class="left"></div> or also like:
<div class="left"><!-- some comment is also ok--></div>
This is necessary because otherwise the :empty selector won't consider this element empty.
In css add this:
.left:empty
{
display:none;
}
new FIDDLE
How about wrapping the content of your .left class within another div - like so:
<div class="left">
<div class="inner"></div>
</div>
Then, if you like, you could remove the border if .inner has no content - like so:
.inner:empty
{
border:none;
}
FIDDLE
You can change the div containing the left class in your HTML to a span
<span class="left">
<!-- no content, should not be displayed !-->
</span>
Then change your CSS changing your display from table-cell to inline-block
.left {
display:inline-block;
border:dashed 1px orange;
}
You could also try this...
FIDDLE Again
Related
I'm trying to place 2 divs side by side inside of another div, so that I can have 2 columns of text and the outer div drawing a border around both of them:
HTML
<div id="outer">
<div id="left">
...
<div id="right">
</div>
CSS
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
However, the outer div registers a height of 0px and so the border doesn't go around the other divs. How do I make the outer div recognize the heights of the things inside it?
It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element.
You can use the overflow style to make the parent element take the floating elements in consideration:
#outer { overflow: auto; }
There are a couple of solutions to this issue:
#outer: overflow: hidden;
or add some non-displaying content to the outer div that comes after the floated divs that you then add a clear: both style rule to.
You can also add, through css, the :after pseudo-element to insert content after those divs that you then apply clear: both to - this has the advantage of not requiring extra markup.
My preference is the first one.
Try this:
<div id="outer">
<div id="left">
...
<div id="right">
<div style="clear:both"></div>
</div>
add overflow: hidden; to the main div.
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
overflow: hidden;
border: 1px solid green;
}
#left{
float:left;
border: 1px solid red;
}
#right{
width:500px;
float:right;
border: 1px solid yellow;
}
</style>
You could clear the float by inserting an element after the floated elements that has a clear property applied to it because floated child elements cause the parent to have 0 height since they don't take the height of the floated children into consideration.
<div id="outer">
<div id="left">
...
<div id="right">
<div class="clear"></div>
</div>
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
.clear{ clear: both; }
You must also float the outer div.
Div's that contain floatet divs and that are not floated themselves collapse.
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
float:left;
}
#left{
float:left;
width:300px;
}
#right{
width:500px;
float:right;
}
How bout like this:
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
border:thin solid #000000;
height:300px;
margin:5px;
padding:10px;
}
#left{
float:left;
border:thin dashed #000000;
width:385px;
height:100px;
margin:5px;
}
#right{
width:385px;
float:left;
border:thin dashed #000000;
height:100px;
margin:5px;
}
</style>
<div id="outer">
<div id="left">
</div>
...
<div id="right">
</div>
</div>
if div inside a parent is floated it is no longer part of parent div:check it by inspecting parent element.no to fix your problem there are two methods:
1)make a empty div at end inside parent class it as .blank all following css
.blank:after{
content: "";
clear:both;
display:block;
}
Or
2) give parent a class .clear-fix and add css
.clearfix:after {
content: "";
clear: both;
display: block;
}
it will give parent a height equal to contents
I'm making a form with two input area, the second area should be content editable div somehow.
I want to make the #topic_title_input sitting above the #topic_content_input with the same width. It could be easily achieved by giving them display:inline-block, But for some complicated reason, I can't change the display-inline property of the second input area
Any idea how to make the #topic_title_input sitting above the #topic_content_input?
html
<div class="left_container">
<input id="topic_title_input" >
<div id="topic_content_input" contenteditable="true" ></div>
</div>
<div class="right_container">
</div>
<div class="clear_float">
</div>
css
#topic_title_input{
width:521px;
}
#topic_content_input{
/*do not change the display:inline */
width:521px;
display:inline;
background-color: orange;
border: solid 1px black;
}
/*do not change the css below*/
.left_container{
position:relative;
float:left; width:50%;
}
.right_container{
position:relative;
float:right; width:50%;
}
</style>
I am trying to create a div which is as wide as the page and has two div's inside it, one which is aligned left and one aligned right. Its' turning out to be a lot more difficult than I expected. With the code below, both div's align left. I have made a jsFidle to demonstrate the problem. Thanks for reading.
<style>
#container{
border:1px solid;
}
#left{
text-align:left;
border:1px solid red;
display:table-cell;
}
#right{
text-align:right;
border:1px solid blue;
display:table-cell;
}
</style>
<body>
<div id = "container">
<div id = "left">far left</div>
<div id = "right">far right</div>
</div>
</body>
There's no need to use floating elements or absolute positioning in general for something like this.
It's an approach that should've stopped along with using tables for general layouts.
Sample Jsfiddle
CSS:
#container {
display: table;
width: 100%;
}
#container > div {
display: table-cell;
}
.right {
text-align: right;
}
HTML:
<div id="container">
<div>
<p>Left</p>
</div>
<div class="right">
<p>Right</p>
</div>
</div>
You need to use
float: left
and
float: right
on the divs.
A cooler way of aligning things is to use
display:flex
on the container, but you'll have to check the browser compatibility for that I'm afraid.
I have this CSS:
#center{
display:table-row;
border:solid #55A 1px;
background-color:#AAF;
height:100%;
}
Actually, the border property is just ignored. Why? How can I Fix it?
DEMO
Thanks
Table rows can't have borders. Cells within a table row can, but the row itself cannot.
If you add a 'cell' to the table-row, for example:
<div id="content">
<div id="top">TOP</div>
<div id="center">
<div>CENTER</div>
</div>
</div>
Then the following CSS works:
#center{
display:table-row;
}
#center > div {
display: table-cell;
border:solid #55A 1px;
background-color:#AAF;
height:100%;
}
JS Fiddle demo.
It's important to remember that the browser will render an element as you tell it to; so if you tell a div to display: table-row it will display that way; and a table-row does not have a border. table-cells do, though, which is why I added the child div and assigned it that display property.
CSS
#content{
display:table;
border:solid black 1px;
width:250px;
height:300px;
}
.center{
display:table-row;
}
.center > div {
display: table-cell;
border:solid #55A 1px;
background-color:#AAF;
}
#top{
border:solid red 1px;
}
HTML
<div id="content">
<div class="center" style="height:50px">
<div id="top">TOP</div>
</div>
<div class="center" style="height:100%">
<div>CENTER</div>
</div>
<div class="center" style="height:50px">
<div>BOTTOM</div>
</div>
</div>
demo
I'm trying to place 2 divs side by side inside of another div, so that I can have 2 columns of text and the outer div drawing a border around both of them:
HTML
<div id="outer">
<div id="left">
...
<div id="right">
</div>
CSS
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
However, the outer div registers a height of 0px and so the border doesn't go around the other divs. How do I make the outer div recognize the heights of the things inside it?
It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element.
You can use the overflow style to make the parent element take the floating elements in consideration:
#outer { overflow: auto; }
There are a couple of solutions to this issue:
#outer: overflow: hidden;
or add some non-displaying content to the outer div that comes after the floated divs that you then add a clear: both style rule to.
You can also add, through css, the :after pseudo-element to insert content after those divs that you then apply clear: both to - this has the advantage of not requiring extra markup.
My preference is the first one.
Try this:
<div id="outer">
<div id="left">
...
<div id="right">
<div style="clear:both"></div>
</div>
add overflow: hidden; to the main div.
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
overflow: hidden;
border: 1px solid green;
}
#left{
float:left;
border: 1px solid red;
}
#right{
width:500px;
float:right;
border: 1px solid yellow;
}
</style>
You could clear the float by inserting an element after the floated elements that has a clear property applied to it because floated child elements cause the parent to have 0 height since they don't take the height of the floated children into consideration.
<div id="outer">
<div id="left">
...
<div id="right">
<div class="clear"></div>
</div>
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
}
#left{
float:left;
}
#right{
width:500px;
float:right;
}
.clear{ clear: both; }
You must also float the outer div.
Div's that contain floatet divs and that are not floated themselves collapse.
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
float:left;
}
#left{
float:left;
width:300px;
}
#right{
width:500px;
float:right;
}
How bout like this:
<style type="text/css">
#outer{
background-color:rgba(255,255,255,.5);
width:800px;
border:thin solid #000000;
height:300px;
margin:5px;
padding:10px;
}
#left{
float:left;
border:thin dashed #000000;
width:385px;
height:100px;
margin:5px;
}
#right{
width:385px;
float:left;
border:thin dashed #000000;
height:100px;
margin:5px;
}
</style>
<div id="outer">
<div id="left">
</div>
...
<div id="right">
</div>
</div>
if div inside a parent is floated it is no longer part of parent div:check it by inspecting parent element.no to fix your problem there are two methods:
1)make a empty div at end inside parent class it as .blank all following css
.blank:after{
content: "";
clear:both;
display:block;
}
Or
2) give parent a class .clear-fix and add css
.clearfix:after {
content: "";
clear: both;
display: block;
}
it will give parent a height equal to contents