2016-05-25 164 views
0

我已經閱讀了幾乎所有關於並列排列的div的帖子,但沒有一個適用於我。兩個Div並非並排排列

看起來好像我錯過了兩個塊上的float:left;,但無論出於何種原因,他們都沒有尊重這一點。如果我對合適的容器做float:right;,那麼它只是將正確的容器推到左邊的容器下。

到目前爲止,我已經嘗試過css屬性:

float:right and left; display:in-line; display:block; 左導航和右導航容器的寬度

在我看來,似乎「主容器」阻止正確的容器向上移動,但是,這個div在容器內。我也試過z-index和位置。

我在做什麼錯在這裏?

body { 
 
    font-family: Arial, Helvetica, sans-serif !important; 
 
    background: URL("http://www.mscdirect.com/global/application-content/images/header/ribbon-bg-1.gif") repeat-x scroll top left; 
 
    clear: both; 
 
} 
 

 
#header {} 
 

 
#content-container { 
 
    width: 961px; 
 
} 
 

 
.top-container { 
 
    border-bottom: 1px solid #ccc; 
 
    cursor: pointer; 
 
    font-size: 14px; 
 
    font-weight: bolder; 
 
    margin: 5px 0px 0px 20px; 
 
    padding: 10px 0 18px; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
} 
 

 
.reg-text { 
 
    font-weight: normal; 
 
    text-transform: uppercase; 
 
    font-size: 12.5px; 
 
    cursor: pointer; 
 
} 
 

 
.red-text { 
 
    color: #db403b; 
 
    font-size: 18px; 
 
    text-transform: uppercase; 
 
    cursor: pointer; 
 
} 
 

 
.left-container { 
 
    width: 262px; 
 
    border: 1px solid #ccc; 
 
    margin: 20px 0px 0px 20px; 
 
} 
 

 
.left-nav { 
 
    width: 245px; 
 
    margin-top: 10px; 
 
} 
 

 
.left-nav-head { 
 
    text-transform: uppercase; 
 
    width: 208px; 
 
    margin: 0px 0px 6px 7px; 
 
    padding: 5px 20px 3px 14px; 
 
    font-size: 12px; 
 
    list-style-image: none; 
 
    color: #fff; 
 
    background: #345599; 
 
} 
 

 
.left-nav li { 
 
    background: rgba(0, 0, 0, 0) url("http://www.mscdirect.com/global/application-content/images/content/contentArrow2.gif") no-repeat scroll 16px 7px; 
 
    list-style: inside none none; 
 
    margin: 0px 0px 5px 5px; 
 
    padding: 4px 16px 6px 27px; 
 
    font-size: 13px 
 
} 
 

 
.right-container { 
 
    width: 662px; 
 
    display: block; 
 
} 
 

 
ul { 
 
    list-style: none; 
 
} 
 

 
li:nth-child(odd) .prod-text-box { 
 
    float: left; 
 
} 
 

 
li:nth-child(odd) .prod-img-box { 
 
    position: relative; 
 
    padding-right: 100px; 
 
} 
 

 
#main-container { 
 
    width: 961px; 
 
    margin: 0px auto; 
 
} 
 

 
a { 
 
    text-decoration: none; 
 
    color: #2866B1 !important; 
 
    font: bold 12px Arial, Helvetica, sans-serif !important; 
 
    text-decoration: none; 
 
    padding: 5px 0 5px 
 
} 
 

 
a:hover:visted:link { 
 
    color: #2866B1 !important; 
 
    font: bold 12px Arial, Helvetica, sans-serif !important; 
 
    text-decoration: none; 
 
    padding: 5px 0 5px 
 
} 
 

 
.product-content { 
 
    float: left; 
 
    border: 1px solid #ccc; 
 
    width: 664px; 
 
    margin-left: 300px 
 
} 
 

 
.prod-box { 
 
    width: 644px; 
 
    border-bottom: 1px solid #ccc; 
 
    float: left; 
 
    clear: both; 
 
    margin: 0px 0px 10px 13px; 
 
    min-height: 200px; 
 
} 
 

 
.prod-img-box { 
 
    overflow: hidden; 
 
    margin-top: 20px; 
 
    float: right; 
 
    position: absolute; 
 
} 
 

 
.prod-text-box { 
 
    width: 272px; 
 
    position: relative; 
 
    float: right; 
 
    clear: both; 
 
} 
 

 
.prod-title { 
 
    font-size: 26px; 
 
    color: #00337a; 
 
    line-height: 30px; 
 
    text-align: left; 
 
    padding-top: 10px; 
 
    clear: both; 
 
} 
 

 
.prod-cta { 
 
    color: #2866B1 !important; 
 
    font: bold 12px Arial, Helvetica, sans-serif !important; 
 
    text-decoration: none; 
 
    padding: 5px 0 5px; 
 
    clear: both; 
 
} 
 

 
</style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <!-- IE is not seeing HTML5 tags so I added this to fix that --> <!--[if lt IE 9]> <script> document.createElement('header'); 
 
