Div breaking the size of the div beside - html

I have a screen that contains 4 divs, divs these are resizable on the monitor screen until a certain point, as you can see in this link
All I'm trying to do is get a way to put the div Main D beside of Main B and Main C, I tried to put the float: left; with a width of 100% encompassing these two divs, but it did not work.
If I remove the width of 100%, the Main D go to left, but changes the original size of the divs (Main B, Main C), and this is becoming a big problem, how can I solve this?

Is this what you're trying to do? http://jsfiddle.net/r5vsb/1/
Wrap Menu B and Menu C in a div and float it:
HTML
<div id="topo" style="">
<br>
<br>Main A</div>
<div id="sidebar">
<div id="Menu_B" style="">
<br>
<br>Menu B</div>
<div id="Menu_C" style=" ">
<br>
<br>Menu C</div>
</div>
<div id="main-content">
<div id="Feed" style="">
<br>
<br>Main D</div>
</div>
css
#topo {
background:#EEEEEE;
text-align:center;
border:1px solid #BBBBBB;
height:10%;
width:100%;
border-radius:5px;
position: relative;
min-width:839px;
min-height:86px;
max-width:1920px;
max-height:1080px;
}
#sidebar {
width:40%;
max-width:764px;
min-width:337px;
float:left;
}
#main-content {
width:60%;
min-width:500px;
float:left;
}
#Menu_B {
background:#EEEEEE;
text-align:center;
border:1px solid #BBBBBB;
max-height:428px;
min-height:290px;
height:40%;
border-radius:5px;
position: relative;
margin-top:5px;
}
#Menu_C {
background:#EEEEEE;
text-align:center;
border:1px solid #BBBBBB;
max-height:513px;
min-height:320px;
height:48%;
border-radius:5px;
position: relative;
margin-top:5px;
}
#Feed {
background:#EEEEEE;
text-align:center;
border:1px solid #BBBBBB;
border-radius:5px;
position: relative;
margin-top:5px;
margin-left:5px;
height:500px;
}
(I extracted your inline CSS to make it easier to read).

I know what you're trying to do, but you can not do it without actually going beyond limitations of CSS. I've done it before, and it involved more hassle than it was worth. Position divs next to each other with float: left is dead simple, yes, but you can not have 2 float left divs next to a floated left div with a width of 100%.
There are several methods, and most of them involve whipping out a good ol' fashioned calculator and do some number crunching. Just find the maximum width of divs B and C (as well as their padding and margins) and then in your CSS, take that number and change the width from 100% to:
width: calc(100% - x);
Assuming x is that number you got. This may or may not work, depending on your understanding of how margins and paddings work in CSS. If they don't have paddings or margins (or even borders for that matter), than getting x is easy. If you can find it, just plug and chug numbers in there.
Your next best bet is jquery, because jquery is an end all, be all solution to most problems that CSS can't solve. All you need to do is use a script like this:
function widthSetter(divB, divC) {
var getWindow = $(window).width();
var getB = $(divB).width();
var getC = $(divC).width();
var widthBC = getB + getC;
var getD = getWindow - widthBC;
$(divD).css("width", getD)
}
$(document).ready(function(){
widthSetter(divB, divC);
$(window).resize(function(){widthSetter(divB, divC)});
});
Granted you understand basic jquery, just be sure to plug in the div names where they need to be and you'll be good to go.

http://jsfiddle.net/fHZRD/
This works, you have to move the D above the other two, remove the width:100% from the container on B and C, and float the D right instead.
They will not scale down unless you add a container around all three of them with
style="width:100%;display:block;min-width:860px;height:100%;"
so that once the veiwport hits the min-width they stay that wide rather than squishing each other out. The max-width on D will cause a white space but I'm not sure if that's intended.
Here it is with that update
http://jsfiddle.net/R5DWF/

