The top property in CSS can move an element vertically relative to the top edge of its parent element. You can use length, percentage, or auto to specify the offset. length is the numerical length, percentage is the percentage offset, and auto is automatically calculated by the browser. top only applies to elements with positioning, the top edge of the element will be affected by the margin box or content box.
The top attribute in CSS
The role of the top attribute
The top property sets the offset of an element relative to the top edge of its parent element. It moves an element vertically up or down.
Using syntax
<code class="css">top: <length> | <percentage> | auto;</code>
Attribute value
<length>
: one A numeric value specifying the offset length, such as "5px" or "1em". <percentage>
: A numeric value that specifies the offset percentage, such as "5%" or "25%". auto
: The browser automatically calculates the offset based on the element's parent element and content. Example
Move an element up 20px:
<code class="css">element { top: 20px; }</code>
Move an element down 50% of the height of its parent element:
<code class="css">element { top: 50%; }</code>
Note
position: absolute
or position: relative
)Elements. auto
, the element will be aligned along the top edge of its parent element. bottom
, left
, and right
to achieve precise positioning of elements. The above is the detailed content of How to use top in css. For more information, please follow other related articles on the PHP Chinese website!