如何阻止内容落后于绝对定位元素

问题描述 投票:0回答:2

我的大多数内容都在<main>元素中:

<main>
<h1>A nice big, long, long heading</h1>
<p>Lots of text</p>
</main>

main {
max-width:200px;
margin:0 auto;
padding:0 20px;
}

h1 {
position:absolute;
max-width:300px;
padding-right:20px;
}

这实现了这个:

enter image description here

标题比主列宽:它从右边缘突出 - 正是我想要的。

但是,标题下方的文本显示在标题后面(因为标题已从正常流程中取出)。

我不知道标题是一行还是两行或换行更多行。

有没有办法让标题下方的文字显示在标题下方(不在标题后面):尊重标题的高度(就像它在正常流程中一样)?

html css
2个回答
2
投票

像这样?

main {
max-width: 200px;
margin: 0 auto;
padding: 0 20px;
background: blue;
height: 300px;

}

h1 {
position: relative;
width: 300px;
padding-right: 20px;
background: red;
}
<main>
<h1>A nice big, long, long heading</h1>
<p>Lots of text Lots of textLots of textLots of textLots of textLots of textLots of textLots of textLots of text</p>
</main>

0
投票

将主要位置设置为相对位置。您可以删除h1上的最大宽度

main {
    max-width:200px;
    margin:0 auto;
    padding:0 20px;
    position: relative;
}

h1 {
    position:absolute;
    padding-right:20px;
}
© www.soinside.com 2019 - 2024. All rights reserved.