Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial review: CSS anchor positioning 4: sizing and positioning part 2 #33511

Merged
merged 30 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fc39b9a
Add anchor-size() function ref page
chrisdavidmills May 9, 2024
7fddda7
Add anchor-size() details to relevant property pages
chrisdavidmills May 9, 2024
143df3f
Add inset-area ref page
chrisdavidmills May 11, 2024
df5262c
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills May 11, 2024
19e47ab
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills May 15, 2024
a8a67ff
Fixes for mfreed7 review comments
chrisdavidmills May 15, 2024
989e788
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills May 15, 2024
4e4af03
Update files/en-us/web/css/inset-area/index.md
chrisdavidmills May 15, 2024
2e5bf9a
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills May 21, 2024
e13e118
Change anchor names to make them more distinct and less confusing
chrisdavidmills May 31, 2024
b1ff88c
Fix wording and casing for reference links
chrisdavidmills May 31, 2024
dc680e0
Anchor name consistency
chrisdavidmills May 31, 2024
48ff49a
update anchor-size() fallback description
chrisdavidmills May 31, 2024
800cd21
estelle comments answered, inset-area type page added
chrisdavidmills Jun 5, 2024
f914db6
Update files/en-us/web/css/anchor-size/index.md
chrisdavidmills Jun 5, 2024
4690bcb
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills Jun 5, 2024
7c4025d
fix relative link error
chrisdavidmills Jun 5, 2024
9381333
Update syntax section for consistency
chrisdavidmills Jun 14, 2024
94385c0
Simply CSS explanation
chrisdavidmills Jun 14, 2024
78ac1c0
improve see also links
chrisdavidmills Jun 14, 2024
40ed111
Merge branch 'main' into css-anchor-positioning-4
estelle Jun 25, 2024
2e7ceff
remove bolding from words not subsequently defined
estelle Jun 26, 2024
e9b4290
removed from property values; integrated into length syntax.
estelle Jun 26, 2024
cfb7bf1
integrated into length syntax.
estelle Jun 26, 2024
1c7557e
anchor-size guide
estelle Jun 27, 2024
ed12f90
anchor-size guide - reread edits
estelle Jun 27, 2024
693e118
Merge pull request #10 from mdn/ap4-1
chrisdavidmills Jun 27, 2024
67e9862
few final tweaks to anchor-size()
chrisdavidmills Jun 27, 2024
3196892
Merge branch 'main' into css-anchor-positioning-4
chrisdavidmills Jul 2, 2024
7de2557
remove inset-area property and value pages
chrisdavidmills Jul 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions files/en-us/web/css/anchor-size/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
---
title: anchor-size()
slug: Web/CSS/anchor-size
page-type: css-function
browser-compat: css.types.anchor-size
---

{{CSSRef}}

The **`anchor-size()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) can be used as a value for an **anchor-positioned** element's [sizing properties](#properties_that_accept_anchor-size_function_values), to size it relative to the dimensions of its associated **anchor element**.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define anchor element, maybe:

Suggested change
The **`anchor-size()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) can be used as a value for an **anchor-positioned** element's [sizing properties](#properties_that_accept_anchor-size_function_values), to size it relative to the dimensions of its associated **anchor element**.
The **`anchor-size()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) can be used as a value for an **anchor-positioned** element's [sizing properties](#properties_that_accept_anchor-size_function_values), to size it relative to the dimensions of its associated **anchor element**, the element you want to size an element relative to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is needed. We've got links for more information for people that are confused as to what this means.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. will debold.


For detailed information on anchor usage, see the [CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning) module landing page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For detailed information on anchor usage, see the [CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning) module landing page.
For detailed information on anchor usage, see the [CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning/Using) guide.

The module page should just be a list of links to everything mentioned in the module. I see in the see also you're creating a guide, so that is likely where we want to link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed this.


## Syntax

```css
/* property: anchor-size(anchor-size) */
width: anchor-size(width);
block-size: anchor-size(block);

/* property: anchor-size(anchor-element anchor-size) */
width: anchor-size(--my-anchor width);
block-size: anchor-size(--my-anchor block);

