2013-03-19 93 views
2

我是新來的HTML和CSS,所以在這裏忍受我。我有兩列的引導行。我想將左欄中的div與右欄中的div垂直對齊。我怎樣才能做到這一點?在引導列中垂直對齊div

具體而言,我想在左邊有文字說明右邊的某些元素的div。例如,想象一下閱讀一本書,但在頁面的左邊空白處有一些標註,用於定義與這些詞相鄰的單詞。

右列中的內容是動態的。我知道某些div會出現,我只是不知道它們在哪裏垂直。

這是一個jsfiddle:http://jsfiddle.net/dylanlingelbach/C73ap/。它應該是我想要做的事情的一個相當小的重複。在我的實際案例中,休息時間是我不知道大小的html內容。

我使用的是引導2.

我寧願如果可能做到這一點沒有的JavaScript。我相信我可以通過在右列中找到div的位置並動態調整左列中div的絕對位置來動態地實現它,但我覺得像純粹的CSS解決方案會更優雅。

HTML:

<div class="modal"> 
    <div class="modal-header"> 
    <button type="button" class="close cancel" aria-hidden="true">×</button> 
    <h2 id="invite-modal-label">Some text</h2> 
    </div> 
    <div class="modal-body"> 
    <div class="row-fluid"> 
     <div class="span3"> 
     <div class="one"> 
      Align with #anchor1 
     </div> 
     <div class="two"> 
      Align with #anchor2 
     </div> 
     </div> 
     <div class="span9 padded"> 
      Content 
      <br/><br/><br/><br/><br/><br/> 
      <div id="anchor1"> 
       #anchor1 
      </div> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      <div id="anchor1"> 
       #anchor2 
      </div> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      <br/><br/><br/><br/><br/><br/> 
      Text at the end 
     </div> 
    </div> 
    </div> 
    <div class="modal-footer"> 
    <button class="btn btn-success confirm">Send</button> 
    <button class="btn btn-danger cancel">Cancel</button> 
    </div> 
</div> 

CSS:

@import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css'); 

.modal { 
    width: 60%; 
    left: 5%; 
    margin: auto auto auto auto; 
} 

.one { 
    border:1px solid black; 
} 

.two { 
    border:1px solid black; 
} 

UPDATE: 我不想重新格式化我的HTML。右列中的內容單獨呈現。基本上這個數據顯示在幾個地方,而不是所有地方都有左列。我想讓我的代碼將右列的內容與其餘的標記分開。

+0

/右列這些項目應該在自己的行中。每兩個要素對齊的新行。 – 2013-03-19 18:48:00

回答

2

對於每個項目要垂直對齊,他們需要在自己的行:

<div class="row-fluid"> 
    <div class="span6"></div> 
    <div class="span6">content content content content content content content content content content content content content content content content content content</div> 
</div> 
<div class="row-fluid"> 
    <div class="span6">Align with #anchor1</div> 
    <div class="span6">#anchor1</div> 
</div> 

Here is the correct jsfiddle

+0

哎呀。我試圖避免這種情況,因爲右列的渲染完全獨立於左列。 – Dylan 2013-03-19 19:02:07

0

你或許應該返工你的HTML,支持更多的你正試圖格式實現。

我建議將每個錨標籤和它的內容分成它自己的行。

我所做的是每個部分分解成它自己的.fluid-row,就像這樣:

<div class="modal"> 
    <div class="modal-header"> 
     <button type="button" class="close cancel" aria-hidden="true">×</button> 
     <h2 id="invite-modal-label">Some text</h2> 
    </div> 
    <div class="modal-body"> 
     <div class="row-fluid"> 
      <div class="span3"> 
       <div class="one">Align with #anchor1</div> 
      </div> 
      <div class="span9 padded"> 
       Content for anchor 1 
       <br /><br /><br /><br /><br /><br /> 
      </div> 
     </div><!-- end .row-fluid --> 
     <div class="row-fluid"> 
      <div class="span3"> 
       <div class="two">Align with #anchor2</div> 
      </div> 
      <div class="span9 padded"> 
       Content for anchor 2 
       <br /><br /><br /><br /><br /><br /> 
      </div> 
     </div><!-- end .row-fluid -->   
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-success confirm">Send</button> 
     <button class="btn btn-danger cancel">Cancel</button> 
    </div> 
</div> 

你可以看到這這裏的結果:http://jsfiddle.net/C73ap/38/如果要垂直對齊的東西在左

+0

我也在上面評論過,但我試圖避免這種情況。右列中的內容與左列中的內容完全獨立。如果我這樣做了,我將不得不在幾個地方複製數據的渲染代碼。我希望呈現一次然後對齊。我會更新我的問題以清楚說明。 – Dylan 2013-03-19 19:03:37

+0

嗯,這取決於你決定什麼更容易。很明顯,你當前的HTML格式不會輕鬆。你將不得不在某個地方妥協,找到一個可行的解決方案。 – Axel 2013-03-19 19:09:44

+0

我結束了剛剛使用JavaScript來顯示左側的div。這並不難 – Dylan 2013-03-20 03:10:58