1

I am using <div> to create a horizontal bar across the screen. Within that horizontal bar, I have 3 more <div> each of a different width. They are supposed to be all in a row horizontally next to each other. Instead, they are on top of each other. How do I fix this?

Also, if I don't have any text within the <div> in my HTML code, the <div> does not appear. Ex: <div>anything</div>

JSFiddle

4
  • @xec I think you will know the answer Commented Aug 6, 2013 at 17:50
  • Add display: inline-block in .right, .mid and .left Commented Aug 6, 2013 at 17:54
  • why not float:left in first div @PiLHA Commented Aug 6, 2013 at 17:56
  • @RitabrataGautam i dont know :) Commented Aug 6, 2013 at 17:57

3 Answers 3

1

You can add css float:left to div and If you also don't want any text in div you should add css height to div.

.horizon div{
    float: left;
    height: 20px;
}

like this http://jsfiddle.net/KG5B3/

Sign up to request clarification or add additional context in comments.

Comments

1

Just use a float, which IS cross-browser compliant. Also you should clear your floats which can be seen on the updated JsFiddle

.horizon div{
    float: left;
}

1 Comment

w3schools.com/cssref/pr_class_float.asp shows all the major browsers support the float. Also I'll add the clear in
1

Fiddle

You can float those inner DIVs. You can also use inline-block (not shown).

<div class="horizon">
    <div class="left">left</div>
    <div class="mid">middle</div>
    <div class="right">right</div>
    <br style="clear: both" />
</div>

body {
    margin: 0;
}
.horizon {
    background: #000000;
    width: 100%;
}

div.horizon div {
    float: left;
}

.right {
    width: 25%;
    background: #ff0000;
}
.mid {
    width: 50%;
    background: #00ff00;
}
.left {
width: 25%;
    background: #0000ff;
}

3 Comments

jsfiddle.net/pUnQZ/5 The red one never shows up, also, what do you mean by clear?
Sorry, sloppy can & paste. I added the clear for you so you can see what I mean.
why is the clear needed? what does it do? Is there a way I can have the div without text in it? Just have it empty but still show up what ever color?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.