/* property: anchor-size(anchor-element anchor-size, length-percentage) */
width: anchor-size(--my-anchor width, 50%);
block-size: anchor-size(--my-anchor block, 200px);
```

### Parameters

The `anchor-size()` function's syntax is as follows:

```text
anchor(anchor-element anchor-size, length-percentage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
anchor(anchor-element anchor-size, length-percentage)
anchor-size(anchor-element anchor-size, length-percentage)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, fixed!

```

The parameters are:

- `anchor-element` {{optional_inline}}
- : The {{cssxref("anchor-name")}} set on the anchor element you want to size an element relative to. This is optional — if omitted, the positioned element is sized relative to the anchor referenced by its {{cssxref("position-anchor")}} property (this is sometimes referred to as the element's **default anchor**).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as anchor()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

- `anchor-size`

- : Specifies the dimension of the anchor element that the positioned element will be sized relative to. This can be expressed using the following values:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as anchor()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


- Physical values:
- `width`: The width of the anchor element.
- `height`: The height of the anchor element.
- Logical values:
- `block`: The length of the anchor element's [containing block](/en-US/docs/Web/CSS/Containing_block) in the block direction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `block`: The length of the anchor element's [containing block](/en-US/docs/Web/CSS/Containing_block) in the block direction.
- `block`: The block size of the anchor element's [containing block](/en-US/docs/Web/CSS/Containing_block).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this change. It is more concise but harder to understand.

- `inline`: The length of the anchor element's containing block in the inline direction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `inline`: The length of the anchor element's containing block in the inline direction.
- `inline`: The inline-size of the anchor element's containing block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this change. It is more concise but harder to understand.

- `self-block`: The length of the anchor element in the block direction.
- `self-inline`: The length of the anchor element in the inline direction.

- {{cssxref("length-percentage")}} {{optional_inline}}
- : This is a fallback value that specifies what the function should return if the `anchor()` function is invalid.

> **Note:** The anchor size you size the positioned element relative to does not have to be along the same axis as the sizing value being set. For example, `width: anchor-size(height)` is valid.

### Return value

Returns a {{cssxref("length")}} value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as anchor()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same counter comment as for anchor()


## Description

The `anchor()` function enables a positioned element's sizing values to be expressed in terms of an anchor element's dimensions.

### Properties that accept `anchor-size()` function values

The properties that can take an `anchor-size()` function as a value are as follows:

- Physical sizing properties: {{cssxref("width")}}, {{cssxref("height")}}, {{cssxref("min-width")}}, {{cssxref("min-height")}}, {{cssxref("max-width")}}, and {{cssxref("max-height")}}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

- Logical sizing properties: {{cssxref("block-size")}}, {{cssxref("inline-size")}}, {{cssxref("min-block-size")}}, {{cssxref("min-inline-size")}}, {{cssxref("max-block-size")}}, and {{cssxref("max-inline-size")}}.

### Using `anchor-size()` inside `calc()`

The most common `anchor-size()` functions you'll use will just refer to a dimension of the default anchor. You could also use `anchor-size()` functions inside {{cssxref("calc")}} functions to modify the size applied to the positioned element. For example:

```css
.elem {
/* Size the positioned element's width
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as last week: comments before the code, not in them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said previously, I disagree with this. I see the point of not having code comments in the syntax section (except for the category markers), but I don't see a problem with having some comments in code, in situations where you want to provide a quick hint as to what it is doing. Having everything as text can make it larger and harder to follow.

equal to the anchor element's width */
width: anchor-size(width);
}
```

```css
.elem {
/* Size the positioned element's inline size
equal to 4 times the anchor element's inline size */
inline-size: calc(anchor-size(self-inline) * 4);
}
```

### Formal syntax

{{csssyntax}}

## Examples

### Basic `anchor-size()` usage

This example shows two elements positioned relative to an anchor, which are sized using `anchor-size()` functions.

#### HTML

In the HTML, we specify three {{htmlelement("div")}} elements, one with a class of `anchor` and two with a class of `infobox`. These are intended to be the anchor element and the positioned elements we will associate with it, respectively. The positioned elements also have IDs to allow separate styling to easily be applied.

We also include some filler text around the `<div>`s to make the {{htmlelement("body")}} taller so that it will scroll.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the HTML, we specify three {{htmlelement("div")}} elements, one with a class of `anchor` and two with a class of `infobox`. These are intended to be the anchor element and the positioned elements we will associate with it, respectively. The positioned elements also have IDs to allow separate styling to easily be applied.
We also include some filler text around the `<div>`s to make the {{htmlelement("body")}} taller so that it will scroll.
We specify three {{htmlelement("div")}} elements, one `anchor` element and the two `infobox` elements we'll anchor position. We added filler text to make the {{htmlelement("body")}} tall enough to require scrolling.

shorter and just the important stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like it; updated


```html
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>
Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. In arcu
cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet
aliquam.
</p>

<div class="anchor">⚓︎</div>

<div class="infobox" id="infobox1">
<p>This is the first infobox.</p>
</div>

<div class="infobox" id="infobox2">
<p>This is the second infobox.</p>
</div>

<p>
Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit
scelerisque mauris pellentesque pulvinar pellentesque habitant morbi
tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac
habitasse platea dictumst quisque. Dolor sit amet consectetur adipiscing elit.
Ornare lectus sit amet est placerat. Nulla aliquet porttitor lacus luctus
accumsan.
</p>
```

#### CSS

In the CSS, we first declare the `anchor` `<div>` as an anchor element by setting an anchor name on it via the {{cssxref("anchor-name")}} property:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the CSS, we first declare the `anchor` `<div>` as an anchor element by setting an anchor name on it via the {{cssxref("anchor-name")}} property:
In the CSS, we first declare the `anchor` `<div>` as an anchor element by giving it an anchor name using the {{cssxref("anchor-name")}} property:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


```css hidden
.anchor {
font-size: 2rem;
color: white;
text-shadow: 1px 1px 1px black;
background-color: hsl(240 100% 75%);
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
border-radius: 10px;
border: 1px solid black;
padding: 3px;
}

body {
width: 80%;
margin: 0 auto;
}
```

```css
.anchor {
anchor-name: --infobox;
}
```

The positioned elements are then associated with the anchor element by setting its anchor name as the value of the positioned elements' `position-anchor` properties. We also set the positioned elements' {{cssxref("position")}} properties to `fixed`, so they can be positioned relative to the anchor's position on the page.

```css hidden
.infobox {
color: darkblue;
background-color: azure;
border: 1px solid #ddd;
padding: 10px;
border-radius: 10px;
font-size: 1rem;
text-align: center;
}
```

```css
.infobox {
position-anchor: --infobox;
position: fixed;
}
```

Finally, we set some distinct property values on the positioned elements separately:

- Different {{cssxref("inset-area")}} values position the elements in different places around the anchor element.
- The first positioned element has its {{cssxref("width")}} set to be double the anchor element's width. `anchor-size(width)` is used to retrieve the anchor element width, and the result is multiplied by two inside a {{cssxref("calc()")}} function. The second positioned element has its {{cssxref("height")}} set to be two-thirds of the anchor element's height, using a similar technique.
- Appropriate margin values are set on the positioned elements to give them some separation from the anchor element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Finally, we set some distinct property values on the positioned elements separately:
- Different {{cssxref("inset-area")}} values position the elements in different places around the anchor element.
- The first positioned element has its {{cssxref("width")}} set to be double the anchor element's width. `anchor-size(width)` is used to retrieve the anchor element width, and the result is multiplied by two inside a {{cssxref("calc()")}} function. The second positioned element has its {{cssxref("height")}} set to be two-thirds of the anchor element's height, using a similar technique.
- Appropriate margin values are set on the positioned elements to give them some separation from the anchor element.
Finally, we set some distinct property values on the positioned elements. We set different {{cssxref("inset-area")}} values to position the elements in different places relative to the anchor element. The {{cssxref("width")}} on the first infobox sets it to be twice as wide as the anchor by using the`anchor-size(width)`, which returns the anchor element width, within a {{cssxref("calc()")}} function. The second positioned element has its {{cssxref("height")}} set to be two-thirds of the anchor element's height, using a similar technique. We also added appropriate margin values to separate the positioned elements from their associated anchor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this as one big paragraph; I think it is harder to parse. I've taken some of your suggestions and made the bullets more concise.


```css
#infobox1 {
inset-area: right;
width: calc(anchor-size(width) * 2);
margin-left: 5px;
}

