中心DIV Css无法正常工作

时间:2014-01-29 06:07:56

标签: html css

我正试图将一组图像居中,似乎没有任何效果。我使用margin: 0 auto;display: table;等尝试了display: inline-block;。我不知道还有什么可以尝试,因为我以前从未遇到过这个问题。

<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>

这是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;
}

3 个答案:

答案 0 :(得分:1)

您的标记非常不正确,例如您已将div放入内联a标记内。

为什么您的标记失败:

您的position ed div没有placement left /rightwidth也丢失了!

.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;
}
ca-item_two课程中

第二:,你有不必要的float,删除它,以便div占据适当的空间以在中间对齐,否则将100%宽div放在中心是没有意义的!

working fiddle

答案 1 :(得分:0)

试试这个小提琴:
http://jsfiddle.net/jbwebtech/99hkT/1/

我添加了一个绿色和红色边框,以便我们可以看到正在发生的事情,您可以将其删除。

问题是您的position:absolute覆盖了您正确设置的margin:0 auto。其他一切看起来都不错!

答案 2 :(得分:0)

您可以使用此代码

.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>