Home Web Front-end CSS Tutorial How Can I Create a Centered Single-Column Layout with Header and Footer in Web Design?

How Can I Create a Centered Single-Column Layout with Header and Footer in Web Design?

Dec 23, 2024 am 02:19 AM

How Can I Create a Centered Single-Column Layout with Header and Footer in Web Design?

Single-Column Centralized Fixed-Width Design with Header and Footer

In the realm of web design, achieving a single-column layout with centered content that spans the entire screen while accommodating header and footer elements can be a pivotal undertaking. This question delves into several approaches and their suitability for this design requirement.

Approach 1: Three-Column with Faux Center

Creating three columns, with margins adjusted to center the central column and a faux center element for height expansion, has been suggested. However, it introduces unnecessary empty side columns, negating its practicality.

Approach 2: Single Column with Auto Margins and Top Offset

Positioning a single column with auto margins for centering and a top offset can achieve the desired effect. However, extending the column to occupy the remaining height requires further exploration.

Solution: Flexbox

For contemporary browsers, the advent of Flexbox offers a straightforward solution. Flexbox provides comprehensive layout control, enabling the creation of single-column designs with ease.

Using JavaScript is not advised. Instead, we recommend the following CSS code:

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
  width: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

#main {
  flex-grow: 1;
}

/* optional */
header {
  min-height: 50px;
  background: green;
}

#main {
  background: red;
}

footer {
  min-height: 50px;
  background: blue;
}
Copy after login
<header>header</header>
<div>
Copy after login

This solution utilizes Flexbox to create a single-column layout, with the #main element occupying the remaining height (minus header and footer). The optional header and footer styles can be customized as needed.

The above is the detailed content of How Can I Create a Centered Single-Column Layout with Header and Footer in Web Design?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

Create a JavaScript Contact Form With the Smart Forms Framework

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

Demystifying Screen Readers: Accessible Forms & Best Practices

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Create an Inline Text Editor With the contentEditable Attribute

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

Working With GraphQL Caching

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

Making Your First Custom Svelte Transition

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

File Upload With Multer in Node.js and Express

See all articles