html - 自动调整 CSS 中内嵌图像的图像大小

标签 html css

我希望我的内嵌图像能够根据浏览器大小自动调整大小,同时它也应该与标题内嵌。我已经尝试过给出的解决方案 here但它对我不起作用。

这是我的代码:

#header_title {
  font-weight: bolder;
  text-align: center;
}

#header_photo {
  display: none;
}

.floatingimage {
  float: right;
  max-width: 100%;
  height: auto;
}

@media all and (max-width: 1024px) {
  #header_title {
    margin-left: 10%;
    min-width: 67%;
    max-width: 77%;
    display: inline-block;
    vertical-align: top;
    position: relative;
  }
  #header_photo {
    display: inline-block;
    vertical-align: top;
    max-width: 20%;
    height: auto;
  }
}
<header>
  <div id="header_title">
    <h1>Title h1</h1>
    <h3>Title h3</h3>
  </div>
  <aside id="header_photo">
    <img class="floatingimage" src="http://www.planwallpaper.com/static/images/Awesome-Nature-Background.jpg" width="100px" height="100px" />
  </aside>
</header>

您可以在 jsfiddle here 上找到此代码.

注意:我使用的是 Firefox 53.0.3(32 位)

最佳答案

我稍微修改了您的 HTML 和 CSS。我添加了与宽度相关的柔性显示和图像高度。

HTML 代码:

<header>
    <div id="header_title">
            <h1>Title h1</h1>
            <h3>Title h3</h3>
    </div>
            <aside id="header_photo">
                <img class="floatingimage" src="http://www.planwallpaper.com/static/images/Awesome-Nature-Background.jpg"/>
            </aside>

CSS 代码:

header {
  display: flex;
}

#header_title
{
  font-weight: bolder;
  text-align: center;
}

#header_photo
{
  display: none;
  max-width: 100%;
  height: auto;
  align-self: center;
}

#header_photo img {
  width: 100%;
  height: auto;
  max-height: 120px;
}

.floatingimage
{
  float: right;
}

@media all and (max-width: 1024px)
{
 #header_title
 {
    margin-left: 10%;
    min-width: 67%;
    max-width: 77%;
    display: inline-block;
    vertical-align: top;
    position: relative;
 }

  #header_photo
  {
    display: inline-block;
    vertical-align: top;
    max-width: 20%;
    height: auto;
  }
 }

你用 fiddle 检查一下 here

关于html - 自动调整 CSS 中内嵌图像的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484347/

相关文章:

html - CSS:底部居中引导分页元素

javascript - 如何在 anchor 标记中指定数据 URI 资源的文件名?

javascript - 导航栏标题和下拉菜单之间的线

javascript - 选择全部和一个时切换的 div 消失

jquery - 使用 jQuery、HTML 和 CSS 制作我的下拉列表 'drop up'

javascript - 滚动菜单事件类 - 如何在事件类上添加字体颜色和字体大小更改?

html - 如何使用CSS中的绝对位置将子div定位到每个父div的中心

html - 为什么这些 block 没有在同一高度对齐?

javascript - 在 localstorage 或 html 中搜索框搜索

html - 如何将文本放在交叉渐变 slider 上?