Jump to content

Recommended Posts

Why do the two divs collide into each other?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS</title>
</head>

<body>	

<div style="width: 100px; border: solid 3px #CCCCCC; float: left;">
<p>test</p>
</div>

<div style="width: 100px;border: solid 5px #FF0000;">
<p>test2</p>
</div>

</body>

</html>

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/208840-a-simple-css-question/
Share on other sites

It is because of floats. The red box will wrap around the grey box. It'll be come a lot more clear if you add some dummy text to the red box.

<div style="width: 300px;border: solid 5px #FF0000;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer placerat pulvinar faucibus. 
    Nam ac elit enim, non blandit mauris. Aenean volutpat nulla a justo molestie ac lobortis elit iaculis. 
    In tellus purus, mattis in sollicitudin eu, convallis vel nibh. Aenean a nisl in velit tristique euismod. <p>

    <p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin volutpat 
    aliquam ligula, non auctor nunc elementum id. Quisque aliquam vestibulum purus, eget eleifend mi 
    adipiscing ac. Aenean hendrerit tincidunt ipsum, quis pretium lectus porta in. In elementum purus non 
    eros interdum consequat. Pellentesque volutpat consequat dictum.</p>
</div>

You'll now notice the text is being wrapped around the grey box

 

If you don't want the two boxes wrapping around each other, add overflow: hidden to your second div. Now the two boxes will sit next to each other.

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.