html - float 在 flex 容器中不起作用

标签 html css flexbox

我想将文本(foo 链接)放置在页脚元素的右侧。

我需要页脚显示保持 flex

但是当我将它设置为 flex 时,float:right 不再适用于 span。

<footer style="display: flex;">
     <span style="text-align: right;float: right;">
        <a>foo link</a>
     </span>
</footer>

https://jsfiddle.net/dhsgvxdx/

最佳答案

float 属性在 flex 容器中被忽略。

来自 flexbox 规范:

3. Flex Containers: the flex and inline-flex display values

A flex container establishes a new flex formatting context for its contents. This is the same as establishing a block formatting context, except that flex layout is used instead of block layout.

For example, floats do not intrude into the flex container, and the flex container’s margins do not collapse with the margins of its contents.

float and clear do not create floating or clearance of flex item, and do not take it out-of-flow.

相反,只需使用 flex 属性:

footer {
    display: flex;
    justify-content: flex-end;
}
<footer>
    <span>
       <a>foo link</a>
    </span>
</footer>

如果页脚中有更多元素,并且需要其他对齐选项,那么这里有两个指南:

关于html - float 在 flex 容器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39194630/

相关文章:

html - 使子 flexbox 容器可滚动

javascript - 范围过滤器仅在第一个 Controller 中工作

javascript - .htaccess 忽略对 css/js/images 的请求?

jquery - 带有自定义图像的复选框在 IE 上不起作用

jquery - 数据表单列搜索与滚动不兼容

css - 具有绝对位置的 Flexbox 容器不适合 IE 中的内容宽度

javascript - 菜单有 "scroll to #href"功能,但无法在那里使用目标空白链接

javascript - 获取 <div> 的内容,知道它的 id

css - IE7 CSS float 与其他一切(Webkit、IE8/9 等)不一样

html - css删除行中的开始和最后间隔