html body position,html - body {position: relative;} - Stack Overflow

I'm looking to position my document with CSS, and I was wondering what the consensus is on setting the body element's position property to relative to establish the initial containing block. Is this considered bad practice? I've read that it isn't necessary, and that, say, if the body element's first child is absolutely positioned, it's containing block defaults to the HTML element, which is equivalent to the viewport. I'm confused because I can't seem to get the positioning to work without body {position: relative;}.

Here is the HTML and CSS:

Title

content

content

body{

position: relative;

}

#div1 {

position: absolute;

top: 500px;

width:auto;

margin: auto;

}

#p1 {

position: absolute;

width: auto;

background-color: aqua;

}

#div2 {

position:absolute;

top: 200px;

background-color: red;

}

#p2 {

position: absolute;

}

I threw this example together so I can start messing with the different values for position, margin, and width. This is a lot different than what I originally posted. I was hoping to understand how to set the initial context for all of the other positioned elements in the document so that I can eventually understand how the browser calculates the auto value. I started to get confused by trying to automatically set widths and center elements on the page.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值