css - 从中心偏移 div

标签 css html css-position

我正在尝试将 div x 像素数量定位到页面中心的右侧。因此,div 将相对于中心。

到目前为止,我已经尝试过一些愚蠢的事情

margin:0 auto;
margin-left:20px;

但是您不需要在浏览器中测试它就知道它行不通。

在此先感谢您的帮助。

最佳答案

我会尝试使用两个 DIV,一个在另一个里面。像这样:

<div class="outer">
    <div class="inner">Hello, world!</div>
</div>

.outer {
    width: 1px; /* Or zero, or something very small */
    margin: auto;
    overflow: visible;
    background: red; /* A color just for debug */
}
.inner {
    margin-left: 20px;
    background: yellow; /* A color just for debug */
    width: 100px; /* Depending on the desired effect, width might be needed */
}

参见 this example live at jsfiddle .

根据这个问题/答案,外部 div 将水平居中:How to horizontally center a <div> in another <div>?

然后,内部差异仅向右移动 20 像素,使用边距。

请注意,如果您将 width 保留为 auto,宽度将为零,这可能不是您想要的。

关于css - 从中心偏移 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783902/

相关文章:

javascript - HTML 表单提交问题

调整窗口大小后,css 背景修复失败

html - 修复重叠元素

css - 如何确定表单组中的元素

html - 在 td 元素内对齐类

firefox - css3 边框图像 firefox 15.0.1

jquery - WordPress 在悬停时只针对一篇文章

javascript - 从数据库中提取数据并显示在排行榜中

html - 如何使用 foreach 在 1 行中显示正确的 4 个缩略图

javascript - 固定位置不适用于 ios 设备