Jake Ols
Jake Ols

Reputation: 2852

Center DIV Css not working

I'm trying to center a group of images, and nothing seems to be working. I've tried margin: 0 auto; with display: table;, display: inline-block;, and the like. I don't know what else to try, as I've never encountered this problem before.

<div class="main-carousel_two hideme dontHide">
 <div class=" results_wrapper">
     <div class="ca-item_two ca-item-14">
    <div class="f-single_two">
        <a data-gal="prettyPhoto[gallery2]" href="https://vimeo.com/8234379" class="mfp-   iframe">
            <div class="f-image">
                <img src="<?php echo base_url()?>assets/images/reslults/cle2.jpg" alt='img'>
                <div class="image-hover-overlay"></div>
                <span class="f-category"></span>
                <div class="portfolio-meta">
                    <div>txt</div>
                    <div class="clear"></div>
                    <div>Database Video</div>
                </div>
            </div>
            <div class="f-info">text</div>
        </a>
    </div>
</div>
<div class="ca-item_two ca-item-15"> 
    <div class="f-single_two">
        <a data-gal="prettyPhoto[gallery2]" href="http://vimeo.com/469331" class="mfp-iframe">
            <div class="f-image">
                <img src="<?php echo base_url()?>assets/images/reslults_shit/connecticut2.jpg" alt='img'>
                <div class="image-hover-overlay"></div>
                <span class="f-category"></span>
                <div class="portfolio-meta">
                    <div>text</div>
                    <div class="clear"></div>
                    <div>Database Video</div>
                </div>
            </div>
            <div class="f-info">text</div>
        </a>
    </div>
</div>
<div class="ca-item_two ca-item-16">
    <div class="f-single_two">
        <a data-gal="prettyPhoto[gallery2]" href="https://vimeo.com/8486420" class="mfp-iframe">
            <div class="f-image">
                <img src="<?php echo base_url()?>assets/images/reslults/ardl2.jpg" alt='img'>
                <div class="image-hover-overlay"></div>
                <span class="f-category"></span>
                <div class="portfolio-meta">
                    <div>txt</div>
                    <div class="clear"></div>
                    <div>Database Video</div>
                </div>
            </div>
            <div class="f-info">text</div>
        </a>
    </div>
</div>

Here is the css:

.main-carousel_two
{
    overflow: hidden;
    width: 100%;
    height:376px;
}
.results_wrapper
{
overflow: hidden;
position: absolute;
left: 2%;
}
.ca-item_two
{
    position: relative;
    float: left;
    width: auto;
    text-align: center;
}
.f-single_two{
    width: 375px;
    float: left;
    margin: 0 15px;
    position: relative;
    overflow: hidden;
}

Upvotes: 0

Views: 539

Answers (3)

Piyush Teraiya
Piyush Teraiya

Reputation: 747

you can use this code

.center-div {
            margin: 0 auto;
            width: 500px;
            height: 350px;
            background-color: #ccc;
            border-radius: 3px;
            margin-bottom: 30px;
            padding: 15px;
        }
        .center-div a {
            text-align: center;
            text-decoration: none;
        }
        .center-div a .portfolio-meta {
            color: #333;
            font-size: 30px;
        }
        .center-div a .f-info {
            color: #333;
            font-size: 30px;
        }
<div class="main">
        <div class="center-div">
            <a data-gal="prettyPhoto[gallery2]" href="https://vimeo.com/8234379" class="mfp-   iframe">
                <div class="f-image">
                    <img src="https://www.gstatic.com/webp/gallery/1.sm.jpg" alt='img'>
                    <div class="image-hover-overlay"></div>
                    <span class="f-category"></span>
                    <div class="portfolio-meta">
                        <div>txt</div>
                        <div class="clear"></div>
                        <div>Database Video</div>
                    </div>
                </div>
                <div class="f-info">text</div>
            </a>
        </div>
        <div class="center-div">
            <a data-gal="prettyPhoto[gallery2]" href="https://vimeo.com/8234379" class="mfp-   iframe">
                <div class="f-image">
                    <img src="https://www.gstatic.com/webp/gallery/1.sm.jpg" alt='img'>
                    <div class="image-hover-overlay"></div>
                    <span class="f-category"></span>
                    <div class="portfolio-meta">
                        <div>txt</div>
                        <div class="clear"></div>
                        <div>Database Video</div>
                    </div>
                </div>
                <div class="f-info">text</div>
            </a>
        </div>
        <div class="center-div">
            <a data-gal="prettyPhoto[gallery2]" href="https://vimeo.com/8234379" class="mfp-   iframe">
                <div class="f-image">
                    <img src="https://www.gstatic.com/webp/gallery/1.sm.jpg" alt='img'>
                    <div class="image-hover-overlay"></div>
                    <span class="f-category"></span>
                    <div class="portfolio-meta">
                        <div>txt</div>
                        <div class="clear"></div>
                        <div>Database Video</div>
                    </div>
                </div>
                <div class="f-info">text</div>
            </a>
        </div>
    </div>

Upvotes: 0

NoobEditor
NoobEditor

Reputation: 15871

Your markup is pretty incorrect, for instance you have placed div inside an inline a tag.

Why your markup was failing:

Your positioned div didn't had placement of left /right and a missing width too!!

.results_wrapper {
    overflow: hidden;
    position: absolute;
    left:0; /* missing from your css*/
    right:0;/* missing from your css*/
    width:350px;/* missing from your css*/
    margin:0 auto;/* missing from your css*/
    border:1px solid red;
}

second : in ca-item_two class, you had unnecessary float given, remove it so that div occupies proper space to align in the middle, else there is no sense of placing 100% wide div in center!

working fiddle

Upvotes: 1

jbwebtech
jbwebtech

Reputation: 434

Try this fiddle:
http://jsfiddle.net/jbwebtech/99hkT/1/

I added a green and red border so we can see what's going on, which you can delete.

The issue is your position:absolute is overriding the margin:0 auto you correctly set. Everything else looks good!

Upvotes: 0

Related Questions