Struggling to keep up with responsive behavior on a page featuring vertically and horizontally centered content that spans the full height

I'm working on a full-height website and the first section needs to have both vertical and horizontal centered content. The issue I'm facing is that there's an image included, which doesn't scale responsively when the screen size changes. How can I address this problem while still keeping the content centered? Also, the solution should take into consideration the header at the top of the first section.

Fiddle: https://jsfiddle.net/yevr2tLm/1/

HTML

<section class="container-fluid">
  <header>This is a header</header>
  <div class="content__1">
    <img class="img-responsive" src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" />
    <p>
      This is some awesome text
    </p>
  </div>
</section>

CSS

body,
html {
  height: 100%;
}

section {
  position: relative;
  min-height: 100%;
  min-height: 100vh;
}

.content__1 {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

header {
  top: 0;
  position: absolute;
  padding-top: 2em;
}

Answer №1

To achieve this layout, you can utilize Flexbox. By setting the flex-direction: column on the container element, you create two flex-items stacked on top of each other: the header and the content. Next, apply flex: 1 to the content so it fills the remaining height in the window after the header, resulting in a layout like this:

https://i.stack.imgur.com/1Esy9.png

For centering the content both vertically and horizontally within the content__1 element, you can use display: flex on content__1, along with adding align-items: center and justify-content: center. To maintain image responsiveness, consider using properties like width: 60% and height: auto, depending on the image size.

https://i.stack.imgur.com/nRDQI.png

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  padding: 0;
}
.container-fluid.flex,
.content__1 {
  display: flex;
  flex-direction: column;
}
.container-fluid.flex {
  height: 100vh;
}
.content__1 {
  align-items: center;
  justify-content: center;
  flex: 1;
}
header {
  background: black;
  padding: 15px;
  color: white;
  z-index: 1;
}
img {
  width: 60%;
  max-height: 70%;
  height: auto;
}
p {
  margin: 0;
}
<section class="container-fluid flex">
  <header>This is a header</header>
  <div class="content__1">
    <img class="img-responsive" src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" />
    <p>This is some awesome text</p>
  </div>
</section>

You can also achieve a similar result using CSS Tables. Suppose the header has a height: 50px, then you can set the content element's height to be calc(100vh - 50px) with display: table. To center the content inside the table, wrap the content in another div with display: table-cell and vertical-align: middle:

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  padding: 0;
}
.container-fluid.flex {
  height: 100vh;
}
header {
  background: black;
  height: 50px;
  line-height: 50px;
  color: white;
  z-index: 1;
}
content__1 {
  display: table;
  height: calc(100vh - 50px);
  text-align: center;
  width: 80%;
  margin: 0 auto;
}
inner {
  display: table-cell;
  vertical-align: middle;
}
img {
  width: 60%;
  max-height: 70%;
  height: auto;
}
p {
  margin: 0;
}
<section class="container-fluid flex">
  <header>This is a header</header>
  <div class="content__1">
    <div class="inner">
      <img class="img-responsive" src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" />
      <p>This is some awesome text</p>
    </div>
  </div>
</section>

Answer №2

Based on my understanding of your inquiry, here are the steps you can take:

<style>
    body,
    html {
        background-image: url('https://example.com/background.jpg');
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: 80%;
        background-position: center;
        height: 100%;
    }

    section {
        position: relative;
        min-height: 100%;
        min-height: 100vh;
    }

    .content__1 {

    }

    header {
        top: 0;
        position: absolute;
        padding-top: 2em;
    }
</style>

The updated body content should look like this:

<body>
    <section class="container-fluid">
        <header>This is the new header text</header>
        <div class="content__1">
            <p>
                This is some amazing text
            </p>
        </div>
    </section>
</body>

Answer №3

In case you're not a fan of the small image size after resizing, try adding a min-width of 300px or your preferred pixel value to the image.

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

Replace the icon in Material UI Stepper for steps that have errors

I am utilizing Material UI's Stepper component to display a checklist in this manner. The image below is from their documentation. While attempting to add an error state to the checklist, I discovered a prop called error for the StepLabel that allows ...

Aligning the tooltip element vertically

I've created a unique flexbox calendar layout with CSS tooltips that appear on hover. One challenge I'm facing is vertically aligning these tooltips with the corresponding calendar dates effectively. Although I prefer to achieve this alignment u ...

