html - 如何将浮动元素与容器中心对齐
问题描述
我正在学习前端开发。
我想在容器中间对齐矩阵和右侧列。我该怎么做呢?
我创建了以下内容(它是在反应中制作的,但这是原始 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>
任何帮助深表感谢!
解决方案
如今实现这种事情的最好方法(在我看来)是使用弹性盒子。因此,要完成您正在寻找的内容,请使用弹性框。这是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 的更多信息。这是此解决方案的现场演示https://codepen.io/hileamlak/pen/bGpqLgb
推荐阅读
- elasticsearch - 为什么 Elasticsearch 仍然抱怨引导检查失败
- r - 在函数内部运行 rms::Predict 会引发各种错误
- c# - 如何将 DBObject 值转换为 long
- c# - 在单独的项目中链接类的正确方法是什么?
- c - 比较两个函数的执行时间?
- python - 在这种情况下如何重命名多个文件.jpg?
- postgresql - 如何根据行数据和参数值动态旋转?
- php - 我想压缩下载指定目录下的所有文件
- java - 为什么我们必须手动清除 PhantomReference?
- javascript - 无法在 Gnome Shell 扩展 ES6 中调用子类函数