前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >position-try-fallbacks

position-try-fallbacks

作者头像
阿超
发布2024-09-23 09:37:16
830
发布2024-09-23 09:37:16
举报
文章被收录于专栏:快乐阿超

教育的胜利,可以压倒国民性。——爱默生

https://css-tricks.com/almanac/properties/p/position-try-fallbacks/

今天分享一个position-try-fallbackscss属性

它就像字面意思是说:

位置-尝试-反馈

代表着 它在页面移动时尽可能调整位置进行反馈

举个栗子:

代码语言:javascript
复制
.target {
  position: absolute;
  position-anchor: --my-anchor;

  position-area: top;
  position-try-fallbacks: bottom;
}

我写下代码:

代码语言:javascript
复制
<style>
    /* 定义一个锚点元素 */
    .anchor {
        anchor-name: --my-anchor; /* 使用自定义的锚点名称 */

        height: 80px; /* 高度设为80px */
        aspect-ratio: 1; /* 宽高比保持1:1 */
    }

    /* 定义目标元素 */
    .target {
        position: absolute; /* 绝对定位 */
        position-anchor: --my-anchor; /* 使用自定义的锚点定位 */
        position-area: top; /* 默认在顶部对齐 */
        position-try: bottom; /* 尝试从底部定位 */

        width: 80px; /* 宽度设为80px */
        aspect-ratio: 1; /* 宽高比保持1:1 */
    }

    /* 设置页面主体样式 */
    #mainstay,#article{
        position: initial !important;
    }
    .tmp-container {
        display: flex; /* 使用 Flexbox 布局 */
        align-items: center; /* 垂直居中 */
        justify-content: center; /* 水平居中 */

        height: 200vh; /* 页面高度设置为两倍视窗高度 */

        font-family: monospace; /* 使用等宽字体 */
    }

    /* anchor 样式 */
    .anchor {
        display: flex; /* 使用 Flexbox 布局 */
        align-items: center; /* 垂直居中 */
        justify-content: center; /* 水平居中 */

        border-radius: 10px; /* 圆角设置为10px */

        background-color: #ffbd59; /* 设置背景颜色为黄色 */
    }

    /* target 样式 */
    .target {
        display: flex; /* 使用 Flexbox 布局 */
        align-items: center; /* 垂直居中 */
        justify-content: center; /* 水平居中 */

        border-radius: 10px; /* 圆角设置为10px */
        background-color: #cb6ce6; /* 设置背景颜色为紫色 */
    }

    /* 如果浏览器支持 position-try: inset-area(bottom) 属性,则应用 */
    @supports (position-try: inset-area(bottom)) {
        .target {
            position-try: inset-area(bottom); /* 尝试从底部插入区域定位 */
        }
    }

    /* 如果浏览器支持 inset-area: top 属性,则应用 */
    @supports (inset-area: top) {
        .target {
            inset-area: top; /* 将插入区域设置为顶部 */
        }
    }
</style>
<div class="tmp-container">
    <div class="anchor">anchor</div>
    <div class="target">target</div>
</div>

/* 定义一个锚点元素 */ .anchor { anchor-name: --my-anchor; /* 使用自定义的锚点名称 */ height: 80px; /* 高度设为80px */ aspect-ratio: 1; /* 宽高比保持1:1 */ } /* 定义目标元素 */ .target { position: absolute; /* 绝对定位 */ position-anchor: --my-anchor; /* 使用自定义的锚点定位 */ position-area: top; /* 默认在顶部对齐 */ position-try: bottom; /* 尝试从底部定位 */ width: 80px; /* 宽度设为80px */ aspect-ratio: 1; /* 宽高比保持1:1 */ } /* 设置页面主体样式 */ #mainstay,#article.article{ position: initial !important; } .tmp-container { display: flex; /* 使用 Flexbox 布局 */ align-items: center; /* 垂直居中 */ justify-content: center; /* 水平居中 */ height: 200vh; /* 页面高度设置为两倍视窗高度 */ font-family: monospace; /* 使用等宽字体 */ } /* anchor 样式 */ .anchor { display: flex; /* 使用 Flexbox 布局 */ align-items: center; /* 垂直居中 */ justify-content: center; /* 水平居中 */ border-radius: 10px; /* 圆角设置为10px */ background-color: #ffbd59; /* 设置背景颜色为黄色 */ } /* target 样式 */ .target { display: flex; /* 使用 Flexbox 布局 */ align-items: center; /* 垂直居中 */ justify-content: center; /* 水平居中 */ border-radius: 10px; /* 圆角设置为10px */ background-color: #cb6ce6; /* 设置背景颜色为紫色 */ } /* 如果浏览器支持 position-try: inset-area(bottom) 属性,则应用 */ @supports (position-try: inset-area(bottom)) { .target { position-try: inset-area(bottom); /* 尝试从底部插入区域定位 */ } } /* 如果浏览器支持 inset-area: top 属性,则应用 */ @supports (inset-area: top) { .target { inset-area: top; /* 将插入区域设置为顶部 */ } }

anchor

target

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档