【问题标题】:How to Align Floating Element to Centre of Container如何将浮动元素与容器中心对齐
【发布时间】:2020-12-14 15:04:11
【问题描述】:

我正在学习前端开发。

我想在容器中间对齐矩阵和右侧列。我该怎么做?

我创建了以下内容(它是在 react 中制作的,但这是原始 html):

我想有一种方法可以使用 HTML 和 CSS 来做到这一点,但我不知道如何去做。我尝试将事物居中,但似乎不起作用。

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="/manifest.json" />
    <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
    <style type="text/css">
      body {
        margin: 0;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
          "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans",
          "Helvetica Neue", sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
      }

      code {
        font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
          monospace;
      }
    </style>
    <style type="text/css">
      .App {
        text-align: center;
        width: 500px;
        margin: auto;
      }

      .matrix {
        float: left;
        display: grid;
        /* grid-template-columns: auto auto auto; */
        /* background-color: #2196f3; */
        padding: 10px;
      }

      .matrix-component {
        border-width: thin;
        border-style: solid;
        border-color: black;
        width: 50px;
        height: 50px;
      }

      .rhs {
        display: grid;
        padding: 10px;
        /* float: right; */
      }

      .content {
        background: grey;
      }
    </style>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root">
      <div class="App">
        <div class="content">
          <div>rows: <input />cols: <input /></div>
          <div class="matrix">
            <div>
              <input class="matrix-component" id="0" value="" /><input
                class="matrix-component"
                id="1"
                value=""
              /><input class="matrix-component" id="2" value="" />
            </div>
            <div>
              <input class="matrix-component" id="3" value="" /><input
                class="matrix-component"
                id="4"
                value=""
              /><input class="matrix-component" id="5" value="" />
            </div>
            <div>
              <input class="matrix-component" id="6" value="" /><input
                class="matrix-component"
                id="7"
                value=""
              /><input class="matrix-component" id="8" value="" />
            </div>
          </div>
          <div class="rhs">
            <input class="matrix-component" id="rhs0" value="" /><input
              class="matrix-component"
              id="rhs1"
              value=""
            /><input class="matrix-component" id="rhs2" value="" />
          </div>
        </div>
      </div>
    </div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
    <script src="/static/js/bundle.js"></script>
    <script src="/static/js/0.chunk.js"></script>
    <script src="/static/js/main.chunk.js"></script>
    <script src="/main.65e0a7c3d67e68d31704.hot-update.js"></script>
  </body>
</html>

非常感谢任何帮助!

【问题讨论】:

    标签: html css grid frontend


    【解决方案1】:

    如今实现这种事情的最佳方式(在我看来)是使用弹性盒子。因此,要完成您正在寻找的内容,请使用弹性框。这是css的一个实现。

    首先让我们在你的 html 中像这样将每个列元素组合在一起。

    <div class="new-div">
        <div>rows: <input />cols: <input /></div>
    </div>
    <div class="new-div2">
        <div class="matrix">
        <div>
            <input class="matrix-component" id="0" value="" /><input
            class="matrix-component"
            id="1"
            value=""
            /><input class="matrix-component" id="2" value="" />
        </div>
        <div>
            <input class="matrix-component" id="3" value="" /><input
            class="matrix-component"
            id="4"
            value=""
            /><input class="matrix-component" id="5" value="" />
        </div>
        <div>
            <input class="matrix-component" id="6" value="" /><input
            class="matrix-component"
            id="7"
            value=""
            /><input class="matrix-component" id="8" value="" />
        </div>
        </div>
        <div class="rhs">
        <input class="matrix-component" id="rhs0" value="" /><input
            class="matrix-component"
            id="rhs1"
            value=""
        /><input class="matrix-component" id="rhs2" value="" />
        </div>
    </div>
    

    那么这个 css 应该可以解决问题。

       .content{
            display:flex;
            justify-content:center;
            align-items:center;
            flex-direction:column;
        }
    

    告诉我这是否能解决您的问题。你可以阅读更多关于 flex-box 的here。这是此解决方案的现场演示https://codepen.io/hileamlak/pen/bGpqLgb

    【讨论】:

    • 谢谢!不幸的是,它弄乱了我的矩阵输入,并使其结构不规则。有什么办法吗?
    • 我注意到了一件事。具有类名 rhs 的 div 不是沿着边矩阵出现的。我有一个解决方案,但它涉及更改 html。如果你改变你的反应可以吗?
    • 为什么不简单地用网格来做呢?没有任何浮动但指定确切位置的网格
    • 如果我以编程方式为我的矩阵指定行和列,网格会起作用吗?我对它不太熟悉
    • 当然,您设置了一个与表格相同的网格,只是您可以在每个单元格中放置任何项目,甚至使其跨越多个单元格。都是一个问题,如何定位项目以及如何定义网格行和列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2021-11-16
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多