[英]CSS: margin-left scale as function of image max-width
I have 2x Divs and 1x Img with the following CSS 我有以下CSS的2x Divs和1x Img
#StageDiv {
position: absolute;
left: 50%;
margin-left: -200px;
}
#LogoDiv {
position: absolute;
margin-top: 135px;
left: 50%;
margin-left: -500px;
}
#logoimg {
/* max-width: 75%; /* */
width: 1000px; /* */
}
inside of #logoimg, I would like to use max-width: 75%;
在#logoimg内部,我想使用
max-width: 75%;
and then have margin-left:
of both #LogoDiv and #StageDiv be a function of #logoimg as it changes 然后让#LogoDiv和#StageDiv
margin-left:
#logoimg的函数,因为它会发生变化
http://jsfiddle.net/3KLUW/1/
http://jsfiddle.net/3KLUW/1/
Is this possible in pure CSS or will I have to do this in javascript in a on resize
event? 在纯CSS中是否可行,还是在
on resize
事件中必须在javascript中做到这一点? (not sure what the actual function call is currently but im sure my buddy google will know) I think in the long run, I will most likely have to use a javascript event to scale my kineticjs stage anyway but I am curious to know if there is some CSS wizardry to do the first part. (不确定当前实际的函数调用是什么,但我肯定我的好友谷歌会知道)从长远来看,我认为无论如何我还是很有可能必须使用javascript事件来扩展我的dynamicjs阶段,但我很好奇是否有是第一部分的一些CSS向导。
Thoughts? 有什么想法吗?
Edit: 编辑:
window.onresize=function(){
var img = document.getElementById('logoimg');
var width = img.offsetWidth;
var div = document.getElementById('LogoDiv');
div.style.marginLeft= "-" + width/2 + "px";
};
still would be interested in a CSS solution 仍然会对CSS解决方案感兴趣
If you can get away with a wrapper div for the whole logo: 如果您可以使用整个div包装器div来标识整个徽标:
<div id="logo">
<div id="StageDiv">...</div>
<div id="LogoDiv">
<img id="logoimg" src="..." />
</div>
</div>
Then you can set the width
and max-width
on it, and use margin: auto
to center it on the page: 然后,您可以在其上设置
width
和max-width
,并使用margin: auto
将其在页面上居中:
#logo {
width: 1000px;
max-width: 75%;
margin: auto;
position: relative;
}
And positioning the other elements become much easier: 定位其他元素变得容易得多:
#LogoDiv {
top: 135px;
position: absolute;
}
#StageDiv {
text-align: center;
}
#logoimg {
width: 100%;
}
The margin: auto
and text-align: center
together give us the automatic margin you wanted. margin: auto
和text-align: center
,可为我们提供所需的自动边距。
Updated fiddle: http://jsfiddle.net/3KLUW/2/ 更新的小提琴: http : //jsfiddle.net/3KLUW/2/
The canvas will need to be scaled though, as you said on the question. 正如您在问题中所说的那样,画布将需要缩放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.