---
slug: "cssでアスペクト比を固定するless"
title: "CSSでアスペクト比を固定する(LESS)"
description: "よくあるやつですが。いつも忘れてしまうので。"
url: "https://www.ytyng.com/blog/cssでアスペクト比を固定するless"
publish_date: "2017-06-18T14:07:59Z"
created: "2017-06-18T14:07:59Z"
updated: "2026-02-27T05:43:40.234Z"
categories: ["css"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/72f217281ec54adf8530e30e1897bd84.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "ja"
---

# CSSでアスペクト比を固定する(LESS)

<p>よくあるやつですが。いつも忘れてしまうので。</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;">.<span style="color: #000080; font-weight: bold;">aspect-ratio</span>(<span style="color: #660e7a; font-weight: bold;">@ratio</span>) {<br />  <span style="color: #0000ff; font-weight: bold;">position</span>: <span style="color: #008000; font-weight: bold;">relative</span>;<br />  &amp;:<span style="color: #000080; font-weight: bold;">before </span>{<br />    <span style="color: #0000ff; font-weight: bold;">content</span>: <span style="color: #008000; font-weight: bold;">""</span>;<br />    <span style="color: #0000ff; font-weight: bold;">display</span>: <span style="color: #008000; font-weight: bold;">block</span>;<br />    <span style="color: #0000ff; font-weight: bold;">padding-top</span>: <span style="color: #660e7a; font-weight: bold;">@ratio</span>;<br />  }<br />  &gt; .<span style="color: #000080; font-weight: bold;">content </span>{<br />    <span style="color: #0000ff; font-weight: bold;">position</span>: <span style="color: #008000; font-weight: bold;">absolute</span>;<br />    <span style="color: #0000ff; font-weight: bold;">top</span>: <span style="color: #0000ff;">0</span>;<br />    <span style="color: #0000ff; font-weight: bold;">left</span>: <span style="color: #0000ff;">0</span>;<br />    <span style="color: #0000ff; font-weight: bold;">right</span>: <span style="color: #0000ff;">0</span>;<br />    <span style="color: #0000ff; font-weight: bold;">bottom</span>: <span style="color: #0000ff;">0</span>;<br />    <span style="color: #0000ff; font-weight: bold;">width</span>: <span style="color: #0000ff;">100</span>%;<br />    <span style="color: #0000ff; font-weight: bold;">height</span>: <span style="color: #0000ff;">100</span>%;<br />  }<br />}<br /><br />.<span style="color: #000080; font-weight: bold;">aspect-ratio</span>(<span style="color: #660e7a; font-weight: bold;">@width</span>, <span style="color: #660e7a; font-weight: bold;">@height</span>) {<br />  .<span style="color: #000080; font-weight: bold;">aspect-ratio</span>(<span style="color: #660e7a; font-weight: bold;">@height </span>/ <span style="color: #660e7a; font-weight: bold;">@width </span>* <span style="color: #0000ff;">100</span>%);<br />}</pre>
<p style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"></p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;">.<span style="color: #000080; font-weight: bold;">pickup-article-image-box </span>{<br />  .<span style="color: #000080; font-weight: bold;">aspect-ratio</span>(<span style="color: #0000ff;">16</span>, <span style="color: #0000ff;">9</span>);<br /><br />  <span style="color: #000080; font-weight: bold;">img </span>{<br />    <span style="color: #808080; font-style: italic;">// object-fit: contain;  // アスペクトフィット<br /></span><span style="color: #808080; font-style: italic;">    </span><span style="color: #0000ff; font-weight: bold;">object-fit</span>: <span style="color: #008000; font-weight: bold;">cover</span>;  // アスペクトフィル<br />  }<br />}</pre>
<p></p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"> <span style="background-color: #efefef;">&lt;</span><span style="color: #000080; background-color: #efefef; font-weight: bold;">div </span><span style="color: #0000ff; background-color: #efefef; font-weight: bold;">class=</span><span style="color: #008000; background-color: #efefef; font-weight: bold;">"pickup-article-image-box"</span><span style="background-color: #efefef;">&gt;</span><span style="background-color: #efefef;"><br /></span>   <span style="background-color: #efefef;">&lt;</span><span style="color: #000080; background-color: #efefef; font-weight: bold;">img </span><span style="color: #0000ff; background-color: #efefef; font-weight: bold;">class=</span><span style="color: #008000; background-color: #efefef; font-weight: bold;">"unveil content" </span><span style="color: #0000ff; background-color: #efefef; font-weight: bold;">data-src=</span><span style="color: #008000; background-color: #efefef; font-weight: bold;">"</span><span style="background-color: #f7faff;">{{ post.image_url }}</span><span style="color: #008000; background-color: #efefef; font-weight: bold;">" </span><span style="background-color: #efefef;">/&gt;</span><span style="background-color: #efefef;"><br /></span> <span style="background-color: #efefef;">&lt;/</span><span style="color: #000080; background-color: #efefef; font-weight: bold;">div</span><span style="background-color: #efefef;">&gt;</span></pre>
<p style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"></p>
