Expand child over parent padding - html

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;
}

Related

Make a div stick to the right edge of the parent and overlap others in the parent

I need to place a div stick to the right edge of its parent div and when re-sizing the browser window, it should overlap other elements in the same parent and they should be hidden.
This image tells the story
Please note that, I don't want that div to have fixed position. It should scroll just like others and the elements (texts or whatever) should be under it. Just like the attached image.
I tried the following code but, it made the red div stick to the edge of its grandparent.
.redarea{
position:absolute;
float:right;
}
What's the way of getting this done ?
This one does exactly what you want
#parent{
border:1px solid red;
width:100%;
height:60px;
position:relative;
white-space: nowrap;
overflow: hidden;
}
#rightchild{
top: 0;
width:100px;
right:0;
bottom: 0;
background:red;
position:absolute;
}
<div id="parent" style="">
<p>This area is getting hidden This area is getting hidden This area is getting hiddenThis area is getting hidden</p>
<div id="rightchild">
</div>
</div>
This is the easiest way to do it imo.
Give your outer box a padding on the right side, and let the inner box fill up the padding by giving it the same width and positioning it absolutely to the right.
<!DOCTYPE html>
<html>
<head>
<style>
div.outer{
width: 90%;
height: 30px;
padding-right: 30px;
position: relative;
border: 2px solid #ddd;
margin: 0 auto;
}
div.inner{
width: 30px;
position: absolute;
top: 0;
bottom: 0;
right: 0;
background: red;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
Thanks everyone, using all of your help, I managed to write this code. Let me know if it's illegal.
#parent{
border:1px solid red;
width:100%;
height:60px;
position:relative;
overflow:hidden;
white-space: nowrap;
}
#rightchild{
width:100px;
right:0;
height:60px;
background:red;
position:absolute;
}
p{
float:left;
}
<div id="parent" style="">
<p>This area is getting hidden This area is getting hidden This area is getting hiddenThis area is getting hidden</p>
<div id="rightchild">
</div>
</div>
Demo
You shouldn't even need a float on it. You would just need to have:
position: fixed;
right:0;
You may also need to specify a z-index on it depending how you coded it. If it's underneath instead of on top, do a:
z-index:14; or something of the like.
you can try to use display:inline-block too. see this fiddle: https://jsfiddle.net/ahmadabdul3/sasc1a7h/
keep in mind the calc() css property is not supported by all browsers yet (especially older ones)
To keep text on a single line: white-space:nowrap .
display should help you here to avoid the use of position: either table or flex :
.flex {
display: flex
}
.full {
white-space:nowrap;
}
.flex .full {
flex: 1;
}
.table {
display: table;
width: 100%;
table-layout: fixed;
}
.table p {
display: table-cell;
}
p {
border: solid;
}
.cds {
padding: 0.25em;
background: tomato;
width: 100px;
}
<div class="flex">
<p class="full"> text to spray in flex display text to spray in flex display text to spray in flex display </p>
<p class="cds">condense</p>
</div>
<div class="table">
<p class="full"> text to spray in table display text to spray in table display text to spray in table display</p>
<p class="cds">condense</p>
</div>

Chrome and Opera creating small padding when using display:table

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.

How can I stack divs on float?

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;
}

Placing two divs one below another

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;
}​

Height of outer div not expanding with inner div

I have a bodyMain div of 100% width. Inside it is a body div 800px with auto margin(can I use 'body' as id ?). Inside this are two divs bodyLeft and bodyRight 200px and 600px wide respectively. When I add content to inner divs neither bodyMain nor body expands in height . All heights are auto.
Here is the code: http://jsfiddle.net/TqxHq/18/
HTML:
<body>
<div id="bodyMain">
<div id="body">
<div id="bodyLeft"> left text goes here<br />
</div>
<div id="bodyRight">Right text goes here
</div>
</div>
</div>
</body>
CSS:
#bodyMain{
border:1px solid red;
width:100%;
height:auto;
}
#body{
border:1px solid green;
width:804px;
height:auto;
margin:auto;
}
#bodyLeft{
border:1px solid blue;
float:left;
width:200PX;
height:auto;
}
#bodyRight{
border:1px solid orange;
float:right;
width:600PX;
height:auto;
}
You must add
<div style="clear:both;"></div>
at the end of floating div to fix this issue. see
here
Problem happens when a floated element is within a container box and element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow. You can use 2 methods to fix it:
clear:both
clearfix
This is a common issue when working with floats. There are a couple of common solutions:
Add a div after the floats with clear: both
Add the two floats into a container with the CSS attribute overflow: auto
Make the parent element a float
Using the :after CSS pseudo element with the CSS: .clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
Adding a set height to the parent element
See this article
The simple solution is to have outer div overflow:hidden (in style attribute).
Thank you
jsFiddle demo
*{
margin:0;
padding:0;
}
#bodyMain{
position:relative;
overflow:hidden; /*added*/
border:1px solid red;
/*removed height:auto;*/
/*removed width:100%;*/
}
#body{
display:table;/*added*/
border:1px solid green;
width:804px;
margin: 0 auto; /*improved*/
}
#bodyLeft{
border:1px solid blue;
float:left;
width:200px;
/*removed height:auto;*/
}
#bodyRight{
border:1px solid orange;
float:right;
width:600px;
/*removed height:auto;*/
}
To avoid confusion with predefined tag names, refrain from using body, html, or head as ID attribute values.
I agree with Muhammed Irfan's idea. I don't agree with his method though. Avoid inline styling except for small snippets. Especially in this case, because it is likely that there will be another case where clear: both is necessary. So, add a div, give it a meaningful class name and apply the additional CSS.
See this fiddle for an example.