2 div in an other "container" div - Adapt width - html

I have a problem with my css. I have 2 div "left" and "main" in a container div "container". My container's width is 90℅ of the body's width, and have a margin: %3 auto ( to center the container ).
.container{
margin:3% auto;
width:90%;}
Now, i want somethink special in my container: my "left" width = 20% of the container, a margin right on this " left div" to separate the "left" and the "main"
#left {
display:inline-block;
padding:10px;
background-color:#32CD32;
vertical-align:top;
list-style:none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
border:solid 2px black;
/*width:????? HERE I WANT AN ADAPTABLE WIDTH TAKING 20% OF THE CONTAINER WIDTH*/}
And my main's width have to take the rest of the container's width.
#main {
background-color:#32CD32;
vertical-align:top;
display:inline-block;
padding:10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
border:solid 2px black;
text-align:center;
/*width:80%; HERE, I WANT AN ADAPTABLE WIDTH TAKING THE REST OF THE CONTAINER*/}
But when i use % for the main's width and the left's width, it take the body ℅. Moreover, i dont know how to say " take the rest of the container's width.
<div class="container">
<div id="left">
<li>LIST1</li>
<li>LIST2 </li>
<li>LIST3 </li></div>
<div id="main">
<div id="new_comment"class="eblock">new comment</div>
<div id="comments"class="eblock">
<div id="onecomment"> new comment </div>
<div id="onecomment"> new comment </div>
<div id="onecomment"> new comment </div>
<div id="onecomment"> new comment </div>
<div id="onecomment"> new comment </div>
</div>
</div>
</div>
Thanks for the help.

Here you go
Working jsfiddle
.container {
width:90%;
height:200px;
border:1px solid;
}
.left {
width:auto;
height:200px;
background:red;
overflow:hidden;
}
.main {
height:200px;
width:60%;
background:blue;
float:left;
margin-right: 10px;
}
With inspiration from https://stackoverflow.com/a/1767270/5999686

The problem you have comes from a thing that CSS does called box sizing. This tells the browser how to layout your boxes (e.g. your div elements).
The default box-sizing that modern browsers use is called padding-box. It calculates the width of your boxes (in your example 20% or 80% of the #container) and then adds padding and borders to that calculated width. This means your elements end up taking up more space than you initially intended them to do.
One possible solution is to override the default box-sizing. Setting box-sizing: border-box tells the browser to include padding and borders when calculating the width, i.e. these values are included in the 20% of the containers width. Lots of people advise just setting this on all elements (see Paul Irish's article on the topic).
In your case I'd suggest setting:
#left {
width: 20%;
margin-right: 5%;
}
#main {
width: 75%;
}
Now, in some cases the browser has to round values. Because of this you might end up with values that sum up to more than the available space. Should this happen, just play around with some of the percentages (e.g. set margin-right: 4%).

Related

center a container in an elastic background environment

Please can you check this example website. if I read the code well (just had a sight) it's setup with tables with some javascript so that a centered container can always stay at the center, and that the body has got two fluid color backgrounds which expands according to the screen size.
I was attempting to reproduce something like this but just using css, I am quite sure I could but can't figure how. please could you give me some indication/document to read.
i have designed a simple structure here in Jsfiddle,have a look
MARK-UP::
<div class="wrapper">
<div class="head_wrapper">
<div class="left_head">left</div>
<div class="right_head">right</div>
</div>
<div class="body_wrapper">
<div class="left_body">left</div>
<div class="right_body">right</div>
</div>
</div>
CSS ::
.wrapper{
overflow:hidden;
width:100%;
display:table;
}
.head_wrapper,.body_wrapper{
overflow:hidden;
width:100%;
padding:0px;
display:table-row;
}
.left_head,.left_body,.right_head,.right_body{
display:table-cell;
text-align:center;
vertical-align:middle;
}
.left_head{
background:black;
height:300px;
font-size:36px;
color:white;
}
.right_head{
background:blue;
height:300px;
font-size:36px;
}
.left_body{
background:yellow;
height:800px;
font-size:36px;
}
.right_body{
background:green;
height:800px;
font-size:36px;
}
.left_head,.left_body{
width:70%;
overflow:hidden;
}
You're just asking for horizontal centering, on a fixed-width container. This is easily done entirely in CSS. Simply set for your container (the element that wraps around your entire site):
.container {
width: 800px;
margin: 0 auto;
}
The "auto" will automatically even out the left and right margins (with no margin on top and bottom.)
[Edit: oops forgot a bit]
As for the blocks of colour, you can achieve this with a background image on your element, that's 1px wide and however tall you need. Just set it to repeat-x.
If your two sections have the possibility of having different heights, you can break it up, so that:
One container is full-width, and has the background colour. An inner container will then be fixed-width with auto margins as above;
Another container is full-width, and has the lighter background colour. An inner container will then be fixed-width with auto margins as above.
This means your code will be something like:
<div class="headercontainer">
<div class="header> This is my header </div>
</div>
<div class="maincontainer">
<div class="main"> This is rest of my copy. </div>
</div>
And your CSS:
.headercontainer { background-color: #222; }
.maincontainer { background-color: #444; }
.headercontainer .header,
.maincontainer .main { width: 800px;
margin: 0 auto; }
HTH :)

Can't get floats to match 100% width or expand parent

I have some basic CSS which im trying to make a post layout for a forum but i cannot get it to work.
I have one div 100% width with two floats below it side by side. They seem to never equal 100% width and so don't line up with properly.
Equally the parent div of the two floats does not expand if the floats expand and i do not know how to fix it.
This is what i have so far:
CSS
.parent{
width: 100%;
top: 10px;
position: relative;
clear: both;
color: black;
}
.line{
height:20px;
padding-left:10px;
lineHeight: 20px;
margin:0px;
border: 1px solid black;
}
.container{
width:100%;
text-align: center;
border-bottom:1px solid red;
}
.fleft{
float:left;
width:10%;
text-align:left;
margin:0px;
padding-left:10px;
border-right:1px solid black;
}
.fleft2{
float:left;
width:86%;
text-align:left;
margin:0px;
padding-left:10px;
border-right:1px solid black;
}
The HTML:
<div class="parent">
<div class="line">
<span style="float:left;">Test</span>
<span style="float:right;">Test 2</span>
</div>
<div class="container">
<div class="fleft"> Hello </div>
<div class="fleft2"> Hello Message</div>
</div>
</div>
JS Fiddle also provided:
http://jsfiddle.net/yMaqR/10/
I have one div 100% width with two floats below it side by side. They seem to never equal 100% width and so don't line up with properly.
You have to take into consideration the padding & margin. So if you add up width + padding + margin of the floated elements and they overflow the width of the parent, they'll be wrapped.
So a possible solution is to remove the padding and add it maybe to child elements.
Equally the parent div of the two floats does not expand if the floats expand and i do not know how to fix it.
The solution is to use a clearfix
More about floats and understanding how they work.

CSS div inline-block + img max-width

I've been looking for answer for some time now & was tinkering with it for a while but cannot seem to find solution.
Probably it's easy as hell but I cannot wrap my head around it.
I'v got image with inline style of 'max-width:44%' wrapped in container.
How do I make container to be the same width as image inside it?
<div class="post" >
<img src="http://placekitten.com/200/300" style="vertical-align:bottom; max-width:44%;"/>
</div>
.post{position:relative; display:inline-block; border: 1px solid lime; }
Here's jsfiddle example: http://jsfiddle.net/37Fyb/2/
A percentage width sets the child element to that percentage of the width of its parent. In this case, the child is your img and the parent is your .post divider. Setting the image's width to 44% sets it to 44% of the divider's width - meaning no matter what you to the container, the image will always be 44% of its width.
One way to get around this problem would be to give your img 100% width and your .post divider the 44% width, then wrap that in another divider with fixed width:
HTML
<div class="outerContainer">
<div class="post" >
<img src="..." style="... width:100%;" />
</div>
</div>
CSS
.outerContainer {
width:202px;
}
.post{
border: 1px solid lime;
width:44%;
}
JSFiddle example.
Try this:
.post{
position:relative;
display:inline-block;
border: 1px solid lime;
max-width:44%}
img{
vertical-align:bottom;
max-width:100%;
}

Prevent float left div from going to a new line

I have 4 divs that are set to float left but the end div keeps wrapping two a new line on a smaller screen which is really annoying me...i want them to scale with the screen size so they always stay on the same line regardless of screen size... and im trying not to use a table (which is very tempting giving they v.reliable for this!!!)
I'm wondering how to fix this annoying issue so they always stay in position regardless of screen size??
I have this as my CSS:
.wrapper{
margin:0 auto;
width: 80%;
display: table-cell;
}
.gridf{
float:left;
margin-right:3px;
width:200px;
min-height:200px;
border:1px solid white;
}
.grid{
float:left;
margin-left: 3px;
margin-right:3px;
width:200px;
min-height:200px;
border:1px solid white;
}
.gridl{
float:left;
margin-left: 3px;
width:200px;
min-height:200px;
border:1px solid white;
}
My HTML:
<div style="overflow: hidden;">
<div class="wrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
Please help :D
Your wrapper is a percentage width container with 4 fixed-width child elements floated.
The width of the wrapper is dependent on the width of the viewport. If the viewport is narrowed to the point that the wrapper's width is less than that of the 4 child element widths together, then naturally they won't all fit and therefore will wrap.
The fix is to make sure your wrapper doesn't get smaller than the combination of the children.
So, add up with widths, borders and margins of the child elements and then give the wrapper a min-width attribute equal to that.
Hi i think you should this check to this demo
.wrapper {
margin: 0 auto;
width: 80%;
border: solid 1px red;
overflow: hidden;
}
.gridf,
.grid,
.gridl {
Background: green;
width: 24%;
min-height: 100px;
float: left;
margin: 2px 0;
}
.gridf {} .grid {
margin: 2px 1%;
}
.gridl {
background: yellow;
}
<div class="wrapper">
<div class="gridf">One</div>
<div class="grid">Two</div>
<div class="grid">Three</div>
<div class="gridl">Four</div>
</div>
Although this is an old post, I think that the problem, which I also run into, is the fact that you want all these cells to be of a fixed size, and not %, right? The solution you chose changed initial format where you specified width:200px;
Well, I would suggest to look here: http://jsfiddle.net/gn2bg/
The ONLY one thing I did is to add inner wrapper around your cells:
.inwrapper{
float:left;
overflow: hidden;
min-width: 830px;
}
and new html as this:
<div class="wrapper">
<div class="inwrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
Notice that your wrapper requires 80% of space.
The inwrapper, however, tells that its size is fixed - 830px (total of all internal div sizes plus room for padding.)
This way inwrapper uses 'elbows' to stretch the width, and override these 80% of 'wrapper'
I understand that you already made decision as to what is your best solution. I am leaving this response to anyone else in the future who needs exact answer to your exact question.
You can try removing the table-cell display rule from the wrapper and setting percentages (or min-widths) on the child divs like this jsFiddle example.
That should do the trick :
<div class="wrapper">
<div style="width:850px">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
And that will be supported on any browser.
http://jsfiddle.net/5GrKU/3/
HTML
<div style="overflow: hidden;">
<div class="wrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
CSS
.wrapper{
margin:0 auto;
width: 80%;
display: inline;
}
.gridf{
float:left;
margin-right:3px;
width:20%;
min-height:200px;
border:1px solid red;
}
.grid{
float:left;
margin-left: 3px;
margin-right:3px;
width:20%;
min-height:200px;
border:1px solid red;
}
.gridl{
float:left;
margin-left: 3px;
width:20%;
min-height:200px;
border:1px solid red;
}
for you reference i have also added the URL of the demo. http://jsfiddle.net/sg8FE/
UPDATE
just change display:inline in wrapper class to display:block rest all is right and the div's are centered.
by giving a fixed width in your inner divs you are forcing them to have that width no matter what is the size of the view port. And giving the outer div a width of 80% you are shrinking its size with the width of your view port. You need to do either giving fixed width to all those divs or giving a relative width to all.

Vertical Centering Absolutely Positioned Block

Ok, so I am trying to center a div with dynamic content (both its width and height are unknown because the text takes up unknown space and wraps an unknown amount of lines).
Per this post, I managed to center the div horizontally.
However when I apply the same principle to vertical centering, the block only moves 50% down (and doesn't move up at all).
JSFiddle of my problem here: http://jsfiddle.net/nMqJG/2/ ; as you can see, it is centered horizontally but not vertically...
Thanks and any help appreciated,
Edit: FYI, I am using FF10.0.2
If you don't need to support old browsers, use display: table-cell. Details here
HTML:
<div class="wrapper">
<div class="in">
DYNAMIC CONTENT DYNAMIC CONTENT DYNAMIC CONTENT DYNAMIC CONTENT
</div>
</div>
CSS:
.wrapper{
border:1px solid #F00;
width:200px;
height:200px;
display: table-cell;
vertical-align: middle
}
.in{
border:1px solid #00F;
}
Fiddle: http://jsfiddle.net/nMqJG/25/
You need to be thinking in terms of %width and %height:
.wrapper{
border:1px solid #F00;
width:200px;
height:200px;
position:relative;
}
.in{
float:left;
width:100px;
height:100px;
margin:25%;
display:inline-block;
border:1px solid #00F;
}
<div class="wrapper">
<div class="in">
DYNAMIC CONTENT
</div>
</div>
If you are using fixed pixel widths, then you are going to need to think about how your %margin will affect interior divs based on space constraints.
For example, you have a 200x200 container, with a 100x100 interior DIV. So if you move you interior div 25% away from the exterior, you are moving 200*.25 = (50px). 50+100+50 is 200 which is centering your interior div on all sides.
Will this work for you? (Borrowing code and adjusting from other answer)
.wrapper{
border:1px solid #F00;
width:200px;
height:200px;
position:absolute;
}
.in{
left: 25%;
right: 25%;
top: 25%;
bottom: 25%;
position: absolute;
display:inline-block;
border:1px solid #00F;
}
<div class="wrapper">
<div class="in">
DYNAMIC CONTENT
</div>
</div>
Using absolute positioning and 25% on all top/left/bottom/down sides should get your inner div right in the middle regardless of the wrapper size or position on the page.