CSS aspect-ratio پراپرٹی جنریٹر

استعمال کا طریقہ 3 مراحل
  1. تناسب چنیں، یا اپنی مرضی کا تناسب لکھیں۔
  2. پیش منظر کا خانہ دیکھیں۔
  3. تناسب کا CSS نقل کریں دبائیں اور اپنی اسٹائل شیٹ میں چسپاں کریں۔
تیار کردہ CSS کوڈ

یہ ٹول کیسے کام کرتا ہے

aspect-ratio ڈبے کو ایک مقررہ تناسب پر تھامے رکھتا ہے: چوڑائی دیں تو اونچائی خود آ جاتی ہے، یا اونچائی دیں تو چوڑائی۔ اس خصوصیت کے وجود میں آنے سے پہلے یہی نتیجہ padding والی چال سے حاصل ہوتا تھا، جہاں عنصر کا اوپری وقفہ اس کی چوڑائی کے فیصد کے طور پر مقرر کیا جاتا اور اصل مواد اوپر مطلق طور پر رکھ دیا جاتا۔ اس خصوصیت نے کئی سطروں کے گھمانے پھرانے کی جگہ ایک اعلان لے لیا۔

متبادل بلاک اب بھی بنایا جاتا ہے، اور اس کا وقفہ آپ کے چنے ہوئے تناسب سے نکالا جاتا ہے، فرض نہیں کیا جاتا۔ یہ اس لیے اہم ہے کہ فیصد خود تناسب کے الٹ ہوتا ہے: 16:9 بن کر 56.25% ہوتا ہے، مگر 4:3 بن کر 75% اور 21:9 بن کر 42.86%۔ غلط فیصد اٹھائے ہوئے متبادل ڈبے کو غلط شکل میں بچھا دیتا ہے جبکہ اسٹائل شیٹ میں بالکل درست دکھتا ہے۔

تناسب کہاں قائم رہتا ہے اور کہاں ہار مان لیتا ہے

  • مواد اسے رد کر سکتا ہے: تناسب مرغوب سائز مقرر کرتا ہے، سخت حد نہیں۔ جس عنصر کا مواد تناسب کی اجازت سے اونچا ہو وہ بڑھ جائے گا، جب تک overflow یا مقررہ اونچائی اسے نہ روکے۔
  • padding کا فیصد الٹا ہوتا ہے: یہ اونچائی کو چوڑائی کے کسر کے طور پر بیان کرتا ہے، اس لیے چوڑا تناسب چھوٹا فیصد دیتا ہے۔ ہاتھ سے کرنے میں یہی قدم سب سے آسانی سے غلط ہوتا ہے۔
  • تصویروں کو اس کے ساتھ object-fit چاہیے: ڈبہ اپنی شکل تو تھامے رکھتا ہے مگر اندر کی تصویر بگڑ جائے گی جب تک object-fit: cover یا contain یہ نہ بتائے کہ اسے جگہ کیسے بھرنی ہے۔
  • یہ صرف ویڈیو کے لیے نہیں: کارڈ، اوتار، نقشے کے خانے اور تصویروں کے گرڈ سب کو مقررہ تناسب کا فائدہ ہوتا ہے، اور یہی وہ چیز ہے جو ہر تصویر لدنے پر خاکے کو ہلنے سے روکتی ہے۔

One declaration replacing a workaround

aspect-ratio holds a box at a fixed proportion: give it a width and the height follows, or give it a height and the width does.

Before it existed, the same result needed the padding trick — an element with top padding set to a percentage of its width, and the real content positioned absolutely on top of it. Several lines of workaround, two elements where one would do, and a percentage nobody could read at a glance. The [msm_tool slug="css-aspect-ratio"] generates both, so the fallback is there if you need it.

The fallback percentage is inverted

This is the part that goes wrong by hand, and it goes wrong silently.

The padding trick expresses height as a percentage of width, so the number is the inverse of the ratio rather than the ratio itself. 16:9 becomes 56.25%. But 4:3 becomes 75%, and 21:9 becomes 42.86%.

Copying 56.25% from a snippet and changing the aspect-ratio line to 4:3 produces a fallback that lays out at widescreen while the stylesheet claims it is a classic photo shape. It looks entirely correct in the code and wrong on any browser that uses the fallback.

Content can override it

The property expresses a preference, not a hard limit. An element whose contents are taller than the ratio allows will grow to fit them.

That is usually what you want — text should not be clipped because a box wanted to be square — but it means a grid of cards set to 1:1 can still come out uneven if one card has more text. If the ratio must hold regardless, pair it with overflow: hidden or a fixed height, and accept that something will be cut off.

Images need object-fit alongside it

Setting a ratio on an image element holds the box at that shape and stretches the image to fill it, which distorts anything whose natural proportions differ.

object-fit: cover crops to fill the shape and is what you want for photographs in a grid. object-fit: contain fits the whole image inside with space around it, which suits logos and product shots where nothing should be cut off. Without one of them, the ratio is a distortion tool.

object-position is the companion worth knowing: cover crops from the centre by default, so a subject near the top of a photograph loses its head in a wide crop unless you shift the position.

Its best use is stopping layout shift

An image with no dimensions occupies no space until it loads, at which point everything below it jumps. That is a measurable and widely penalised problem, and it is the reason width and height attributes came back into fashion on <img> tags.

aspect-ratio does the same job in CSS, and does it for elements that are not images: embedded video, map panels, ad slots, chart containers. Reserve the space and nothing moves when the content arrives.

Common ratios and where they come from

  • 16:9 — video, and by extension most embeds and thumbnails.
  • 4:3 — older photography and screens; still common for product images.
  • 3:2 — the 35mm frame, and what most cameras still produce.
  • 1:1 — avatars, product grids, and most social feeds.
  • 1.91:1 — the shape most social platforms crop link previews to.
  • 9:16 — vertical video.

These are conventions rather than rules, but anything displaying your image will crop toward one of them. Choosing a ratio the destination does not expect means the crop happens somewhere you do not control.

Support

Broadly supported for years now, and there is no strong reason to avoid it. The generated fallback is there for genuinely old browsers — wrapped in a @supports not block so it applies only where the property is missing, rather than fighting with it everywhere else.

اکثر پوچھے گئے سوالات

خاص طور پر CSS aspect-ratio پراپرٹی جنریٹر کے بارے میں۔ اس زمرے سے متعلق عمومی سوالات کے جواب زمرے کے صفحے پر موجود ہیں۔

اس کا مواد تناسب کی اجازت سے اونچا ہے۔ یہ خصوصیت ترجیح بیان کرتی ہے، اور مواد جیت جاتا ہے جب تک مقررہ اونچائی یا overflow کا اصول اسے نہ روکے۔
صرف واقعی پرانے براؤزروں کے لیے — برسوں سے وسیع تعاون موجود ہے۔ یہ اس لیے بنایا جاتا ہے کہ آپ فیصلہ کر سکیں، اور اب اس کا وقفہ آپ کے چنے ہوئے تناسب سے میل کھاتا ہے۔
یہ اونچائی کو چوڑائی کے کسر کے طور پر بیان کرتا ہے، اس لیے یہ الٹ ہے۔ 16:9 بن کر 56.25% اور 4:3 بن کر 75% ہو جاتا ہے۔
شکل کے مطابق کاٹنے کے لیے object-fit: cover لگائیں، یا پوری تصویر کو اندر سمانے اور گرد جگہ چھوڑنے کے لیے contain۔
جی ہاں، اور یہ اس کی بہتر وجوہات میں سے ایک ہے۔ ڈبہ تصویر آنے سے پہلے ہی اپنی جگہ روک لیتا ہے، اس لیے نیچے کی کوئی چیز نہیں اچھلتی۔

نئے ٹولز، خصوصیات اور کارکردگی بڑھانے کے مشوروں کی اطلاع پانے کے لیے سبسکرائب کریں۔