CSS negative margins, what am I doing wrong? - html

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>

Related

Div breaking the size of the div beside

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

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

Fluid layout with optional sideboxes

I want a layout with three boxes (two optional) like this:
[side box 1] [ main content
[side box 2] . main content ]
or
[ main content spans 100% if side boxes aren't provided ]
I want the main content box to span the entire height and width available in #load (minus margins) except if the side boxes are there, then I want it to only span up until those boxes (and their right margin).
My CSS:
#load {
margin: 10px;
height: 100%;
min-width: 1080px;
}
#primary,#secondaryOne,#secondaryTwo {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #ccc;
}
#primary {
float: right;
height: inherit;
width: 75%;
height:500px;
background:red;
}
#secondaryOne,#secondaryTwo {
min-width: 250px;
max-width: 300px;
height: 220px;
margin-right: 10px;
width: 20%;
clear:left;
float:left;
}
#secondaryTwo {
margin-top: 10px;
}
Simple HTML
<div id='load'>
<div id='primary'></div>
<div id='secondaryOne'></div>
<div id='secondaryTwo'></div>
</div>
JSFiddle
Problems
*SOLVED*Making #primary span the entire width if the sideboxes are missing.
*SOLVED*Is there a way to line the two sideboxes (#secondaryOne,#secondaryTwo) on the left side of #primary without nesting them in a separate div? If I use float: left on them, they line side by side, if I don't float them, the #primary generates below them, not beside them.
Solutions
Problem #1 was solved by joeytje50 using the secondary + primary tags and placing the secondary side boxes before the primary in HTML.
Problem #2 was solved in more than one way. The way I chose so that the secondary tags were placed together and before the primary was by NoobEditor using clear: left and a negative margin-top.
The solution can be found at: http://jsfiddle.net/v4cvv/67/
The main part of the solution is:
#primary {
width: 100%;
}
#secondaryOne + #primary, #secondaryTwo + #primary {
margin-top: -221px;
width: 75%;
}
Alternate Solution
One problem I found with the above solution, is it requires the two boxes and them to be the same height. A solution around this is by grouping the boxes in their own div. This solution is:
HTML
<div id='load'>
<div id="sideboxes">
<div id="boxOne" class="box"></div>
<div id="boxTwo" class="box"></div>
<div id="boxThree" class="box"></div>
</div>
<div id="primary" class="box"></div>
</div>
CSS
.box {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #fff;
}
#primary {
float: right;
display:block;
height: 97%;
width: 100%;
}
#sideboxes + #primary {
width: 75%;
}
#sideboxes {
float: left;
height: 97%;
width: 23%;
margin-right: 10px;
}
#sideboxes .box {
float: left;
height: 220px;
margin-bottom: 10px;
width: 100%;
}
The alternate solution no longer requires clear and can be extended for other uses. You may now also have 1, 2, 3, or however many boxes you want in the sideboxes div.
Thanks all for their help.
To answer your question about the primary box being 100% width when the secondary boxes are not there, you could do the following:
#primary {width:100%;}
.secondary + #primary {width:75%;}
If you put that CSS code at the bottom of your stylesheet, and then put the primary div tag after your first secondary div tag, then it'll by default be 100% wide, unless there's an element that has class="secondary". This won't change anything about the position the div is rendered, but it will fix your problem.
Alternatively, if your secondary divs are possibly hidden instead of not being there, you could do this:
#primary, .secondary.hidden + #primary {width:100%;}
.secondary + #primary {width:75%;}
That is, assuming you hide the secondary tabs via a class such as .hidden.
Here is a working version that becomes 100% width when the secondaries are removed, but still is 75% width when there is a .secondary element before it.
Keeping your HTML markup smae, here is the solution for your problem : demo
CSS
html.body {
height:100%;
}
#load {
margin: 10px;
height: 100%;
width:100%;
}
#primary, #secondaryOne, #secondaryTwo {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #ccc;
}
#primary {
float: right;
display:block;
height: 100%;
max-width:100%;;
width: 68%;
margin-top:-70%; /* this is the key */
}
#secondaryOne, #secondaryTwo {
width:30%;
height: 220px;
margin-right: 10px;
}
#secondaryTwo {
margin-top: 10px;
}
Problem #1
To get your #primary div to adapt it's width, you can use jquery to verify the presence of the .secondary divs and to set the an other width to the #primary div.
With .secondary demo
without .secondary demo
JQUERY:
if ($('.secondary').length){
$('#primary').css('width', '75%');
}
Problem #2
You can use clear:left; and by changing the order of the divs in your html markup you will have your 2 divs stacked on the left and your content div on the right.
FIDDLE
HTML:
<div id='load'>
<div id='primary'></div>
<div id='secondaryOne' class="secondary"></div>
<div id='secondaryTwo' class="secondary"></div>
</div>
CSS :
.secondary{
clear:left;
float:left;
}
Try
#primary {
min-width:75%;
max-width:100%;
}
http://jsfiddle.net/Paramasivan/v4cvv/65/

Bottom Padding Approach Does not work for Equal Height Div

I want to make two divs equal heights – left and right divs.
I referred the following posts and found a bottom padding approach.
How do I achieve equal height divs (positioned side by side) with HTML / CSS ?
CSS: How to make left float div to adjust height dynamically?
I tried to apply this concept in my page; but it doesn’t work correctly. On top of the right div there is unwanted space. How can we rectify it?
CODE
<!DOCTYPE html>
<style type="text/css">
.myContent {
width: 100%;
border: 2px solid violet;
min-width: 1210px;
}
.myHeader {
width: 100%;
/*width: 1200px;*/
clear: both;
background-color: #DFE8EF;
border: 1px solid red;
}
.leftPart {
border: 1px solid red;
width: 200px;
height: 200px;
float: left;
background-color: silver;
}
.rightPart {
border: 1px solid orange;
background-color: beige;
float: left;
min-width: 1000px;
/*
margin-bottom: -1000px;
padding-bottom: 1000px;
margin-right: -5000px;
padding-right: 5000px;
*/
}
</style>
<html>
<head>
<title>UpdateAccrualByItem</title>
<link href="Content/MasterLayoutStyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="body">
<div class="myContent">
<div class="myHeader">
<img src="/Images/logo_header.jpg" />
</div>
<div class="leftPart">
Menu
</div>
<div class="rightPart">
<h2>UpdateAccrualByItem</h2>
</div>
</div>
</div>
</body>
</html>
You are very close, but just have a few little things wrong.
You don't need a width for the right column, just the default width:auto. I used the same negative margin and padding trick to make the right column's height the size of the left's height and also to give the right column the illusion of taking up the rest of the space. You also should float the right container and take away the margin. You can remove the clear:both of the left column because it's not used
Demo here
.leftPart {
border: 1px solid blue;
width: 200px;
height:200px;
float:left;
background-color: orange;
}
.rightPart {
border: 1px solid orange;
background-color: beige;
float:left;
margin-bottom: -1000px;
padding-bottom: 1000px;
margin-right: -5000px;
padding-right: 5000px;
}
Edit
You might also add some type of #media query to allow adjusting the window to look more smooth. Here is an example. It's semi-hard coded based on the text length in the example, but on your final product it might be something you add on at the end

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.