Tillitsdone
down Scroll to discover

CSS object-position Controlling Image Alignment

Learn how to use the CSS object-position property to control the alignment of images within their containers.

Explore available options like keywords, percentages, and lengths.
thumbnail

Introduction

The object-position property in CSS is a handy tool for web developers and designers. It lets you control the alignment of a replaced element’s content within its box. This is super useful for images and videos, allowing you to move the content around precisely.

When you use the object-position property, you can adjust where the content sits inside the element’s box. Any areas not covered by the content will show the element’s background. This property works well with the object-fit property, which adjusts the size of the replaced element to fit within the box. Together, these properties give you fine control over how your content is displayed, making your web design more dynamic and visually appealing.

In this guide, we’ll dive into the object-position property, covering its syntax, values, and practical examples. Whether you’re a pro or just starting out, this guide will help you master the object-position property and elevate your web design skills.

Specification

The object-position property is part of the CSS Images Module Level 3. This module enhances CSS capabilities for handling images and replaced elements like <img>, <video>, and others. The object-position property is a key feature, allowing precise control over content alignment within its container.

The specification for the object-position property can be found in the CSS Images Module Level 3 documentation. This property works seamlessly with other CSS properties, like object-fit, to provide comprehensive tools for styling and positioning media content.

By following the CSS Images Module Level 3, the object-position property ensures compatibility and consistent behavior across different web browsers. This makes it essential for creating responsive and visually appealing web designs that work well on various devices and platforms.

Description

The object-position property in CSS lets you control the alignment of a replaced element’s content within its box. This is especially useful for images and videos, allowing precise positioning of the content inside the element’s box.

By using the object-position property, you can move the content around within its container, ensuring it aligns perfectly with your design vision. Any areas of the box not covered by the replaced element’s content will display the element’s background, giving you flexibility in how you present your media.

The object-position property works hand in hand with the object-fit property, which adjusts the intrinsic size of the replaced element to fit within the element’s box. This combination offers a powerful way to manage the appearance and positioning of media content, making your web designs more dynamic and visually appealing.

In web development, the object-position property is a valuable tool for creating responsive and well-structured layouts. Whether you’re designing a website, a web application, or any other type of web content, mastering this property can help you achieve a polished and professional look.

Syntax

The object-position property in CSS is used to specify the alignment of a replaced element’s content within its box. The syntax is straightforward and flexible, allowing for various positioning options.

Here is the basic syntax for the object-position property:

/* Keyword values */
object-position: top;
object-position: bottom;
object-position: left;
object-position: right;
object-position: center;
/* <percentage> values */
object-position: 25% 75%;
/* <length> values */
object-position: 0 0;
object-position: 1cm 2cm;
object-position: 10ch 8em;
/* Edge offsets values */
object-position: bottom 10px right 20px;
object-position: right 3em bottom 10px;
object-position: top 0 right 10px;
/* Global values */
object-position: inherit;
object-position: initial;
object-position: revert;
object-position: revert-layer;
object-position: unset;

Explanation of Syntax Components

  • Keyword Values: These are predefined positions such as top, bottom, left, right, and center. They position the content to the specified edge or the center of the box.
  • Percentage Values: These values allow you to position the content using percentages relative to the width and height of the box. For example, object-position: 25% 75%; positions the content 25% from the left and 75% from the top.
  • Length Values: These values use specific units like pixels (px), centimeters (cm), characters (ch), or ems (em). For example, object-position: 10px 20px; positions the content 10 pixels from the left and 20 pixels from the top.
  • Edge Offsets Values: These values combine keywords with lengths to position the content relative to specific edges. For example, object-position: bottom 10px right 20px; positions the content 10 pixels from the bottom and 20 pixels from the right.
  • Global Values: These are standard CSS values that apply to most properties. They include inherit, initial, revert, revert-layer, and unset.

Usage

The object-position property is typically used with replaced elements such as <img>, <video>, <object>, and <embed>. By utilizing this property, you can fine-tune the positioning of these elements within their containers, ensuring a polished and professional appearance for your web designs.

In the following sections, we will explore the various values and examples of how to use the object-position property effectively in your CSS.

Values

The object-position property in CSS accepts a range of values that allow you to precisely control the positioning of a replaced element’s content within its box. These values include keywords, percentages, lengths, edge offsets, and global values. Let’s explore each type of value in detail:

Keyword Values

Keyword values are predefined positions that position the content relative to the edges or center of the box.

  • top: Positions the content at the top edge of the box.
  • bottom: Positions the content at the bottom edge of the box.
  • left: Positions the content at the left edge of the box.
  • right: Positions the content at the right edge of the box.
  • center: Positions the content at the center of the box.
