I've noticed that Chrome (34.0.1847.131 m) and Opera (21.0.1432.67) are creating an small gap between two divs when using the property display:table;. (and not when using display:block, for example)
Here's a fiddle reproducing it. (adjust the width of the panel, it doesn't take place with every width)
To reproduce it:
HTML
<div class="left"></div>
<div class="right"></div>
CSS
.left {
left: 0px;
}
.right {
right:0px;
}
.left, .right {
width: 50%;
position: absolute;
height: 350px;
background:#000;
display:table;
border-spacing:0;
border:0;
margin:0;
padding:0;
}
How can I get rid of this gap? Is this some kind of bug?
Changing table to table-cell seemed to do the trick:
http://jsfiddle.net/3z24S/7/
add 1 px to the placement of the right div:
.right {
right:1px;
}
http://jsfiddle.net/3z24S/12/
I have two potential solutions:
Option 1:
Use css calc(); to set the width of the two divs, like so:
Working Example 1
.left, .right {
width: calc(50% + 0.1px); /* Important bit */
position: absolute;
height: 350px;
background:#000;
display:table;
border-spacing:0;
border:0;
margin:0;
padding:0;
}
Option 2:
Use JavaScript to set the width of the two divs, like so:
Working Example 2
wid = function () {
$('.left, .right').width(Math.ceil($(window).width() / 2)); //Math.ceil() will round the value up
};
$(document).ready(wid);
$(window).resize(wid);
If you can get away with using calc() its probably the better option, using JavaScript seems expensive for something like this.
If it is a matter of vertical-align and known height, you can do without display:table/table-cell; DEMO or you could do without absolute position.
You may use inline-block, vertical-align and pseudo élément.
HTML :
<div class="left">
<div class='content'>
<p>content</p>
</div>
</div>
<div class="right">
<div class='content'>
<p>content</p>
<p>content</p>
</div>
</div>
the div.content will be inline-block or is display:table-cell in your problem.
CSS
.left {
left: 0px;
}
.right {
right:0px;
}
.left, .right {
width: 50%;
position: absolute;
height: 350px;
background:#000;
color:white;
border-spacing:0;
border:0;
margin:0;
padding:0;
}
.left:before,
.right:before ,
.content {
display:inline-block;
vertical-align:middle;
max-width:95%;
}
.left:before,
.right:before {
content:'';
height:100%;
width:0;
}
Even answering here to your question , i still do not understand why position:absolute; and still not sure if elements are suppose to have an known height. It looks more like you are not using the proper or best method to your needs.
The pixel bug is, in my opinion, already answered in comments and obviously a different way for chrome to handle this display value.
Related
I've been trying this for a while and I don't seem to find a solution.
HTML:
<table>
<tr>
<td>
<div>this div has to expand over the td padding</div>
</td>
</tr>
</table>
CSS:
table {
height:100%;
}
td {
height:100%;
background: green;
padding:5px;
}
div {
min-width:100%;
height:100%;
background:yellow;
float:left;
white-space:nowrap;
}
I want the div to expand exactly as much as the td but to also expand over the td padding.
Moving the padding to the div element is not a solution since the div has to be 100% height and at least 100% width, the rest of the div's width is overflow:hidden and appears on hover but I try to keep the example as simple as possible so I didn't include that here.
Edit:
#codehorse I've tried your approach but now it appears that the div expands on the whole body so I guess Era is right, relative positioning might not work on td. I could use another wrapper between the td and div but I would like to avoid that if possible. I'm looking for a standard solution on this.
#Era Works perfect Thank you!
Although this is not the right way to do this but if it works for you then use this CSS for div:
div {
margin: -5px;
padding: 5px;
position: relative;
}
div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
td {
position: relative;
}
If your table structure is not too complex,i'll suggest you use display:table to achieve your purpose.....this way, you'll avoid position attributes, which otherwise conflict with layout sometimes making a big mess of things.
Also, html table is not suggested these days, since you have css tables!!
here is a demo
HTML
<div class="table">
<div class="td">
<div class="inner">this div has to expand over the td padding</div>
</div>
</div>
CSS
.table {
height:100%;
display:table;
}
.td {
height:100%;
background: green;
padding:5px;
display:table-cell;
}
div.inner {
min-width:100%;
margin:-2px; /* change this to suit your need */
background:yellow;
float:left;
white-space:nowrap;
}
I have the following html:
<body>
<div id="page">
<div id="header"></div>
<div id="content"></div>
</div>
</body>
and css:
html,body {
margin:0;
padding:0;
height:100%;
}
#page {
height:100%;
margin:auto;
left:0;
right:0;
width:500px;
}
#header {
height:100px;
width:500px;
}
#content {
width:500px;
height:100%;
}
The problem is that content div is the height of the window + the height of the header.
How can i make it to be the height of the window - the height of the header, I mean to stretch horizontally all over the remaining window. ??
In case you don't need to support IE7 and below - you can use a useful trick with
position: absolute
for #header and
padding-top: 100px;
box-sizing: border-box;
for #content.
Check out this fiddle: http://jsfiddle.net/Jx4sC/2/
Details regarding box-sizing support: http://caniuse.com/#feat=css3-boxsizing
You could use calc() in modern browsers and let the browser calculate the height of your content box:
#content {
width:500px;
height: calc(100% - 100px);
}
Similarly you could use some JavaScript to do the same. But then make sure to update your calculations each time the browser height gets changed.
This is supprisingly hacky to get going and you may not have to do it. for example, say you wanted to give #content a background-color, put it on #page instead.
html,body {
margin:0;
padding:0;
/* body should have height:100% by default */
}
#page {
height:100%;
margin: 0 auto;
width:500px;
/* use page as you would have used #content*/
}
#header {
height:100px;
}
#content {
}
edit: but if you really need to do this, you can do it like so
#page {
position: relative;
}
#content{
position: absolute;
top: 100px;
bottom: 0px;
left: 0px;
right: 0px;
}
this is not ideal because if you wanted to make #header bigger you need to remember to update #content, you can no longer use the normal page layout
Site: http://bit.ly/13nL8jV
Demo: http://jsfiddle.net/bBckp/
Brief: I am trying to get the CURRENT PROGRAMS to float under the SIGNATURE PROGRAMS with no luck. All of the columns in the footer have the CSS:
float: left;
width: 29%;
The columns are dynamic so I can't just wrap SIGNATURE and CURRENT in it's own div (I can probably hack it with JS)...CLARRIFICATION - I'm referring to the menus in the FOOTER.
Any thoughts how I can do this with just CSS?
You can tweak the element like so. This does leave a hole where it used to be, but that's what relative positioning does.
.item-130 {
position:relative;
left:-180px;
top:25px
}
Alternately you can set the parent UL to position:relative, and use absolute positioning:
.nav-pills {
position:relative;
}
.item-130 {
position:absolute;
left:0px;
top:25px
}
it may help you
html like this way;
<div class="wrapper">
<div class="SIGNATURE PROGRAMS">
..........
</div>
<div class="CURRENT PROGRAMS">
..........
</div>
</div>
and css
.wrapper{
overflow:hidden;
}
.SIGNATURE PROGRAMS{
float:left;
}
.CURRENT PROGRAMS{
clear:both;
}
EDIT:: if you cant change your html..then you may try this
.moduletable.current-prog {
position: relative;
left: -29%;
margin-top: 100px;
}
I want two elements to take up an exact percent of the parent's width, but I also need margins on them holding them apart. I have the following markup:
<div class='wrap'>
<div class='element'>HELLO</div><div class='element'>WORLD</div>
</div>
.wrap {
background:red;
white-space:nowrap;
width:300px;
}
.element {
background:#009; color:#cef; text-align:center;
display:inline-block;
width:50%;
margin:4px;
}
As you can see in http://jsfiddle.net/NTE2Q/ the result is that the children overflow the wrapper:
How can I get them to fit within the space? Sadly, there is no box-sizing:margin-box for this case.
Technique #1 - Modern CSS3 calc()
Using CSS3's calc() length, you can do this by setting the width of the .element to:
.element {
width: 49%; /* poor approximation for old browsers */
width: calc(50% - 8px); /* standards-based answer for IE9+, FF16+ */
width: -moz-calc(50% - 8px); /* support for FF4 - FF15 */
width: -webkit-calc(50% - 8px); /* support for Chrome19+ and Safari6+ */
}
Demo: http://jsfiddle.net/NTE2Q/1/
See http://caniuse.com/calc for details on which browsers and versions support this.
Technique #2 - Old School Wrapping
Calculations can be made by piling up multiple elements. For this case, we wrap each 'element' in a wrapper that is 50% wide but with a 4px padding:
<div class='wrap'>
<div class='ele1'>
<div class='element'>HELLO</div>
</div><div class="ele1">
<div class='element'>WORLD</div>
</div>
</div>
.ele1 {
display:inline-block;
width:50%;
padding:4px;
box-sizing:border-box; /* Make sure that 50% includes the padding */
-moz-box-sizing:border-box; /* For Firefox */
-webkit-box-sizing:border-box; /* For old mobile Safari */
}
.element {
background:#009; color:#cef; text-align:center;
display:block;
}
Demo: http://jsfiddle.net/NTE2Q/2/
Technique #3 - Using (CSS) Tables
The same result can be made by treating the wrapper as a 'table' and each element as a cell within the same row. With this, whitespace between elements is not important:
<div class='wrap'>
<div class='element'>HELLO</div>
<div class='element'>WORLD</div>
</div>
.wrap {
background:red;
width:300px;
display:table;
border-spacing:4px
}
.element {
background:#009; color:#cef; text-align:center;
width:50%;
display:table-cell;
}
Demo: http://jsfiddle.net/NTE2Q/4/
Note that this last technique collapses the 4px spacing between the two elements, while the first two techniques cause 8px to appear between the two items and 4px at the edges.
What you are describing is basically a border. So why not to use CSS border property with background-clip? Just don't forget appropriate vendor prefixes.
http://jsfiddle.net/NTE2Q/8/
.wrap {
background-color: red;
white-space:nowrap;
width:300px;
}
.element {
background:#009; color:#cef; text-align:center;
display:inline-block;
width:50%;
border:4px solid rgba(0,0,0,0);
box-sizing: border-box;
background-clip: padding-box;
}
None of the above techniques worked consistently enough cross browser for me. I found a slightly different technique using display:table-cell allowed me to place 2 or more elements next to each other. Here is an example of it in action.
The CSS:
display:table-cell;
background:#009; color:#cef; text-align:center;
width:22%; /*Set percentage based on # of elements*/
border:4px solid rgb(255,0,0);/*no longer need background to be red, just make border red*/
You no longer even need the wrapper since the div is now treated as a <td>.
Though I strongly suggest using Phorgz's calc() technique whenever possible, I also want to propose an old-school way of doing this that uses only one wrapper and position: relative to achieve the effect.
.two-blocks-by-side() LESS Mixin:
.two-blocks-by-side(#padding) {
padding: #padding (#padding + #padding / 2);
font-size: 0;
& > div {
position: relative;
display: inline-block;
font-size: initial;
width: 50%;
&:first-child { left: -1 * #padding / 2 };
&:last-child { right: -1 * #padding / 2 };
}
}
JS Bin example
I am having some problems with placing two divs one below another.
I tried out some solutions found in Stackoverflow like below.
But Nothing seems to be working.
Code:
#wrapper {
position: absolute;
}
#up {
position: absolute;
float: left;
}
#down {
position: absolute;
float: left;
clear: left;
}
<div id="wrapper">
<div id="up"></div>
<div id="down"></div>
</div>
Here's My Attempt,
Fiddle
Helps would be appreciated.
Remove the CSS. DIV tags are block elements and would naturally flow down the page. You are floating them which would cause them to be displayed side by side.
Especially remove the "float" attributes.
That's how DIV's work by default, just remove your css. See a working example here: jsfiddle
<div id="wrapper">
<div id="up"></div>
<div id="down"></div>
</div>
I'm not sure if you want the outer div to be greater than the height of the page, but that's what this does:
#DivSlider
{
width:100%;
position:absolute;
height:170%;
background-color:green;
}
#DivHome
{
height:26%;
background-color:orange;
border:1px solid black; /* You were missing the 'px' here */
}
#DivSkills
{
height:25%;
background-color:white;
border:1px solid black;
}