2015-04-28 16 views
0

也許這是一個有點愚蠢的問題,但如果我有一箇中心網站(使用固定寬度+ margin:0 auto)我如何「附加」一個廣告元素? (左摩天大樓)。如何將廣告元素「附加」到居中網站?

我做了什麼,不喜歡,現在我有一個包裝內的內容和廣告。但包裝必須有比內容(頁面)更大的寬度。因此,網站不再居中,當廣告沒有顯示時(使用adblock等)時,它看起來非常難看。

JS是唯一的方法嗎?

+0

我敢肯定你可以在沒有JavaScript的情況下做到,但我們需要看到一個[最小,完整,可驗證的E xample](http://stackoverflow.com/help/mcve)來幫助你。 – TylerH

+0

見下文,我評論並提供了更多信息。謝謝。 – olaf

回答

0

不知道'attach'是什麼意思,但下面是我如何構建/構建它。

<div id="container"> 
     <div class="column_left">I don't know what your using for ads, but you could place right here. Image, text, ad script?</div> 
    <!-- just place your ad in the above div --> 
     <div class="column_right">Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div> 
    </div> 

CSS:

#container { 
    overflow: hidden; 
    margin: 0 auto; 
    width: 960px; 
    position: relative; 
} 
.column_left { 
    width: 200px; 
    float: left; 
    margin: 0 10px 0 0; 
    background-color: #f7f7f7; 
    border: 1px solid #f5f5f5; 
} 

.column_right { 
    width: 700px; 
    float: left; 
    background: #ddd; 
} 

http://jsfiddle.net/jkLMK/36/

+0

謝謝,這正是我所擁有的,但我不想要的。 :)我想要的是保持內容的中心,無論它的左側或右側(摩天大樓)。例如,這個網站www.dsl.cz使用了一些腳本(可能無法在源代碼中找到它)來計算skyscrapper的頁邊距(假設來自瀏覽器的網頁檢查器)並將其粘貼到內容中。 – olaf

+0

對不起,我很困惑,你不能在互聯網上使用膠水。 –

0

如果你想下去腳本選項,你可以做這樣的事情:

<div id="content" style="width: 960px; margin: 0 auto;"> 
    ... 
</div> 

<div id="ad" style="position: absolute; top: 0px; left: 0px;"> 
    ... 
</div> 

<script> 

    window.onresize = function() { 

    var contentWidth = 960; 
    var marginWidth = (window.innerWidth - contentWidth)/2; 

    document.getElementById("ad").style.width = marginWidth + "px"; 

    }; 

    window.onresize(); 

</script>