object-position: top;
object-position: bottom;
object-position: left;
object-position: right;
object-position: center;

Percentage Values

Percentage values allow you to position the content using percentages relative to the width and height of the box. This gives you fine-grained control over the positioning.

object-position: 25% 75%;

In this example, the content is positioned 25% from the left edge and 75% from the top edge of the box.

Length Values

Length values use specific units like pixels (px), centimeters (cm), characters (ch), or ems (em) to position the content.

object-position: 0 0;
object-position: 1cm 2cm;
object-position: 10ch 8em;

In these examples, the content is positioned using specific measurements from the left and top edges of the box.

Edge Offsets Values

Edge offsets values combine keywords with lengths to position the content relative to specific edges. This provides a flexible way to position the content.

object-position: bottom 10px right 20px;
object-position: right 3em bottom 10px;
object-position: top 0 right 10px;

In these examples, the content is positioned 10 pixels from the bottom and 20 pixels from the right, 3 ems from the right and 10 pixels from the bottom, and at the top with 10 pixels from the right, respectively.

Global Values

Global values are standard CSS values that apply to most properties. They include:

  • inherit: Inherits the value from the parent element.
  • initial: Sets the value to its default value, which is 50% 50%.
  • revert: Resets the property to its default value as defined by the user agent’s stylesheet.
  • revert-layer: Resets the property to its default value as defined by the user agent’s stylesheet, but only if it is not overridden by a more specific rule.
  • unset: Resets the property to its natural value, which means it behaves as though it were not set.
object-position: inherit;
object-position: initial;
object-position: revert;
object-position: revert-layer;
object-position: unset;

Summary

The object-position property provides a versatile way to control the alignment of replaced elements within their boxes. By using keyword values, percentages, lengths, edge offsets, and global values, you can achieve precise positioning that enhances the visual appeal of your web designs. In the next sections, we will explore practical examples and see how the object-position property can be effectively used in real-world scenarios.

Formal Definition

The object-position property in CSS is formally defined to specify the alignment of a replaced element’s content within its box. This property is particularly useful for images and videos, enabling precise control over their positioning. Here are the key formal definitions associated with the object-position property:

  • Initial Value: 50% 50%
    • This means that by default, the content is centered within its box.
  • Applies To: Replaced elements
    • This property is applicable to elements like <img>, <video>, <object>, and <embed>.
  • Inherited: Yes
    • This property can inherit its value from the parent element.
  • Percentages: Refer to the width and height of the element itself
    • When using percentage values, they are relative to the dimensions of the containing element.
  • Computed Value: As specified
    • The computed value is the same as the specified value.
  • Animation Type: A repeatable list
    • This property can be animated using CSS animations.

Formal Syntax

The formal syntax for the object-position property is defined as follows:

object-position = <position>

Where <position> can be:

  • Keyword Values: top, bottom, left, right, center
  • Percentage Values: <percentage>
  • Length Values: <length>
  • Edge Offsets Values: Combination of keywords and lengths (e.g., bottom 10px right 20px)
  • Global Values: inherit, initial, revert, revert-layer, unset

Detailed Syntax Definition

<position> =
[ left | center | right | top | bottom | <length-percentage> ]
[ | left | center | right ] [&& [ top | center | bottom ] <length-percentage> ]
<length-percentage> = <length> | <percentage>
  • Left | Center | Right | Top | Bottom: Keyword values for positioning.
  • <length-percentage>: Length or percentage values for positioning.
  • &&: Indicates that all specified entities must be present, in any order.

Explanation of Syntax Components

  • Keyword Values: These are predefined positions such as top, bottom, left, right, and center. They position the content to the specified edge or the center of the box.
  • Percentage Values: These values allow you to position the content using percentages relative to the width and height of the box. For example, object-position: 25% 75%; positions the content 25% from the left and 75% from the top.
  • Length Values: These values use specific units like pixels (px), centimeters (cm), characters (ch), or ems (em). For example, object-position: 10px 20px; positions the content 10 pixels from the left and 20 pixels from the top.
  • Edge Offsets Values: These values combine keywords with lengths to position the content relative to specific edges. For example, object-position: bottom 10px right 20px; positions the content 10 pixels from the bottom and 20 pixels from the right.
  • Global Values: These are standard CSS values that apply to most properties. They include inherit, initial, revert, revert-layer, and unset.

Summary

The formal definition and syntax of the object-position property provide a clear and structured way to control the alignment of replaced elements within their boxes. By understanding these definitions, you can effectively use the object-position property to create visually appealing and well-structured web designs. In the following sections, we will explore practical examples and see how this property can be applied in real-world scenarios.

Examples

