Div alignment not working properly - css

css
#content2
{
clear:both;
width:1024px;
height:auto;
position:relative;
}
#content2 div:first-child
{
background:#E4ECF7;
width:445px;
height:25px;
margin:15px 0px 0px 223px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border:1px solid #E0DCD1;
padding:5px 0px 0px 5px;
position:absolute;
}
#content2 div:last-child
{
width:1024px;
height:200px;
position:absolute;
border:1px solid #E0DCD1;
clear:both;
}
Html
<div id="content2">
<div>content</div>
<div>content</div>
</div>
Result
div1 is showing inside div2
I need
div1 then
div2
Please help me.

why using position absolute,no need for clear property for last-child
#content2
{
clear:both;
width:1024px;
height:auto;
position:relative;
}
#content2 div:first-child
{
background:#E4ECF7;
width:445px;
height:25px;
margin:15px 0px 0px 223px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border:1px solid #E0DCD1;
padding:5px 0px 0px 5px;
/*position:absolute;*/
}
#content2 div:last-child
{
width:1024px;
height:200px;
/*position:absolute;*/
border:1px solid #E0DCD1;
/*clear:both;*/
}
div are cleared by default unless after using float property

remove position absolute from both child divs...

If you are using Position: absolute; specify the margin for both div's.
eg:
#content2 div:last-child
{
width:1024px;
height:200px;
position:absolute;
margin-top: xxx; /* specify the top margin */
border:1px solid #E0DCD1;
clear:both;
}

i think you are looking like this :- http://tinkerbin.com/3qRLgscO
Actually you made CSS bit of complicated for yourself.You can get your desired results through very simple CSS without using of positioning.
And if we are using float than we should use the clear for clearing the floated div's otherwise no need to use the clear property.
Here is the simple code of yours i have some simple changes in your CSS.....
HTML
<div id="content2">
<div>div1</div>
<div>div2</div>
</div>
CSS
#content2 {
background: none repeat scroll 0 0 red;
height: 200px;
width: 1024px;
}
#content2 div:first-child {
background: none repeat scroll 0 0 #E4ECF7;
height: 45px;
}
#content2 div:last-child {
background: none repeat scroll 0 0 yellow;
border: 1px solid #E0DCD1;
height: 45px;
}
I hope this will help you........

you can use the following properties in your div class.
float:left
clear:right;

Related

DIV inline-block + width 100%