document.createElement('nav'); 
 
document.createElement('section'); 
 
document.createElement('article'); 
 
document.createElement('figure'); 
 
document.createElement('figcaption'); 
 
document.createElement('footer'); 
 
</script> <![endif]-->
<div id="main-container"> 
 
    <div id="header"><img src="header.png" width="961px" height="144px"></div> 
 
    <div class="top-container">save up to <span class="red-text">25% </span>on orders over <span class="red-text">$249</span> + get free shipping when you order online.<span class="reg-text">&nbsp;&nbsp;&nbsp;&nbsp;enter code:</span> ALL25H <span class="reg-text">click to apply &#9658;</span></div> 
 
    <div id="content-container"> 
 
    <div class="left-container"> 
 

 
     <!-- Left Navigation --> 
 
     <div class="left-nav"> 
 
     <div class="left-nav-head">Head Protection</div> 
 
     <ul> 
 
      <li>Hard Hats</li> 
 
      <li>Bump Caps</li> 
 
      <li>Welding Helmets</li> 
 
      <li>Face Sheilds</li> 
 
      <li>Winter Liners</li> 
 
     </ul> 
 
     </div> 
 

 
     <!-- Products Section --> 
 
     <div class="right-container"> 
 
     <div class="product-content"> 
 
      <ul> 
 
      <li> 
 
       <a name="hardhat"></a> 
 
       <div class="prod-box"> 
 
       <div class="prod-text-box"> 
 
        <div class="prod-title">Hard Hats</div> 
 
        Protect yourself from minor head and neck injuries with the comfort and reliability of bump caps. Choose from a variety of styles, colors, and adjustment types. 
 
        <div class="prod-cta"><a href="/browse/tn/Safety/Personal-Protective-Equipment/Hard-Hats-Bump-Caps/Bump-Caps?navid=12106709&rdrct=Bump+Caps&intcmp=BumpCaps_SafetyHeadProtectionStaticPage_CTA_April2016_v1">Shop Bump Caps<span>&#9658;</span></a></div> 
 
       </div> 
 
       <div class="prod-img-box"><img src="images/hardhat.jpg" alt="Hard Hats"></div> 
 
       </div> 
 
      </li> 
 
      <li> 
 
       <a name="bumpcaps"></a> 
 
       <div class="prod-box"> 
 
       <div class="prod-text-box"> 
 
        <div class="prod-title">Bump Caps</div> 
 
        Protect yourself from minor head and neck injuries with the comfort and reliability of bump caps. Choose from a variety of styles, colors, and adjustment types. 
 
        <div class="prod-cta"><a class="prod-cta" href="/browse/tn/Safety/Personal-Protective-Equipment/Hard-Hats-Bump-Caps/Bump-Caps?navid=12106709&rdrct=Bump+Caps&intcmp=BumpCaps_SafetyHeadProtectionStaticPage_CTA_April2016_v1">Shop Bump Caps<span>&#9658;</span></a></div> 
 
       </div> 
 
       <div class="prod-img-box"><img src="images/hardhat.jpg" alt="Hard Hats"></div> 
 
       </div> 
 
      </li> 
 
      <li> 
 
       <a name="bumpcaps"></a> 
 
       <div class="prod-box"> 
 
       <div class="prod-text-box"> 
 
        <div class="prod-title">Bump Caps</div> 
 
        Protect yourself from minor head and neck injuries with the comfort and reliability of bump caps. Choose from a variety of styles, colors, and adjustment types. 
 
        <div class="prod-cta"><a class="prod-cta" href="/browse/tn/Safety/Personal-Protective-Equipment/Hard-Hats-Bump-Caps/Bump-Caps?navid=12106709&rdrct=Bump+Caps&intcmp=BumpCaps_SafetyHeadProtectionStaticPage_CTA_April2016_v1">Shop Bump Caps<span>&#9658;</span></a></div> 
 
       </div> 
 
       <div class="prod-img-box"><img src="images/hardhat.jpg" alt="Hard Hats"></div> 
 
       </div> 
 
      </li> 
 
      <li> 
 
       <a name="bumpcaps"></a> 
 
       <div class="prod-box"> 
 
       <div class="prod-text-box"> 
 
        <div class="prod-title">Bump Caps</div> 
 
        Protect yourself from minor head and neck injuries with the comfort and reliability of bump caps. Choose from a variety of styles, colors, and adjustment types. 
 
        <div class="prod-cta"><a class="prod-cta" href="/browse/tn/Safety/Personal-Protective-Equipment/Hard-Hats-Bump-Caps/Bump-Caps?navid=12106709&rdrct=Bump+Caps&intcmp=BumpCaps_SafetyHeadProtectionStaticPage_CTA_April2016_v1">Shop Bump Caps<span>&#9658;</span></a></div> 
 
       </div> 
 
       <div class="prod-img-box"><img src="images/hardhat.jpg" alt="Hard Hats"></div> 
 
       </div> 
 
      </li> 
 
      <li> 
 
       <a name="bumpcaps"></a> 
 
       <div class="prod-box"> 
 
       <div class="prod-text-box"> 
 
        <div class="prod-title">Bump Caps</div> 
 
        Protect yourself from minor head and neck injuries with the comfort and reliability of bump caps. Choose from a variety of styles, colors, and adjustment types. 
 
        <div class="prod-cta"><a class="prod-cta" href="/browse/tn/Safety/Personal-Protective-Equipment/Hard-Hats-Bump-Caps/Bump-Caps?navid=12106709&rdrct=Bump+Caps&intcmp=BumpCaps_SafetyHeadProtectionStaticPage_CTA_April2016_v1">Shop Bump Caps<span>&#9658;</span></a></div> 
 
       </div> 
 
       <div class="prod-img-box"><img src="images/hardhat.jpg" alt="Hard Hats"></div> 
 
       </div> 
 
      </li> 
 
      </ul> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+1

