如何在仍然响应的同时将图像放在左上角?

时间:2019-07-10 10:20:31

标签: html css angular bootstrap-4

我是Angular / Bootstrap的新手,目前正在尝试将图像放在左上角导航栏的顶部。使用填充/边距可以解决正常分辨率/静态屏幕尺寸的问题,更改尺寸可以将图像放置在其他位置。

我曾尝试使用padding和margin,但是正如我所说,在更改浏览器尺寸时,无法使其在左上角“发粘”。 同样使用引导类,例如“ pull-right”(仅适用于测试用例)不起作用。

我当前的代码如下:

<header>
    <div class="cloud">
        <!-- Navigation -->
        <nav class="navbar navbar-expand-lg navbar-dark $green static-top">
            <div class="container">
                <a class="navbar-brand" href="#">
                    <img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf" class="pull-right" alt="" width="431.27" height="55.35">
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
                    aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item active">
                            <a class="nav-link" href="#">Home
                                <span class="sr-only">(current)</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">About</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Profile</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Impressum</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
    <div class="first">
        <h1>TestSite</h1>
    </div>
</header>

我希望左上角有一张图片,它会在更改浏览器大小/设备时响应地停留在该图片上

1 个答案:

答案 0 :(得分:0)

请检查此代码以查看这是否是所需的结果。可以正常使用的演示程序here

对代码进行的主要更改是

  1. 添加了CSS类bg-dark,并在导航中删除了$green static-top<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  2. 使用CSS将品牌形象的宽度固定为50%。可以将其更改为其他值。
  3. width标记中删除了heightimg属性。

.navbar-brand img{
  width: 50%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <div class="container">
                <a class="navbar-brand" href="#">
                    <img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf">
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
                    aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item active">
                            <a class="nav-link" href="#">Home
                                <span class="sr-only">(current)</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">About</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Profile</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Impressum</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

  
</div>