float on some element, and div does not track content

Asked

Viewed 1,334 times

2

I’m trying to float an element to the right, and this element is inside a div no set size. The problem is that when I float the element p to div does not follow the content.

Follow the HTML code:

<div id="divPrincipal">
   <p>Conteúdo dentro da div</p>
</div>

And the CSS:

#divPrincipal{
    border: 1px solid red;
}

#divPrincipal p{
    border: 1px solid blue;
    float: right;
}

How to solve this?

  • I tested the position, and applied on it a 1000px left. The contents came out of the div. This is because the element p is being treated as a block, I can define it with a display: table. It would be a solution. And on the width: 100%, did not solve the situation no.

  • After all, you are wanting to align the div the right or the elements of div, or even both?

  • The element! ;

  • Kazzkiq has already replied.

1 answer

4


You need to clean up the float for the parent element to assume the size of their children with float. There are several ways to do this, but the most modern nowadays would be using something like this solution:

.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

Where this CSS class would be added to its parent element, and HTML would look like this:

<div id="divPrincipal" class="cf">
   <p>Conteúdo dentro da div</p>
</div>

Example: FIDDLE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.