Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

html - 将边框放在div内部而不是边缘(Placing border inside of div and not on its edge)

I have a <div> element and I want to put a border on it.

(我有一个<div>元素,我想在它上面加一个边框。)

I know I can write style="border: 1px solid black" , but this adds 2px to either side of the div, which is not what I want.

(我知道我可以写style="border: 1px solid black" ,但这会增加2px到div的任何一侧,这不是我想要的。)

I would rather have this border be -1px from the edge of the div.

(我宁愿让这个边界距离div的边缘是-1px。)

The div itself is 100px x 100px, and if I add a border, then I have to do some mathematics to make the border appear.

(div本身是100px x 100px,如果我添加一个边框,那么我必须做一些数学来使边框出现。)

Is there any way that I can make the border appear, and ensure the box will still be 100px (including the border)?

(有什么方法可以让边框出现,并确保框仍然是100px(包括边框)?)

  ask by TheMonkeyMan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Set box-sizing property to border-box :

(将box-sizing属性设置为border-box :)

 div { box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; width: 100px; height: 100px; border: 20px solid #f00; background: #00f; margin: 10px; } div + div { border: 10px solid red; } 
 <div>Hello!</div> <div>Hello!</div> 

It works on IE8 & above .

(它适用于IE8及以上版本 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...