I can't display a few DIV's in one line. display: inline-block and float: left doesn't work. My site width is not fixed so I want it to be dynamic to fit any width of screen.
HTML:
<div id="all">
<div id="a">25px</div>
<div id="b">200px</div>
<div id="c">
<div id="c1">100%</div>
<div id="c2">100%</div>
<div id="c3">100%</div>
</div>
500px
</div>
CSS:
DIV {
margin:5px;
font-size:10px;
}
DIV#all {
width:500px;
border:1px dotted black;
}
DIV#a {
display:inline-block;
width:25px;
height:200px;
border:1px solid red;
color:red;
}
DIV#b {
display:inline-block;
width:150px;
height:200px;
border:1px solid green;
color:green;
}
DIV#c {
display:inline-block;
width:auto;
height:200px;
border:1px solid blue;
color:blue;
}
DIV#c1 {
width:auto;
border:1px dotted blue;
color:blue;
}
DIV#c2 {
width:auto;
border:1px dotted blue;
}
DIV#c3 {
width:auto;
border:1px dotted blue;
color:blue;
}​
Live Demos:
PROBLEM: http://jsfiddle.net/BC2d9/
RESOLVED: http://jsfiddle.net/RAds3/ (display:table)
​
The problem with your current attempt is the width: 100%; on the third column div#c. 100% here will be 100% of its parent - which contains all three columns. Depending on what level of flexibility you want you have a few options.
If the site width is fixed, set a fixed width for the third column.
If you want the third column to stretch to its content, set max-width.
If you want the third column to stretch to fill its parent, you're probably better off with (css) tables.
Check out http://somacss.com/cols.html for a great resource on css column layout.
Problem is with third column. You can't set width to 100%. Also, you need float: left;. Here is fixed code:
<div id="all">
<div id="a">25px</div>
<div id="b">200px</div>
<div id="c">
<div id="c1">100%</div>
<div id="c2">100%</div>
<div id="c3">100%</div>
</div>
<div style="clear:both;"></div>
500px
</div>
and CSS:
DIV {
margin:5px;
font-size:10px;
}
DIV#all {
width:500px;
border:1px dotted black;
}
DIV#a {
float: left;
width:25px;
height:200px;
border:1px solid red;
color:red;
}
DIV#b {
float: left;
width:200px;
height:200px;
border:1px solid green;
color:green;
}
DIV#c {
float: left;
width:210px;
min-height:190px;
border:1px solid blue;
color:blue;
padding: 5px;
}
DIV#c1 {
width:100%;
border:1px dotted blue;
color:blue;
margin: 0 0 5px 0;
}
DIV#c2 {
width:100%;
border:1px dotted blue;
margin: 0 0 5px 0;
}
DIV#c3 {
width:100%;
border:1px dotted blue;
color:blue;
margin: 0 0 5px 0;
}​
And also LIVE DEMO
If your site width is fixed, then just replace 100% with all remained width in the container.Example: jsFiddle
If you want it to be dynamic and to fit any width of screen, I think it's not possible with pure CSS. I made it with jQuery:
var containerWidth = $('#all').outerWidth();
var widthLeft = $('#a').outerWidth(true) + $('#b').outerWidth(true);
var widthRight = containerWidth - widthLeft - 20; // 20px = spacing between elements
$('#c').css('width', widthRight+ 'px');
$('#c1, #c2, #c3').css('width', widthRight-10+ 'px'); // 10 = padding on the right side
Modified CSS:
DIV#c {
display:inline-block;
height:200px;
border:1px solid blue;
color:blue;
float: right;
}
DIV#c1 {
border:1px dotted blue;
color:blue;
}
DIV#c2 {
border:1px dotted blue;
}
DIV#c3 {
border:1px dotted blue;
color:blue;
}
Removed width: 100% and set float:right to #c.
Live demo: jsFiddle
Check out this update. I hope is good enough :)
DIV {
margin:5px;
font-size:10px;
}
DIV#all {
width:500px;
border:1px dotted black;
}
DIV#a {
display:inline-block;
width:25px;
height:200px;
border:1px solid red;
color:red;
float:left;
}
DIV#b {
display:inline-block;
width:150px;
height:200px;
border:1px solid green;
color:green;
float:left;
}
DIV#c {
display:inline-block;
width:277px;
height:200px;
border:1px solid blue;
padding:0 7px 0 5px;
color:blue;
float:left;
}
DIV#c1 {
width:100%;
margin:5px 0;
border:1px dotted blue;
color:blue;
}
DIV#c2 {
width:100%;
margin:5px 0;
border:1px dotted blue;
}
DIV#c3 {
width:100%;
margin:5px 0;
border:1px dotted blue;
color:blue;
}
div { float:left; width:10px; height:10px; }
Helps?

CSS horizontal centered line

