我只是想显示一个盒子的网格,每个盒子都有关于狗的品种的信息。我的问题是:当我声明一个内联样式与一个类中的相同样式时,行为上似乎有所不同。我很困惑为什么有区别,而据我所知应该没有。

这是正常运行的代码:http://imgur.com/a/z2b5c
这在下面的代码中发生。要注意的代码是用样式声明的dogDisplay类,以及主体中的dogDisplay类的div。 div具有嵌入式样式=“ position:absolute”。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

   <link href="bootstrap-3.3.6-dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<style>
    .dogDisplay {
        display: inline-block;
        width: 300px;
        height: 300px;
        margin: 10px;
        background-color: grey;
    }

    .dogName {
        font-size: 20px;
        text-align: center;
    }

    .img {
        width: 200px;
        height: 200px;
        display: block;
        margin-left: auto;
        margin-right: auto;
    }

    .content {
        background-color: red;
    }

</style>

</head>

<script>
    //This just loads the dog information from a JSON file
    var dogInfo = [];

    function displayInfo(dogBreeds) {
        $("#container").append("<div class='dogDisplay'></div>");
    }

    window.onload = function() {

        $.getJSON("breeds.json", function(json) {
            var i;
            console.log(typeof(json.dogBreeds));
            dogInfo = json.dogBreeds;
            displayInfo(json.dogBreeds);
        })
    }

</script>

<body>

    <div class="well" style="height:300px"></div>

    <div id="container" class="container" style="width:100%;background-color:lightblue;border:black 2px solid;">
        <div class="dogDisplay" style="position:absolute">
            <div class="content">
                <p class="dogName">Dog Name</p>
                <img class="img" src="images/place-holder.jpg" alt="Place-holder">
            </div>
        </div>
    </div>
</body>




然而。将style =“ position:absolute”移到dogDisplay类中时,这是新行为:http://imgur.com/a/sv3Oh

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="bootstrap-3.3.6-dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<style>
    .dogDisplay {
        display: inline-block;
        width: 300px;
        height: 300px;
        margin: 10px;
        background-color: grey;
        position: absolute;
    }

    .dogName {
        font-size: 20px;
        text-align: center;
    }

    .img {
        width: 200px;
        height: 200px;
        display: block;
        margin-left: auto;
        margin-right: auto;
    }

    .content {
        background-color: red;
    }

</style>

</head>

<script>
    var dogInfo = [];

    function displayInfo(dogBreeds) {
        $("#container").append("<div class='dogDisplay'></div>");
    }

    window.onload = function() {

        $.getJSON("breeds.json", function(json) {
            var i;
            console.log(typeof(json.dogBreeds));
            dogInfo = json.dogBreeds;

            displayInfo(json.dogBreeds);
        })

    }

</script>

<body>
    <div class="well" style="height:300px"></div>
    <div id="container" class="container" style="width:100%;background-color:lightblue;border:black 2px solid;">
        <div class="dogDisplay">
            <div class="content">
                <p class="dogName">Dog Name</p>
                <img class="img" src="images/place-holder.jpg" alt="Place-holder">
            </div>
        </div>
    </div>
</body>




为什么有区别?谢谢!

最佳答案

在您的第一个示例中,此行:
$("#container").append("<div class='dogDisplay'></div>");
与起始布局冲突:
<div class="dogDisplay" style="position:absolute">

意味着只有初始的.dogDisplay具有绝对位置。在第二个示例中,所有这些都将具有绝对定位。

09-25 19:20