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

Picture is not displaying properly post-rotation

Is there a way to rotate an image by 90 degrees and display it in a Card Component without it covering the text? Currently, I am utilizing Element.io, but encountering issues with the rotated image overlapping the text. The image currently has the followi ...

The Element should take up the entire page with the exception of a 20px margin

I am struggling to create an element that covers the entire page except for a 20px margin on all sides. I have tried implementing a solution using this code, which works in webkit browsers and Firefox but seems to have issues with Internet Explorer (10) an ...

"Repetitive" elements arranged horizontally

My goal is to create a looped row of elements, similar to this design: This row should function like a carousel where clicking the "Next" button changes the current element and positions it in the center of the row. I envision this row as being looped, wi ...

The CSS focus-within and hover properties are not functioning as expected

How can I make the search tags visible (the buttons above the text input field) when the search bar below them is clicked? I've tried using :hover, :focus, and :focus-within, but none of them seem to be working. Can someone help me figure out the issu ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

Enhancing the readability of modals with Angular Dialog Service

I have been utilizing the Angular Dialog Service to create popup forms on my website. The source code for this service can be accessed here: https://github.com/m-e-conroy/angular-dialog-service/blob/master/README.md However, I am experiencing an issue wit ...

CSS Switchable Style Feature

I am in need of some assistance with the navigation on my website. You can find the current code at this link: http://jsfiddle.net/Sharon_J/cf2bm0vs/ Currently, when you click on the 'Plus' sign, the submenus under that category are displayed bu ...

Chrome is where all the action happens, while Firefox prefers to keep things on the left

After a long break, I returned to designing my website and re-learning CSS. My approach involved creating a WordPress-integrated site with minimal HTML/PHP work, focusing more on utilizing the available CSS classes and IDs provided by a template theme (Bla ...

Styling the tab view in PrimeFaces

I am currently working with the tab view feature in primefaces and I have encountered a couple of issues. 1) When using Internet Explorer, the tabs are displayed vertically instead of horizontally. However, it works fine in Firefox. FireFox : Internet E ...

Elements missing from the Bootstrap 5 framework

I've been attempting to link my bootstrap card deck to a mongo database with ejs. The website renders without any errors, but for some reason, the cards are not displaying. Does anyone have any insight into what could be causing this issue? Below is ...

"Learn how to easily format specific characters in JavaScript using the text plugin to create bold text strings with a unique

I would like to transform this text into something like "|| something 1 || something 2 || more || last one ||" and then make all of that string bold by adding "|" around it then it would look like this "|| something 1 || something 2 || more || last one | ...

Tips for aligning a dropdown button with the other elements in your navbar

I followed the code outline from a tutorial on We3schools, but I'm having an issue with the button not aligning correctly with the navbar. I attempted to adjust the code for proper alignment before publishing, but was unsuccessful. Here is the GitHub ...

Difficulty in accurately identifying the current page on the appbar

I'm attempting to make the active page in my navbar stand out by giving it a white background color and skyblue text. However, for some reason, I can't get the text color to display as skyblue in the appbar. You can see how it currently looks in ...

Restrict the width of CSS columns to the value of an assigned variable

I'm struggling with centering a one-row table within a div. Specifically, I'm having trouble adjusting the size of two columns to match the length of the content inside them (team name and round). The HTML code is: <div id="greeting"> ...

What's causing ng-show to malfunction in IE11 on AngularJS?

I am experiencing a strange issue with my code - ng-show works perfectly fine on Firefox, but not on IE 11. <div ng-show="isExist" class="panel panel-default"> Here is the relevant code snippet from the controller: $scope.isExist = false; if(user ...

How to implement CSS styling for a disabled component

In my application, I have a FormControlLabel and Switch component. When the Switch is disabled, the label in FormControlLabel and the button in Switch turn gray. However, I want to maintain the color of both the label (black) and the button (red). To test ...

Arrangement: Div beside vertically-aligned hyperlinks

Example code: p { color: red; } span { font-weight: bold; } <p>This is a paragraph.</p> <p>Another paragraph here.</p> <span>Bold text</span> This is the desired outcome: https://example.com/image.png It&ap ...

Ways to make text within a container smoothly slide down

Hello, I have a question about jQuery as I am relatively new to it. I am currently working on creating a team members section where clicking on a team member will slide down information about that person underneath. I have managed to set up the parent co ...

What could be the reason for the lack of responsiveness in my input box?

http://jsfiddle.net/4ws3L6kn/1/ Can anyone help me figure out why this isn't responsive? What mistake did I make? <div id="DIV_1"> <div id="DIV_2"> <div id="DIV_3"> <button id="BUTTON_4"> ...

Is the CSS :not selector failing to operate as expected?

I have been trying to use the :not selector in my code, but for some reason it's not working. I can't figure out why this is happening, especially since the ul element does have the webmedia-gallery class. Any thoughts on what could be causing th ...