2013-07-31 5 views
0

Я пытаюсь выровнять два div бок о бок. Изображение слева, текст справа для первого элемента. Текст слева, изображение справа второй элемент. И, наконец, изображение слева, текст справа на третьем.DIVs Неправильное выравнивание

Он правильно работает для первого и третьего предметов. Второй элемент не может быть выровнен. Что я сделал не так?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
.container { 
padding: 5px; 
background-color:#66C; 
} 

.imageContainer { 
    margin: 0 25px 0 0; 
    float: left; 
    height: 301px; 
    width: 301px; 
    background-color:#0CC; 
    position:absolute; 
    margin-bottom: 50px; 
} 

.imageContainerRt { 
    margin: 0 0 0 0px ; 
    float: left; 
    height: 301px; 
    width: 301px; 
    background-color:#0CC; 
    margin-bottom: 50px; 
} 

.text { 
    height: 301px; 
    padding: 5px 5px 0 0; 
    background-color:#C96; 
    margin-left:321px; 
    margin-bottom: 50px; 
} 

.text2 { 
    height: 301px; 
    padding: 5px 5px 0 0; 
    background-color:#C96; 
    margin-right:301px; 
    margin-bottom: 50px; 
} 
</style> 
</head> 

<body> 
<!-- First --> 

    <div class="container"> 
    <div class="imageContainer"><img alt="It takes a winning strategy to achieve business success" src="http://market-velocity.com/wp-content/uploads/2013/07/strategy400x400-300x300.jpg" width="300" height="300" /></div> 
    <div class="text"> 
     <h5><span style="color: #66448a;">It takes a winning strategy to achieve business success</span></h5> 
     Market-Velocity helps companies map their destination and guides them for a strong competitive advantage. 

     The way most companies talk to the market is ineffective. We utilize Strategy Drivers to help your team clearly differentiate you from your competition. We take a close look at what you are saying to your clients and prospects and how your brand affects lead generation and business development. 
    <h6><span style="color: #666666;">Strategy Drivers</span></h6> 
    <ul class="insideBullet"> 
    <li><strong>Navigate</strong> – mapping your core identity and differentiation that is compelling to your clients</li> 
    <li><strong>Advance</strong> – defining the perception that you want others to have about your company</li> 
    <li><strong>Arrive</strong> – developing your messages and fostering the culture of being relentless in your pursuit</li> 
    </ul> 
     </div> 

    <!-- Second --> 

    <div class="text2"> 
     <h5><span style="color: #66448a;">It takes a winning strategy to achieve business success</span></h5> 
     Market-Velocity helps companies map their destination and guides them for a strong competitive advantage. 

     The way most companies talk to the market is ineffective. We utilize Strategy Drivers to help your team clearly differentiate you from your competition. We take a close look at what you are saying to your clients and prospects and how your brand affects lead generation and business development. 
    <h6><span style="color: #666666;">Strategy Drivers</span></h6> 
    <ul class="insideBullet"> 
    <li><strong>Navigate</strong> – mapping your core identity and differentiation that is compelling to your clients</li> 
    <li><strong>Advance</strong> – defining the perception that you want others to have about your company</li> 
    <li><strong>Arrive</strong> – developing your messages and fostering the culture of being relentless in your pursuit</li> 
    </ul></div> 
    <div class="imageContainerRt"></div> 



    <!-- Third --> 

    <div class="imageContainer"><img alt="It takes a winning strategy to achieve business success" src="http://market-velocity.com/wp-content/uploads/2013/07/strategy400x400-300x300.jpg" width="300" height="300" /></div> 
    <div class="text"> 
     <h5><span style="color: #66448a;">It takes a winning strategy to achieve business success</span></h5> 
     Market-Velocity helps companies map their destination and guides them for a strong competitive advantage. 

     The way most companies talk to the market is ineffective. We utilize Strategy Drivers to help your team clearly differentiate you from your competition. We take a close look at what you are saying to your clients and prospects and how your brand affects lead generation and business development. 
    <h6><span style="color: #666666;">Strategy Drivers</span></h6> 
    <ul class="insideBullet"> 
    <li><strong>Navigate</strong> – mapping your core identity and differentiation that is compelling to your clients</li> 
    <li><strong>Advance</strong> – defining the perception that you want others to have about your company</li> 
    <li><strong>Arrive</strong> – developing your messages and fostering the culture of being relentless in your pursuit</li> 
    </ul> 
    </div> 
    </div></div> 
    </div> 
</body> 
</html> 
+0

Просто Sidenote: Зачем использовать архаичный доктайп? Должно работать нормально. – MightyPork

+0

Можете ли вы, пожалуйста, воссоздать свою проблему в JSFiddle, чтобы мы могли видеть, что происходит? Или даже скриншот. – Michael

ответ

0

.imageContainerRt должен float:right, не left

.imageContainerRt { 
    margin: 0 0 0 0px ; 
    float: right; 
    height: 301px; 
    width: 301px; 
    background-color:#0CC; 
    margin-bottom: 50px; 
} 

также убедитесь, очистите свои поплавки, поместите это после всех ваших флотированных контейнеров, добавив clearfix класса в родительский контейнер

<div class="container clearfix"></div> 

и добавить CSS

.clearfix { 
    *zoom: 1; 
} 

.clearfix:before, 
.clearfix:after { 
    display: table; 
    line-height: 0; 
    content: ""; 
} 

.clearfix:after { 
    clear: both; 
} 

FIDDLE HERE

+0

Не включайте пустой div только для ясного. Вместо этого используйте родительский элемент [clearfix] (http://css-tricks.com/snippets/css/clear-fix/). – Axel

+0

Выглядит хорошо в Fiddle. Когда я копирую код для редактирования, вторая строка неверно работает. Похоже, дополнительное пространство изображения после текста. Итак, текст, пустое пространство, затем изображение. – Kyle

+0

Я изменил поле .text2 на 25px и решил проблему. – Kyle

0

просто добавить дополнительный DIV (в качестве оболочки) для каждой строки и и CSS будет

.wrapper{ 
    width:100%; 
    overflow:hidden; 
} 
0

я переписал свой код немного. Удаленные накладные расходы и написали его более масштабируемыми с подклассами.

http://codepen.io/anon/pen/Getju

Причины, почему он не работает с вашим кодом. - пустые дивы - ошибки разметки - плавучие вопросы (контейнеры использование)

Если вы плаваете элементы друг к другу положить их всегда вместе в контейнере - не плавают контейнер.

поплавков хорошо объяснили: http://css-tricks.com/all-about-floats/

+0

Есть ли способ, чтобы текст не двигался позади изображения при изменении размера окна? – Kyle

+0

Если вам не нужно устанавливать свойство width в процентах% - вы можете вернуться к пикселам, и проблема исчезнет. Другие возможности: - установите минимальную ширину 300 пикселей на изображениеContainer (но здесь возможно, что вторая строка перерывается на следующую строку, если пространства недостаточно) –

Смежные вопросы