它不得在格式使用的標題'我的代碼不work'。另外,請發佈人們可以與之互動的** jsbin **(或任何其他頁面) – vsync

+0

您能否提供您想要的設計方案? 這裏是你的情況jsfiddle - 想自己下次提供一個;) https://jsfiddle.net/f3sm7n0f/ – Will

+0

恕我直言,你真的需要做的是回到HTML和簡化。有幾個容器沒有真正的用途,我可以說。例如,你不需要一個左容器和一個右容器。一旦HTML結構被簡化後,我也會重構我的CSS。我認爲你已經發現自己處於一種經典的情況下,你一直在嘗試的東西,它已經失控。把你的問題分解成小塊,解決這些問題,然後把它們放回去。當我發現自己處於這樣的狀況時,這個建議對我很好。 – orangeh0g

回答

1

當前保存在您的標記設置是這樣的:

<container> 
    <left-container> 
     <left-nav></left-nav> 
     <right-container></right-container> 
    </left-container> 
</container> 

它需要:

<container> 
    <left-container> 
     <left-nav></left-nav> 
    </left-container> 
    <right-container></right-container> 
</container> 

左邊的容器不跨越容器的整個寬度。從那裏,你添加

float: left; 

到您的左側導航和

float: right; 

到您的權利,容器,你應該準備就緒!

0

@kiaanabal說了什麼,並且在左邊容器上的css位置絕對是非常有用的。

.left-container { 
    position: absolute; 
    width: 262px; 
    border: 1px solid #ccc; 
    margin: 20px 0px 0px 20px; 
    float: left; 
    overflow: auto; 
} 

http://codepen.io/Thrizian/pen/zBOZNE?editors=1100