I have two div inside a container div: JS Fiddle Link
<div id="container">
<div class="leftContainer">
Left
</div>
<div class="rightContainer">
<h3>Right</h3>
</div>
</div>
But I am not sure why they do not align both to top or anywhere with same alignment with this CSS:
div#container{vertical-align:top;background:#e4e4e4}
div#container div{
display:inline-block;
width:200px;
height:50px;
}
div#container div.leftContainer{background:#999}
div#container div.rightContainer{background:#555}
add vertical-align:top; for the child of #container div
div#container div{
vertical-align:top;
}
You have Right inside a <h3> tag, which is adding margin to the tag (which in turn pushes the Right heading down slightly)
Either set both Left and Right to be inside header tags, or remove the one for Right, and they will display the same.
<div id="container">
<div class="leftContainer">
Left
</div>
<div class="rightContainer">
Right
</div>
</div>
And your CSS can be left as it is
div#container{vertical-align:top;background:#e4e4e4}
div#container div{
display:inline-block;
width:200px;
height:50px;
}
div#container div.leftContainer{background:#999}
div#container div.rightContainer{background:#555}
Set float left in the first div to force the align and remove the hr or add the hr to both
div#container{vertical-align:top;background:#e4e4e4}
div#container div{
display:inline-block;
width:200px;
height:50px;
}
div#container div.leftContainer{background:#999; float:left;}
div#container div.rightContainer{background:#555}
<div id="container">
<div class="leftContainer">
Left
</div>
<div class="rightContainer">
Right
</div>
</div>
Just add css:
div#container { float:left;}
div#container div{
float: left;/*remove inline block and add float left to align your div*/
width:200px;
height:50px;
}
SEE DEMO:http://jsfiddle.net/JentiDabhi/x21nv0gm/
if you're able to use external frameworks I'd suggest you using Twitter Bootstrap and use its grid system to align stuff simply by doing somenthing like:
<div class="container">
<div class="row">
<div class="specify column sizes">
Left
</div>
<div class="specify column sizes">
<h3>Right</h3>
</div>
</div>
</div>
Using Bootstrap and specifying your choosen column sizes (col-sm-N, col-md-N, col-xs-N, col-lg-N) instead of using a fixed 200px width allows you to make your page change it's behaviour on different display sizes using only CSS
To retain the display inline-block property, without using floats - you could turn the h3 into an inline element by adding the below CSS:
h3 {display:inline;}
Add vertical-align:top; for the child of #container div
div#container div{
vertical-align:top;
}
And to retain the display inline-block property, without using floats - you could turn the h3 into an inline element by adding the below CSS:
h3 {display:inline;}
Related
I have two div and they have some width. But I want each div to take full space as it's given in width property regardless of the content inside div s. Currently each div is taking space with respect to the content inside them and ignoring what is given in width property.
My HTML is given below,
<div style="margin-left:100px; display:inline; border-width:10px; width:30%; border- style:solid;">
fawad
</div>
<div style=" margin-left:10px; display:inline; border-width:10px; width:30%; border-style:solid">ali</div>
Just change your
display:inline;
to:
display: inline-block;
http://jsfiddle.net/osx76nbr/
You can use css property display:inline-block;.That is help to set div inline with block.
You need to apply a display block style. In this case I would apply
<div style="margin-left:100px; display:inline-block; border-width:10px; width:30%; border- style:solid;">
fawad
</div>
<div style=" margin-left:10px; display:inline-block; border-width:10px; width:30%; border-style:solid">ali</div>
Alternatively I would apply all of these settings to a style sheet and apply a class to the div instead of having inline styles like this.
.inline-div {
display: inline-block;
border: 10px solid;
width: 30%;
}
<div class="inline-div"></div>
Even better is I would look into a grid system much like the ones found in bootstrap or foundation. That way you can just apply the grid styles.
<div class="row">
<div class="col-4">
<div class="col-4">
<div class="col-4">
</div>
Only thing that was needed
display:inline-block;
That's it
Try adding
div { float:left }
That will make the divs respect the with property.
If I am having two divs in a parent div and and all three divs have float left. What I have to do is to align center the two child divs without removing float left.
Use display: inline-block instead of float: left on child divs and you can give text-align: center to the parent div.
Let me see if i have understood,so you have something like this:
<div id="parent" style="float:left;">
<div id="child1" style="float:left;"></div>
<div id="child2" style="float:left;"></div>
</div>
And if im right you want the two div childs to be aligned in the center of the parent div but without removing the left float of none of them.Then i think this might work:
<div id="parent" style="float:left;position:absolute;">
<div id="child1" style="float:left; position:relative; left: 100px"></div>
<div id="child2" style="float:left; position:relative; left: 100px"></div>
</div>
So in the div style,try to center it by assigning a value in percentage or pixels to left:
It should work.I also advice you to use percentage,but first use pixels to understand how it works.And another advice is to not use css in html tags,i just showed you what to do,but it's recommended to have another file (style.css) to include in your html file.
This layout may help you:
HTML
<div class="div-p">
<div class="div-1"></div>
<div class="div-2"></div>
</div>
CSS - DEMO
.div-p{background-color:#eee;width:640px;height:400px;float:left;}
.div-1{background-color:#f00;width:300px;height:300px;float:left;margin:10px;}
.div-2{background-color:#0f0;width:300px;height:300px;float:left;margin:10px;}
If you want to center the parent div then use margin:0 auto; and remove the float:left;
CSS - DEMO
.div-p{background-color:#eee;width:640px;height:400px;margin:0 auto;}
.div-1{background-color:#f00;width:300px;height:300px;float:left;margin:10px;}
.div-2{background-color:#0f0;width:300px;height:300px;float:left;margin:10px;}
#parent{width:100%;}
#child1,#child2{width:90%;margin:0px 5%;}
set your child width in percentage with margin.
it will align both child div in center
you need set these styles for 2 child divs :
{ width:50%; text-align:center; float:left; height:50px;}
and the parent div :
{ width:100%; margin: 0 auto; float:left; height:50px;}
Note: height style is optional.
Background should shows in all divs but it's not visible.
http://jsfiddle.net/uxUqJ/1/
I need to my backgroud be shows in all divs. Background is defining in styles container div. Etc.
<div id="container">
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
</div>
How can I fix this?
I think you need to add overflow:auto; to your css... I've also removed the quotes
#content{
width:1049px;
overflow:auto;
background: #000 url(http://s1.wp.com/wp-content/themes/pub/dark-wood/images/postflowerback.png) ;
}
the Div currently has no height... however what do you want to do with the background regards repeating or position, here's a good reference point https://developer.mozilla.org/en/CSS/background
You must specify height property and set repeating of background.
#content{
width:349px;
height: 700px;
...
}
Alternately, you could add <div style="clear:both;"></div> after the closing for text3. This would then stretch the container divs around the elements even though they are floated.
You have your elements floated. You need to clear the float before closing the wrapper element in order to have the #content to grow normally:
See this working Fiddle Example!
CHANGE YOUR MARKUP TO THIS:
<div id="content">
<div id="text">
<div id="text_1">...</div>
<div id="text_2">...</div>
<div id="text_3">...</div>
<div style="clear:both;"></div> <!-- Add this line -->
</div>
</div>
Additionally, you can remove the inline style and use a class.
Change your css to smart way
<style>#content{ display:table;
width:1049px;
background: url("image_src") ;
}
#text{
display:table-row;
width:1049px;
padding-left:27px;
padding-right:28px;}
#text_1,#text_2,#text_3{ display:table-cell;
padding-left: 40px;
width: 278px;
}#text_3{
padding-right:40px;
}
</style>
Clear the #text div.
add the following styles:
#text {
zoom:1; /* For IE 6/7 (trigger hasLayout) */
}
#text:after {
content:"";
display:table;
clear: both;
}
DEMO:
http://jsfiddle.net/uxUqJ/13/
jsFiddle
I am trying to get these 4 divs to sit on the same vertical line. Why does the presence of buttons change the position of the first div?
HTML
<div class="box">
<button>Y</button> <button>N</button>
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
CSS
.box {
width:200px;
height:200px;
padding:10px;
display:inline-block;
border:1px solid black;
text-align:center;
}
RESULT
Add vertical-align:bottom to the style of your divs.
The cause of your problem is the presence of any content, as it defines the baseline for the div.
Adjusting the css like this works
.box {
width:200px;
height:200px;
padding:10px;
border:1px solid black;
text-align:center;
}
div.box{display:inline;float:left;}
jsFiddle solution
Just change the display property of the div's and they'll align vertically:
display: block
Edit: #ben said he wanted the div's aligned vertically. Vote me down if you must, but it's not my fault if he didn't know the difference between horizontal and vertical.
I'm working on a website that uses two columns inside a container. The container has a white background that should stretch to the bottom of whichever column is highest, so I'm using the holy grail method for that.
However, both columns should positioned so that a part of it exceeds the white background (this example uses a fixed height, which should be fluid). As far as I know, this can only be done by setting the overflow to visible but this break the equal height of the columns.
How do I fix this with as little additional elements as possible?
The easiest fix in this case seems to be adding <br style="clear:both" /> before the closing tag for #container.
You can change it to <br class="clearfix" /> and .clearfix{clear:both} if you wish.
Solution is to use inline-block elements..
Css
.container{
width:300px;
background-color:#ccc;
margin:0 auto;
border:1px solid red;
}
.container > div{
width:150px;
display:inline-block;
vertical-align:top;
}
.inner{
background-color:#666;
margin-top:10px;
width:130px;
}
.left .inner{
margin-left:-10px;
}
.right .inner{
margin-right:-10px;
margin-left:auto;
}
HTML
<div class="container">
<div class="left">
<div class="inner">left 1st inner panel</div>
<div class="inner">left 2nd inner panel</div>
</div><div class="right">
<div class="inner">right 1st inner panel</div>
<div class="inner">right 2nd inner panel with arbitrary text to show the increase in parent elements</div>
</div>
</div>
view demo