html - 调整窗口大小时如何修复图像?

标签 html css

当我调整窗口大小时,图像移出位置,因此与另一个 div 重叠。我该怎么做才能将图像设置到位?

这是我的网站在窗口全尺寸显示时的样子 1

然后当我调整屏幕大小时,图像现在已经与 div 重叠了。 2

我已经设法使标题 div 具有响应性,但不确定如何解决图像问题。

这是我的index.js文件

import React, { Component } from 'react';

import openLips from '../ComingSoon/img/openLips.png';

import '../ComingSoon/styles.scss';

class ComingSoon extends Component {
  render() {
    return (
      <div className="comingSoon">
        <div className="openLips">
          <img src={openLips} alt="Open lips"/>
        </div>
        <div className="comingSoonBorder">
          <p className="comingSoonText">Stay tuned<br/>
          Something is on the way</p>
        </div>
      </div>
    );
  }
}

export default ComingSoon;

这是我的styles.scss 文件

@font-face {
    font-family: 'ObelixPro';
    src: url('../../fonts/ObelixPro/ObelixPro.ttf') format('truetype'),
         url('../../fonts/ObelixPro/ObelixPro.eot') format('embedded-opentype'),
         url('../../fonts/ObelixPro/ObelixPro.woff2') format('woff2'),
         url('../../fonts/ObelixPro/ObelixPro.woff') format('woff'),
         url('../../fonts/ObelixPro/ObelixPro.svg#ObelixPro') format('svg');
}

body {
    background-color: #E4C938;
}

.comingSoon {
    margin: auto;
    top: 0px;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 100%;
    height: auto;
    position: absolute;
}

.openLips {
    z-index: 2;
    position: absolute;
    top: 5%;
    left: 61%;
}

.comingSoonBorder {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 80vh;
    border: 7px solid black;
    background-color: white;
}

.comingSoonText {
    text-align: center;
    font-family: 'ObelixPro';
    font-size: 7vh;
}

最佳答案

将舌头移入 .comingSoonBorder 并将其放置在相对于该容器的位置。现在,只要页面宽度/高度发生变化,它就会随之移动。另外使用 vh 设置它的宽度/高度,所以当 .comingSoonBorder 元素的宽度改变时它们也会改变。

const openLips = 'https://i.ibb.co/SK4pdjz/tongue-clipart-animated-4-transparent.png';

class ComingSoon extends React.Component {
  render() {
    return (
      <div className="comingSoon">
        <div className="comingSoonBorder">
          <img className="openLips" src={openLips} alt="Open lips"/>
          <p className="comingSoonText">Stay tuned<br/>
          Something is on the way</p>
        </div>
      </div>
    );
  }
}

ReactDOM.render(
  <ComingSoon />,
  demo
)
body {
  background-color: #E4C938;
}

.comingSoon {
  margin: auto;
  top: 0px;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 100%;
  height: auto;
  position: absolute;
}

.openLips {
  position: absolute;
  z-index: 2;
  right: 0;
  transform: translate(70%, -70%);
  width: 40vh;
  height: 40vh;
  background-size: cover;
}

.comingSoonBorder {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 80vh;
  border: 7px solid black;
  background-color: white;
}

.comingSoonText {
  text-align: center;
  font-family: 'ObelixPro';
  font-size: 7vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="demo">
  </demo>

关于html - 调整窗口大小时如何修复图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927175/

相关文章:

css - 文本的 HTML 流体多列布局(报纸)

javascript - 如何在网络浏览器上忽略 Control+C(复制)

html - 在 Opera 中滚动时固定位置元素闪烁

html - :hover but :not on a specific class

html - CSS 内置内容不工作

css - 在 Windows 中基于 urls 获取 less 文件的 css 文件的最简单方法

css - 鼠标悬停 2 种颜色与 css

html - 缺少像素 css 固定 div 包装器

javascript - 响应式菜单在窗口大小调整时消失

javascript - 如何仅当 glymphicon 图标具有键盘焦点时才应用 CSS 样式?