I want to align two elements,in the same line,like this: click here
full code
<div id="element1"> element 1 markup </div>
<div id="element2"> element 2 markup </div>
css
#element1 {
display:inline-block;
margin-right:10px;
width:200px;
background-color:red;
}
#element2 {
display:inline-block;
width:200px;
background-color:red;
}
Also,without overlapping each other.For example if a have a parent div,and two child divs.
The parent div,have 1000px width,and the childs have 500px each,so they fit.
But if i resize the first div to 600px,i want the second one to auto resize,and keep staying inline,without changing his position,or the first to overlap the second.
In the fiddle above,they are aligned in the same line,but doesnt matter what i do,the second one changes his position instead resizing,or they overlap each other.
Any idea?
I know it must be done with percentage,but is not working either
http://jsfiddle.net/a4aME/507/
#element1 {width:50%; background-color:red;float:left}
#element2 {width:50%; background-color:red;float:left}
Take off the the display: and float it all left.
The width attribute accepts percentage value, base on its parent (or its nearest parent with the position:relative attribute if the element has the property position set as "absolute" or "fixed").
So you can use this CSS to the child
#element1 {display:inline-block;margin-right:10px; width:50%; background-color:red;}
#element2 {display:inline-block; width:50%; background-color:red;}
PS: If you are using inline-block, you have to make sure that there is no space between the tags, so you HTML must became this
<div id="element1"> element 1 markup
</div><div id="element2">
element 2 markup </div>
Check this jsfiddle to see if it is what you wanted. I'm not using display:inline-block since it looks that it is what's causing the problem. If you don't mind using floats, then this is your answer.
EDIT:
Check this resource here to see/correct your problem.
Related
HTML
<div class="menu">
<p>normal</p>
</div>
<div class="menu">
<p>normal</p>
</div>
<div class="menu">
<p>normal</p>
</div>
<div class="menu">
<p>Why does this div stays at the top</p>
</div>
CSS
.menu{
width:120px;
background-color:red;
display:inline-block;
height:400px;
}
http://jsfiddle.net/jezVt/
I have four divs aligned next to each other using inline-block. When I enter text inside the div using p tag, the div with two lines stays at the top while the other three divs(has just one line text) are aligned properly.
Help please..
add you code vertical-align:top; demo
The reason is that just like every inline element, your .menu elements have the default value baseline for their vertical-align property. This means that when the browser calculates layout for the .menu elements that appear side-by-side, each element is positioned so that the baseline of their contents is vertically aligned with that of the others.
In this specific case, this means that the baseline of last line of text in each .menu is aligned with that of the others. You will notice that by adding more text and making it to occupy three or more lines, the element that contains this text is going to be "pulled upwards" more and more in relation to the others.
Finally, as everyone has said using vertical-align: top lets the browser know that you want the divs to be aligned with respect to the top of their content, which produces the desired result.
write vertical-align:top; in css:http://jsfiddle.net/aashi/jezVt/1/
From my first look, is that you have to much text in the fourth column.
But use "vertical-align: top;" as the two previous answers.
Why do you want to make divs as inline-block. When div is a block level element you can use that property itself.
[http://jsfiddle.net/jezVt/12/][1]
Alternatively you can try:
.menu{
width:120px;
background-color:red;
display:inline-block;
height:400px;
float:left;
margin: 2px;
}
I have 6 divs that will hold images for products and I want them side by side in rows of three but then I want that centered.
This is the code I created for the parent and its all perfect apart from them being central.
<div id="parent" style="display:inline-block; margin:0 auto; width:730px">...</div>
As you can see, the divs are aligned left when compared with the horizontal rule..
Here is all of my code:http://jsfiddle.net/FNkUP/
Remove the "display:inline-block" property, and it will work well. or make it "display:block".
div id="parent" style="display:inline-block;/*no need of this*/">...</div>
For text align check this fiddle
just add this to your css
#parent
{
text-align:center;
}
or change the parent style tag like
style="display:inline-block; margin:0 auto; width:730px; text-align:center"
See your updated FIDDLE
This is my code :
HTML
<div class="item">
</div>
<div class="item">
<div class="item-abs">
</div>
</div>
<div class="item">
</div>
CSS
.item
{
position:relative;
z-index:5;
float:left;
background-color:red;
width:100px;
height:100px;
margin:10px;
}
.item-abs
{
position:absolute;
top:0px;
left:40px;
z-index:500;
background-color:blue;
width:100px;
height:100px;
}
as you can see, if children have got 500 of z-index, is under the next parents. Is there a way to force the children z-index? Get riding the stack of the father? Or this scenario can't be done?
I can't change z-index of parents, and I'd like to do it without setting a proper descent z-index with javascript...
Sure there is, you don't even need to set the z-index of the child.
Just set the z-index of the last div to a lower z-index, and you are done.
Example
The reason for this behaviour is the following: If parents have the same z-index, the one coming last in the DOM "wins". So technically a simple z-index:1 on your parent with the absolute child would be enough. Other possibility is to set the z-index just on the child:
Example
The following fiddle seems to do what you want (I believe):
http://jsfiddle.net/BhQNr/3/
This is done by removing the positioning on the parent elements and making the child relative (it works with absolute but the left and top values are closer than when using absolute).
I have two inner divs which are nested inside a wrapper div. I want the two inner div's to get arranged one below the other. But as of now they are getting arranged on the same line.
#wrapper{
margin-left:auto;
margin-right:auto;
height:auto;
width:auto;
}
#inner1{
float:left;
}
#inner2{
float:left;
}
<div id="wrapper">
<div id="inner1">This is inner div 1</div>
<div id="inner2">This is inner div 2</div>
</div>
Try a clear: left on #inner2. Because they are both being set to float it should cause a line return.
#inner1 {
float:left;
}
#inner2{
float:left;
clear: left;
}
<div id="wrapper">
<div id="inner1">This is inner div 1</div>
<div id="inner2">This is inner div 2</div>
</div>
If you want the two divs to be displayed one above the other, the simplest answer is to remove the float: left;from the css declaration, as this causes them to collapse to the size of their contents (or the css defined size), and, well float up against each other.
Alternatively, you could simply add clear:both; to the divs, which will force the floated content to clear previous floats.
Set the main div CSS to somthing like:
<style>
.wrapper{
display:flex;
flex-direction: column;
}
</style>
<div id="wrapper">
<div id="inner1">This is inner div 1</div>
<div id="inner2">This is inner div 2</div>
</div>
For more flexbox CSS refer: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
The default behaviour of divs is to take the full width available in their parent container.
This is the same as if you'd give the inner divs a width of 100%.
By floating the divs, they ignore their default and size their width to fit the content. Everything behind it (in the HTML), is placed under the div (on the rendered page).
This is the reason that they align theirselves next to each other.
The float CSS property specifies that an element should be taken from
the normal flow and placed along the left or right side of its
container, where text and inline elements will wrap around it. A
floating element is one where the computed value of float is not none.
Source: https://developer.mozilla.org/en-US/docs/Web/CSS/float
Get rid of the float, and the divs will be aligned under each other.
If this does not happen, you'll have some other css on divs or children of wrapper defining a floating behaviour or an inline display.
If you want to keep the float, for whatever reason, you can use clear on the 2nd div to reset the floating properties of elements before that element.
clear has 5 valid values: none | left | right | both | inherit. Clearing no floats (used to override inherited properties), left or right floats or both floats. Inherit means it'll inherit the behaviour of the parent element
Also, because of the default behaviour, you don't need to set the width and height on auto.
You only need this is you want to set a hardcoded height/width. E.g. 80% / 800px / 500em / ...
<div id="wrapper">
<div id="inner1"></div>
<div id="inner2"></div>
</div>
CSS is
#wrapper{
margin-left:auto;
margin-right:auto;
height:auto; // this is not needed, as parent container, this div will size automaticly
width:auto; // this is not needed, as parent container, this div will size automaticly
}
/*
You can get rid of the inner divs in the css, unless you want to style them.
If you want to style them identicly, you can use concatenation
*/
#inner1, #inner2 {
border: 1px solid black;
}
You don't even need the float:left;
It seems the default behavior is to render one below the other,
if it doesn't happen it's because they are inheriting some style from above.
CSS:
#wrapper{
margin-left:auto;
margin-right:auto;
height:auto;
width:auto;
}
</style>
HTML:
<div id="wrapper">
<div id="inner1">inner1</div>
<div id="inner2">inner2</div>
</div>
I'm trying to put div elements right next to each other. The problem is, even if there is enough room for the two elements to be on the same line, the new div moves itself to the next line, I need the other div only to go to the next line if there isn't enough room.
Does anybody know how to do this?
Set the CSS display style to display:inline-block;.
This allows the element to keep it's block-like functionality, while also allowing it to be displayed inline. It's a half-way house between the two.
(But note that there are some compatibility issues with older versions of IE)
Divs are block level elements, so by default they will always occupy an entire line. The way to change this is to float the divs:
<div style="float: left"></div>
Use float's and margin's; that way when there's no space you can just put overflow:hidden to the container hide the rest of the div instead of making it go to the next line.
CSS
.container {
width:500px;
background:#DADADA;
overflow:hidden; /* also used as a clearfix */
}
.content {
float:left;
background:green;
width:350px;
}
.sidebar {
width:155px; /* Intentionaly has more width */
margin-left:350px; /* Width of the content */
background:lime;
}
HTML
<div class="container">
<div class="content">Content</div>
<div class="sidebar">Sidebar</div>
</div>
In this demo you can see: floats, margin+floats, display:inline-block.
Demo here: http://jsfiddle.net/kuroir/UupbG/1/
You need to use float CSS rule. Just use some class or identifier and set float to left or right.
Make sure that you have a fixed width to the divs