Avoid prolonging lines in React Styled Components

I am encountering an issue with the lines between the OR. I need to ensure that they do not extend beyond their specified boundaries. To view the code on codesandbox, please visit HERE EXPECTED OUTPUT CODE const OR = styled.div` display: flex; flex- ...

I am having an issue in AngularJS/Bootstrap where my cancel button is triggering the submit event. Can

My Angular and Bootstrap form includes a submit button and a cancel button to hide the form. However, I noticed that when I click on the cancel button, it first calls the cancel event handler and then proceeds to call the submit handler. Is this the expe ...

Customizing the primary CSS style of an element without the need to individually reset every property

Is there a way to reset just one property of a selector that has multiple properties stored in main.css, without listing all the properties and setting them to default values? I always struggle with CSS interference and constantly need to reset properties ...

Resolving CSS glitches that mysteriously vanish in Internet Explorer versions 8 and 9

My website was created using WordPress, with various plugins added to enhance its functionality. However, I have encountered an issue when viewing the site in Internet Explorer 8 & 9. The visual effects of the plugins seem to lose all CSS styling in IE bro ...

On-page refresh triggering HTTP 404 error in Vue3 routing situation

Issue Upon reloading a route, such as /installer, in Vue3.js, I encounter the following error: Code Snippet I am utilizing the Router with the setup below: const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes, }); E ...

How to make a Material UI Dialog Box automatically expand to fit the Dialog Content Area

In my project, I am working on a Dialog component which has a maximum width and a minimum height. Inside the DialogContent, I want to have a Box element that stretches to fill out the remaining space of the DialogContent. The goal is to have a background i ...

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

How can I successfully transfer all user information when the edit button is clicked?

A new display page has been developed using php to show all user records in a table. The page includes delete and edit buttons for managing users. While the delete option is working fine, there is an issue with getting the edit button to function correctly ...

Passing data from Bootstrap 4 to a modal on a different server

I am currently using NodeJs and Express for my project. I have a requirement to pass data from a MongoDB database to a remote modal in Bootstrap 4. I understand that the "remote" option was removed in version 4 of Bootstrap, but I need to display different ...

What is the best way to style HTML content with MathJax following its retrieval with jQuery.load?

I recently encountered an issue while using jQuery.load to load a new page. The content on the original page is being treated strangely in some way. Specifically, I have code on the original page that formats LaTeX commands with MathJax: <script type=" ...

The SrollToTop function is ineffective when used with a component in Ionic 6/Angular

Recently, I implemented a fabbutton feature that allows users to scroll to the top of a page with just one click. Initially, I tested this functionality without using it as a component, and everything worked perfectly. However, now I want to turn this fabb ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

Can I receive a PHP echo/response in Ajax without using the post method in Ajax?

Is it feasible to use Ajax for posting a form containing text and images through an HTML form, and receiving the response back via Ajax? Steps 1.) Include the form in HTML with a submit button. 2.) Submit the form to PHP, where it will process and upload ...

Determining the aspect ratio of an image is essential in using flexbox to achieve consistent heights

Recently, I came across a fascinating code pen titled: this demonstration of equal responsive height images I am determined to make sure that all images have the same height, regardless of their width/height variances. The CSS displayed below utilizes fle ...

Using Jquery to add elements one by one

Here is the Code for my project: Displayed below is the content of my CSV file: Item, Quantity, Price; LED, 100, $10; PIR, 1, $5; DS18B20, 10, $5; This segment showcases the jquery file I've composed: $(document).ready(function() { $.ajax({ ...

Learn how to effectively share an image using the Math.random function

Is there a way to display a random number of images on a webpage using JavaScript? Let's say we want to show X number of images, where X is a randomly generated number. For the sake of this example, let's set X to be 10. <input class="randomb ...

Generate div elements corresponding to individual objects

Previously, I inquired about a similar issue but have not come across any solutions. I made updates to my code, which is now functioning well, however, I am facing a new dilemma. Here is the PHP code I am currently working with: class individual { pu ...

Here's a solution to prevent the "jump" effect when hiding elements in a navbar

Currently, I am facing an issue with my h5 elements within the navbar when scrolling. I am using Bootstrap 4 and jQuery for this project. The problem arises in my navbar where there are four sets containing an icon, followed by an "h5" and then a "p" elem ...