ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 前端定位方式 ![](https://img.kancloud.cn/c3/d2/c3d2b08902c6f6d581cc5d9b7a7258be_885x418.png) ### 阴影 ![](https://img.kancloud.cn/2b/66/2b66b47f0abb94ebd475e10a8f125a6b_553x79.png) ![](https://img.kancloud.cn/26/b5/26b551fd0b11db73b69662ff76c14b73_695x355.png) ### 定位 1. ``` <pre class="calibre15">``` Absolute:绝对定位,是相对于最近的且不是<span class="token2">static</span>定位的父元素来定位 ``` ``` 2. ``` <pre class="calibre15">``` Fixed:绝对定位,是相对于浏览器窗口来定位的,是固定的,不会跟屏幕一起滚动。 ``` ``` 3. ``` <pre class="calibre15">``` Relative:相对定位,是相对于其原本的位置来定位的。 ``` ``` 4. ``` <pre class="calibre15">``` Static:默认值,没有定位。 ``` ``` 5. ``` <pre class="calibre15">``` Inherit:继承父元素的position值。 ``` ``` ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html lang<span class="token">=</span><span class="token4">"en"</span><span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"UTF-8"</span><span class="token">></span> <span class="token"><</span>meta http<span class="token">-</span>equiv<span class="token">=</span><span class="token4">"X-UA-Compatible"</span> content<span class="token">=</span><span class="token4">"IE=edge"</span><span class="token">></span> <span class="token"><</span>meta name<span class="token">=</span><span class="token4">"viewport"</span> content<span class="token">=</span><span class="token4">"width=device-width, initial-scale=1.0"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>positon<span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div <span class="token2">class</span><span class="token">=</span><span class="token4">"container"</span><span class="token">></span> <span class="token"><</span>div <span class="token2">class</span><span class="token">=</span><span class="token4">"div1"</span><span class="token">></span>第一个<span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>div <span class="token2">class</span><span class="token">=</span><span class="token4">"div2"</span><span class="token">></span>第二个<span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>div <span class="token2">class</span><span class="token">=</span><span class="token4">"div3"</span><span class="token">></span>第三个<span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>div <span class="token2">class</span><span class="token">=</span><span class="token4">"div4"</span><span class="token">></span>第四个<span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span>style<span class="token">></span> <span class="token1">.</span>container<span class="token1">{</span> display<span class="token1">:</span> flex<span class="token1">;</span> flex<span class="token">-</span>wrap<span class="token1">:</span> wrap<span class="token1">;</span> <span class="token1">}</span> <span class="token1">.</span>div1 <span class="token1">{</span> height<span class="token1">:</span> <span class="token5">50</span>px<span class="token1">;</span> background<span class="token">-</span>color<span class="token1">:</span> <span class="token3">rgb</span><span class="token1">(</span><span class="token5">240</span><span class="token1">,</span> <span class="token5">8</span><span class="token1">,</span> <span class="token5">136</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">.</span>div2 <span class="token1">{</span> position<span class="token1">:</span> absolute<span class="token1">;</span> top<span class="token1">:</span> <span class="token5">30</span>px<span class="token1">;</span> left<span class="token1">:</span><span class="token5">30</span>px<span class="token1">;</span> height<span class="token1">:</span> <span class="token5">50</span>px<span class="token1">;</span> background<span class="token">-</span>color<span class="token1">:</span> <span class="token3">rgb</span><span class="token1">(</span><span class="token5">34</span><span class="token1">,</span> <span class="token5">231</span><span class="token1">,</span> <span class="token5">87</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">.</span>div3 <span class="token1">{</span> height<span class="token1">:</span> <span class="token5">50</span>px<span class="token1">;</span> background<span class="token">-</span>color<span class="token1">:</span> <span class="token3">rgb</span><span class="token1">(</span><span class="token5">35</span><span class="token1">,</span> <span class="token5">147</span><span class="token1">,</span> <span class="token5">245</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">.</span>div4 <span class="token1">{</span> height<span class="token1">:</span> <span class="token5">50</span>px<span class="token1">;</span> background<span class="token">-</span>color<span class="token1">:</span> <span class="token3">rgb</span><span class="token1">(</span><span class="token5">240</span><span class="token1">,</span> <span class="token5">163</span><span class="token1">,</span> <span class="token5">19</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token"><</span><span class="token">/</span>style<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` ![](https://img.kancloud.cn/97/e9/97e9e851e283346f1ac9085748eb62c3_414x311.png) **第二个div设置了absolute,则该div的宽度就由文本决定,且下面的div会上移占据之前第二个div的位置,top和left是相对于离它最近且不是static定位的父元素来定位的,在此div2因为没有父元素,所以第二个div相对于根元素即html元素来定位** ``` <pre class="calibre13">``` 将第二个div设置为relative: <span class="token1">.</span>div2<span class="token1">{</span> height<span class="token1">:</span><span class="token5">100</span>px<span class="token1">;</span> background<span class="token">-</span>color<span class="token1">:</span> blueviolet<span class="token1">;</span> position<span class="token1">:</span>relative<span class="token1">;</span> left<span class="token1">:</span><span class="token5">50</span>px<span class="token1">;</span> top<span class="token1">:</span><span class="token5">50</span>px<span class="token1">;</span> <span class="token1">}</span> ``` ``` ![](https://img.kancloud.cn/25/6b/256b0ddc62a748ca50c92c26bc34526d_414x311.png) **设置relative的div不会影响其他div的位置,且top和left是相对于它原本自身的位置来定位。**