{"id":6293,"date":"2024-10-07T21:54:27","date_gmt":"2024-10-07T12:54:27","guid":{"rendered":"https:\/\/code-plus.jp\/gp\/?p=6293"},"modified":"2026-01-15T17:21:51","modified_gmt":"2026-01-15T08:21:51","slug":"instyle-anchor-6293","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/instyle-anchor-6293\/","title":{"rendered":"CSS\u5909\u6570\u3068JavaScript\u3092\u4f7f\u3063\u3066\u3001\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u306e\u30b8\u30e3\u30f3\u30d7\u5148\u306e\u4f4d\u7f6e\u3092\u67d4\u8edf\u306b\u8abf\u6574\u3059\u308b"},"content":{"rendered":"<p>\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u3067\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u3092\u4f7f\u7528\u3059\u308b\u969b\u3001\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u304c\u3042\u308b\u3068\u76ee\u7684\u306e\u4f4d\u7f6e\u306b\u6b63\u78ba\u306b\u30b8\u30e3\u30f3\u30d7\u3067\u304d\u306a\u3044\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001CSS\u5909\u6570\u3068JavaScript\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u67d4\u8edf\u306a\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\r\n\r\n<!-- \u2605\u76ee\u6b21\u30ea\u30b9\u30c8 -->\r\n<ul class=\"tocList mt-1r\" data-toc-id=\"js-tocBWGD\" style=\"height:28rem\"><\/ul>\r\n<ins class=\"tocBtn is-fixed\" data-toc-id=\"js-tocBWGD\"><\/ins>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<h2 class=\"fz-xl\">\u89e3\u6c7a\u7b56\u306e\u6982\u8981<\/h2>\r\n<ul style=\"list-style-type:bullet;\">\r\n  <li>CSS\u5909\u6570\u3092\u4f7f\u3063\u3066\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30a2\u30f3\u30ab\u30fc\u4f4d\u7f6e\u8abf\u6574\u5024\u3092\u8a2d\u5b9a<\/li>\r\n  <li>HTML\u3067\u500b\u5225\u306e\u8abf\u6574\u3092\u53ef\u80fd\u306b\u3059\u308b<\/li>\r\n  <li>JavaScript\u3092\u4f7f\u3063\u3066\u52d5\u7684\u306b\u30b9\u30bf\u30a4\u30eb\u3092\u9069\u7528<\/li>\r\n<\/ul>\r\n<h2 class=\"fz-xl\">\u30b3\u30fc\u30c9\u3068\u89e3\u8aac<\/h2>\r\n<h3>1. CSS<\/h3>\r\n<pre><code class=\"language-css\">:root {\r\n    --header-height: 4.125pc;\r\n    --anchor: var(--header-height);\r\n}\r\n\r\n@media print, screen and (min-width: 744px) {\r\n    :root {\r\n        --header-height: 8.25pc;\r\n        --anchor: var(--header-height);\r\n    }\r\n}\r\n\r\n[anchor] {\r\n    margin-top: calc(var(--anchor) * -1);\r\n    padding-top: var(--anchor);\r\n}\r\n<\/code><\/pre>\r\n<p>\u3053\u306eCSS\u306f\u3001<code>:root<\/code>\u30bb\u30ec\u30af\u30bf\u3092\u4f7f\u3063\u3066\u30b0\u30ed\u30fc\u30d0\u30eb\u306aCSS\u5909\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<code>--header-height<\/code>\u306f\u30d8\u30c3\u30c0\u30fc\u306e\u9ad8\u3055\u3092\u3001<code>--anchor<\/code>\u306f\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u306e\u8abf\u6574\u5024\u3092\u8868\u3057\u307e\u3059\u3002\u30e1\u30c7\u30a3\u30a2\u30af\u30a8\u30ea\u3092\u4f7f\u7528\u3057\u3066\u3001\u753b\u9762\u30b5\u30a4\u30ba\u306b\u5fdc\u3058\u3066\u5024\u3092\u5909\u66f4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<h3>2. HTML<\/h3>\r\n<pre><code class=\"language-html\">&lt;!-- \u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u8abf\u6574\u3092\u4f7f\u7528 --&gt;\r\n&lt;hr id=&quot;section1&quot; anchor \/&gt;\r\n\r\n&lt;!-- \u30ab\u30b9\u30bf\u30e0\u5024\u3067\u8abf\u6574 --&gt;\r\n&lt;hr id=&quot;section2&quot; anchor=&quot;20pc&quot; \/&gt;\r\n\r\n&lt;!-- \u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u3067\u8abf\u6574 --&gt;\r\n&lt;hr id=&quot;section3&quot; anchor style=&quot;--anchor:20pc;&quot; \/&gt;\r\n<\/code><\/pre>\r\n<p>HTML\u3067\u306f\u3001<code>anchor<\/code>\u5c5e\u6027\u3092\u4f7f\u3063\u3066\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u306e\u8abf\u6574\u3092\u9069\u7528\u3057\u307e\u3059\u3002\u5c5e\u6027\u5024\u3092\u6307\u5b9a\u3059\u308b\u304b\u3001\u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u3067CSS\u5909\u6570\u3092\u4e0a\u66f8\u304d\u3059\u308b\u3053\u3068\u3067\u3001\u500b\u5225\u306e\u8abf\u6574\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\r\n<h3>3. JavaScript<\/h3>\r\n<pre><code class=\"language-javascript\">(function () {\r\n    const elements = document.querySelectorAll(&#39;[anchor]&#39;);\r\n    const STYLE = document.createElement(&#39;style&#39;);\r\n    document.head.appendChild(STYLE);\r\n\r\n    elements.forEach((el) =&gt; {\r\n        const anchorValue = el.getAttribute(&#39;anchor&#39;);\r\n        let id = el.id || &#39;anchor-&#39; + Math.random().toString(36).substr(2, 9);\r\n        el.id = id;\r\n\r\n        const anchorStyle = `\r\n            #${id} {\r\n                margin-top: calc(-1 * (${anchorValue || &#39;var(--anchor)&#39;}));\r\n                padding-top: ${anchorValue || &#39;var(--anchor)&#39;};\r\n            }\r\n        `;\r\n\r\n        try {\r\n            STYLE.sheet.insertRule(anchorStyle, STYLE.sheet.cssRules.length);\r\n        } catch (e) {\r\n            console.error(`Failed to insert CSS rule for element with id &quot;${id}&quot;: ${e.message}`);\r\n        }\r\n    });\r\n})();\r\n<\/code><\/pre>\r\n<p>\u3053\u306eJavaScript\u306f\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u52d5\u4f5c\u3057\u307e\u3059\uff1a<\/p>\r\n<ol style=\"list-style-type:decimal;\">\r\n  <li><code>anchor<\/code>\u5c5e\u6027\u3092\u6301\u3064\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u9078\u629e<\/li>\r\n  <li>\u65b0\u3057\u3044<code>&lt;style&gt;<\/code>\u8981\u7d20\u3092\u4f5c\u6210<\/li>\r\n  <li>\u5404\u8981\u7d20\u306b\u5bfe\u3057\u3066\uff1a<ul style=\"list-style-type:bullet;\">\r\n      <li><code>anchor<\/code>\u5c5e\u6027\u306e\u5024\u3092\u53d6\u5f97<\/li>\r\n      <li>\u30e6\u30cb\u30fc\u30af\u306aID\u3092\u78ba\u4fdd<\/li>\r\n      <li>CSS\u30eb\u30fc\u30eb\u3092\u751f\u6210<\/li>\r\n      <li>\u751f\u6210\u3057\u305f\u30eb\u30fc\u30eb\u3092\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u306b\u633f\u5165<\/li>\r\n    <\/ul>\r\n  <\/li>\r\n<\/ol>\r\n\r\n<h2 class=\"fz-xl\">\u4f7f\u3044\u65b9<\/h2>\r\n<ol style=\"list-style-type:decimal;\">\r\n  <li>CSS\u3092\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u306b\u8ffd\u52a0<\/li>\r\n  <li>JavaScript\u3092\u30da\u30fc\u30b8\u306b\u8aad\u307f\u8fbc\u3080<\/li>\r\n  <li>\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u3092\u8abf\u6574\u3057\u305f\u3044\u8981\u7d20\u306b<code>anchor<\/code>\u5c5e\u6027\u3092\u8ffd\u52a0<\/li>\r\n  <li>\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u3001\u5c5e\u6027\u5024\u3084\u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u3067\u500b\u5225\u306b\u8abf\u6574<\/li>\r\n<\/ol>\r\n\r\n<h2 class=\"fz-xl\">\u307e\u3068\u3081<\/h2>\r\n<strong class=\"fz-ml mt-1r\">\u5229\u70b9<\/strong>\r\n<ol style=\"list-style-type:decimal;\">\r\n  <li><b class=\"fw-600\">\u67d4\u8edf\u6027<\/b>: \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u306b\u8a2d\u5b9a\u3057\u3064\u3064\u3001\u500b\u5225\u306e\u8981\u7d20\u3067\u7c21\u5358\u306b\u8abf\u6574\u53ef\u80fd<\/li>\r\n  <li><b class=\"fw-600\">\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027<\/b>: \u30d8\u30c3\u30c0\u30fc\u306e\u9ad8\u3055\u304c\u5909\u66f4\u3055\u308c\u305f\u5834\u5408\u3001CSS\u5909\u6570\u3092\u66f4\u65b0\u3059\u308b\u3060\u3051\u3067\u5168\u4f53\u306b\u53cd\u6620<\/li>\r\n  <li><b class=\"fw-600\">\u518d\u5229\u7528\u6027<\/b>: \u3053\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u306f\u69d8\u3005\u306a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u518d\u5229\u7528\u53ef\u80fd<\/li>\r\n  <li><b class=\"fw-600\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<\/b>: JavaScript\u306b\u3088\u308bDOM\u64cd\u4f5c\u304c\u6700\u5c0f\u9650\u3067\u3001\u30da\u30fc\u30b8\u8aad\u307f\u8fbc\u307f\u6642\u306b\u4e00\u5ea6\u3060\u3051\u5b9f\u884c<\/li>\r\n  <li><b class=\"fw-600\">\u4e92\u63db\u6027<\/b>: <code>scroll-padding-top<\/code>\u306e\u30d6\u30e9\u30a6\u30b6\u30b5\u30dd\u30fc\u30c8\u306b\u4f9d\u5b58\u305b\u305a\u3001\u5e45\u5e83\u3044\u30d6\u30e9\u30a6\u30b6\u3067\u52d5\u4f5c<\/li>\r\n  <li><b class=\"fw-600\">\u30c7\u30b6\u30a4\u30ca\u30fc\u30d5\u30ec\u30f3\u30c9\u30ea\u30fc<\/b>: HTML\u306e\u5c5e\u6027\u3084\u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u3067\u7c21\u5358\u306b\u8abf\u6574\u53ef\u80fd<\/li>\r\n<\/ol>\r\n\r\n<p>\u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u306e\u30b8\u30e3\u30f3\u30d7\u5148\u4f4d\u7f6e\u3092\u67d4\u8edf\u304b\u3064\u52b9\u679c\u7684\u306b\u8abf\u6574\u3067\u304d\u307e\u3059\u3002CSS\u5909\u6570\u3068JavaScript\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u3088\u308a\u3001\u958b\u767a\u8005\u3068\u30c7\u30b6\u30a4\u30ca\u30fc\u306e\u4e21\u65b9\u306b\u3068\u3063\u3066\u4f7f\u3044\u3084\u3059\u3044\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u3092\u4f7f\u7528\u3059\u308b\u30ec\u30b9\u30dd\u30f3\u30b7\u30d6\u30c7\u30b6\u30a4\u30f3\u306e\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u3067\u7279\u306b\u6709\u7528\u3067\u3042\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u30a8\u30af\u30b9\u30da\u30ea\u30a8\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\r\n\r\n\r\n<h2 class=\"fz-xl\">\u95a2\u9023\u8a18\u4e8b<\/h2>\r\n<a href=\"https:\/\/code-plus.jp\/gp\/scroll-padding-top-10134\/\" class=\"linkbox my-1.5r\">\r\n<h4 class=\"linkbox-title fz-md\">scroll-padding-top\u306f\u30ea\u30ed\u30fc\u30c9\u6642\u306b\u30ba\u30ec\u308b\uff1fCSS\u5909\u6570\u3068JavaScript\u3067\u89e3\u6c7a\u3059\u308b\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u8abf\u6574<\/h4>\r\n<picture class=\"linkbox-thumbnail\">\r\n<img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/code-plus.jp\/gp\/wp-content\/uploads\/croll-p_2512181255.jpg\" alt=\"scroll-padding-top\u306f\u30ea\u30ed\u30fc\u30c9\u6642\u306b\u30ba\u30ec\u308b\uff1fCSS\u5909\u6570\u3068JavaScript\u3067\u89e3\u6c7a\u3059\u308b\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u8abf\u6574\" loading=\"lazy\" \/>\r\n<\/picture>\r\n<span class=\"linkbox-excerpt\">scroll-padding-top\u306f\u30ea\u30ed\u30fc\u30c9\u6642\u306b\u30ba\u30ec\u308b\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u539f\u56e0\u306f\u30d6\u30e9\u30a6\u30b6\u306e\u30b9\u30af\u30ed\u30fc\u30eb\u5fa9\u5143\u6a5f\u80fd\u3068\u306e\u7af6\u5408\u3002CSS\u5909\u6570\u3068JavaScript\u3092\u4f7f\u3063\u305f\u4ee3\u66ff\u624b\u6cd5\u3067\u3001\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u3067\u3082\u6b63\u78ba\u306a\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u4f4d\u7f6e\u8abf\u6574\u3092\u5b9f\u73fe\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\r\n&emsp;<u class=\"linkbox-more\">\u7d9a\u304d\u3092\u8aad\u3080<\/u>\r\n<\/span>\r\n<\/a>","protected":false},"excerpt":{"rendered":"\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u3067\u30a2\u30f3\u30ab\u30fc\u30ea\u30f3\u30af\u3092\u4f7f\u7528\u3059\u308b\u969b\u3001\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u304c\u3042\u308b\u3068\u76ee\u7684\u306e\u4f4d\u7f6e\u306b\u6b63\u78ba\u306b\u30b8\u30e3\u30f3\u30d7\u3067\u304d\u306a\u3044\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001CSS\u5909\u6570\u3068JavaScript\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u67d4\u8edf\u306a\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u89e3\u6c7a\u7b56\u306e\u6982[...]","protected":false},"author":1,"featured_media":10534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":"0"},"categories":[12],"tags":[],"group":[],"class_list":["post-6293","post","type-post","status-publish","format-standard","has-post-thumbnail","category-javascript"],"acf":[],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/6293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/comments?post=6293"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/6293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media\/10534"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=6293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=6293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=6293"},{"taxonomy":"group","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/group?post=6293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}