Let’s dive into some practical examples to see how the object-position property can be used effectively in real-world scenarios.

Example 1: Basic Keyword Positioning

In this example, we’ll use the keyword values to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: top;
}
</style>
</head>
<body>
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</body>
</html>

In this example, the image is positioned at the top of its container using the object-position: top; property.

Example 2: Percentage Positioning

In this example, we’ll use percentage values to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 25% 75%;
}
</style>
</head>
<body>
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</body>
</html>

In this example, the image is positioned 25% from the left and 75% from the top of its container using the object-position: 25% 75%; property.

Example 3: Length Positioning

In this example, we’ll use length values to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 10px 20px;
}
</style>
</head>
<body>
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</body>
</html>

In this example, the image is positioned 10 pixels from the left and 20 pixels from the top of its container using the object-position: 10px 20px; property.

Example 4: Edge Offsets Positioning

In this example, we’ll use edge offsets values to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: bottom 10px right 20px;
}
</style>
</head>
<body>
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</body>
</html>

In this example, the image is positioned 10 pixels from the bottom and 20 pixels from the right of its container using the object-position: bottom 10px right 20px; property.

Example 5: Using the Initial Value

In this example, we’ll use the initial value to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: initial;
}
</style>
</head>
<body>
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</body>
</html>

In this example, the image is positioned using the initial value of the object-position property, which centers the image within its container.

Example 6: Inheriting from a Parent Element

In this example, we’ll use the inherit value to position an image within its container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Position Example</title>
<style>
#parent {
object-position: 60% 80%;
}
.container {
width: 300px;
height: 300px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: inherit;
}
</style>
</head>
<body>
<div id="parent">
<div class="container">
<img src="WebsiteUrl" alt="Sample Image">
</div>
</div>
</body>
</html>

In this example, the image inherits the object-position value from its parent element, which positions the image 60% from the left and 80% from the top of its container.

Summary

These examples show how versatile the object-position property is in CSS. You can use keyword values, percentages, lengths, edge offsets, and global values to precisely position replaced elements within their boxes. This property is especially useful for images and videos, allowing you to create visually appealing and well-structured web designs. In the next section, we’ll explore browser compatibility and related properties.

Browser Compatibility

The object-position property in CSS is well-supported across modern web browsers, making it a reliable tool for web developers and designers. Here’s a quick overview of browser compatibility:

Chrome

  • Version: 31.0
  • Release Date: November 2013

Firefox

  • Version: 36.0
  • Release Date: February 2015

Internet Explorer / Edge

  • Version: 16.0 (Edge)
  • Release Date: September 2017

Opera

  • Version: 19.0
  • Release Date: January 2014

Safari

  • Version: 10.0
  • Release Date: September 2016

Summary

The object-position property is widely supported in all major browsers, ensuring consistent display across different platforms. This makes it a valuable tool for creating responsive and visually appealing web content.

See Also

If you want to learn more about CSS properties related to image manipulation and positioning, check out these resources:

  1. CSS object-fit Property
    • Use object-fit with object-position to control how an image or video is resized to fit within its container.
  2. CSS image-orientation Property
    • Control the orientation of an image, useful for correcting images taken with cameras or smartphones.
  3. CSS image-rendering Property
    • Control the scaling algorithm used when an image is resized.
  4. CSS image-resolution Property
    • Specify the resolution of an image, useful for optimizing high-resolution images on different devices.
  5. HTML Image Tutorial
    • A comprehensive guide to working with images in HTML, covering different formats, inclusion methods, and best practices.
  6. CSS width Property Reference
    • Set the width of an element, crucial for controlling the dimensions of containers.
  7. CSS height Property Reference
    • Set the height of an element, important for defining the size of elements and ensuring media is displayed correctly.

These resources will help you understand how to use CSS properties to manage and enhance the appearance of images and other media content in your web designs. Mastering these properties will allow you to create visually appealing and responsive web pages.

Supported Browsers

The object-position property is supported across a wide range of web browsers, making it a reliable choice for web developers and designers. Here is a list of major browsers and their respective versions that support the object-position property:

  • Google Chrome: Version 31.0 and later (Released in November 2013)
  • Mozilla Firefox: Version 36.0 and later (Released in February 2015)
  • Microsoft Edge: Version 16.0 and later (Released in September 2017)
  • Opera: Version 19.0 and later (Released in January 2014)
  • Safari: Version 10.0 and later (Released in September 2016)

Summary

The broad support for the object-position property across these major browsers ensures that your web designs will display consistently and correctly on various platforms. This makes the object-position property a valuable tool for creating responsive and visually appealing web content. By using this property, you can achieve precise control over the alignment of replaced elements within their boxes, enhancing the overall user experience.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.