How to prevent background image from shifting upwards when soft keyboard appears on mobile and tablet?

I’m encountering an issue in my Angular application where, upon clicking a textarea with id “message,” the soft keyboard appears as expected on mobile and tablet devices. However, when the soft keyboard appears, my background image is pushed upwards, causing an undesirable shift in the layout. How can I ensure that the background image stays in position while the soft keyboard appears on top of it?

HTML:

<main class="start-a-conversation-container with-background-start-a-conversation">
  <section class="start-a-conversation-text">
    <article class="start-a-conversation">
      <p class="text-effect">
      <h2 class="sub-header-start-a-conversation centered-text-start-a-conversation">
        Start a conversation</h2>
    </article>

    <article class="start-a-conversation-form-container">
      <section class="contact-form">
        <p class="intro-text">
          Open to collaborations and connections for exciting ventures. <br>
          Let's build something meaningful together – reach out today!</p>

        <form (ngSubmit)="submitForm()" #contactForm="ngForm">
          <div class="form-group">
            <label for="name">Name</label>
            <input type="text" id="name" name="name" autocomplete="name"
                   [(ngModel)]="formData.name" required
                   (focus)="onFocus('name')"
                   (blur)="onBlur('name')">
          </div>

          <div class="form-group">
            <label for="email">Email</label>
            <input type="email" id="email" name="email" autocomplete="email"
                   [(ngModel)]="formData.email" required
                   (focus)="onFocus('email')"
                   (blur)="onBlur('email')">
          </div>

          <div class="form-group-message">
            <label for="message">Message</label>
            <textarea id="message" name="message"
                      [(ngModel)]="formData.message" required
                      (focus)="onFocus('message')"
                      (blur)="onBlur('message')"
            ></textarea>
          </div>

          <button class="submit-button" type="submit" [disabled]="contactForm.invalid">Send Message</button>

          <div *ngIf="successMessage" class="success-message">
            <p>{{successMessage}}</p>
          </div>

          <div *ngIf="failureMessage" class="failure-message">
            <p>{{failureMessage}}</p>
          </div>

        </form>
      </section>
    </article>
  </section>
</main>

CSS:

.start-a-conversation-container.with-background-start-a-conversation {
  background: var(--black) url("src/assets/img/background-start-a-conversation.png") center center/cover no-repeat;
  height: 100vh;
  width: 100%;
  padding: 2rem;
  box-sizing: border-box;
  position: relative;
}

.with-background-start-a-conversation::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  background: var(--black) url("src/assets/img/background-start-a-conversation.png") center center/cover no-repeat;
}

I attempted to address the issue by implementing various CSS approaches. Initially, I set the background image using the background property with position: fixed. This was supposed to keep the background fixed while allowing the soft keyboard to appear on top of it. However, when the soft keyboard appeared, the background image still shifted upwards.

To rectify this, I then tried setting the background image as a pseudo-element (::before) with position: fixed. This approach was expected to prevent the background from shifting when the soft keyboard appeared. Nevertheless, the issue persisted, and the background image continued to move upwards.

Subsequently, I implemented a TypeScript function to adjust the background size dynamically based on the content height. This function was triggered on resize events and initially on component initialization. Despite these efforts, the background image still shifted when the soft keyboard appeared on mobile and tablet devices.

Overall, none of these attempts successfully resolved the issue, and the background image continued to move upwards when the soft keyboard appeared.

New contributor

Zoë is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật