「CSS」positionプロパティのfixedで要素の位置を固定して配置する
環境
Google Chrome 100.0.4896.88
Windows10 home 64bit
書式
position : 値
fixed ウィンドウの表示領域を基準とした配置になります。位置が固定され、スクロールしても動きません。
top:ウィンドウの上の位置から下に指定px数ずれます
left:ウィンドウの左の位置から右に指定pxずれます。
使用例
<!DOCTYPE html>
<html>
<head>
<title>positionのfixedで要素の位置を固定</title>
</head>
<body>
<style>
.cft {
position: fixed;
top: 25px;
left: 25px;
width: 350px;
height: 55px;
background-color: skyblue;
color: #000;
border: 1px solid #000;
}
</style>
<body>
<div class="cft">fixedで要素の位置を固定して配置する</div>
</body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>positionのfixedで要素の位置を固定</title>
</head>
<body>
<style>
.cft {
position: fixed;
top: 25px;
left: 25px;
width: 350px;
height: 55px;
background-color: skyblue;
color: #000;
border: 1px solid #000;
}
</style>
<body>
<div class="cft">fixedで要素の位置を固定して配置する</div>
</body>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>positionのfixedで要素の位置を固定</title> </head> <body> <style> .cft { position: fixed; top: 25px; left: 25px; width: 350px; height: 55px; background-color: skyblue; color: #000; border: 1px solid #000; } </style> <body> <div class="cft">fixedで要素の位置を固定して配置する</div> </body> </body> </html>