2015-11-22 67 views
1

我试图让图像元素具有固定的对齐方式,不管屏幕大小。我希望它留下。我已经尝试了很多东西,包括float,horizo​​ntal-align和margin-left,但这些都是硬编码值,我想要一个解决方案,以确保图像能够保持向左,而不管屏幕大小。任何帮助,将不胜感激。Img元素固定对齐左

这里是我的HTML:

<img class="profile" src="image.jpg" /> 

,这里是我的CSS:

.profile { 
position: absolute; 
height: 400px; 
width: 400px; 
margin-left: -300px; 
top: 100px; 
} 

它也可能是值得一提的还有,除了页面上的IMG没有任何元素。

+1

<img>标签,你试过'left',而不是'保证金left'? – Alex

+0

检查[this](http://jsfiddle.net/kc6f9vkg/) – Alex

+0

默认情况下,一个元素被格式化到左边,尽管我不确定它有多吸引人。 –

回答

1

使用position: fixed;

html,body{ 
 
    height: 1300px; 
 
} 
 

 
.profile { 
 
    position: fixed; 
 
    height: 400px; 
 
    width: 400px; 
 
    margin-left: -300px; 
 
    top: 100px; 
 
}
<img class="profile" src="image.jpg" />

广场body