Try this:
HTML:
<div id="topo">
<br>
<br>Main A
</div>
<div id="menu-holder">
<div id="Menu_B">
<br>
<br>Menu B</div>
<div id="Menu_C">
<br>
<br>Menu C
</div>
</div>
<div id="Feed">
<br>
<br>Main D
</div>
CSS:
#topo {
background: none repeat scroll 0 0 #eeeeee;
border: 1px solid #bbbbbb;
border-radius: 5px;
height: 10%;
max-height: 1080px;
max-width: 1920px;
min-height: 86px;
min-width: 839px;
position: relative;
text-align: center;
width: 100%;
}
#menu-holder {
float: left;
max-width: 768px; /* 40% of 1920 max-width of topo*/
min-width: 335px; /* 40% of 839 min-width of topo*/
width: 40%;
}
#Menu_B {
background: none repeat scroll 0 0 #eeeeee;
border: 1px solid #bbbbbb;
border-radius: 5px;
height: 40%;
margin-top: 5px;
max-height: 428px;
max-width: 764px;
min-height: 290px;
min-width: 337px;
position: relative;
text-align: center;
width: 100%;
}
#Menu_C {
background: none repeat scroll 0 0 #eeeeee;
border: 1px solid #bbbbbb;
border-radius: 5px;
height: 48%;
margin-top: 5px;
max-height: 513px;
max-width: 764px;
min-height: 320px;
min-width: 337px;
position: relative;
text-align: center;
width: 100%;
}
#Feed {
background: none repeat scroll 0 0 #eeeeee;
border: 1px solid #bbbbbb;
border-radius: 5px;
float: left;
height: 87.2%;
margin-left: 5px;
margin-top: 5px;
max-height: 944px;
max-width: 1142px;
min-height: 678px;
min-width: 502px;
position: relative;
text-align: center;
width: 58.5%;
}

Related

Center floating divs in container

I am currently making a website without using framework however I have run into a problem. My divs are not getting centered within the container even though the container itself is centered in the body.
Html
<body>
<div id="content">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
Css
#content{
width: 90%;
height: 100%;
margin: 0 auto;
}
.box{
width: 400px;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
The divs are perfectly centered when I have my window to full width, but once I resize it, they just reorganize without centering.
Before resizing:
http://cl.ly/image/241R2I24280w/Screen%20Shot%202014-09-26%20at%2021.49.23.png
After resizing the window: http://cl.ly/image/2y2g2W0n230g/Screen%20Shot%202014-09-26%20at%2021.50.21.png
I have tried different methods to solve it, such as doing margin: 0 -10%; and margin: 0 25%;
When it comes to positioning I get confused.
Thanks.
Just change your CSS like this, this way you can adapt your boxes in many ways and they will react to responsive layouts as expected:
#content {
width: 90%;
height: 100%;
margin: 0 auto;
text-align:center;
display:block;
}
.box {
width: 45%;
height: 150px;
display:inline-block;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px 2%;
}
See fiddle here
Explanation:
I have removed your floats, used block elements and replaced your fixed sizes by percentages. Then, I used a text-align:center property in your container box #content so everything is nicely aligned in the center of that container. Now, if you resize, columns will take 45% of the width of the screen, but you can obviously change the behavior via media queries and use something like .box{display:box} for small screens
There are multiple solutions to your problem. Depending on what you have inside those boxes this might be the simplest one: text-align:centerwith a display:inline-block combo; See here.Fiddle
2 solutions :
You can use a percentage for the width your boxes.
#content{
width: 90%;
height: 100%;
margin: 0 10%;
}
.box{
width: 30%;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
Boxes will resize with the content but the stuff in the boxes might look weird in small sizes.
Or
You can use a pixel value for the width of your content.
#content{
width: 1200px;
height: 100%;
margin: 0 10%;
}
.box{
width: 400px;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
Width of boxes will not change while resizing, nor the stuff in it, but that can be painful on small screens.
add auto margin for your box
.box{
width: 400px;
height: 150px;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin-top: 13px;
margin-left:auto;
margin-right:auto;
}
I made your files on my machine and the divs are not centered so I assume your screen or resolution settings are different, or your content container is within one or more other divs?
Anyhow, try adding 'clear:left;' in your box class code and it should resolve your issue (put it just above the 'float:left' line. good luck!

CSS positioning - two elements next to each other

Ok, I know this question has been here at least hundred of times but this positioning is driving me crazy - can someone help me?
I have a portlet page (its basically html) with a table and a div tag. I would like to position them next to each other (table on the left, div on the right). Here are parts of my html:
<div id="page>
<table id="logtable">
...
</table>
<div id="divMessage>
...
</div>
</div>
...and CSS:
#page {
width: 1200px;
margin: 0px auto -1px auto;
padding: 15px;
}
#logtable {
width: 800px;
float: left;
}
#divMessage {
width: 350px;
position:relative;
right:-5px;
top: -20px;
}
I have tried various positions - absolute, fixed, float etc, but I cant seem to get it right... Thanks for any help!
You could use...
float: left;
on your div 'logtable'
I would advise using DIVs to do you alignment of content so wrap the table in a DIV.
I also prefer to use inline-block over float left and gives more predictable results.
so...
<div id="page">
<div id="divTable" class="InsideContent">
<table id="logtable">
Left
</table>
</div>
<div id="divMessage" class="InsideContent">
Right
</div>
</div>
#page {
width: 1200px;
margin: 0px auto -1px auto;
padding: 15px;
}
.InsideContent{
display:inline-block;
}
}
#divTable {
width: 800px;
}
#divMessage {
width: 350px;
}
Code needs tidying up but you get the idea...
JSFiddle http://jsfiddle.net/3N53d/
Use float:left on the element which should be on the left and float:right on the right one. Keep in mind that if the sum of their widths exceeds the available space in the parent element they will be split into two lines anyway.
Here you go , no need of right:-5px;
#divMessage {
width: 350px;
position: relative;
top: -20px;
float: left;
}
I see that you forgot closing the quotes at <div id="page>, this might cause some problems, but basically you have to use:
float: left;
for the last div.
I have created this JSFiddle for you to see if this fits your needs.
You can just use display: inline-* to put them side by side in a row
#logtable {
width: 800px;
display: inline-table;
}
#divMessage {
width: 350px;
display: inline-block;
}
JSFiddle
Just try this.
Fiddle
CSS
#logtable {
width: 500px;
float: left;
background:red;
}
#divMessage {
width: 350px;
position:relative;
float:left;
background:blue;
}
try this:
<table id="logtable">
<tr>
<td>
table area
</td>
</tr>
</table>
<div id="divMessage">
div area
</div>
</div>
#page {
width: 800px;
margin: 0px auto -1px auto;
padding: 15px;
border:red solid 1px;
height:170px;
}
#logtable {
width: 400px;
height:150px;
float: left;
border: blue dashed 1px;
}
#divMessage {
width: 350px;
height:150px;
right:-5px;
top: -20px;
border: green dashed 1px;
float:right;
}
here is a smaple
In simple we can do like this:
table#logtable, div.divMessage{
display:inline-block;
}
Or
table#logtable, div.divMessage{
float:left;
width:50%;
}

Can't center div in another div

I'm trying to make a menu bar centered horizontally in the header of my page. For some reason, i can't get the centering to work. I made a little test page roughly displaying the problem: JSFiddle. The inner div has to be 5px away from the bottom, that's whatI use the position: absolute for.
I've tried searching on the web alot, but everything I find gives me the same result, or none at all. Most problems I found were when text-align: center wasn't in the container div, but even with it, it still doesn't work.
I removed two css attributes and it work.
position: absolute;
bottom: 5px;
Check this Fiddle
5px from bottom. Fiddle
This is not a perfect way, but it's still kind of useful. I first think of this idea from this Q&A.
You'll have to make some change to your HTML:
<div id="container">
<div id="wrapper-center"> <!-- added a new DIV layer -->
<div id="inner_container">
TEXT ELEMETNES IN THIS THING!!!!
</div>
</div>
</div>
And the CSS will change to:
#container {
background: black;
width: 100%;
height: 160px;
position: relative;
}
#inner_container {
display: inline-block;
width: auto;
color: white;
background-color: #808080;
padding: 5px;
position: relative;
left:-50%;
}
#wrapper-center {
position:absolute;
left:50%;
bottom:5px;
width:auto;
}
Demo fiddle
The trick is to place the wrapper at the given top-bottom position, and 50% from left (related to parent), and then make the true content 50% to left (related to the wrapper), thus making it center.
But the pitfall is, the wrapper will only be half the parent container's width, and thus the content: in case of narrow screen or long content, it will wrap before it "stretch width enough".
If you want to centre something, you typically provide a width and then make the margins either side half of the total space remaining. So if your inner div is 70% of your outer div you set left and right margins to 15% each. Note that margin:auto will do this for you automatically. Your text will still appear to one side though as it is left-aligned. Fix this with text-align: centre.
PS: you really don't need to use position absolute to centre something like this, in fact it just makes things more difficult and less flexible.
* {
margin: 0;
padding: 0;
}
#container {
background: black;
width: 100%;
height: 160px;
}
#inner_container {
color:red;
height:50px;
width: 70%;
margin:auto;
text-align:center;
}
If you don't want a fixed width on the inner div, you could do something like this
#outer {
width: 100%;
text-align: center;
}
#inner {
display: inline-block;
}
That makes the inner div to an inline element, that can be centered with text-align.
working Ex
this CSS changes will work :
#container {
background: black;
width: 100%;
height: 160px;
line-height: 160px;
text-align: center;
}
#inner_container {
display: inline;
margin: 0 auto;
width: auto;
color: white;
background-color: #808080;
padding: 5px;
bottom: 5px;
}
Try this:
html
<div id="outer"><div id="inner">inner</div></div>
css
#outer {
background: black;
width: 100%;
height: 160px;
line-height: 160px;
text-align: center;
}
#inner{
display: inline;
width: auto;
color: white;
background-color: #808080;
padding: 5px;
bottom: 5px;
}
example jsfiddle
You may set the inline style for the inner div.
HTML:
<div id="container">
<div align="center" id="inner_container" style="text-align: center; position:absolute;color: white;width:100%; bottom:5px;">
<div style="display: inline-block;text-align: center;">TEXT ELEMETNES IN THIS THING!!!!</div>
</div>
</div>
Here is working DEMO