How can I achieve this view with CSS:
------------------TITLE
or
TITLE------------------
I have
<div id="titleBlock">
<div id="title">Some text</div>
<div id="titleLine"></div>
</div>
And my styles are:
#titleLine {
border-top: 1px solid black;
width: 84%;
clear: both;
height: 20px;
}
#title {
height: 10px;
float: right;
}
My approach is here: jsFiddle
However the line width is defined with percents and I need it adjust automatically with CSS.
This may be what you are after: http://jsfiddle.net/XpSWX/1/
Hope this helps
<div id="titleBlock">
<div id="title">Some text</div>
<div id="titleLine"></div>
</div>​
#titleLine {
border-top: 1px solid black;
width: 84%;
float:left;
height: 20px;
margin-top:8px;
}
#title {
height: 10px;
float: right;
}​
http://jsfiddle.net/sY2SV/1
<div id="titleBlock">
<div id="title">Some text</div>
<div id="titleLine"></div>
</div>​
#titleLine {
border-top: 1px solid black;
width: 84%;
float:right;
height: 20px;
margin-top:8px;
}
#title {
height: 10px;
float: left;
}​
http://jsfiddle.net/sY2SV/2
Here is a solution:
#titleBlock {
width:100%;
}
#titleLine {
background:black;
position:absolute;
z-index:1;
left:0px;
top:14px;
width:100%;
height: 1px;
}
#title {
display:inline-block;
padding:4px;
background:white;
position:relative;
z-index:2;
/* Only variable to change... Just say left and it woulb be title------- */
float:right;
}​​​​
DEMO
Hey now you can used this
HTML
<div class="hello"><span>Hello i m sony</span></div>
Css
.hello{
background:green;
text-align:left;
position:relative;
}
.hello span{
padding-right:10px;
background:green;
display:inline-block;
position:relative;
z-index:1
}
.hello:after{
content:'';
border-top:solid 5px red;
position:absolute;
right:0;
left:0;
top:7px
}
Live demo
http://tinkerbin.com/1guJzKcI
Check my answer in Horizontal Line in Background using Css3
You can do it with a 1% gradient like this
.datedAside {
background: linear-gradient(0deg, transparent 49%, #000 50%, transparent 51%);
}
.datedAside span{
background: #FFF;
padding: 0 0.5rem;
}
You'll nedd the extra span to be the same background color as the background of the component to make it look like it has "deleted" the line going behind the text.
For text, it's best to use text-align

After clearing float, text in next div is pushed to the left

I am using CSS to float a div next to another one. This div only appears if the user is looking at their own "business." When I don't clear anything, a large space appears between these divs and the next one. When I do clear the float, the text in the next div is pushed to the left. I think I am misunderstanding something about how to use the float and clear. I'm not very good with CSS.
How can I remove the space without destroying the "fs" div?
Here are pictures to show what is happening:
Here's the CSS and HTML code:
div.stuff {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
margin:10px;
width:35%;
height:65px;
border-radius: 5px;
}
div.container {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
top:-97px;
right:10px;
border-radius: 5px;
overflow: hidden;
float:right;
clear:right;
}
div.fs {
border-style:double;
text-align:center;
padding:10px;
margin:10px;
margin-left:20%;
width:60%;
border-radius: 5px;
}
<div class=stuff>
<img src=/economy/images/cash.png> Cash on Hand: 10,245<br>
<img src=/economy/images/worker.png> Workers Employed: 6<br>
<img src=/economy/images/machine.png> Machines Leased: 4
</div>
<div class=container>
Click Here to Manage Cash on Hand.<br>
Click Here to Manage this Business.<br>
Click Here to Disband this Business.
</div>
<br>
<div class=fs><a href=/economy.php?section=fs&id=7>Historical Financial Statements</a></div>
You need to float your left hand div, and use clear:both on the div at the bottom. I've made some changes in this jsFiddle.
perhaps this:
div.container {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
/*top:-97px;*/
margin-top:-97;
right:10px;
border-radius: 5px;
overflow: hidden;
float:right;
/*clear:right;*/
}
I would float your div.stuff to the left and your div.container to the right and just use clear: both on the div.fs element. I made a small fiddle to illustrate this. In this fiddle I added a wrapper class for clarity where I set a min-width to prevent that the right div floats down one line when the browser window is resized. Try it out!
Here's the CSS:
div.stuff {
border: 1px dotted black;
padding:10px;
margin:10px;
width:35%;
height:65px;
border-radius: 5px;
float: left;
}
div.container {
border: 1px dotted black;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
margin: 10px;
border-radius: 5px;
overflow: hidden;
float:right;
}
div.fs {
clear: both;
border-style:double;
text-align:center;
padding:10px;
margin:10px;
margin-left:20%;
width:60%;
border-radius: 5px;
}​

IE7 Bug: float:right width 100% rather than element sizes

I am trying to something pretty simple.
I have two buttons inside a div. One needs to be float right, one needs to be float left
<div class="btnwrapper">
<div class="btnright"><span>Continue</span></div>
<div class="btnleft"><span>Back</span></div>
<div style="clear:both;"></div>
</div>
And the corresponding CSS
.calculator .btnwrapper { width:607px; }
.calculator .btnleft { float:left; border:1px solid green; }
.calculator .btnright { float:right; border:1px solid red; }
a.button { background:url(../images/bg-button-left.gif) no-repeat; float:right; height:29px; width:auto; padding:0 0 0 8px; display:block; color:#FFF; text-decoration:none; font-weight:bold; font-size:13px; cursor:pointer;}
a.button span { background:url(../images/bg-button-right.gif) top right; height:16px; padding:8px 8px 5px 0px; display:block; width:auto; cursor:pointer; }
Here is the results I'm getting in IE7. All other modern browsers handle this correctly.
Remove float: right from a.button.
With it: http://jsfiddle.net/K8XQr/
Without it: http://jsfiddle.net/K8XQr/1/
They look identical, except that losing float: right fixes it in IE7.

How can I center the contents of a div that float: left?

I found this article and I decided that I liked the way they styled links and buttons.
So I took the CSS from the article...
.buttons a, .buttons button{
display:block;
float:left;
margin:0 7px 0 0;
background-color:#f5f5f5;
border:1px solid #dedede;
border-top:1px solid #eee;
border-left:1px solid #eee;
font-family:"Lucida Grande", Tahoma, Arial, Verdana, sans-serif;
font-size:100%;
line-height:130%;
text-decoration:none;
font-weight:bold;
color:#565656;
cursor:pointer;
padding:5px 10px 6px 7px; /* Links */
}
.buttons button{
width:auto;
overflow:visible;
padding:4px 10px 3px 7px; /* IE6 */
}
.buttons button[type]{
padding:5px 10px 5px 7px; /* Firefox */
line-height:17px; /* Safari */
}
*:first-child+html button[type]{
padding:4px 10px 3px 7px; /* IE7 */
}
.buttons button img, .buttons a img{
margin:0 3px -3px 0 !important;
padding:0;
border:none;
width:16px;
height:16px;
}
Then I have several buttons in a row I want to use like this...
<div class="buttons">
<img src="pict1.png" class="positive" alt=""/>Button 1
<img src="pict2.png" alt=""/>Button 2
<img src="pict3.png" class="negative" alt=""/>Button 3
</div>
See this example: http://reljac.com/csstest.php
But that row of buttons may need to be aligned center, not all to the right or left. If I change the CSS to...
.buttons a, .buttons button{
/*display:block;
float:left;*/
margin:0 7px 0 0;
The buttons no longer appear correctly when there is an image, specifically in IE 6,7 & 8.
See this example: http://reljac.com/csstest_wo.php
I can change the float to right to get the buttons to align right but I can't figure out what to do to get them centered (like in a <td></td>).
So the short of it is I want to use the style as it is but I also need to be able to center justify the buttons if necessary.
Try adding this to the CSS:
.buttons
{
text-align:center;
margin: 0px auto 0px auto;
}
The auto makes the margins equal on each side. The text-align is a bodge for older browsers.
EDIT:
Add an extra div around the buttons called buttonwrapper. then apply this CSS
.buttonwrapper
{
position:relative;
float:left;
left:50%;
}
.buttons
{
position:relative;
float:left;
left:-50%;
}
Method taken (but not tested) from http://www.pmob.co.uk/temp/centred-float4.htm
I found that when you have a parent div float left and a child div you're trying to center, a lot of times people forget inheritance.
HTML:
<div class="parent">
<div class="child">
Text
</div>
</div>
CSS:
.parent {
float: left;
width: 300px;
}
.child {
float: none; /* <-- IMPORTANT! */
width: 200px;
margin-left: auto;
margin-right: auto;
}
IMPORTANT :
Inheritance states that the child div is also floated left. So, make sure that the child div has no float, then margin: auto like usual.

Resources