I need your help,
How can the CSS code below be modified, such that I would be able to have a parent (container) div at 100% width while the 2 inner divs are 70% and 30% width inside the box? As it stands now, it seems that the 2nd div is pushing out of the container div?
<style type="text/css">
#containerdiv {
width:100%;
}
#outerdiv {
height:300px;
border: 1px solid blue;
position: relative;
}
#innerdiv1 {
height:300px;
float:left;
border: 1px solid red;
width: 70%;
}
#innerdiv2 {
height:300px;
border: 1px solid green;
width: 30%;
}
</style>
<div id="outerdiv">
<div id="innerdiv1">
</div>
<div id="innerdiv2">
</div>
</div>
SOLUTION :
I updated your CSS code in this FIDDLE
EXPLANATION :
The 1px border you put around the inner-divs increases the with of these divs to prevent that and include the border in the CSS width property, You can use box-sizing:border-box; with float:left on both inner divs.
You can learn more about box-sizing property here
CSS :
#containerdiv {
width:100%;
}
#outerdiv {
height:300px;
border: 1px solid blue;
position: relative;
}
#innerdiv1 {
height:300px;
float:left;
border: 1px solid red;
width: 70%;
box-sizing:border-box;
}
#innerdiv2 {
height:300px;
border: 1px solid green;
width: 30%;
box-sizing:border-box;
float:left;
}
for one your innderdiv2 needs float: left; in the code you provided, but besides that it looks like you're experiencing the pains of the box-model. Your divs are indeed 30% and 70% width of the parent container, however they each have a 1px border, which causes them each to be 2px too large. Try using box-sizing: border-box;. I generally do something like this:
*,
*:before,
*:after {
box-sizing: border-box;
}
See here:
JSFiddle
The 1px borders push the divs past 100%, because they add to the overall width.
Use -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; on your inner divs so you can add attributes such as padding and borders without contributing extra to the dimensions.
Borders increases the size of the element you give it to.
Remove the borders and it should work like you want it to.
And you dont need the double-width.
#containerdiv {
width:100%;
}
#outerdiv {
height:300px;
background-color:blue;
position: relative;
}
#innerdiv1 {
height:300px;
float:left;
background-color:red;
width: 70%;
}
#innerdiv2 {
height:300px;
background-color:green;
width: 30%;
}
JSFiddle
on your innserdiv2 you can use a margin-left attribute as well to fix this...
#innerdiv2 {
height:300px;
border: 1px solid green;
width: 30%;
box-sizing:border-box;
margin-left:70%;
}
and you dont need to have a width:100% on #containerdiv. jsut have width:100% in #outerdiv.
your #innerdiv1 looks like
#innerdiv1 {
height:300px;
float:left;
border: 2px solid red;
width: 70%;
box-sizing:border-box;
}
this takes care of your border from overflowing as well outside the div. hopefully this helps. I know you have alrdy accepted an answer.
Related
I have a block position absolutely within its parent. The parent has a border left and right. This causes the absolutely positioned block (which also has borders) to be 2px too small.
What is the best way to go about fixing this?
Goal:
I basicly want the two blocks to align. Their borders should basicly look like 1 border. The problem is that even with border-box the child div is smaller and thus doesn't align.
html
<div class="container">
<div class="diagonal"></div>
</div>
css
* {
box-sizing: border-box;
}
body {
background-color:red;
}
.container {
width:1170px;
margin:0 auto;
margin-top:200px;
height:700px;
position:relative;
z-index:3;
background-color:white;
border-style:solid;
border-color:transparent #D2D8DE #D2D8DE #D2D8DE;
border-width:0 1px 1px 1px;
}
.diagonal {
width:100%;
height:400px;
transform:skewY(-10deg);
position:absolute;
top:-200px;
left:0;
background-color:white;
border-style:solid;
border-color:transparent #D2D8DE;
border-width:0 1px;
z-index:-1;
}
JSFiddle
I think you're looking for this:
* {
box-sizing: border-box;
}
This property tells the browser to account for any border and padding in the value you specify for width and height
EDIT :
If you want to have different borders for inner and outer div and you want them to align, then set .diagonal{ left:-1px; } where 1px is width of inner div's border.
I've changed width and color so that result would be easier to notice. NB: In this case you don't need box-sizing: border-box;
body {
background-color: red;
}
.container {
width: 1170px;
margin: 0 auto;
margin-top: 200px;
height: 700px;
position: relative;
z-index: 3;
background-color: white;
border-style: solid;
border-color: transparent black black black;
border-width: 0 3px 3px 3px;
}
.diagonal {
width: 100%;
height: 400px;
transform: skewY(-10deg);
position: absolute;
top: -200px;
left: -3px;
background-color: white;
border-style: solid;
border-color: transparent blue;
border-width: 0 3px;
z-index: -1;
}
<div class="container">
<div class="diagonal"></div>
</div>
My HTML
<div id="wrapper">
<div id="div1">The two divs are</div>
<div id="div2">next to each other.</div>
</div>
My CSS
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width:49%;
height:120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width:49%;
height:160px;
border: 1px solid green;
}
A JSFiddle
So, when as you can see the width of each div is 49%, that's the only way I'm getting it to work. If you set the width of each to 50%, the divs aren't displayed next to each other anymore... Why is that?
Because of two things
Border size so you need to change box-sizing to border-box
White space on inline-block elements
* {
box-sizing: border-box;
}
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width: 50%;
height: 120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width: 50%;
height: 160px;
border: 1px solid green;
}
<div id="wrapper">
<div id="div1">The two divs are</div><div id="div2">next to each other.</div>
</div>
You need to remove the line break between the <div> tags and box-sizing:border-box;
The two divs arenext to each other.
Another approach would be to use float
#wrapper {
border: 1px solid blue;box-sizing:border-box;
}
#div1 {
float:left;
width:50%;
height:120px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
#div2 {
float:left;
width:50%;
height:160px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
The other option is to use Flex.
#wrapper {
border: 1px solid blue;
display: flex;
}
#div1 {
width:50%;
height:120px;
border: 1px solid red;
}
#div2 {
width:50%;
height:160px;
border: 1px solid green;
}
This is because you have added a border of 1px to wrapper. Your border with take 2px of total width of your page.
If you wanna keep the border and still keep the width of each div as 50%, you can refer to #NenadVracar 's answer
Another option is to use calc() and calculate the width to be 50% - 2px. I'm just listing that as an option #Nenad Vracar has the right answer
I'm trying to get the 100 % height work but at some point the text flows over the divs. (Site url: http://uusilegenda.net/)
Here is the wrapper css:
#wrapper {
box-shadow: 0px 0px 20px #666;
background-color: #ddd;
margin:0 auto;
width: 1000px;
border-left: 2px solid #666;
border-right: 2px solid #666;
height:auto !important;
height:100%;
min-height:100%;
position:relative;
And here is the css of the menu bar:
#content2 {
float: left;
left:0;
width: 200px;
position: absolute;
height: 100%;
bottom: 0;
background-color: rgba(255,255,255,0.05);
border-right: 1px solid #666;
If I add
clear: both;
overflow:hidden;
to the wrapper div and remove
position: absolute;
from the content div it almost looks like right but then the menu bar isn't full height.
And before you say that the css is poor, don't blame me, it is my friend's layout/css. :)
Remove position:absolute from #content2
and changed the width of #content1main
but ultimately you should rethink your whole css as your's is quite bad.
Agree with the 1st answer about your css/layout needing to be re-written in a better way, but a quick fix would be this:
Add the following css to the existing style rules:
#wrapper { clear: both; overflow:hidden; }
#content1main { width: 760px; }
Remove the following css on the existing style rule:
#content2 { position: absolute }
These will fix your layout issues.
I have two divs inside a div. One of the two is floated to the left and it has some links in it. It has a width of 200px The second of the two has a value of overflow:hidden and it has a width of rest to the right. It has some content in it which makes its height longer than first div.
I want first div to expand to parent's or the second div's height according to the increment of the second div's height
<div id ="main">
<div id ="first">
Link
Link
Link
Link
</div>
<div id ="second">
Link
Link
Link
Link
Link
Link
Link
Link
</div>
</div>
.
#main{
overflow:hidden;
border:1px solid black;
}
#first{
border:1px solid black;
width:200px;
float:left;
}
a{
display:block;
padding:10px;
}
#second{
border:1px solid black;
overflow:hidden;
}
JSFiddle
The solution for your problem is: first you have to give a height to the parent div and then set the height of the child, #first to min-height: 100%, the code would be like this:
#main {
overflow:hidden;
border:1px solid black;
height: 400px;
}
#first {
border:1px solid black;
width:200px;
float:left;
min-height: 100%;
}
You can use display: table; applied to the #main container and display:table-row; and display:table-cell; applied to #first and #second to make the first child container take the height of its sibling container #second. Remember to add overflow:auto; to allow the first container to make it expand its height until the bottom.
CSS
#main{
overflow:hidden;
border:1px solid black;
display:table;
width:100%;
}
#first{
border:1px solid black;
width:200px;
float:left;
display:table-row;
overflow:auto;
height:100%;
}
a{
display:block;
padding:10px;
}
#second{
border:1px solid black;
overflow:hidden;
height:400px;
display:table-cell;
width:100%;
}
DEMO http://jsfiddle.net/a_incarnati/djobkh7t/7/
I've removed the floats and changed the display types on your divs to fix the problem. See example CSS + fiddle:
#main{
border: 1px solid black;
display: table;
width: 500px;
}
#first{
border: 1px solid black;
width: 25%;
display: table-cell;
}
#second{
border: 1px solid black;
width: 75%;
display: table-cell;
}
http://jsfiddle.net/djobkh7t/13/
You can set the display type of 'table' on the parent div, then 'table-cell' on the child div's.
In the .SalesPanel class, I have set it to 100% width. I have notice its over-lapping the padding on the right that I have set in content id.
See: http://jsfiddle.net/CBAE7/9/ and look at the right padding and compare it with left padding.
What causing this and how to fix?
Also I was expecting 3 div's on 1 row even using 100% width.. how to fix?
HTML:
<div id='content'>
<div class='SalesPanel'> One </div>
<div class='SalesPanel'> Two </div>
<div class='SalesPanel'> Three </div>
</div>
CSS:
#content {
width: 700px;
padding: 8px;
overflow: hidden;
background-color: white;
border: 1px solid #C8CCD5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.SalesPanel {
border:1px solid #dddddd;
height:30px;
float:left;
width: 100%
}
Try this jsFiddle example.
#content {
width: 700px;
padding: 8px;
background-color: white;
border: 1px solid #C8CCD5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.SalesPanel {
border:1px solid #dddddd;
height:30px;
width: 33%;
display:inline-block;
margin:0;
}
Change box-sizing property:
.SalesPanel {
/* your stuff */
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Initial value of box-sizing for modern browsers is content-box. It means that width: 100% will affect only the content, not counting padding and borders (your case). By changing this property to box-sizing you will fit it in the container. The actual width of the content will be calc(100% - 2px).
It's CSS 3 property and supported by IE 8+ and all other modern browsers.
Also, seeing as you are using a static 3 of them, 33% width never hurt either.
.SalesPanel {
border:1px solid #dddddd;
height:30px;
float:left;
width: 33%;
}
Try this code:
.SalesPanel {
border:1px solid #dddddd;
height:30px;
float:left;
width: 33%;
display:inline-block;
}