简体   繁体   English

CSS-浮动 <aside> 元素缩进文本

[英]CSS - Floated <aside> element indents text

I'm trying to create a layout with a centered column of text, and aside elements which sit to the right and are "attached" to the right side of the column. 我正在尝试创建一个居中的文本列的布局,并保留位于右侧并“附加”到该列右侧的元素。

Demonstration codepen 演示码笔

The positioning works correctly, 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. 我希望它表现得好像没有浮动。

Current code: 当前代码:

HTML 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 CSS

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

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

Any help is very appreciated! 任何帮助都非常感谢!

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. 我希望它表现得好像没有浮动。

One simple way to achieve that could be to give the floated element a negative margin-left of the same absolute value as you positioned it to the left : 一种简单的实现方法是给浮动元素一个负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> 

This is not possible with float . 对于float这是不可能的。

You would need to eitehr rethink your HTML layout or use absolute positioning. 您可能需要重新考虑HTML布局或使用绝对定位。

 div { width: 400px; margin: auto; background: red; position: relative; } aside { width: 200px; left: 100%; position: relative; background: yellow; position: absolute; } 
 <div> <aside>This is a footnote that should be aligned to the right</aside> <p>Assumenda animi asperiores ut corrupti veniam. Recusandae omnis pariatur est beatae. Consequatur voluptatem facere quaerat consectetur consequatur sequi quod dolor. Aspernatur aut et est. Quis assumenda labore nulla id fugit consequatur explicabo. Eaque aut nemo quia.</p> </div> 

However there are drawbacks to absolute positioning as you can see. 但是,如您所见,绝对定位存在弊端。 It is quite inflexible and not greatly responsive. 这是相当不灵活的,并且反应不灵敏。

You want something like this, I guess: 我想您想要这样的东西:

https://codepen.io/bra1N/pen/RRdWRg https://codepen.io/bra1N/pen/RRdWRg

CSS: CSS:

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

aside {
  float: right;
  clear: right;
  width: 30%;
  position: relative;
  background: yellow;
}

p{
  margin: 0;
  width: 70%;
  float: left;
  background: red;
  text-align: center;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM