Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Row of centered images and text

    I'm trying to create a row of banner-like images, centered and gap between each auto-adjusted, each with an aligned text below it. I tried to use the CSS property columns, but I'm unable to change the amount of space between each element.

    Picture:


    This is what I have so far:

    HTML:
    Code:
    <div class="container">
    
    	<div id="pic1">
    		<img src="pictures/characters/1.png">
    		<br>
    		<br>
    		<span id="pic1text">text1</span>
    	</div>
    				
    	<div id="pic2">
    		<img src="pictures/characters/2.png">
    		<br>
    		<br>
    		<span id="pic2text">text2</span>
    	</div>
    				
    	<div id="pic3">
    		<img src="pictures/characters/3.png">
    		<br>
    		<br>
    		<span id="pic3text">text3</span>
    	</div>
    				
    	<div id="pic4">
    		<img src="pictures/characters/4.png">
    		<br>
    		<br>
    		<span id="pic4text">text4</span>
    	</div>
    				
    	<div id="pic5">
    		<img src="pictures/characters/5.png">
    		<br>
    		<br>
    		<span id="pic5text">text5</span>
    	</div>
    				
    </div>
    CSS:
    Code:
    .container{
    	width:950px;
    	height:350px;
    	-moz-column-count:8; /* Firefox */
    	-webkit-column-count:8; /* Safari and Chrome */
    	column-count:8;
    }
    
    /*div of each image*/
    .container > div {
    	display:inline;
    	height:350px;
    	width:125px;
    }
    But the moment I add "column-gap:5px;" the pictures won't be in a single row anymore and the formatting is completely messed up. I also need an identifier for each text, as I'm gonna need to fade in/out these text when the mouse hovers over each image, which I'll do once the formatting is done.
    Last edited by Windbrand; 11-18-2012 at 03:33 AM.

  • #2
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't know about columns, but you could just use "float: left" instead. Then your "columns" will be side-by-side without any additional fuss.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,354
    Thanks
    23
    Thanked 618 Times in 617 Posts
    @Windbrand the CSS3 column-count property is used to divide text in the div element into columns, like a news paper. It has no place here. Use the float as comport9 said and set a margin-right for the space between them. You also should use a padding-left on the container to move the divs to the right.

    You don't need .container > div A .container div will work.

  • #4
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Using <br /> to space things out is a bad idea. A better option is to use margin and padding and to make your layout elastic (auto-adjusted as you put it) you can use % for dimensions.

    Here is a basic example of an elastic layout with 4 images. If you have a different number of images, just adjust the %'s for .picContainer in the css to suit.

    The images and their container will automatically shrink or enlarge as you change the browser size.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css">
                #container {
                    width: 70%;
                    margin: 30px auto 0px auto;
                    border: 3px solid blue;
                    overflow: hidden;
                }
                .picContainer{
                    float: left;
                    width: 20%;
                    margin: 0px 2.5% 0% 2.5%;
                }
                .picContainer img {
                    display: block;
                    width: 100%;
                }
                .picContainer p{
                    margin: 10px 0px 0px 0px;
                    text-align: center;
                }
                #gutter{
                    margin: 10px 10px 10px 10px;
                    overflow: hidden;
                }
            </style>
        </head>
        <body>
            <div id="container">
                <div id="gutter">
                    <div class="picContainer" >
                        <img src="image1.jpg" alt="" />
                        <p>caption 1</p>
                    </div>
                    <div class="picContainer">
                        <img src="image2.jpg" alt="" />
                        <p>caption 2</p>
                    </div>
                    <div class="picContainer">
                        <img src="image3.jpg" alt="" />
                        <p>caption 3</p>
                    </div>
                    <div class="picContainer" >
                        <img src="image4.jpg" alt="" />
                        <p>caption 4</p>
                    </div>
                </div>
            </div>
        </body>
    </html>
    Last edited by minder; 11-18-2012 at 06:28 AM.

  • #5
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello Windbrand,
    Floats are great for putting elements side by side but you'll have to do the math to center them.
    Look at this approach (the margin is what controls the space between them)
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style type="text/css">
    html, body {
    	margin: 0;
    	background: #fc6;
    }
    .container{
    	width: 950px;
    	padding: 10px 10px 50px;
    	text-align: center;
    	background: #333;
    }
    .container > div {
    	height: 350px;
    	width: 125px;
    	margin: 0 5px;
    	display: inline-block;
    	background: #f00;
    }
    </style>
    </head>
    <body>
    	<div class="container">
            <div id="pic1">
                <img src="pictures/characters/1.png">
                <br>
                <br>
                <span id="pic1text">text1</span>
            </div>				
            <div id="pic2">
                <img src="pictures/characters/2.png">
                <br>
                <br>
                <span id="pic2text">text2</span>
            </div>				
            <div id="pic3">
                <img src="pictures/characters/3.png">
                <br>
                <br>
                <span id="pic3text">text3</span>
            </div>				
            <div id="pic4">
                <img src="pictures/characters/4.png">
                <br>
                <br>
                <span id="pic4text">text4</span>
            </div>				
            <div id="pic5">
                <img src="pictures/characters/5.png">
                <br>
                <br>
                <span id="pic5text">text5</span>
            </div>
        <!--end .container--></div>
    </body>
    </html>
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •