【发布时间】:2016-04-21 14:09:40
【问题描述】:
我想将以下图片居中显示:
我有以下样式项目:
var itemStyle = {
display: 'block',
width: this.computeWidth(),
height: '250px',
backgroundImage: 'url(' + imageLocation + ')',
backgroundPosition: 'center !important',
backgroundSize: 'cover',
boxShadow: '10px 10px 5px #888888',
borderRadius: '15px',
marginLeft: 'auto !important',
marginRight: 'auto !important'
};
我是这样显示的:
<div style={itemStyle}>
</div>
this.computeWidth()就是这个方法,我根据页面调整图片的宽度:
computeWidth: function() {
console.log("this.state.window.width: " + this.state.window.width);
if(this.state.window.width > 350) {
return '250px';
}
return Math.floor(0.7 * this.state.window.width).toString() + 'px';
},
我也尝试使用这种方法计算marginLeft 和marginRight:
computeMargin: function() {
if(this.state.window.width > 350) {
return 'margin-auto';
}
return Math.floor(0.15 * this.state.window.width).toString() + 'px';
},
但是,图像没有再次居中。
那么如何确保图像位于页面的中心?(最好不要更改包含它的组件的css)
【问题讨论】:
-
您是定位元素还是背景-图像,不清楚。居中元素有 数以千计 个您可以检查的相关问题。
-
backgroundImage。但这是一样的,不是吗?因为背景图片占据了整个元素。
标签: javascript html css reactjs twitter-bootstrap-3