为什么按钮位于左侧而不是图片下方?

时间:2017-12-12 15:27:11

标签: html css

为什么按钮位于左侧而不是图片下方?



<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="~/Content/My.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">WebSiteName</a>
            </div>
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">Page 3</a></li>
            </ul>
        </nav>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-3">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <img src="~/Images/FirstImage.jpg" class="img" />
                        <button type="button" class="btn btn-default">Buy</button>
                    </div>
                </div>
            </div>
        </div>
            </div>
        </div>
    </div>
</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

图像是内联元素,例如span,而不是div等块元素,因此下一个元素跟在同一行的图像上。

要使其成为块元素,您可以将图像放在div中:

<div><img ...></div>

或使用样式使其成为块元素

<img style="display: block;" >

或使用css注释:

.img { display: block; }

答案 1 :(得分:0)

因为图像或按钮都不是块元素。这与bootstrap没有任何关系。

&#13;
&#13;
img {
 display: block;
}
&#13;
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="~/Content/My.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">WebSiteName</a>
            </div>
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">Page 3</a></li>
            </ul>
        </nav>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-3">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <img src="https://placebear.com/100/100" class="img" />
                        <button type="button" class="btn btn-default">Buy</button>
                    </div>
                </div>
            </div>
        </div>
            </div>
        </div>
    </div>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您需要设置此CSS规则:

.panel-body { display: grid; }

或者这个:

.img { display: block; }

here 是jsfiddle