Html 将div定位到页面中心

Html 将div定位到页面中心,html,css,margin,centering,Html,Css,Margin,Centering,您好,我在将div定位到页面中心时遇到问题 CSS: 我做错什么了吗?我还要提到的是,这个div在另一个div中,所以这可能是个问题吗?如何避免它比 这应该可以完成以下工作: margin-right:25%; margin-left:25%; 您是否尝试输入比外部div小的宽度 诸如此类: .fancyClass{ width: 50%; margin: 0 auto; <!-- that does the actual centering --> #all th

您好,我在将div定位到页面中心时遇到问题

CSS:


我做错什么了吗?我还要提到的是,这个div在另一个div中,所以这可能是个问题吗?如何避免它比

这应该可以完成以下工作:

margin-right:25%;
margin-left:25%;

您是否尝试输入比外部div小的宽度

诸如此类:

.fancyClass{
    width: 50%;
    margin: 0 auto; <!-- that does the actual centering -->
#all the other attributes you have
}
.fancyClass{
宽度:50%;
保证金:0自动;
#您拥有的所有其他属性
}

请尝试此选项并告知我们

HTML:

<div class="outterDiv">
    <div class="innerDiv">
        some thing here
    </div>
</div>
.outterDiv{
    width: 100%;
    background-color: #ccc;
    padding: 25px;
}
.innerDiv{
    width: 50%;
    background-color: #eee;
    margin: 0 auto;
    text-align: center;
}

在这里,您可以看到div在页面的中央水平
和垂直
。这是

看看CSS

body{margin:0 auto;
text-align:center;
}

.fancyClass{
  border: 1px solid #999;
  border-radius: 10px;
  display: inline-block;
  text-align:left;
  width: 25%;
  height: 25%;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}

 <div class="fancyClass"> 
    this text will be center
  </div>
body{margin:0自动;
文本对齐:居中;
}
白痴{
边框:1px实心#999;
边界半径:10px;
显示:内联块;
文本对齐:左对齐;
宽度:25%;
身高:25%;
溢出:自动;
保证金:自动;
位置:绝对位置;
顶部:0;左侧:0;底部:0;右侧:0;
}
此文本将居中显示

试用您当前的解决方案将允许您将div放置在父div的中心,如果没有上下文,则无法提供更多有用的详细信息。@PSL我需要文本向左对齐……但这没有帮助。@n这是屏幕截图-它的外观。你需要什么信息?可能是因为你没有指定盒子的宽度?直接父元素有多宽?另外:内联块不是导致方框向左悬停的原因吗?有些联系信息向左对齐:yadi.sk/d/K_IGeq1gLDQ8h,将文本对齐到中心没有帮助哇,谢谢,这很有效!!!!!你是我的救世主。我一个多小时来一直想把这玩意儿放在中心。编辑:我不明白为什么人们会投反对票you@polyx添加外部div代码,因为它应该可以工作
body{margin:0 auto;
text-align:center;
}

.fancyClass{
  border: 1px solid #999;
  border-radius: 10px;
  display: inline-block;
  text-align:left;
  width: 25%;
  height: 25%;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}

 <div class="fancyClass"> 
    this text will be center
  </div>