Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with DIV's & CSS

Tags:

html

css

OK, if anyone could help me with this I'd be much appreciative. If you copy and paste the following and open up in IE or Firefox

<div style="border: solid 1px navy; float: left;">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
    </ul>
</div>
<div style="background-color: blue;">
    <p>Some Text</p>
    <p>Another paragraph</p>
</div>

Why does the second div which has a blue background expand to be behind the first div that contains the list of items? How do I get it to really float next to the first div?

like image 333
user44702 Avatar asked Dec 10 '08 17:12

user44702


People also ask

How do you use HTML tags?

The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. By default, links will appear as follows in all browsers: An unvisited link is underlined and blue.

What is the purpose of div?

The Div is the most usable tag in web development because it helps us to separate out data in the web page and we can create a particular section for particular data or function in the web pages. It is used to the group of various tags of HTML so that sections can be created and style can be applied to them.

How do I split a website into multiple sections?

In HTML, we can divide a whole webpage into sections by using <div> tag along with the CSS. By default, a <div> tag divides a webpage into horizontal sections. However, you can use the float property of CSS to make the vertical sections of the webpage.

Why do we use div and SPAN?

Span and div are both generic HTML elements that group together related parts of a web page. However, they serve different functions. A div element is used for block-level organization and styling of page elements, whereas a span element is used for inline organization and styling.


1 Answers

The reason you are seeing the behavior you describe is because those are the instructions you are giving the DIVs.

Let's break it down:

<div style="border: solid 1px navy; float: left;">

This is relying on default behavior for everything except the border and, by floating it, you are telling the box to pin itself to the left margin (of it's parent, which is assumed to be the body here) regardless of what else belongs there. The default width of the DIV is 100% of the parent object's width (still the body tag). The default position is to be the next block element in the flow - since there aren't any other block elements, it vertically aligns with the top margin.

Then you asked another DIV to do this:

<div style="background-color: blue; float:left;">

Which is essentially the same thing. Here, you haven't given either DIV a new width or any additional instruction about where in the layout they should now go, so it pins itself to the left margin and it's top margin finds the nearest block element to pin to (still the body).

To get these to line up side by side, do the following:

  <style type="text/css">
    .leftBox, .rightBox
    {
       width: 48%; /*leave some room for varying browser definitions */
       border: 1px solid navy;
       float: left;
       display: inline; /* follow the semantic flow of the page and don't break the line */
       clear: left; /* don't allow any other elements between you and the left margin */
     }

    .rightBox
    {
       border: none;
       background-color: blue;
       clear: right;
    }
</style>
<div class="leftBox">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
    </ul>
</div>
<div class="rightBox">
    <p>
        some other text</p>
</div>
like image 51
Rob Allen Avatar answered Nov 08 '22 11:11

Rob Allen