jquery - 用 jQuery 居中 div

标签 jquery html css center

我正在尝试将一组 div(1 类)置于屏幕中央。我的问题是每个 div 都有不同的尺寸。我遵循了一些我发现的使用 jQuery 使 div 居中的准则:

$(document).ready(function () {
            $(".content").position({
                "my": "center center",
                "at": "center center",
                "of": "center center"
            });
        });

但这似乎对我不起作用: http://i.imgur.com/Znyju4z.png 我做错了什么?

HTML:

<div class="galleryarea">
        <div class="galleryframe">
            <div class="galleryentity" style="background-image:url('link_here');">
                <a href="javascript:void(0)" class="gallerylink" onclick = "document.getElementById('pic1').style.display='block';document.getElementById('fade').style.display='block'"></a>
                <div id="pic1" class="content"><img class="galleryfullsize" src="link_here"></div>
            </div>
        </div>

CSS:

.galleryentity{
    display: table;
    margin-bottom: 35px;
    float: left;
    width: 259px;
    height: 250px;
    background-color: #2B3039;
    margin-right: 30px;
    position: relative;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    -webkit-box-shadow: 0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 3px 5px 2px rgba(50, 50, 50, 0.75);
}
.white_content {
    display: none;
    position: absolute;
    margin: 0 auto;
    border: 8px solid orange;
    background-color: #eee;
    z-index:1002;
    overflow: auto;
}
.newsframe{
    display: inline-block;
    margin: 0 auto;
    max-width: 1200px;
    margin-top: 35px;
}
.newsentity{
    display: table;
    float: left;
    width: 259px;
    height: 250px;
    background-color: #2B3039;
    margin-right: 30px;
    position: relative;
    -webkit-box-shadow: 0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 3px 5px 2px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 3px 5px 2px rgba(50, 50, 50, 0.75);
}
.newsarea{
    width: 100%;
    min-height: 320px;
    background-color: #3F444D;
    text-align: center;
}

最佳答案

尝试这样的事情

$(".content").each(function() {
    $(this).css("position","relative");
    $(this).css("margin-left","auto");
    $(this).css("margin-right","auto");
});

关于jquery - 用 jQuery 居中 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265931/

相关文章:

javascript - 谷歌浏览器如何在没有扩展的情况下永久水平翻转

php - 使用 JavaScript 更改不需要的表数据

html - 条件 CSS : if a selector contains another selector?

javascript - jquery 在加载之前从页面中删除图像

html - 使用 mod_rewrite 后,我的 css imges js 路径发生了变化

javascript - 清除正常 div 和固定 div 之间的边距

html - 单击时禁用标题工具提示文本的隐藏?

JQuery Accordion 内容右边框不显示

javascript - fancybox 输入在 <a> 内

JavaScript计算元素的总价