HTML layout skills: How to use positioning layout for positioning control, specific code examples are required
HTML layout is the cornerstone of web design. Through reasonable layout, web content can be More organized and beautiful. Among them, positioning layout is a commonly used method, which can accurately control the position and hierarchical relationship of elements. This article will introduce how to use positioning layout for positioning control and provide specific code examples.
1. The basic concept of positioning layout
2. Usage of positioning layout
Static positioning (static): The default positioning method of elements is not affected by positioning attributes and is arranged according to the flowing layout. Static positioning can be set through CSS in the code:
<style> .box { position: static; } </style> <div class="box">这是一个静态定位的元素</div>
Relative positioning (relative): The element is positioned relative to its original position. You can set top, right, bottom, left Attributes to control the position of elements. Relative positioning can be set through CSS in the code:
<style> .box { position: relative; top: 10px; right: 20px; } </style> <div class="box">这是一个相对定位的元素</div>
Absolute positioning (absolute): The element is positioned relative to its nearest non-statically positioned parent element. If there is no non-static positioning The parent element of the element is positioned relative to the body element. You can also control the position of elements by setting the top, right, bottom, and left attributes. Absolute positioning can be set through CSS in the code:
<style> .box { position: absolute; top: 100px; right: 50px; } </style> <div class="box">这是一个绝对定位的元素</div>
Fixed positioning (fixed): The element is positioned relative to the browser window and does not change position as the scroll bar scrolls. You can also control the position of elements by setting the top, right, bottom, and left attributes. Fixed positioning can be set through CSS in the code:
<style> .box { position: fixed; top: 20px; right: 30px; } </style> <div class="box">这是一个固定定位的元素</div>
3. Example of positioning layout
The following is a sample code that shows how to use positioning layout to control elements The position and hierarchical relationship:
<style> .container { position: relative; width: 300px; height: 200px; border: 1px solid #ccc; } .box1 { position: absolute; top: 10px; left: 10px; width: 100px; height: 100px; background-color: red; } .box2 { position: absolute; top: 50px; left: 50px; width: 150px; height: 150px; background-color: blue; } </style> <div class="container"> <div class="box1"></div> <div class="box2"></div> </div>
In the above code, we create a container element (.container) and set its width, height and border style. Inside the container, we create two positioned elements (.box1 and .box2) and place them at the specified location within the container by setting their positioning properties and position.
Summary
Positioning layout is an important means of web page layout. By carefully controlling the position and hierarchical relationship of elements, complex web page layout effects can be achieved. This article briefly introduces the basic concepts and usage of positioning layout, and provides specific code examples. I hope it will be helpful for you to understand and use positioning layout.
The above is the detailed content of HTML layout tips: How to use positioning layout for positioning control. For more information, please follow other related articles on the PHP Chinese website!