【问题标题】:Why won't the button float right?为什么按钮不能正确浮动?
【发布时间】:2020-08-23 21:33:19
【问题描述】:

我试图让登录按钮在我制作的导航栏上浮动,但它不起作用,我到处搜索正确答案。有什么想法吗?

import React, { Component } from "react";
import "./App.css";

class Home extends Component {
    render() {
        return (
            <div className="nav-bar">
                <h1 style={{color: 'white'}}>To-do list</h1>
                <div style={{float: 'right'}}>
                    {/*The button I am trying to fix*/}
                    <a className="btn btn-dark btn-lg" href="/login">Login</a>
                </div>
            </div>
        )
    }
}

export default Home;

【问题讨论】:

    标签: html reactjs button


    【解决方案1】:

    问题在于h1 是块级元素,因此您的按钮不会浮动到它的右侧,因为h1 占据了整行。

    一种解决方案是让您的 .nav-bar 使用 flexbox 布局模式:

    .nav-bar {
        display: flex;
        flex-flow: row nowrap;
        justify-content: space-between;
    }
    

    一旦将布局模式设置为 flexbox,float 属性将不再有任何作用。您可以将其从元素的内联样式中移除。

    【讨论】:

    • 您可能应该补充一点,浮动样式也应该被删除,以防开发人员不明显
    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2015-07-15
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多