简体   繁体   English

如何在CSS中按比例缩放标题图像?

[英]How to style a proportionally scaling header image in CSS?

I have a header with image in the following HTML page: 我在以下HTML页面中具有带有图片的标题:

<div class="wrapperHeader">
    <div class="header">
        <img src="header.png">
    </div>
    <!-- header ends here -->
    <div class="menu-wrap">
        <nav class="menu">
            ...
        </nav>
    </div>
    <!-- menu-wrap ends here-->
</div>
<!-- wrapper Header ends here-->

The relevant CSS is as follows: 相关的CSS如下:

.menu-wrap {
    width: 70%;
    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
    background: #808080;
    margin: 0 auto;
}
.menu {
    width: 700px;
    margin: 0px auto;
}
.wrapperHeader {
    margin-left: auto;
    margin-right: auto;
    display: block;
}
.header {
    width: 1210px;
    height: 192px;
    margin: 0 auto;
}
.header img {
    position: absolute;
    max-width: 100%;
    max-height: 100%;
    margin: 0 auto;
}

When I zoom in and out the image in the header scales (up or down) faster than the menu, so I loose the proportions of the page. 当我放大和缩小页眉中的图像比例(向上或向下)时,其速度比菜单要快,因此我松开了页面的比例。 I would like some scaling similar to what you can see in http://www.studio-efekt.com.pl/ Any ideas how to solve this? 我想要一些类似于您在http://www.studio-efekt.com.pl/中可以看到的缩放比例。

This works for you. 这为您工作。 The Image is placed into the empty block. 图像放置在空白块中。

 .menu-wrap { width: 70%; box-shadow:0px 1px 3px rgba(0,0,0,0.2); background:#808080; margin: 0 auto; } .menu { width:700px; margin:0px auto; } .wrapperHeader { margin-left:auto; margin-right:auto; display:block; } .header { width:70%; height: 192px; margin: 0 auto; border: 1px solid black; } .header img { position: absolute; max-width:100%; max-height:100%; margin: 0 auto; } 
 <div class="wrapperHeader"> <div class="header"> <img src="header.png"> </div><!-- header ends here --> <div class="menu-wrap"> <nav class="menu"> ... </nav> </div> <!-- menu-wrap ends here--> </div> <!-- wrapper Header ends here--> 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM