CSS Portal

:right CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :right pseudo-class in CSS is used to select elements that are aligned or positioned on the right side within their containing block. While it is less commonly used than pseudo-classes like :hover or :first-child, it can be valuable when working with text alignment, directional layouts, or elements that need styling based on their placement relative to the right edge of a container.

This pseudo-class is particularly useful in designs that require different styling for right-aligned elements. For instance, in right-to-left (RTL) language layouts, elements that appear visually on the right side might need unique styles, such as different margin, padding, or text-align adjustments. It allows developers to apply styles conditionally without adding extra classes or relying solely on JavaScript.

Additionally, :right can interact with other pseudo-classes and elements for more advanced styling. For example, it can be combined with :nth-child() to style every right-most element in a sequence or used alongside div containers to visually balance layouts.

Here is a practical example:

<div class="container">
  <p class="align-left">Left-aligned text</p>
  <p class="align-right">Right-aligned text</p>
</div>
p:right {
  text-align: right;
  background-color: #f0f0f0;
  padding: 10px;
}

In this example, only the paragraph that is positioned on the right side of its container receives the right alignment and background styling. This makes it easier to maintain consistent styling for right-aligned elements across a webpage without adding extra classes manually.

Another practical use is in tables, where you might want numeric values or dates to automatically align to the right:

td:right {
  text-align: right;
  font-weight: bold;
}

Overall, :right provides a semantic, CSS-native way to target elements based on their visual alignment rather than structural classes, helping maintain cleaner HTML and more responsive designs.

Syntax

:right {
  /* ... */
}

Example

<body>

<section class="page-break">
<h1>Page 1 (Right Page)</h1>
<p>In a double-sided print layout, the first page is usually a "right" page.
Notice the larger left margin when you print this.</p>
</section>

<section class="page-break">
<h1>Page 2 (Left Page)</h1>
<p>This page uses the standard @page margins or the :left pseudo-class if defined.</p>
</section>

<section class="page-break">
<h1>Page 3 (Right Page)</h1>
<p>The :right styling applies here again automatically.</p>
</section>

</body>




CSS :right Pseudo-class Example




Page 1 (Right Page)


In a double-sided print layout, the first page is usually a "right" page.
Notice the larger left margin when you print this.





Page 2 (Left Page)


This page uses the standard @page margins or the :left pseudo-class if defined.





Page 3 (Right Page)


The :right styling applies here again automatically.





Browser Support

The following information will show you the current browser support for the CSS :right pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported in some modern browsers, but not all.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!