用这个定位可以达到要求为什么我用marginleft,top方法不行呢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Document</ title > < style > /*完善下列代码*/ .per{ width: 300px; height: 300px; background: red; margin-left:200px; margin-top: 200px; position:relative; } .son{ width: 100px; height: 100px; background: blue; /*top:0px;*/ /*left:0px;*/ /*right:0px;*/ /*bottom:0px;*/ /*position:absolute;*/ margin-left:100px; margin-top:100px; margin-bottom:100px; } </ style > </ head > < body > < div class = "per" > < div class = "son" ></ div > </ div > </ body > </ html > |
0
收起
正在回答
2回答
您好,如果只使用margin时会造成整个布局margin-top距离上方100px;,那么就需要使用绝对定位完成距离per父元素100px像素的效果。绝对定位:生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Document</ title > < style > /*完善下列代码*/ .per{ width: 300px; height: 300px; background: red; } .son{ width: 100px; height: 100px; background: blue; position:absolute; margin-left:100px; margin-top:100px; } </ style > </ head > < body > < div class = "per" > < div class = "son" ></ div > </ div > </ body > </ html > |
祝学习愉快!
好帮手慕查理
2019-01-17 09:49:30
您好,margin设置的是外边距,可以使用padding设置,但会将父元素撑大。如下效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Document</ title > < style > /*完善下列代码*/ .per{ width: 300px; height: 300px; background: red; position:relative; padding-left:200px; padding-top:200px; } .son{ width: 100px; height: 100px; background: blue; } </ style > </ head > < body > < div class = "per" > < div class = "son" ></ div > </ div > </ body > </ html > |
祝学习愉快!
PHP小白零基础入门
- 参与学习 人
- 提交作业 626 份
- 解答问题 4928 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