Creating a dynamic and flexible div using CSS

I am currently working on a small webpage and encountering an issue with responsiveness when scaling the browser window in and out. The design looks perfect at 320x568 resolution, but when I scale it down, a black box (`.transbox`) overlaps the image, which is not desired.

https://i.sstatic.net/nZe5u.png

Here is how it looks after scaling down:

https://i.sstatic.net/o2oCr.png

Instead of using CSS media queries, I want to achieve the responsive design using only CSS. Below are my current HTML and CSS snippets:

.masthead {
  height: 20vh;
  min-height: 370px;
  background-image: url("http://lorempixel.com/1920/1080");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

div.transbox {
  margin: 30px;
  background-color: rgba(0, 0, 0, 0.6);
  min-height: 300px;
}

div.transbox p {
  font-weight: bold;
  color: white;
}

.transbox h1 {
  color: white;
}
<header className="masthead">
  <div className="container">
    <div className="row align-items-center">
      <div className="col-12 text-center">
        <div class="transbox">
          <h1 className="font-weight-light">
            All your Escort needs within a single place!
          </h1>
          <p className="lead">
            Find the best set of escort services in Sri Lanka
          </p>
        </div>
      </div>
    </div>
  </div>
</header>

Is there a way, using either Bootstrap classes or pure CSS, to prevent the black box from overlapping the photo when scaling the browser window?

Answer №1

Check out this demonstration:

body {
margin: 0;
padding: 0;
}
.masthead {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background-image: url("http://lorempixel.com/1920/1080");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
}

div.transbox {
  margin: 30px;
  background-color: rgba(0, 0, 0, 0.6);
  min-height: 300px;
}

div.transbox p {
  font-weight: bold;
  color: white;
}

.transbox h1 {
  color: white;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<header class="masthead">
  <div class="container">
    <div class="row align-items-center">
      <div class="col-12 text-center">
        <div class="transbox">
          <h1 class="font-weight-light">
            All your Escort needs within a single place!
          </h1>
          <p class="lead">
            Find the best set of escort services in Sri Lanka
          </p>
        </div>
      </div>
    </div>
  </div>
</header>

Answer №2

.top-section {
  height: 20vh;
  min-height: 400px;
  background-image: url("http://lorempixel.com/1920/1080");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

div.overlay {
  margin: 30px;
  background-color: rgba(0, 0, 0, 0.6);
  min-height: 300px;
}

div.overlay p {
  font-weight: bold;
  color: white;
}

.overlay h1 {
  color: white;
}
<section className="top-section">
  <div className="container">
    <div className="row align-items-center">
      <div className="col-12 text-center">
        <div class="overlay">
          <h1 className="font-weight-light">
            Find all your escort services in one place!
          </h1>
          <p className="lead">
            Explore the top escort services in Sri Lanka
          </p>
        </div>
      </div>
    </div>
  </div>
</section>

Your content exceeds the width of the background image. To fix this, either increase the height of your background image or decrease the font size using a media query.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Shutting down the jQuery pop-up

I am struggling with trying to display this code as a popup on my website. Here is the code I have: <div id="myDialog" title="myTitle"> <div class="table_cell"> <div class="message"></div> </div> <div class="tabl ...

Customizing your Sharepoint Web part with unique text properties

When a user inputs custom text in a web part property, it will be shown on a form. Here is the code for the web part property located in the testTextWebPart.ascx.cs file: public partial class testTextWebPart: WebPart { private string _customtxt; [We ...

What is the process for updating the CSS style of every other piece of data (such as an image) retrieved

Is it possible to utilize PHP code when retrieving data from a MySQL database in order to have every other record display with unique CSS formatting? For instance, I am incorporating images similar to this in my comment system: http://prntscr.com/3hpiep ...

Exploring the contrast between window and document within jQuery

I'm curious about the distinction between document and window in jQuery. These two are commonly utilized, but I haven't quite grasped their differences. ...

PHP variable inaccessible

When using JQuery Ajax to call sessions variables from a PHP steamauth file, I encountered an issue. Even though the code works fine when "$data['test'] = "ok";" is used instead of "$data['test'] = $_SESSION['steamid'];", ther ...

Troubleshooting the Issue with WordPress Image Customization Settings

I'm in the process of designing a Wordpress theme, but I've hit a roadblock. I'm utilizing the theme customize page to upload both a main logo and a mobile logo, which should then be displayed on the page. Initially, it was working for about ...

Tips for implementing fluid transitions between mouse X and Y coordinates using JavaScript

I recently developed a function that enables a DOM element to follow the mouse cursor. You can check out the code here. Currently, I am looking for suggestions on how to add a nice animation to this feature. Ideally, I want to incorporate a slight delay w ...

What might be causing my animation to not run as expected?

I recently tried to create a bouncing arrow following a tutorial, but the code I used is almost identical with minor differences. However, when I try to incorporate it into my hero unit, the animation does not work as expected. The issue could be related ...

arranging a collection of images based on their values in increasing order

cardShop is a container with various images, each with its own unique ID and value attribute. These values consist of two numbers separated by a comma, such as 2000,500 or 1500,200. My goal is to sort these images in ascending order based on the first numb ...

Is it possible to activate multiple animations simultaneously within a single div using UIKit?

I recently started developing a to-do web application and was looking to enhance its appearance with some animations using UIKit. Currently, I have an animation that toggles when the todo-items are brought into view: <div class="todo-items uk-anim ...

Is there a way to use JavaScript to import several custom fonts at once?

Is there a way to import a long list of custom fonts as icons using Javascript? I have been obtaining the font list from an API, but importing them one by one with pure CSS using @font-face is time-consuming and difficult to maintain. @font-face { fon ...

What are some ways to eliminate the excess white space beneath the footer in HTML?

After creating a responsive webpage that works flawlessly on mobile devices and tablets, I noticed a problem with the footer when viewed on desktop. There is an empty white space at the end of the webpage, and upon inspecting the browser, it focuses on the ...

Is there a way to incorporate my personalized styling and HTML code into Django forms?

I've been struggling to customize form styling in Django. I want to exclusively use my own styles for the forms. How can I achieve this? <form id="contact" action="" method="post"> <div class="row"> ...

Utilizing tag keys for inserting text and adjusting text sizes within a Web application

Creating an online editing interface for coursework where keyboard events are used. The goal is to have the tab key insert text, while also reducing the size of the text on that line. However, upon using getElementById, an error message pops up stating: ...

What is the approach for incorporating JavaScript variables in Jade templates for writing inline CSS styles?

Struggling to inject CSS code dynamically? You're not alone. style(type='text/css') #header a#logo { background:url(constants.logo) no-repeat; } @media only screen and (-webkit-min-device-pixel-ratio: 1.5) { #header a#logo { ...

Shrink image sizes using PHP

Currently I am trying to find a way to decrease the sizes of images using PHP when the page loads. Despite obtaining the dimensions, I am uncertain about how to proceed with reducing their sizes in PHP. Below is my existing code: <?php $stmt = $db-> ...

Images are not appearing in the Bootstrap 4 carousel when dynamically populated with Angular 4

My current challenge involves dynamically populating a Bootstrap 4 carousel using an ngFor loop that iterates over an array of strings containing image URLs. However, despite the generated markup looking correct, the images are not displayed in the carouse ...

Arranging an Array by Two Distinct Characteristics

I currently have an array that is grouped and sorted based on the "Program" attribute, which is working well. However, I now need to sort by a different attribute (Deliverable) within each group. Is this possible? If so, how can I achieve it? Below is an ...

Issue with Bootstrap Carousel Interval Setting not Functioning as Expected

I recently added Twitter Bootstrap-Carousel to a website with the intention of using it to navigate through different sections of a module. However, I'm encountering an issue where setting the interval to false does not work as expected. When I set an ...

The true height is never fully accurate when a vertical scrollbar is visible

Having an issue that needs solving. I've created a simple layout, but I'm struggling with getting the sidebar and wrapper to adjust their height properly. This is how the layout looks: <div class="sidebar collapse"> <div class="sideb ...