简体   繁体   English

使用CSS,如何将DIV移动到中心位置的左侧?

[英]Using CSS, how do I move a DIV to the left of the centre position?

A fairly standard way to horizontally centre a DIV is to set the left and right margins to auto . 水平居中DIV一种相当标准的方法是将左右边距设置为auto

However, I have a DIV that I want to be mostly horizontally centred, but I want to nudge it a little toward the left. 但是,我有一个DIV ,我想要大多数水平居中,但我想向左轻推一点。

I tried setting the margins like so: 我试着像这样设置边距:

margin: 100px 60% 24px 40%;

... and also like this: ......还喜欢这样:

margin: 100px 40% 24px 60%;

... but both resulted in the DIV being positioned further to the right. ......但两者都导致DIV位于右侧。

I tried adding padding to the DIV , but that also only moves it to the right. 我尝试在DIV添加填充,但这也只是向右移动。

In short, it seems no matter what I do, the DIV moves to the right, not to the left as desired. 简而言之,无论我做什么, DIV向右移动,而不是向左移动。

How do I nudge a DIV a little to the left of centre? 我如何将DIV轻推到中心左侧?

A different way of handling this is making a parent wrapper div. 处理此问题的另一种方法是创建父包装器div。 Where you set that to auto so that parent is centered, but the child div is then starts at the center but moves to the right. 您将其设置为auto以使父项居中,但子div从中心开始但向右移动。 See fiddle http://jsfiddle.net/4H26W/1/ 见小提琴http://jsfiddle.net/4H26W/1/

html HTML

<div id="red">
    <div id="blue">Some text</div>
</div>

css CSS

#red {
    width: 1px; /* you could actually just change it to 0px */
    margin: 100px auto;
    background: red;
}
#blue {
    width: 200px;
    background: blue;
}

Then if you wanted to further optimize the position of the child div, you could just add some left styles to it 然后,如果你想进一步优化子div的位置,你可以添加一些左边的样式

position: relative; /* has to be position relative for left to work, or you could just do margin-left: -50px; too */
left: -50px;

http://jsfiddle.net/4H26W/2/ http://jsfiddle.net/4H26W/2/

Since you tagged css3 for your question so you can use it : 由于您为问题标记了css3 ,因此您可以使用它:

margin: auto;
-webkit-transform: translateX(10px); /* 10px to left */
-moz-transform: translateX(10px); /* 10px to left */
-ms-transform: translateX(10px); /* 10px to left */
 transform: translateX(10px); /* 10px to left */

there is an option with text-align, display and negative margin . 有一个文本对齐,显示和负边距选项。

DEMO DEMO


HTML test base : HTML测试基地:

<div class="left">center on my left</div>
<hr/>
<div class="right">center on my right</div>
<hr/>
<div >center me</div>

CSS base: CSS基础:

body {
  text-align:center;
  background:linear-gradient(to right,gray 50%,white 50%)
}
div {
  width:20%;/* whatever */
  display:inline-block;
  border:solid;
}
.left {
  margin-left:-20%;/* whatever */
}

.right {
  margin-right:-20%;/* whatever */
}

给予

If you set the margin-right or margin-left as a separate rule after setting the margin rule, you'll still get frustrated, but not as much: 如果设置了margin-rightmargin-left作为一个单独的规则设置后, margin的规则,你还是会感到沮丧,但没有那么多:

<div id="first"></div>
<div id="second"></div>

div {
    height: 100px;
    width: 100px;
    border: 1px solid red;
    margin: 2em auto;
}

#second {
    margin-right: 42%;
}

See example fiddle: http://jsfiddle.net/Q7EHX/ 参见示例小提琴: http//jsfiddle.net/Q7EHX/

Turns out I was able to do what I needed to do with floating the DIVs. 结果我能够做我需要做的漂浮DIV。

.left,
.right {
    display: block;
    position: relative;
    margin-bottom: 2em;
    clear: both;
}

.left {
    float: left;
    margin-left: 20px;
}
.right {
    float: right;
    margin-right: 20px;
}

position relative. 位置相对。

<div style="margin:100px auto 24px;
    position:relative;left:-30px;
    border:1px solid red;width:200px
">should be a little off-center</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM