Home >Web Front-end >CSS Tutorial >Detailed explanation of position in CSS (code example)

Detailed explanation of position in CSS (code example)

不言
不言Original
2018-11-02 17:37:413087browse

The content of this article is to share with you a detailed explanation (code example) about position in CSS. Friends in need can refer to it. I hope it will be helpful to you.

CSS position property is versatile and powerful. It allows setting or changing the position of an element. It has 4 possible values: static (default), relative, absolute, fixed. (Recommended course: css video tutorial)

It is usually used with 4 coordinate attributes: left, right, top, bottom

static

This is the default position value: static elements only follow natural flow. They are not affected by any left, right, top or bottom values.

relative

When position is set to relative, the element can move based on its current position.

<p>汉皇重色思倾国,御宇多年求不得。杨家有女初长成,养在深闺人未识。天生丽质难自弃,一朝选在君王侧。回眸一笑百媚生,六宫粉黛无颜色。春寒赐浴华清池,温泉水滑洗凝脂。侍儿扶起娇无力,始是新承恩泽时。</p>
<p>云鬓花颜金步摇,芙蓉帐暖度春宵。春宵苦短日高起,从此君王不早朝。承欢侍宴无闲暇,春从春游夜专夜。后宫佳丽三千人,三千宠爱在一身。金屋妆成娇侍夜,玉楼宴罢醉和春。</p>
<p>姊妹弟兄皆列土,可怜光彩生门户。遂令天下父母心,不重生男重生女。骊宫高处入青云,仙乐风飘处处闻。缓歌慢舞凝丝竹,尽日君王看不足。渔阳鼙鼓动地来,惊破霓裳羽衣曲。</p>
rrree

The effect is as follows:

Detailed explanation of position in CSS (code example)

Let us continue to look at it:

p{ border: 1px solid blue;}
rrree

The effect is as follows:

Detailed explanation of position in CSS (code example)

The red border is moved 20 pixels from the left and 10 pixels from the top, relative to its natural position, where it should be.

Note that the blue paragraph has not moved at all. By using relative positioning, the red paragraph can be moved freely without breaking the layout. The only thing that's inappropriate is that all other elements except itself don't know that the element has moved.

absolute

When position is set to absolute, elements can move based on the first positioned element.

"Positioning? What is a positioned element?"

A positioned element is a position whose value is relative, absolute, or fixed. Therefore, an element is positioned unless the position is unset or static.

The characteristic of a positioned element is that it can serve as a reference point for its child elements.

Let us imagine a simple hierarchy:

<p>九重城阙烟尘生,千乘万骑西南行。翠华摇摇行复止,西出都门百余里。六军不发无奈何,宛转蛾眉马前死。花钿委地无人收,翠翘金雀玉搔头。君王掩面救不得,回看血泪相和流。黄埃散漫风萧索,云栈萦纡登剑阁。</p>
<p class="second">峨嵋山下少人行,旌旗无光日色薄。蜀江水碧蜀山青,圣主朝朝暮暮情。行宫见月伤心色,夜雨闻铃肠断声。天旋地转回龙驭,到此踌躇不能去。马嵬坡下泥土中,不见玉颜空死处。</p>
<p>君臣相顾尽沾衣,东望都门信马归。归来池苑皆依旧,太液芙蓉未央柳。芙蓉如面柳如眉,对此如何不泪垂。春风桃李花开日,秋雨梧桐叶落时。</p>
p{ border: 1px solid blue;}
.second{ position: relative;border:1px solid red; left: 20px; top: 10px;}

The effect is as follows:

Detailed explanation of position in CSS (code example)

The height of the yellow part is 200px, and its position is set is relative, converts it into the reference point for all child elements.

When the position of the green paragraph is set to absolute, it can move freely according to the yellow part. By setting the bottom and left values, it will move to the lower left corner.

What happens if we set left and right?

If width is not set, applying left: 0 and right: 0 will stretch the element across the entire width. It is equivalent to setting left: 0 and width: 100%.

If width is set, right discards the value.

fixed

When position is set to fixed, it works like absolute positioning: you can set left/right and top/bottom coordinates.

The only difference is that the reference point is the viewport. This means that the fixed element does not scroll with the page; it remains fixed to the screen.

The above is the detailed content of Detailed explanation of position in CSS (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn