【问题标题】:CSS Center responsively响应式 CSS 中心
【发布时间】:2014-11-16 18:58:05
【问题描述】:

我有一个居中的网页,现在我已经使用媒体查询调整了它的大小,但我不知道如何实现类似 stackoverflow 本身的功能。一旦你减小页面的宽度,它会变得越来越小,并且 margin-left 会减少到零;所以在某一时刻页面填满了整个窗口。我使用了很多margin-left: 25% 使页面居中,但这不像我想要的设计那样工作。调整浏览器窗口的大小后,页面宽度会变小并保持居中,而我并不真正希望宽度变小,而是减少页面左侧和右侧的空间。

例如,这是我使用的标题:

margin-top: 3%;
float:left;
font-size: 350%;
margin-left: 25%;
width:10%;

这是站点的“中间”,具有白色背景:

position: absolute;
border-radius: 3px;
top: 0px;
left: 21%;
width: 58%;
min-height: 100%;
background: white;
z-index: -1;
-webkit-box-shadow: 20px 0px 30px 0px rgba(0,0,0,0.5), -20px 0px 30px 0px rgba(0,0,0,0.5);
-moz-box-shadow: 20px 0px 30px 0px rgba(0,0,0,0.5), -20px 0px 30px 0px rgba(0,0,0,0.5);
box-shadow: 20px 0px 30px 0px rgba(0,0,0,0.5), -20px 0px 30px 0px rgba(0,0,0,0.5);

对不起,这可能很容易,但我真的不明白......

谢谢

【问题讨论】:

  • 绝对定位上下文可能是问题所在。您的相关 HTML 是什么样的?对我来说,你想要达到的目标并不完全清楚。
  • 通常要使有宽度的块元素居中,你会给出一个 margin-left:auto/margin-right:auto 或.. margin:0 auto;
  • 你能提供一个包含你的 HTML 的小提琴吗?
  • 您可以在onlinehelp.avs4you.com/de/AVS-Video-Editor/Features/… 上完美地看到它。当您调整站点大小时,站点始终具有相同的宽度,但左右边距变小,并且在某些时候不再有边距。我认为我将元素居中的方式(左边距:20%)有点错误或什么的......

标签: html css responsive-design center


【解决方案1】:

我认为您需要将左右边距设置为自动。不是 25%。

像这样:

margin-left:auto;

margin-right:auto;

但是您必须定义应用auto 值的容器的宽度。

如果您查看您在评论中提供的示例网站的 CSS:

#mainbody {
   width: 980px; /*this line*/
   text-align: center;
   vertical-align: top;
   padding: 0;
   margin: auto; /*this line*/
   height: auto;
   background: #fff;
}

【讨论】:

  • 啊谢谢我不太明白margin: auto;。现在它可以与一个包装 div 和两个自动边距一起正常工作。谢谢!
  • 不客气!这是关于边距和值解释的一个很好的参考——margin CSS | MDN
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2023-03-12
  • 1970-01-01
  • 2018-02-24
相关资源
最近更新 更多