Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Esha Khanna
Esha Khanna
4,069 Points

Aligning content side by side

I'm struggling to align images and text side by side/horizontally so the images would be column 1 and text would be column 2 (corresponding to images). What am I missing? This is my HTML and CSS: :

HTML <ul class="gallery">

<li><img src="images/butter.jpg"></li>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>

<li><img src="images/cheese.jpg"></li>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>

</ul>

CSS .gallery { width: 500px; height: 500px; }

.gallery li { list-style: none; }

.gallery li img { max-width: 100%; max-height: 100%; border-radius: 10%; }

1 Answer

Hi Esha,

You really need to have the correct HTML structure in place to me able to achieve a solid solution for this. It's not to complicated once you understand how the CSS will be able to affect things. There is a good article all about floats here.

I have put together a quick example on codepen for you to take a look over..

http://codepen.io/Craig-Watson/pen/RoxPab?editors=1100

Code is below to...

<ul class="gallery">
    <li class="gallery-item">
        <img src="http://placehold.it/250" alt="..." class="gallery-img left" />
        <p class="gallery-text right">Praesent tristique urna arcu, in interdum nibh tristique non. Curabitur at pulvinar ligula. Curabitur gravida consequat tempus.</p>
    </li>
    <li class="gallery-item">
        <p class="gallery-text left">Praesent tristique urna arcu, in interdum nibh tristique non. Curabitur at pulvinar ligula. Curabitur gravida consequat tempus.</p>
        <img src="http://placehold.it/250" alt="..." class="gallery-img right" />
    </li>
    <li class="gallery-item">
        <img src="http://placehold.it/250" alt="..." class="gallery-img left" />
        <p class="gallery-text right">Praesent tristique urna arcu, in interdum nibh tristique non. Curabitur at pulvinar ligula. Curabitur gravida consequat tempus.</p>
    </li>
    <li class="gallery-item">
        <p class="gallery-text left">Praesent tristique urna arcu, in interdum nibh tristique non. Curabitur at pulvinar ligula. Curabitur gravida consequat tempus.</p>
        <img src="http://placehold.it/250" alt="..." class="gallery-img right" />
    </li>
</ul>
* {
    box-sizing: border-box;
}
body {
    background-color: #f0f0f0;
    padding: 20px;
}
img {
    width: 100%;
    max-width: 100%;
}
.gallery {
    margin: 0;
    padding: 0;
    list-style: none;
}
.gallery-item {
    width: 100%;
    margin-bottom: 20px;
    background: #dedede;
}
.gallery-item::before,
.gallery-item::after {
    content: " ";
    display: table; 
}
.gallery-item::after {
    clear: both;
}
.gallery-text {
    margin: 0;
    padding: 10px;
}
@media (min-width: 480px) {
    .gallery-img,
    .gallery-text {
        display: inline-block;
        width: 50%;
    }
    .gallery-img.left,
    .gallery-text.left {
        float: left;
    }
    .gallery-img.right,
    .gallery-text.right {
        float: right;
    }
}

Note I have used `normalize.css on this.

Hope this helps :)

Craig