Placing an image centrally between the header and footer using CSS

My goal is to create a straightforward landing page that includes a header, footer, and an image perfectly centered between the two (both horizontally and vertically).

The space above and below the image should be equal and adjust based on the height of the browser window.

The image will have a set width (900px) and height (600px).

Note: The footer will be sticky.

I attempted the following CSS:

{display:block; padding:0 40px; width:900px; margin:0 auto; vertical-align:middle;}

This is how I structured my HTML:

<div class="fbg">
    <div class="fbg_resize">
        <img src="images/image.png" width="900" alt="" />
    </div>
</div>

To center the image horizontally, I used this CSS:

.fbg_resize { margin:0 auto; padding:0 40px; width:900px;}

Here is the important code snippet:

http://jsfiddle.net/SFWBL/

Answer №1

Take a look at this JSFiddle to see the basic concept, it should help you kickstart your project.

Here is the HTML code:

<div id='header'></div>
<div id='image'></div>
<div id='footer'></div>

And here is the CSS code:

html, body{
    text-align:center;
    height:100%;
    width:100%;
    margin:0;
    padding:0;
}
#header, #footer{
    height:50px;    
    width:100%;
}
#image{
    height:50px;
    width:50px;
    margin:-25px auto 0 -25px;
    background:grey;
    position:absolute;
    top:50%;
    left:50%;
}
#header{
    background:blue;
}
#footer{
    position:absolute;
    bottom:0px;
    background:red;
}

Answer №2

Instead of relying on an img tag, consider utilizing the background-image property for the styling of the div element.

html, body {
    height: 100%;
}

.fbg {
    background-image: url(http://lorempixel.com/900/600);
    background-position: center;
    background-repeat: no-repeat;
    width: 100%;
    height: 100%;
}

To view the updated version, visit this JSFiddle link.

Answer №3

Relative positioning allows your elements to adapt to the size of the browser window using percentages. By adjusting the top and left edges, you can utilize negative margins to reposition items accordingly. For example, if your image has a fixed height of 600px, you would use -300px to move it up by half its height. This style could look like:

position: absolute;
top: 50%;
margin-top: -300px;

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

Does the <cite> tag go inside a link in HTML?

Whenever I include a citation in HTML using the <cite> tag, I typically place the link around the citation itself. For example: <a href="http://example.com/"><cite>Example Citation></cite></a> This method makes sense to m ...

Compatible with pure vanilla JavaScript, but not jQuery

I am currently facing an issue with attaching VKI (Virtual Keyboard Interface) to an element that is dynamically added to the DOM using JavaScript. The scenario involves a table with initially only one row, along with "Add Row" and "Delete Row" functionali ...

CSS: What could be causing the element on the right to not be selected?

http://codepen.io/oscholz/pen/qNYPVL I've been attempting to target only the "Random unattached paragraph." Despite several attempts using different methods, I haven't had any success in achieving this (see code snippet below). ...

Having difficulty retrieving/binding the correct Entity Id in JQuery, consistently retrieves the first item Id from the iteration

Within my PartialView, I am looping through a Collection of Entities and displaying them in an Html Table. In one column of this table, there is an element with a font awesome class (fa fa-times-circle). I am setting the "id" property of this to the "I ...

How can you ensure your CSS code is functional across various browsers?

I have been exploring CSS and HTML for about a week now, but I am facing a challenge that has me stuck for the past thirty minutes. The issue lies in adapting this code to work smoothly across different browsers. While it aligns images in a row of four per ...

How can you modify an existing animation style upon hovering?

Take a look at the code snippet below. As you hover over an item, you'll notice the background color changes, but the transform: translateY(-5px) property is not being applied. The transform effect on hover only works when the .list-item animation is ...

Eliminate the dark backdrop from html5 videos that only shows up for a brief moment

Is there a way to remove the brief black background that appears when loading an HTML5 video? I have tried using CSS without success. The HTML: <div id="start-screen"> <video id="video-element"> <source src="video-mp4.mp4" type="vide ...

Injecting SVG styling into HTML using the content tag and CSS

I am working with 3 different files: index.html <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <i class="logo myRed" aria-hidden="true"></i> ...

When the checkbox is unchecked

Is it possible to remove the Checkbox element from the array once it is unchecked? { columns.columnNames && columns.columnNames.map(el => { return ( <div> <input type="c ...

Issues with Django and Bootstrap Dropdown Lists Functionality

I've been working on my Django web application and I'm facing an issue with getting the dropdown list to actually drop down when clicked. <div class="panel panel-default"> <div class="panel-body"> <div class="dropdo ...

Incorporating Button Value from Anchor Tag Title Upon Page Load

Currently, I am working on a real estate project and facing an issue with a contact modal box. My goal is to extract the title from tag "a" and place it as the button value in the modal box. English isn't my strong suit, so please excuse any errors i ...

Display PHP output within a styled CSS box

<html> <head> </head> <style type="text/css"> .yellow-box { position:absolute; top:100px; left:500px; width:300px; height:300px; background:yellow } </style> <div class = "yellow-box" > </div ...

jQuery's promise method in CSS is executed successfully

Seeking advice from someone: Scenario: Currently, I have <div id="breadCrumb" style="width:80%"></div> Now, I want to update the width to 100% and retrieve the width in pixels on click $('#menu_toggle').on('click', funct ...

Is there a way to integrate the distanceTo function from Leaflet into my own custom library that I am planning to develop?

I am currently incorporating leaflet into my project and I have encountered the distanceTo method which calculates the distance between two coordinates. My goal is to create a separate JS file with a function named getDistance() where I can house the logic ...

Loading items into the select menu

In my database, I have a table with 3 columns: ID, TITLE, TEXT. These will be used to populate select options in a form. When a user makes a selection, a script will take the title and text, make some adjustments, and store it in a different table. My que ...

Is it possible to automatically adjust the size of components on a html/cshtml page based on the current window size?

Currently, I am facing an issue with my website where multiple panels are created using a foreach loop that includes textareas/textboxes. Whenever I resize my browser window, it messes up the UI by shifting the position of the textareas and other elements. ...

Struggling to align materialUI input or textfield horizontally

import {Input} from 'material-ui'; import Select from 'react-select'; I've been trying different ways to adjust the margins and padding, even wrapping them in divs, but I can't seem to get Material UI textfield or input alig ...

The foundation grid system is experiencing difficulties when implemented on an Angular form

After successfully installing Foundation 6 on my Angular project, I am facing an issue with the grid system not working properly. Despite numerous attempts to troubleshoot and debug, I have not been able to resolve this issue. If anyone has any insights or ...

Seeking a template for a server-side programmer who lacks proficiency in CSS and design?

Hey there all you wise folks! So here's the deal - I'm a PHP/JavaScript wizard when it comes to logic and server-side stuff, but I really suck at design, CSS, and all things arty. Does anyone have any recommendations for a template or tool that ...

What is the most dependable method for extracting text from div elements in a list using Selenium and Python, leveraging sibling relationships?

I'm on a mission to find and read the list of text 1-5. All divs with class="col-md-6" follow the same structure, so I'm aiming to extract the text from: <h5>Header Unique Text</h5> This particular element is unique, and my goal is ...