CSS negative margins, what am I doing wrong?

I'm trying to achieve a 1 column flexible / 1 column fixed layout. 'col-a' should be flexible, taking up 100% - 110px, 'col-b' should be fixed and aligned right.
I' trying to use negative margins but having little luck.
<div class="cont">
<div class="col-a">
Be flexible
</div>
<div class="col-b">
Be fixed
</div>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
.cont {
background-color: #00f;
padding: 10px;
overflow:hidden;
}
.col-a {
background-color: #0ff;
padding-right: 110px;
margin-right: -110px;
float: left;
}
.col-b {
background-color: #ff0;
width: 110px;
float: left;
}
Can it be done using just this mark-up?
/*Answer found */
Here is the solution
.cont {
background-color: #00f;
overflow:hidden;
padding: 10px;
}
.col-a {
width: 100%;
background-color: #0ff;
margin-right: -110px;
float: left;
}
.col-b {
background-color: #ff0;
width: 110px;
float: right;
}
I wouldn't use a negative margin for this.
This is how I would set it up.
Set your column parent container to position relative.
Set your column A to have a padding-right of 110px (to make space for Column B)
Set your column B to be absolutely positioned to the top, right with a fixed width of 110px.
This will allow your Column A to expand 100% horizontally, while leaving space on the right for Column B.
Here's an example of what I outlined above: http://jsfiddle.net/NPn8d/
How about something like this, then.
<style type="text/css">
.cont{position:relative;}
.col-a{
border:1px solid #0000ff;
width:auto;
margin:0,110,0,0;
}
.col-b{
border:1px solid #ff0000;
width:110px;
float:right;
top:0;
position:absolute;
margin:0,0,0,-110
}
</style>
<div class="cont">
<div class="col-a">Be flexible</div>
<div class="col-b">Be fixed</div>
</div>

two divs top and bottom

div alignment one left next two top bottom last one right
it is nt coming like that when I'm doing
see this image
I would like to align the image like that with div tag, unfortunately when i aligned its not coming up like that,
how do i leyout all the images inside one div tag>?
here is my html code
<div class="site_contents">
<div class="header">
<div class="big_logo"></div>
<div class="work_nav"></div>
<div class="testimonial"></div>
<div class="cliants"></div>
<div class="testimonial"></div>
<div class="contact"></div>
</div>
</div
here is my css code
.site_contents {
height:auto;
width: 900px;
background-color: #666;
margin:0 auto;
}
.header {
background-color: #3CF;
height: 262px;
width:100%;
clear:both;
position:relative;
border:2px solid #000;
}
.header div
{
float: left;
}
.big_logo{
background-color: #06C;
height: 262px;
width: 459px;
background: url(images/sitetemplate_header.gif) 0 -21px;
}
.work_nav {
background-color: #F00;
height: 159px;
width: 170px;
}
.testimonial {
background-color: #3F9;
height: 104px;
width: 170px;
}
.cliants {
background-color: #09C;
height: 262px;
width: 171px;
}
.contact {
background-color: #30C;
height: 262px;
width: 101px;
}
could any one help me please
This is almost what you want. http://jsfiddle.net/
You need to be careful about a number of things.
work_nav and testimonial need to be in a separate div which I have included (container2)
The total width needs to be adjusted. I have changed it as well. You can play with it to make it according to what you need.
I have included borders as well to recognize each box. You should remove those borders and the width taken by the borders must be subtracted from the total current width. That means adjust the current width again.