html - font-size changes margin, why? -
this question has answer here:
i'm creating side scrolling div mobile devices , have run odd problem. if run code below font-size commented out body style there margin added @ sides of .scroll_item. set font-size 0 , works fine. i'm trying avoid setting font size in .scroll_item style , rather inherit comes previous in page.
why happening , there way correct without setting font-size 0?
body { background-color: gray; padding: 0; margin: 0; overflow: hidden; /*font-size: 0;*/ } #scroll_cont { height: auto; background-color: red; margin: 0; padding: 0; white-space: nowrap; overflow: auto; } .scroll_item { width: 120px; height: 120px; padding: 5px; margin: 2px; background-color: blue; box-sizing: border-box; white-space: normal; display: inline-block; font-size: 20px; color: white; }
<html> <head> <title>side scroll test</title> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1, user-scalable=no"> </head> <body> <div id="scroll_cont"> <div class="scroll_item">test1</div> <div class="scroll_item">test2</div> <div class="scroll_item">test3</div> <div class="scroll_item">test4</div> <div class="scroll_item">test5</div> </div> </body> </html>
this common problem devs have inline-blocks. inline-block acts little block, little inline. mean? block you're thinking should act right now. however, inline means acts sort of text. , 2 inline elements broken line break white space between them automatically.
for instance:
<span>i</span> <span>don't</span> <span>have</span> <span>to</span> <span>space</span> <span>these!</span>
this render white space automatically though didn't include any. kinda dumb, right? thats how goes. there number of hacks , tricks avoiding concern. set margin-right of around -4px on inline-block elements should stacked side side.
others use different display type table or flexbox. try floating them left , adding clearfix hack them. leave me comment if you're still stuck and/or check out chris coyier's solid post dilemma: https://css-tricks.com/fighting-the-space-between-inline-block-elements/
Comments
Post a Comment