调整图像大小以适合div(作为背景)的最佳方法

时间:2017-12-29 14:22:13

标签: html css bootstrap-4

我正在尝试制作背景,85%高度和100%宽度索引页面。实际上我正在使用bootstrap 4.我的问题是,当我将图像作为背景放入div时,它不会调整大小只显示图像的一部分(图像太大)当我尝试使用img属性但我的导航栏跳转到图像下。当然,我尝试了互联网上的一切。 这是我的代码。

<!DOCTYPE html>
<html lang="sk">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="styles/normalize.css">
<link rel="stylesheet" href="styles/b4.css">
<link rel="stylesheet" type="text/css" href="styles/style.css">
<title>Reštaurácia</title>    

</head>

<body>

 <!-- Navigation -->
<div class="bg">

<nav class="navbar navbar-expand-sm navbar-light bg-faded">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#nav-content" aria-controls="nav-content" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<!-- Brand -->
<a class="navbar-brand mleft" href="#">Reštaurácia</a>

<!-- Links -->
<div class="collapse navbar-collapse mright" id="nav-content">   
<ul class="navbar-nav ml-auto">
<li class="nav-item active ">
<a class="nav-link ilink" href="#">Domov</a>
</li>
<li class="nav-item ">
<a class="nav-link " href="#">Rezervácia</a>
</li>
<li class="nav-item ">
<a class="nav-link " href="#">Menu</a>
</li>
<li class="nav-item ">
<a class="nav-link " href="#">Galéria</a>
</li>
<li class="nav-item ">
<a class="nav-link " href="#">Kontakt</a>
</li>
</ul>
</div>
</nav>
</div>




<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>


</html>

1 个答案:

答案 0 :(得分:1)

取决于宽高比,您可能仍会遇到一些中断,但我的两个最佳猜测是:

background-size: contain;

background-size: cover;

More info on the background-size

快乐的造型!