html:
<body style="height: 2000px"> <div class="regular"></div> </body>
css:
<style> *{ padding: 0; margin: 0; } .regular{ position: fixed; right: 0; top: 50%; transform: translateY(-50%); width: 50px; height:50px; background: pink; } </style>
整体代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>固定位置</title> <style> *{ padding: 0; margin: 0; } .regular{ position: fixed; right: 0; top: 50%; transform: translateY(-50%); width: 50px; height:50px; background: pink; } </style> </head> <body style="height: 2000px"> <div class="regular"></div> </body> </html>