簡體   English   中英

當窗口調整大小時,如何更改div的左邊距?

[英]How can i change the left margin of a div when the window is resized?

當調整窗口大小時,我想在綠色邊框所在的左側放置紅色邊框div。
目前div具有以下css規則:

#twitter-2 { 
width: 332px; 
background-color: #7E7D7D; 
opacity: 0.6; 
margin-left:525px; 
margin-top:-142px; 
}

http://imageshack.us/download/20/changemargin.jpg

我解決了這個問題:

window.onresize = function(event) {
    var elem = document.getElementById("twitter-2");
    elem.setAttribute("style","margin: 20px 1px;");
}

您可以使用媒體查詢

並通過重新調整窗口大小來測試您的網站

調整你想要的設備,並總是嘗試使用%而不是px

一些媒體查詢給你感謝克里斯

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */


Like This 
#twitter-2 { 
margin-left:Some Value ; 
margin-top:Some Value ; 
}
    }

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

試試吧

CSS

.red { 
    width: 332px; 
    background-color: #7E7D7D; 
    opacity: 0.6; 
    margin-left:525px; 
    margin-top:-142px; 
}

.green { 
    width: 332px; 
    background-color: #7E7D7D; 
    opacity: 0.6; 
    margin-left:25px; 
    margin-top:-342px; 
}

假設你的twitter-2 div目前有class red

window.resize function

$(window).resize(function(){
   $('#twitter-2').addClass('green').removeClass('red');
});

您也可以根據需要以不同的width進行操作。

閱讀http://api.jquery.com/resize/

只需將整條線放在一個容器上,而不是左右浮動,當空間太小時,盒子會自然地進入下面。

<div class="row">
    <div class="follow">keep in touch</div>
    <div class="tweet-update">bla bla</div>
</div>

.follow {float:left; margin-bottom:30px;}
.tweet-update {float:right}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM