Div 100% width and a margin

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I'm trying to put a left and right margin on something that is also 100% width. I was using the box-sizing method which I was hoping would make it say 100% including the margin but it does 100% PLUS the margin, meaning I have a vertical scroll bar.

It's demonstrated here:

http://jsbin.com/xomocitono/1

  <div id="main">     Test   </div>  #main {width: 100%; margin-left: 100px; margin-right: 100px; background: red;} 

In my own particular situation I can't use the following:

  1. Padding instead, because even though it works how I want (in terms of 100% width) the padding covers the links below whereas the margin doesn't.

  2. CSS Calc (it isn't as cross browser as I was hoping)

  3. Fixed width #main, because I want it to be fluid, just with a margin either side.

Do I have any other options?

回答1:

You don't have to give width when you are using margin-left and margin-right.

Try This:

#main {  margin-left: 10%;  margin-right: 10%;  background: red; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!