简体   繁体   中英

Why is my display: inline-block; behaving like a block?

I'm attempting to get my boxes of paragraphs to appear next to each other in the given container, but when I set them to display: inline-block; they still display as if they were set to display: block;

 #container { border: 1px solid black; width: 50%; margin: 0 auto; } #container section { width: 400px; border: 1px solid red; display: inline-block; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" href="app-mc.css"> <title>Display</title> </head> <body> <div id="container"> <section> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit, Ipsa deserunt distinctio vel quis mollitia illum minima qui harum architecto praesentium, iure, a obcaecati. ipsam aliquam ut totam excepturi vero enim,</p> </section> <section> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit, Ipsa deserunt distinctio vel quis mollitia illum minima qui harum architecto praesentium, iure, a obcaecati, ipsam aliquam ut totam excepturi vero enim!</p> </section> </div> </body> </html>

The reason is that you applied a fixed width for the inline-blocks (400px) that won't fit into the parent element twice. Use a percentage width instead:

 #container { border: 1px solid black; width: 50%; margin: 0 auto; } #container section { width: 48.5%; border: 1px solid red; display: inline-block; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" href="app-mc.css"> <title>Display</title> </head> <body> <div id="container"> <section> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit, Ipsa deserunt distinctio vel quis mollitia illum minima qui harum architecto praesentium, iure, a obcaecati. ipsam aliquam ut totam excepturi vero enim,</p> </section> <section> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit, Ipsa deserunt distinctio vel quis mollitia illum minima qui harum architecto praesentium, iure, a obcaecati, ipsam aliquam ut totam excepturi vero enim!</p> </section> </div> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM