列表

详情


CSS9. 定位 - absolute - 评注

描述

 绝对定位非常适合创建弹出层、提示和对话框等覆盖于其他内容之上的组件。比如有一篇文章,可以给文章添加一些行内的评注,最好以气泡图的样式显示在文章外部的空白区域中。
 绝对定位的元素默认会待在自己还在流中时的地方,现在给"aside"盒子设置绝对定位属性"position: absolute",此时"aside"盒子从流中出来并且覆盖住了下方的元素,现在可以将它向左移动一些,把它定位到文章内容的左边。"article"盒子的左外边距为10rem,想让"aside"评注放在左边空白处的中间,需要进行计算,这里省略计算步骤。现在只需要将评注组件向左移动9rem即可。现在给评注组件设置"margin-left: -9rem",在CSS中,负边距是完全有效的,负左外边距可以将元素往左拉,其他有趣的效果这里不做赘述。此时观察效果,评注在"相煎何太急?"内容的左边,可以很醒目的告诉读者该句的意思。
emsp;完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1745ms, 内存消耗: 77772KB, 提交时间: 2021-12-26

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            article {\n                margin-left: 10rem;\n            }\n            aside {\n                width: 5rem;\n                padding: 1rem;\n                color: white;\n                background-color: pink;\n                border-radius: 0.5rem;\n                position:absolute;\n                margin-left: -9rem\n            }\n            aside:after {\n                content: '';\n                position: absolute;\n                display: block;\n                width: 0;\n                height: 0;\n                border: 0.5rem solid pink;\n                border-bottom-color: transparent;\n                border-right-color: transparent;\n                right: -1rem;\n                top: 0.5rem;\n            }\n            .note {\n                color: green;\n                text-decoration-line: underline;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section>\n            <article>\n                <p>煮豆燃豆萁,</p>\n                <p>豆在釜中泣。</p>\n                <p>本是同根生,</p>\n                <aside>豆秸怎能这样急迫地煎熬豆子呢?</aside>\n                <p class='note'>相煎何太急?</p>\n            </article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1748ms, 内存消耗: 77772KB, 提交时间: 2021-12-30

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            article {\n                margin-left: 10rem;\n            }\n            aside {\n                width: 5rem;\n                padding: 1rem;\n                color: white;\n                background-color: pink;\n                border-radius: 0.5rem;\n                margin-left: -9rem;\n                position: absolute\n            }\n            aside:after {\n                content: '';\n                position: absolute;\n                display: block;\n                width: 0;\n                height: 0;\n                border: 0.5rem solid pink;\n                border-bottom-color: transparent;\n                border-right-color: transparent;\n                right: -1rem;\n                top: 0.5rem;\n            }\n            .note {\n                color: green;\n                text-decoration-line: underline;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section>\n            <article>\n                <p>煮豆燃豆萁,</p>\n                <p>豆在釜中泣。</p>\n                <p>本是同根生,</p>\n                <aside>豆秸怎能这样急迫地煎熬豆子呢?</aside>\n                <p class='note'>相煎何太急?</p>\n            </article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77804KB, 提交时间: 2022-01-21

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            article {\n                margin-left: 10rem;\n            }\n            aside {\n                width: 5rem;\n                padding: 1rem;\n                color: white;\n                background-color: pink;\n                border-radius: 0.5rem;\n                position: absolute;\n                margin-left: -9rem;\n                \n            }\n            aside:after {\n                content: '';\n                position: absolute;\n                display: block;\n                width: 0;\n                height: 0;\n                border: 0.5rem solid pink;\n                border-bottom-color: transparent;\n                border-right-color: transparent;\n                right: -1rem;\n                top: 0.5rem;\n            }\n            .note {\n                color: green;\n                text-decoration-line: underline;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section>\n            <article>\n                <p>煮豆燃豆萁,</p>\n                <p>豆在釜中泣。</p>\n                <p>本是同根生,</p>\n                <aside>豆秸怎能这样急迫地煎熬豆子呢?</aside>\n                <p class='note'>相煎何太急?</p>\n            </article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1750ms, 内存消耗: 77812KB, 提交时间: 2021-12-30

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            article {\n                margin-left: 10rem;\n            }\n            aside {\n                width: 5rem;\n                padding: 1rem;\n                color: white;\n                background-color: pink;\n                border-radius: 0.5rem;\n                position:absolute;\n                left:30px;\n                margin-left:-9rem;\n            }\n            aside:after {\n                content: '';\n                position: absolute;\n                display: block;\n                width: 0;\n                height: 0;\n                border: 0.5rem solid pink;\n                border-bottom-color: transparent;\n                border-right-color: transparent;\n                right: -1rem;\n                top: 0.5rem;\n            }\n            .note {\n                color: green;\n                text-decoration-line: underline;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section>\n            <article>\n                <p>煮豆燃豆萁,</p>\n                <p>豆在釜中泣。</p>\n                <p>本是同根生,</p>\n                <aside>豆秸怎能这样急迫地煎熬豆子呢?</aside>\n                <p class='note'>相煎何太急?</p>\n            </article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1751ms, 内存消耗: 77820KB, 提交时间: 2021-12-30

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            article {\n                margin-left: 10rem;\n            }\n            aside {\n                width: 5rem;\n                padding: 1rem;\n                color: white;\n                background-color: pink;\n                border-radius: 0.5rem;\n                position:absolute;\n                left:0;\n                margin-left:-9rem;\n            }\n            aside:after {\n                content: '';\n                position: absolute;\n                display: block;\n                width: 0;\n                height: 0;\n                border: 0.5rem solid pink;\n                border-bottom-color: transparent;\n                border-right-color: transparent;\n                right: -1rem;\n                top: 0.5rem;\n            }\n            .note {\n                color: green;\n                text-decoration-line: underline;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section>\n            <article>\n                <p>煮豆燃豆萁,</p>\n                <p>豆在釜中泣。</p>\n                <p>本是同根生,</p>\n                <aside>豆秸怎能这样急迫地煎熬豆子呢?</aside>\n                <p class='note'>相煎何太急?</p>\n            </article>\n        </section>\n    </body>\n</html>","libs":[]}

上一题