gpt4 book ai didi

html - CSS - float

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:20 25 4
gpt4 key购买 nike

我正在尝试创建一个带有居中文本列的布局,以及位于右侧并“附加”到该列右侧的元素。

Demonstration codepen

定位工作正常,但中心列中的文本在 float 位置缩进。我希望它表现得好像 float 不存在一样。

当前代码:

HTML

<div>
<aside>This is an aside that should float to the right</aside>
<p>This paragraph should be centered in the middle of the page.</p>
</div>

CSS

div {
width: 400px;
margin: auto;
}

aside {
float: right;
clear: right;
width: 200px;
left: 200px;
position: relative;
}

非常感谢任何帮助!

最佳答案

but the text in the center column is indented at the position of the float. I would like it to behave as if the float isn't there.

一个简单的实现方法是给 float 元素一个负的 margin-left ,其绝对值与您将其放置在 left 的位置相同:

div {
width: 400px;
margin: auto;
background: red;
}

aside {
float: right;
clear: right;
width: 200px;
left: 200px;
margin-left: -200px;
position: relative;
background: yellow;
}
<div>
<aside>This is an aside that should float to the right</aside>
<p>This paragraph should be centered in the middle of the page. This paragraph should be centered in the middle of the page. This paragraph should be centered in the middle of the page. This paragraph should be centered in the middle of the page.</p>
</div>

关于html - CSS - float <aside> 元素缩进文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com