Reputation: 3782
I have the following html code when I have the attribute float right then the height of the parent div becomes smaller than the child's div
<div id="notificationBar">
<div style="float:right;">
{% if user.is_authenticated %}
Hello {{ user }}
<a href ="/accounts/logout/" style="margin-left:10px;"> Logout </a>
{% else %}
Hello Guest
<a href ="/accounts/login/" style="margin-left:10px;"> Login </a>
{% endif %}
</div>
</div>
However when I remove th float right attribute as given below then the parent div accommodates the child div as it should normally behave.
<div id="notificationBar">
<div>
{% if user.is_authenticated %}
Hello {{ user }}
<a href ="/accounts/logout/" style="margin-left:10px;"> Logout </a>
{% else %}
Hello Guest
<a href ="/accounts/login/" style="margin-left:10px;"> Login </a>
{% endif %}
</div>
</div>
Can anybody explain why is this happening. One solution is to set a height of the parent div, but then that is not a flexible solution. I want the height of the parent div to be changed according to the height of the child div.
Upvotes: 1
Views: 3874
Reputation: 614
How about clear the floating as follows
<div id="notificationBar">
<div style="float:right;">
{% if user.is_authenticated %}
Hello {{ user }}
<a href ="/accounts/logout/" style="margin-left:10px;"> Logout </a>
{% else %}
Hello Guest
<a href ="/accounts/login/" style="margin-left:10px;"> Login </a>
{% endif %}
</div>
<div style="clear: both; height: 0;"></div>
</div>`
Upvotes: 4
Reputation: 21882
Why have a div at all? Why not simply set the text to align right? Float's have no inherent block height. They won't ever keep a partent div as a block if you float internal elements. You could use float: right; display: inline-block; or....
What I would do....
<div id="notificationBar" style="text-align: right;">
{% if user.is_authenticated %}
Hello {{ user }}
<a href ="/accounts/logout/" style="margin-left:10px;"> Logout </a>
{% else %}
Hello Guest
<a href ="/accounts/login/" style="margin-left:10px;"> Login </a>
{% endif %}
</div>
or use a paragraph rather than a div.
Of course, there maybe other factors you aren't sharing.
Upvotes: 2