无法将 Div 置于页面中间
Cannot Centre a Div to be in the Middle of Page
我试图让我的 Div 居中显示在页面中,但是我无法让它正常工作。
尽管使用了 Margin-left: auto 和 Margin-Right:auto 它仍然没有在页面中间居中。
如有任何帮助,我们将不胜感激!
HTML:
<div class="OuterService">
<div class="service">
<div class="virus" style="width:250px; height:218px;">
<center><h3 style="width:200px; text-align:center">Computer Virus</h3>
<img src="virus.jpg" alt="virus" height="140px"/></center>
</div>
<div class="screenRepair" style="width:250px;">
<center><h3 style="width:auto; text-align:center">Screen Replacement</h3>
<img src="smashedScreen.jpg" alt="BrokenScreen" height="160px"/></center>
</div>
<div class="hardwareRepair" style="height:218px;">
<center><h3>Hardware Replacement</h3>
<img src="hardwareRepair.jpg" alt="hardwareRepair" height="130px"/></center>
</div>
<div class="WindowsReinstall" style="height:218px;">
<center>
<h3>OS Reinstallation</h3>
<img src="windowsInstall.jpg" alt="OS Reinstallation" height="150px;" width="220px;"/>
</center>
</div>
<div class="maintenance" style="width:250px;">
<center>
<h3>System Maintenance</h3>
<img src="SystemMaintenance.jpeg" alt="System Maintenance" height=150px;/>
</center>
</div>
<div class="SoftwareRepair" style="width:250px">
<center>
<h3>Software Repair</h3>
<img src="SoftwareRepair.png" alt="Software Repair" height="150px;" width="220px;"/>
</center>
</div>
<div class="MemoryUpgrades" style="width:250px; height:208px;">
<center>
<h3>Memory Upgrades</h3>
<img src="MemoryUpgrades.jpg" alt="Memory Upgrades" height="140px;"/>
</center>
</div>
<div class="DataRecovery" style="width:250px;">
<center>
<h3>Data Recovery</h3>
<img src="DataRecovery.jpg" alt="Data Recovery" height="150px;"/>
</center>
</div>
</div>
</div>
CSS:
.outerService {
width: auto;
margin-left: auto;
margin-right: auto;
}
.service {
max-width: 1100px;
display: table;
margin-bottom: 20px;
margin-left: auto;
margin-right: auto;
}
.virus, .screenRepair, .hardwareRepair, .WindowsReinstall, .maintenance, .SoftwareRepair, .MemoryUpgrades, .DataRecovery {
width:250px;
float:left;
margin-left: auto;
margin-right: auto;
margin-top: 0;
border:1px solid #000;
}
您 HTML 中的 class 名称使用大写 O 表示 OuterService
您的 CSS 使用小写 o 表示 outerService .outerService
试试改成
.OuterService {
宽度:自动;
左边距:自动;
右边距:自动;
}
对于某些div,您必须将div宽度固定在某个px。试试这个:
.outerService {
width: 850px;
margin:0 auto;
}
如果您希望 div 在特定屏幕尺寸下动态变化,您可以尝试使用媒体查询来使宽度具有百分比值。
您需要为 class outerService
指定宽度
.outerService
{
width: 70%;
margin-left: auto;
margin-right: auto;
}
此外,如果您希望 outerService 不指定大小,您可以使用 flexbox。假设outerService的容器是body..
body
{
display: flex;
justify-content: center;
}
.outerService
{
width: auto;
}
Flex 目前在所有主流浏览器的当前版本上都受支持,但如果您支持旧版本,请查看 here.
我试图让我的 Div 居中显示在页面中,但是我无法让它正常工作。
尽管使用了 Margin-left: auto 和 Margin-Right:auto 它仍然没有在页面中间居中。
如有任何帮助,我们将不胜感激!
HTML:
<div class="OuterService">
<div class="service">
<div class="virus" style="width:250px; height:218px;">
<center><h3 style="width:200px; text-align:center">Computer Virus</h3>
<img src="virus.jpg" alt="virus" height="140px"/></center>
</div>
<div class="screenRepair" style="width:250px;">
<center><h3 style="width:auto; text-align:center">Screen Replacement</h3>
<img src="smashedScreen.jpg" alt="BrokenScreen" height="160px"/></center>
</div>
<div class="hardwareRepair" style="height:218px;">
<center><h3>Hardware Replacement</h3>
<img src="hardwareRepair.jpg" alt="hardwareRepair" height="130px"/></center>
</div>
<div class="WindowsReinstall" style="height:218px;">
<center>
<h3>OS Reinstallation</h3>
<img src="windowsInstall.jpg" alt="OS Reinstallation" height="150px;" width="220px;"/>
</center>
</div>
<div class="maintenance" style="width:250px;">
<center>
<h3>System Maintenance</h3>
<img src="SystemMaintenance.jpeg" alt="System Maintenance" height=150px;/>
</center>
</div>
<div class="SoftwareRepair" style="width:250px">
<center>
<h3>Software Repair</h3>
<img src="SoftwareRepair.png" alt="Software Repair" height="150px;" width="220px;"/>
</center>
</div>
<div class="MemoryUpgrades" style="width:250px; height:208px;">
<center>
<h3>Memory Upgrades</h3>
<img src="MemoryUpgrades.jpg" alt="Memory Upgrades" height="140px;"/>
</center>
</div>
<div class="DataRecovery" style="width:250px;">
<center>
<h3>Data Recovery</h3>
<img src="DataRecovery.jpg" alt="Data Recovery" height="150px;"/>
</center>
</div>
</div>
</div>
CSS:
.outerService {
width: auto;
margin-left: auto;
margin-right: auto;
}
.service {
max-width: 1100px;
display: table;
margin-bottom: 20px;
margin-left: auto;
margin-right: auto;
}
.virus, .screenRepair, .hardwareRepair, .WindowsReinstall, .maintenance, .SoftwareRepair, .MemoryUpgrades, .DataRecovery {
width:250px;
float:left;
margin-left: auto;
margin-right: auto;
margin-top: 0;
border:1px solid #000;
}
您 HTML 中的 class 名称使用大写 O 表示 OuterService
您的 CSS 使用小写 o 表示 outerService .outerService
试试改成 .OuterService { 宽度:自动; 左边距:自动; 右边距:自动; }
对于某些div,您必须将div宽度固定在某个px。试试这个:
.outerService {
width: 850px;
margin:0 auto;
}
如果您希望 div 在特定屏幕尺寸下动态变化,您可以尝试使用媒体查询来使宽度具有百分比值。
您需要为 class outerService
指定宽度.outerService
{
width: 70%;
margin-left: auto;
margin-right: auto;
}
此外,如果您希望 outerService 不指定大小,您可以使用 flexbox。假设outerService的容器是body..
body
{
display: flex;
justify-content: center;
}
.outerService
{
width: auto;
}
Flex 目前在所有主流浏览器的当前版本上都受支持,但如果您支持旧版本,请查看 here.