gpt4 book ai didi

HTML CSS FLEX 问题,div 没有正确居中

转载 作者:行者123 更新时间:2023-11-28 01:48:41 25 4
gpt4 key购买 nike

基本上,学徒容器需要在左侧,然后在 onClick 之后打开的 div 需要居中(图 1 中的黄色虚线)我注意到它在不同的显示器尺寸上看起来不同。

这是我到目前为止的两张图片,我已经点缀了一些容器,希望对你们有所帮助。

我希望一切都有意义。

Image 1 with buttons not clicked

Image 2 with buttons clicked

这是 HTML:

.body2 {
text-align: center;
display: flex;
flex-direction: row;
align-items: stretch;
border: 1px dotted red;
}

.body3 {
padding: 5px;
text-align: center;
display: flex;
/* flex-direction: row; */
/* align-items: center; */
border: 2px dotted orange;
}

.body-flexstart {
text-align: center;
justify-content: center;
display: flex;
justify-self: flex-start;
width: 400px;
/* flex-direction: row; */
/* flex-flow: row wrap; */
/* justify-content: space-around; */
/* align-items: flex-start; */
/* border: 1px dotted green; */
}

.column1 {
display: flex;
flex-direction: column;
padding: 5px;
border: 1px dotted yellow;
}
<div class="body2">
<div class="body-flexstart">
<div class="column1">
<div onclick="console.log('clicked')" id="button" class="test1">
<h1 class="itemcomponenttextheader">Apprentice</h1>
<h1 class="itemcomponenttextheader">1 - 75</h1>
</div>
<table id="table">
<thead>
<tr>
<th class="tableheader">Mats</th>
<th class="tableheader">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/inv_enchant_duststrange.jpg"> Strange Dust</div>
</td>
<td class="professionheader3">150x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/soul-dust.png"> Soul Dust</div>
</td>
<td class="professionheader3">90x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/vision-dust.gif"> Vision Dust</div>
</td>
<td class="professionheader3">170x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/dream-dust.gif"> Dream Dust</div>
</td>
<td class="professionheader3">300x</td>
</tr>
</tbody>
</table>
<br>
<div onclick="console.log('clicked')" id="button1" class="test1">
<h1 class="itemcomponenttextheader">Apprentice</h1>
<h1 class="itemcomponenttextheader">1 - 75</h1>
</div>
<table id="table1">
<thead>
<tr>
<th class="tableheader">Mats</th>
<th class="tableheader">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/inv_enchant_duststrange.jpg"> Strange Dust</div>
</td>
<td class="professionheader3">150x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/soul-dust.png"> Soul Dust</div>
</td>
<td class="professionheader3">90x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/vision-dust.gif"> Vision Dust</div>
</td>
<td class="professionheader3">170x</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="body3">
<div class="box2" id="box">
<p class="professionheader3">A</p>
</div>
<div class="box2" id="box1">
<p class="professionheader3">B</p>
</div>
</div>
</div>

最佳答案

div class="body3"> 嵌套在新的 div 中并添加:

{ 显示:flex;
证明内容:居中;
文本对齐:居中;
宽度:100%;

检查这个jsfiddle

.body2 {
text-align: center;
display: flex;
flex-direction: row;
align-items: stretch;
border: 1px dotted red;
}

.body3 {
padding: 5px;
text-align: center;
display: flex;
/* flex-direction: row; */
/* align-items: center; */
border: 2px dotted orange;
}

.new--div {
display: flex;
justify-content: center;
text-align: center;
width: 100%;
}

.body-flexstart {
text-align: center;
justify-content: center;
display: flex;
justify-self: flex-start;
width: 400px;
/* flex-direction: row; */
/* flex-flow: row wrap; */
/* justify-content: space-around; */
/* align-items: flex-start; */
/* border: 1px dotted green; */
}

.column1 {
display: flex;
flex-direction: column;
padding: 5px;
border: 1px dotted yellow;
}
<div class="body2">
<div class="body-flexstart">
<div class="column1">
<div onclick="console.log('clicked')" id="button" class="test1">
<h1 class="itemcomponenttextheader">Apprentice</h1>
<h1 class="itemcomponenttextheader">1 - 75</h1>
</div>
<table id="table">
<thead>
<tr>
<th class="tableheader">Mats</th>
<th class="tableheader">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/inv_enchant_duststrange.jpg"> Strange Dust</div>
</td>
<td class="professionheader3">150x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/soul-dust.png"> Soul Dust</div>
</td>
<td class="professionheader3">90x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/vision-dust.gif"> Vision Dust</div>
</td>
<td class="professionheader3">170x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/dream-dust.gif"> Dream Dust</div>
</td>
<td class="professionheader3">300x</td>
</tr>
</tbody>
</table>
<br>
<div onclick="console.log('clicked')" id="button1" class="test1">
<h1 class="itemcomponenttextheader">Apprentice</h1>
<h1 class="itemcomponenttextheader">1 - 75</h1>
</div>
<table id="table1">
<thead>
<tr>
<th class="tableheader">Mats</th>
<th class="tableheader">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/inv_enchant_duststrange.jpg"> Strange Dust</div>
</td>
<td class="professionheader3">150x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/soul-dust.png"> Soul Dust</div>
</td>
<td class="professionheader3">90x</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="professionheader3">
<div class="matslist">
<div class="matsicon2">
<img class="matsiconlist" src="./enchantingimages/vision-dust.gif"> Vision Dust</div>
</td>
<td class="professionheader3">170x</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="new--div">
<div class="body3">
<div class="box2" id="box">
<p class="professionheader3">A</p>
</div>
<div class="box2" id="box1">
<p class="professionheader3">B</p>
</div>
</div>
</div>
</div>

关于HTML CSS FLEX 问题,div 没有正确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com