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;
}
Related
I'm trying to achieve something simple and the rules make it non-trivial. Here is my fiddle. It is non-trivial for me to align the elements in a natural way, I think the elements should be on the same row when I make a float:left.
Hx elements have default margins.
The "space" from the top cause by the margin-top property of the H1 element.
Consider using a reset.css file to reset those default values.
Another thing is the line-height property.
In conclusion:
margin-top:0;
line-height: 20px; (choose a value that fits to your needs)
http://jsfiddle.net/ynrmwgt9/4/
I would recommend to wrap your div in a container div. A little bit like bootstrap system.
See the fiddle, and ask me if you have questions about :)
I'm using something like this :
<div class='row'>
<div class='col'>Content 1</div>
<div class='col'>Content 2</div>
</div>
The stylesheet has to specify that rowfill 100% of the width, and your two columns, the half, then 50% each.
.row
{
width:100%;
}
.col
{
width:50%;
float:left;
text-align:center;
}
Text align center is just for make it beautiful
http://jsfiddle.net/ynrmwgt9/2/
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.
HTML
<div>Test</div>
CSS
div {
text-align: center;
width:200px;
margin-left:20px;
}
I don't want to have the text at center but pushed slightly right/left. Is this possible?
If it's a single line you should be able to do this:
text-indent:1em;
ref: https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent
It is possible. Currently your margin-left property will affect the div and does not specifically target the inner text.
I would do something like the following. If you surround the text in a <p> tag you can offset it to the left or right by using padding.
div {
text-align: center;
width:200px;
background:blue;
}
div p{
padding-left:30px;
}
<div>
<p>Test</p>
</div>
If you use padding on one side, it will offset the text visually by whatever you set it to. What you have listed would work, as would using padding instead of a margin, but it depends on what the site looks like visually.
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
I'm writing a style sheet, and I want to display three elements horizontally (width=33%) within a container, with a relative layout.
Here's the code:
#container
{
margin:auto;
width:85%;
padding:0;
}
#element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
border:0px;
text-align:center;
}
I don't understand why with three elements:
<div id="container">
<div id="element">hi</div>
<div id="element">every</div>
<div id="element">one</div>
</div>
The last element is shown below the first two, while I believed that they would be drawn on the same line. There are no margins,padding or borders.
If width is set to 32%, in a Full browser window, they are on the same line (it works), but when I reduce the browser-window width, the last element falls on a new line.
Does anybody know why this happens?
These are inline blocks, so they get laid out just like characters would (think of them as really big characters, basically). And in your case you have whitespace between them, so that whitespace becomes a single space on the line between the inline-blocks in the rendering; you can see this if you put borders on them. So the space taken up by all three together ends up being "99.9% of container width plus width of two spaces in the container's font". When you reduce to 32%, then you get line-breaking once two spaces add up to more than 4% of the container width (which is why it only happens at smaller container widths).
If you really want your inline-blocks flush up against each other, take out the spaces between them.
Make you element a class (thanx Jarrett) and add float:left style to that class.
.element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
float:left;
border:0px;
text-align:center;
}
<div id="container">
<div class="element">hi</div>
<div class="element">every</div>
<div class="element">one</div>
</div>
I recommend to play arround with the containers width.
A tip that works for me is giving them a line. Below is my contribution:
http://jsfiddle.net/8dWhF/