#infobox2 {
inset-area: top span-left;
height: calc(anchor-size(height) / 1.5);
margin-bottom: 5px;
}
```

#### Result

The result is as follows.

{{EmbedLiveSample("Basic `anchor-size()` usage", "100%", "240")}}

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- [CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning)
- [Using CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning/Using)
4 changes: 4 additions & 0 deletions files/en-us/web/css/block-size/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ block-size: fit-content;
block-size: fit-content(20em);
block-size: auto;

/* anchor-size() function values */
block-size: anchor-size(height);
block-size: anchor-size(--my-anchor block);

/* Global values */
block-size: inherit;
block-size: initial;
Expand Down
7 changes: 7 additions & 0 deletions files/en-us/web/css/height/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ height: fit-content;
height: fit-content(20em);
height: auto;

/* anchor-size() function values */
height: anchor-size(height);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give these different values from block size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

height: anchor-size(--my-anchor block);

/* Global values */
height: inherit;
height: initial;
Expand All @@ -57,6 +61,8 @@ height: unset;
- : Uses the fit-content formula with the available space replaced by the specified argument, i.e. `min(max-content, max(min-content, <length-percentage>))`.
- {{cssxref("clamp", "clamp()")}}
- : Enables selecting a middle value within a range of values between a defined minimum and maximum.
- {{cssxref("anchor-size()")}} {{experimental_inline}}
- : Resolves to a {{cssxref("&lt;length&gt;")}} value relative to a dimension of an element's associated **anchor element**.

## Accessibility concerns

Expand Down Expand Up @@ -133,3 +139,4 @@ div {
- {{cssxref("box-sizing")}}
- {{cssxref("min-height")}}, {{cssxref("max-height")}}
- The mapped logical properties: {{cssxref("block-size")}}, {{cssxref("inline-size")}}
- [CSS Anchor Positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning)
4 changes: 4 additions & 0 deletions files/en-us/web/css/inline-size/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inline-size: fit-content;
inline-size: fit-content(20em);
inline-size: auto;

/* anchor-size() function values */
inline-size: anchor-size(width);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

inline-size: anchor-size(--my-anchor inline);

/* Global values */
inline-size: inherit;
inline-size: initial;
Expand Down
Loading
Loading