How to Create a Fixed-Width Layout with CSS
A "fixed-width" layout is one in which the layout of the page is contained within a wrapper that doesn't adjust its size when the width of the browser changes.
In this how to, you'll learn how to create a 2-column fixed-width layout.
- Start with the following simple web page containing four content areas: header, footer, menu, and content. <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Two-column fixed layout</title> </head> <body> <div id="wrapper"> <div id="header"> <h1>Two-column fixed layout</h1> </div> <div id="main"> <div id="menu"> <h2>Menu</h2> <p>This column is fixed.</p> <p>This column is fixed.</p> <p>This column is fixed.</p> <p>This column is fixed.</p> <p>This column is fixed.</p> </div> <div id="content"> <h2>Content</h2> <p>This column is fixed.</p> </div> <div class="clearer"></div> </div> <div id="footer">footer</div> </div> </body> </html>
- Create a file for an external styesheet and link to it from the HTML using the following tag: <link href="fixed-two-column.css" rel="stylesheet">
- Inside the stylesheet, start by resetting margins, padding, and borders: * { margin:0; padding:0; border:0; }
- Next, add a border to the wrapper div: #wrapper { border: 1px solid #000; }
- Add a bottom border to the header and give it some padding and a background: #header { border-bottom: 1px solid #000; padding: 10px; background-color: #eee; }
- Float the menu nav left so that the content article will come up to its right edge. We'll also add some other styles to make it more readable and give it a width of 180px and padding of 10px, which gives it a total width of 200px: 180px + (2 x 10px): #menu { width: 180px; float: left; padding: 10px; border-right: 1px solid #000; }
- Set the left margin of the content div to the total width of the menu div. We'll also add some other styles to make it look nicer: #content { margin-left: 200px; border-left: 1px solid #000; padding: 10px; line-height: 2em; }
- Use a the clearer div to force the main section to extend its height when the content div expands. .clearer { clear: both; }
- At this point, you have a fluid-width layout. To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser. #wrapper { border: 1px solid #000; width: 950px; margin: auto; }


Related Articles
- Learn the Very Basics of CSS in One Minute
- How to Create a CSS External Style Sheet
- How to Align Text with CSS
- How to Create a Horizontal Navigation Menu with CSS
- How to Create a Fixed-Width Layout with CSS (this article)
- How to Remove Spacing Between Table Borders with CSS
- How to Set a Background Image with CSS
- How to Set Text Spacing and Placement in CSS
- How to Style a Table with CSS
- How to Create Boxes with Rounded Corners in CSS
- How to Create a Vertical Navigation Menu with CSS
- How to Use the CSS Opacity Property
- How to Use Multiple Background Images with CSS
- Absolute Positioning with CSS
- How to Use the CSS Border Shorthand Property
- How to Create CSS Button Links
- How to Create a Fluid-Width Layout with CSS
- How to Set Text and Background Color with CSS
- How to Create a CSS Embedded Style Sheet
- How to Add Inline Styles to CSS
- How to Create a Border with CSS
- How to Use the CSS Padding Shorthand Property
- How to Create a Fly-Out Menu with CSS
- How to Use CSS Media Queries in Responsive Design
- How to Adjust Margins with CSS
- How to Use the CSS Background Shorthand Property
- How to Create a Form without Tables Using CSS
- How to Modify Fonts in CSS
- How to Create a Drop-Down Menu with CSS
- How to Apply Padding with CSS
- Fixed Positioning with CSS
- How to Use CSS Transitions
- How to Use the CSS list-style Shorthand Property
- How to Change Text Style in CSS
- How to Create CSS Sprites
- How to Use CSS with Different Media Types
- How to Import Style Sheets with @import in CSS
- How to Use the CSS White-Space Property
- How to Use the CSS Z-index Property
- How to Create Drop Shadows with the box-shadow Property in CSS3
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to keep website width fixed (irrespective of screen size)
I am designing a website. I want my website to be 980px width. I can easily do it on my laptop by setting the right and left fields of CSS Box as 15%. But my concern is that, when my website is opened on another PC with no widescreen, that 15% will create a distortion. I want to keep my website at 980px whatever be the screen size may be. Please help!

- Um define the width to be 980px... ? I'm missing something where do you get percentages in there? Or are you asking about margin %? – Jakub Apr 25, 2011 at 17:40
- while creating a box using CSS, I fixed that left box is 15% away from left border of browser and right box is 15% away from right border of the browser. I am talking about left and right margin. – sumit Apr 25, 2011 at 17:46
4 Answers 4
Rather than trying to explicitly set the left and right margins, just have it auto center.
The key is the auto margin for left and right. This will horizontally align that 980px div in the center and keeping the width no matter what.
- If you see the picture, there are two boxes side by side. Looking at only a single box (any of them), you will find that neither of them is in center. But if see both boxes are a single box, then it is in center. This is my concern. I hope you understand what I mean. – sumit Apr 25, 2011 at 17:43
- Yes, add a containing div that houses the left and right div. I will update my answer with more code. – Dustin Laine Apr 25, 2011 at 17:44
I use the following style rules to create fixed-width cross-browser compatible layouts:
Put a div with ID "wrapper" inside your body tag, and the wrapper will always be at the center of the screen, and will always by 980px;
set the width on body tag and set CSS style align to center
- @iSumitG, nothing is created by CSS, its HTML under the hood, CSS just styles it. – Jakub Apr 25, 2011 at 17:39
Create a container div for the portion of the page that you want to be 980px, and set it to width: 980px; margin: 0 auto so that it will be centered and 980px.
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged html css or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- The [amazon] tag is being burninated
- Temporary policy: ChatGPT is banned
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
Hot Network Questions
- Pixel 5 vibrates once when turned face down
- Skip Beat in a Measure
- Choice of Basis for solution space of a Hamiltonian
- How do you ensure that a red herring doesn't violate Chekhov's gun?
- Google maps for Space exploration
- Would Fey Ancestry affect Cutting Words?
- A plastic tab/tag stuck out of the GE dryer drum gap. Does anyone know what it is, if it is a part of the dryer, and if so how I can fix it?
- Should I put my dog down to help the homeless?
- Should I ask why they are interested in me in an interview for a faculty position?
- What kind of wall is this and how can I repair it?
- My code is GPL licensed, can I issue a license to have my code be distributed in a specific MIT licensed project?
- Heating resistor - low current, high temperature
- Does the attacker need to be on the same network to carry out a deauthentication attack?
- How would you design the following table?
- How or would these mechanical wings work?
- Is there any room for negotiation on my PhD program stipend offers?
- How to print hardware models for humans
- How would I use the transfer function with Web3 JS to send ETH?
- Resistance depending on voltage - the chicken and the egg?
- Extract raster value at each vertex from line, while keeping the line
- Full text of the 'Sri Mahalakshmi Dhyanam & Stotram'
- Are demand and time deposit accounts really loans _to_ the bank?
- What video game is Charlie playing in Poker Face S01E07?
- Basic page layout program from the PrintMaster 2.0 era
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
- Data Structure & Algorithm Classes (Live)
- System Design (Live)
- DevOps(Live)
- Explore More Live Courses
- Interview Preparation Course
- Data Science (Live)
- GATE CS & IT 2024
- Data Structure & Algorithm-Self Paced(C++/JAVA)
- Data Structures & Algorithms in Python
- Explore More Self-Paced Courses
- C++ Programming - Beginner to Advanced
- Java Programming - Beginner to Advanced
- C Programming - Beginner to Advanced
- Android App Development with Kotlin(Live)
- Full Stack Development with React & Node JS(Live)
- Java Backend Development(Live)
- React JS (Basic to Advanced)
- JavaScript Foundation
- Complete Data Science Program(Live)
- Mastering Data Analytics
- CBSE Class 12 Computer Science
- School Guide
- All Courses
- Linked List
- Binary Tree
- Binary Search Tree
- Advanced Data Structure
- All Data Structures
- Asymptotic Analysis
- Worst, Average and Best Cases
- Asymptotic Notations
- Little o and little omega notations
- Lower and Upper Bound Theory
- Analysis of Loops
- Solving Recurrences
- Amortized Analysis
- What does 'Space Complexity' mean ?
- Pseudo-polynomial Algorithms
- Polynomial Time Approximation Scheme
- A Time Complexity Question
- Searching Algorithms
- Sorting Algorithms
- Graph Algorithms
- Pattern Searching
- Geometric Algorithms
- Mathematical
- Bitwise Algorithms
- Randomized Algorithms
- Greedy Algorithms
- Dynamic Programming
- Divide and Conquer
- Backtracking
- Branch and Bound
- All Algorithms
- Company Preparation
- Practice Company Questions
- Interview Experiences
- Experienced Interviews
- Internship Interviews
- Competitive Programming
- Design Patterns
- System Design Tutorial
- Multiple Choice Quizzes
- Go Language
- Tailwind CSS
- Foundation CSS
- Materialize CSS
- Semantic UI
- Angular PrimeNG
- Angular ngx Bootstrap
- jQuery Mobile
- jQuery EasyUI
- React Bootstrap
- React Rebass
- React Desktop
- React Suite
- ReactJS Evergreen
- ReactJS Reactstrap
- BlueprintJS
- TensorFlow.js
- English Grammar
- School Programming
- Number System
- Trigonometry
- Probability
- Mensuration
- Class 8 Syllabus
- Class 9 Syllabus
- Class 10 Syllabus
- Class 8 Notes
- Class 9 Notes
- Class 10 Notes
- Class 11 Notes
- Class 12 Notes
- Class 8 Maths Solution
- Class 9 Maths Solution
- Class 10 Maths Solution
- Class 11 Maths Solution
- Class 12 Maths Solution
- Class 7 Notes
- History Class 7
- History Class 8
- History Class 9
- Geo. Class 7
- Geo. Class 8
- Geo. Class 9
- Civics Class 7
- Civics Class 8
- Business Studies (Class 11th)
- Microeconomics (Class 11th)
- Statistics for Economics (Class 11th)
- Business Studies (Class 12th)
- Accountancy (Class 12th)
- Macroeconomics (Class 12th)
- Machine Learning
- Data Science
- Mathematics
- Operating System
- Computer Networks
- Computer Organization and Architecture
- Theory of Computation
- Compiler Design
- Digital Logic
- Software Engineering
- GATE 2024 Live Course
- GATE Computer Science Notes
- Last Minute Notes
- GATE CS Solved Papers
- GATE CS Original Papers and Official Keys
- GATE CS 2023 Syllabus
- Important Topics for GATE CS
- GATE 2023 Important Dates
- Software Design Patterns
- HTML Cheat Sheet
- CSS Cheat Sheet
- Bootstrap Cheat Sheet
- JS Cheat Sheet
- jQuery Cheat Sheet
- Angular Cheat Sheet
- Facebook SDE Sheet
- Amazon SDE Sheet
- Apple SDE Sheet
- Netflix SDE Sheet
- Google SDE Sheet
- Wipro Coding Sheet
- Infosys Coding Sheet
- TCS Coding Sheet
- Cognizant Coding Sheet
- HCL Coding Sheet
- FAANG Coding Sheet
- Love Babbar Sheet
- Mass Recruiter Sheet
- Product-Based Coding Sheet
- Company-Wise Preparation Sheet
- Array Sheet
- String Sheet
- Graph Sheet
- ISRO CS Original Papers and Official Keys
- ISRO CS Solved Papers
- ISRO CS Syllabus for Scientist/Engineer Exam
- UGC NET CS Notes Paper II
- UGC NET CS Notes Paper III
- UGC NET CS Solved Papers
- Campus Ambassador Program
- School Ambassador Program
- Geek of the Month
- Campus Geek of the Month
- Placement Course
- Testimonials
- Student Chapter
- Geek on the Top
- Geography Notes
- History Notes
- Science & Tech. Notes
- Ethics Notes
- Polity Notes
- Economics Notes
- UPSC Previous Year Papers
- SSC CGL Syllabus
- General Studies
- Subjectwise Practice Papers
- Previous Year Papers
- SBI Clerk Syllabus
- General Awareness
- Quantitative Aptitude
- Reasoning Ability
- SBI Clerk Practice Papers
- SBI PO Syllabus
- SBI PO Practice Papers
- IBPS PO 2022 Syllabus
- English Notes
- Reasoning Notes
- Mock Question Papers
- IBPS Clerk Syllabus
- Apply for a Job
- Apply through Jobathon
- Hire through Jobathon
- All DSA Problems
- Problem of the Day
- GFG SDE Sheet
- Top 50 Array Problems
- Top 50 String Problems
- Top 50 Tree Problems
- Top 50 Graph Problems
- Top 50 DP Problems
- Solving For India-Hackthon
- GFG Weekly Coding Contest
- Job-A-Thon: Hiring Challenge
- BiWizard School Contest
- All Contests and Events
- Saved Videos
- What's New ?
- CSS-Properties
- CSS-Selectors
- CSS-Functions
- CSS-Examples
- CSS-Questions
- CSS-Tutorial
- Web Development
- Web-Technology
Related Articles
- Write Articles
- Pick Topics to write
- Guidelines to Write
- Get Technical Writing Internship
- Write an Interview Experience
Fixed Width Design
- What is CSS sprites and how to implement them on a page ?
- How to add CSS Rules to the Stylesheet using JavaScript ?
- Tilt effect in ReactJS
- How to install bootstrap in React.js ?
- ReactJS | forms
- ReactJS | Router
- ReactJS | Types of Routers
- ReactJS Fragments
- ReactJS | ReactDOM
- ReactJS | Rendering Elements
- ReactJS Components
- ReactJS | Props – Set 1
- ReactJS | Props – Set 2
- PHP | sort() Function
- How to Draw a Curved Edge Hexagon using CSS ?
- ReactJS | Methods as Props
- ReactJS | PropTypes
- ReactJS | State in React
- ReactJS | Lifecycle of Components
- ReactJS | Implementing State & Lifecycle
- ReactJS | Importing and Exporting
- Top 10 Projects For Beginners To Practice HTML and CSS Skills
- How to insert spaces/tabs in text using HTML/CSS?
- Hide elements in HTML using display property
- CSS to put icon inside an input element in a form
- How to create footer to stay at the bottom of a Web page?
- Types of CSS (Cascading Style Sheet) Styling
- HTML Calculator
- How to position a div at the bottom of its container using CSS?
- Last Updated : 26 Sep, 2022
In the early days of the world wide web, the majority of people out there were using desktop computers. These days the web is available on laptops, phones, tablets, cars, etc. People expect that the website will look good on every device. Responsive design makes this possible.
Responsive web design isn’t the first approach to building websites. Web developers and designers tried many other techniques before responsive web design. Among various other methods, one of them was fixed-width design.
As the name implies “The width of your content is fixed” means, that no matter what your screen size is, the width of the content is going to be the same all the time. In the early age of the internet and the world wide web, most monitors had screen dimensions of (640 * 480) pixels , these were CRT monitors (Cathode Ray Tube monitors). So web designers and developers thought that it was a safer choice for them to design their website according to that dimensions only.
As time goes, there were screens of sizes: 800*600 pixels , so the developers started feeling that designing their website for a fixed width of 800 pixels is a good choice for them. But again, the screen sizes change to 1024*768 pixels . Now, the developers had to design their website according to that screen size as well. It felt like a never-ending race among web designers, developers and hardware manufacturers.
Fixed Width Design means, when you specify a fixed width for your layout, that your layout will only look good at that specific width.
Note: The above number “1015” is just an example, you can use any value in place of that.
Approach: Fixed-width is generally used when you don’t want the width of your content to change according to the changing width of the screen. To do so, you have to declare a width property in your CSS code with some value in it. Whether it was 640, 800, or 1080 pixels , choosing one specific width to design for is called fixed-width design.
The CSS code of the below example is showing you a fixed-width design. You can easily see that we already defined the width of the body section as 640px which means no matter what the size of the screen will be, the body will always going to be 640px wide . And therefore restricting the web from being responsive.
Example 1: Below you will see an example of fixed width layout, where the units that we are using in the code are in pixels, which means the width is fixed and it will look good only in that specified width. Remember that it’s a fixed-width design, which means it will look good only on certain screen width and it’s not responsive. In responsive layout , we use rem/em units instead of px.
You can easily see in the above gif, that it is not adjusting the content as the screen width is changing because we have already declared a fixed width of certain pixels in our CSS code. This is called fixed-width layout. In the fixed-width layout, the content is not adjusted according to the changing screen width. That’s why it only looks good on one specific screen width. No matter what the screen size is, the content will go to cover only a certain portion of the screen.
Example 2: The below example will show you the comparison between fixed-width design and responsive design.
As you can see, in the GIF above, the black bar has a set size regardless of the pixel count on the screen, but the red bar changes size in response to the pixel count.
Please Login to comment...
- HTML-Questions
- Web Technologies
Improve your Coding Skills with Practice
Start your coding journey now.
HTML vs Body: How to Set Width and Height for Full Page Size
CSS is difficult but also forgiving. And this forgiveness allows us to haphazardly throw styles into our CSS.
Our page still loads. There is no "crash".
When it comes to page width and height, do you know what to set on the HTML element? How about the body element?
Do you just slap the styles into both elements and hope for the best?
If you do, you're not alone.
The answers to those questions are not intuitive.
I'm 100% guilty of applying styles to both elements in the past without considering exactly which property should be applied to which element. 🤦♂️
It is not uncommon to see CSS properties applied to both the HTML and body elements like this:
Does It Matter?
Yes, yes it does.
The above style definition creates a problem:
Setting min-height to 100% on both elements does not allow the body element to fill the page like you might expect. If you check the computed style values in dev tools, the body element has a height of zero.
Meanwhile, the HTML element has a height equal to the visible part of the page in the browser.
Look at the following screenshot from Chrome Dev Tools:

Why Does This Happen?
Using a percentage as a size value requires the element to reference a parent to base that percentage on.
The HTML element references the viewport which has a height value equal to the visible viewport height. However, we only set a min-height on the HTML element... NOT a height property value.
Therefore, the body element has no parent height value to reference when deciding what 100% is equal to.
And The Problem May Be Hidden
If you started out with enough content to fill the body of the page, you might not have noticed this issue.
And to make it more difficult to notice, if you set a background-color on both elements or even on just one of them, the viewport is full of that color. This gives the impression the body element is as tall as the viewport.
It's not. It's still at zero.
The image above is taken from a page with the following CSS:
Reverse-inheritance?
In a strange twist, the HTML element assumes the background-color of the body element if you don't set a separate background-color on the html element.
So What is the Ideal Height Setting for a Full Responsive Page?
For years, the answer was the following:
This allows the HTML element to reference the parent viewport and have a height value equal to 100% of the viewport value.
With the HTML element receiving a height value, the min-height value assigned to the body element gives it an initial height that matches the HTML element.
This also allows the body to to grow taller if the content outgrows the visible page.
The only drawback is the HTML element does not grow beyond the height of the visible viewport. However, allowing the body element to outgrow the HTML element has been considered acceptable.
The Modern Solution is Simplified
This example uses vh (viewport height) units to allow the body to set a minimum height value based upon the full height of the viewport.
Like the previously discussed background-color, if we do not set a height value for the HTML element, it will assume the same value for height that is given to the body element.
Therefore, this solution avoids the HTML element overflow present in the previous solution and both elements grow with your content!
The use of vh units did cause some mobile browser issues in the past, but it appears that Chrome and Safari are consistent with viewport units now .
Page Height May Cause a Horizontal Scrollbar
Wait, what?
Shouldn't this say "Page Width"?
In another strange series of events, your page height may activate the horizontal scrollbar in your browser.
When your page content grows taller than the viewport height, the vertical scrollbar on the right is activated. This can cause your page to instantly have a horizontal scrollbar as well.
So What is the Fix?
You may sleep better knowing it starts with a page width setting.
This problem arises when any element - not just the HTML or body element - is set to 100vw (viewport width) units.
The viewport units do not account for the approximate 10 pixels that the vertical scrollbar takes up.
Therefore, when the vertical scrollbar activates you also get a horizontal scrollbar.
How to Set the Page for Full Width
Maybe just don't.
Not setting a width on the HTML and body elements will default to the full size of the screen. If you do set a width value other than auto, consider utilizing a CSS reset first.
Remember, by default the body element has 8px of margin on all sides.
A CSS reset removes this. Otherwise, setting the width to 100% before removing the margins will cause the body element to overflow. Here's the CSS reset I use:
How to Set Width to Your Preference
While it may not always be necessary to set a width, I usually do.
It may simply be a habit.
If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default.
If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.
Here's an example:
With no height value provided for the HTML element, setting the height and/or min-height of the body element to 100% results in no height (before you add content).
However, with no width value provided for the HTML element, setting the width of the body element to 100% results in full page width.
This can be counterintuitive and confusing.
For a responsive full page height, set the body element min-height to 100vh.
If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars.
I'll leave you with a tutorial from my YouTube channel demonstrating the CSS height and width settings for an HTML page that is full screen size and grows with the content it contains:
Do you have a different way of setting the CSS width and height that you prefer?
Let me know your method!
Hi, I'm currently working on a PhD in Information Systems. Also a full-time Solutions Architect & Developer. "Strive for progress, not perfection." Follow me on Twitter: @yesdavidgray
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
This forum is now read-only. Please use our new forums! Go to forums

how do you fit a webpage size to fit any screen size
ok i will like to know how do you fit your website size to fit any size because it seems that elements of my website are all around the place whenever i shrink the screen. thanks

Answer 5326c7059c4e9d92e9008b1b
well, the simple solution would be this: body { padding: 0; margin: 0; } (by defalut there is a margin of i believe 10px, so lets set it zero. now imagine i have a div inside my body: `
You can make it full screen using: div { width: 100%; height: 100%; }
this is not very efficient. since when the window is made small the page will become very small, happily we can fix that: div { width: 100%; height: 100%; min-width: 1000px; min-height: 1000px; }
now the div can’t be smaller then 1000px (which is not good news for mobile browser, but we will get back to that later). of course you could use em, em depends on the size of the font-size. so this: div { font-size: 16px; height: 2em; } the height of the the div is now 32px, (2*16) you can make it 16px (1em) or 8px (0.5em)
You can not really answer the question you asked, it is fully dependable of how you design your webpage. but since there are scripts that can tell if you have a mobile browsers you can use px as unit. and make a page for phones as well.
The only thing i can answer is what you see with a lot of webpages: full width but probably set width min-width to prevent shifting elements like you have or they make a central wrapper (just a div) and set it like this: div { height: auto; /*if more info comes on the page, it will stretch down*/ width: 1000px; margin: 0 auto; /*this will cause the div to be in the center*/ }
hopefully this answers some of your questions. If you have any questions, just ask them and i will do my best to answer them. But when visited website, try to figure out (just by thinking about how it could be designed) how it is designed. if you have an idea, take a look in the source code (most websites allow you to view source code) don’t worry about lot’s of thing you don’t know yet, that is just because mostly large websites are maintained by lots of people, to set up your own website you can do it a lot simpler but huge websites can give you useful code you can maybe use. also take a look at http://www.w3schools.com/ great site.

thanks a lot for your help buddy
Answer 5327611d9c4e9d7538000177
if you have any other questions, just post them here (i will reply) If you might have visited my profile you have seen i have own site, if you want to know how to set up your site, i can help you.
@stetim your reply has been of help to me as well…But I just wanna ask what would happen if I set my div’s width to auto in the Css editor, will that cause the div to stretch in size if there’s a ‘long’ content coming in? Also do you think it’s advisable to just go all the way with a background div and differentiating it from subsequent ones with an id?
Answer 532f452a7c82caa40e0078d7
good you ask questions, if you have more questions post them, no problem. You can do auto width but this will just keep stretching and stretching and stretching (infinite stretching) so yes, you can but i recommend you set a max-width. just like you set width: auto; you can add the property max-width: 1000px; for example (you can have both property’s) you can even add a min-width. (min-height and max-height as well of course).
The second question i do not fully understand. I think it is good to make a background-div yes. i also recommend in the start of your stylesheet to start with body { margin: 0; padding:0; }
since by default a margin set of 10px (which you want to control).
you can then add div’s in your central div (which i recommend to set to 100% with a min-width).
hope this answer your questions.
Popular free courses
Learn javascript.
CSS Tutorial
Css advanced, css responsive, css examples, css references, css height, width and max-width.
The CSS height and width properties are used to set the height and width of an element.
The CSS max-width property is used to set the maximum width of an element.
CSS Setting height and width
The height and width properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.
CSS height and width Values
The height and width properties may have the following values:
- auto - This is default. The browser calculates the height and width
- length - Defines the height/width in px, cm, etc.
- % - Defines the height/width in percent of the containing block
- initial - Sets the height/width to its default value
- inherit - The height/width will be inherited from its parent value
CSS height and width Examples
Set the height and width of a <div> element:
Try it Yourself »
Set the height and width of another <div> element:
Note: Remember that the height and width properties do not include padding, borders, or margins! They set the height/width of the area inside the padding, border, and margin of the element!
Advertisement
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values , like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!
Note: If you for some reason use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property will be used (and the width property will be ignored).
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
Try it Yourself - Examples
Set the height and width of elements This example demonstrates how to set the height and width of different elements.
Set the height and width of an image using percent This example demonstrates how to set the height and width of an image using a percent value.
Set min-width and max-width of an element This example demonstrates how to set a minimum width and a maximum width of an element using a pixel value.
Set min-height and max-height of an element This example demonstrates how to set a minimum height and a maximum height of an element using a pixel value.
Test Yourself With Exercises
Set the height of the <h1> element to "100px".
Start the Exercise
All CSS Dimension Properties

COLOR PICKER

Get your certification today!

Get certified by completing a course today!

Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
[email protected]
Your Suggestion:
Thank you for helping us.
Your message has been sent to W3Schools.
Top Tutorials
Top references, top examples, web certificates, get certified.

- Latest Articles
- Top Articles
- Posting/Update Guidelines
- Article Help Forum

- View Unanswered Questions
- View All Questions
- View C# questions
- View Python questions
- View Javascript questions
- View C++ questions
- View Java questions
- CodeProject.AI Server
- All Message Boards...
- Running a Business
- Sales / Marketing
- Collaboration / Beta Testing
- Work Issues
- Design and Architecture
- Artificial Intelligence
- Internet of Things
- ATL / WTL / STL
- Managed C++/CLI
- Objective-C and Swift
- System Admin
- Hosting and Servers
- Linux Programming
- .NET (Core and Framework)
- Visual Basic
- Web Development
- Site Bugs / Suggestions
- Spam and Abuse Watch
- Competitions
- The Insider Newsletter
- The Daily Build Newsletter
- Newsletter archive
- CodeProject Stuff
- Most Valuable Professionals
- The Lounge
- The CodeProject Blog
- Where I Am: Member Photos
- The Insider News
- The Weird & The Wonderful
- What is 'CodeProject'?
- General FAQ
- Ask a Question
- Bugs and Suggestions

Adjusting your Websites to Fit all Types of Resolution Using HTML and CSS

Introduction
You might wonder that some web sites are flexible enough to adjust to any screen resolution. This capability can be approached by many ways, by using many web technologies, such like Javascript, Css, Html.
The article that I am going to write today is mainly focus on article specific content. There may be some points where I am not much considering since those are very basics on HTML and CSS.
Using the code
For some web developers are bit struggling to make their web site screen resolution adjusting. In order to give a unique experience to the user, this aspect is very critical. Since with the technology changes users are more into use different devices to browse internet. So it is very important that web site need to accommodate every resolution.
One of the easiest way is to make web site with percentage widths.
But this approach is less effective. If user loads the web site from a mobile device. It takes the width from screens 100%, which is very small amount of space.
Another approach can be give minimum width with percentages.
In above scenario web site decreases its web site only upto 1000px.
So lets take a look at a effective way of make a web site screen size adjustable by using some great things in HTML and CSS.
Lets look at what we are going to do. In this case I demonstrate this with different devices (Iphone 5, Galaxy S4, Windows Phone 920 and ITab 2). Also I tested it from two browsers Chrome, FireFox and IE, but in IE media queries doesn't work. I found a solution for it but didn't work.
Actually the technique that I am going to use can use in many different ways, I have dome my demonstration coding which matches to my scenario. Your development can differ that this. But my ultimate goal is to show how use this.
This is with full browser size.
Browser reduced so that Advertisement area is hidden.
Browser more reduced.
Browser more reduced to make Image and Description align well.
The design layout for the above web site is
What need to be done;
First lets take a look at HTML
For above HTML markup I have mostly used semantic HTML. Which is Header, Footer, Aside, Section tags. Those tags allow you to define better meaning to the browser which improves the search engine readability.
In first line I have define the tags that says or instructs to the web browser about what version of HTML the page is written in. <!DOCTYPE html> means HTML 5.
And then I declared an attribute which specifies the xml namespace for a document.
Then I have used conditional comments that if browser is Internet Explore use the mentioned script that defines HTML 5 tags, because some of old IE browsers cannot identify HTML5 tags. Also I have imported css3-mediaqueries.js script since IE 8 or later does not support media queries.
Then I have used another conditional comment if browser is Not IE. In this statement I first check whether browsing device is a computer ( media="only screen" ), if so I import styles specific to computer screens that adjust for screen resolution. Then in second I check whether browsing device is mobile device ( media="only screen and (max-device-width: 480px) , only screen and (-webkit-min-device-pixel-ratio: 2) , screen and (-webkit-device-pixel-ratio:1.5)" ). Here I have used many media conditions,
First for Iphones screen and (max-device-width: 480px) next for Android Devices screen and (-webkit-min-device-pixel-ratio: 2) next for Iphones with retina display screen and (-webkit-device-pixel-ratio:1.5)
Then again I use another conditional comment for IEMobile browsers.
But here I need to say that. Making media queries compatible with IE 8 or later didn't work with css3-mediaqueries.js script. In a forum I saw that there are waiting for a new release of script.
Rest of the tags are pretty normal; where I wrap all the elements from Container div tag. Then Header tag that includes the web site Heading, next the Nav tag that includes navigation links; which I kept blank cause I don’t have any page navigating.
Then I declared a div that wraps all the content of the web page. I declared this to give a clear separation from Header tag and Footer tag.
Content div includes two main elements, one is <section> tag and <aside> tag. <section> tag defines sections in a web page and <aside> tag defines contents aside from the main content. Here I use <section> tag to include all the page content, such like posts, descriptions and <article> tag to hold Advertisements.
Inside <section> tag I declared an <article> which specifies independent, self-contained content. In <article> tag there are two elements that holds an image and some text.
So where does the Magic happens, it is all with CSS. Here I am not going to do any explanation with basic CSS properties, but I’ll concentrate more on tutorial specific properties.
First things first, lets set the layout of the web page.
First I removed the browser’s default page margin and padding. And set the background color and font.
Then I set the width for main wrapper; the div which I wraps all the page elements. As I discuss in the beginning of the tutorial it is always better to use percentages for widths, cause the the web page is more flexible for what ever the resolution that it’s works on. In this scenario web page container always takes 90% width from browser width. And by setting margin auto, it aligns the element to center of the web page.
Next I set the header element width of 100%; now here you have to make sure that it doesn’t take 100% from browser instead it takes 100% width from Container div. Also I set the height of 120px.
Footer, Nav and Content would looks same as header.
Now comes for a bit tricky point. How to make main_articles <section> tag display in left and the side_articles <aside> tag display in right. Well you could do it by using tables, no fuss at all. But to be frank using tables for page layout alignment is bit out-dated and less maintainable. Using <div> or <section> are always better and safe side.
To main_articles section I set width of 70% from width of Content div, then I make the section float left and to the side_article I declare width of 25% from content width makes it float right.

Using float to adjust elements are now getting out-dated as well, so what’s the new thing?
Using flexbox is the new trend. Its improves the maintainability. First take a look at the layout that we use to display article description and image.

Lets start, first we have to make the wrapping <article> tag (siteDescription) a flexbox. For that I set display: flexbox . Then I set the flex-direction: row .
- row (default): left to right
- row-reverse : right to left
- column : aligns the flex items top to bottom
- column-reverse : aligns the flex items bottom to top
Then I set the desImage <div> and des <div> flexbox items by setting their flexbox-order. Finally I set styles for the Advertisement <div> with width 90% from <aside> width.
Well we have done with initial layout.
Lets do the magic; magic is all with CSS @media queries. By using @media queries, we can have a different layout for screen, print, mobile phone, tablet, etc.
There are many media types.
- all - Used for all media type devices
- aural - Used for speech and sound synthesizers
- braille - Used for braille tactile feedback devices
- embossed - Used for paged braille printers
- handheld - Used for small or handheld devices
- print - Used for printers
- projection - Used for projected presentations, like slides
- screen - Used for computer screens
- tty - Used for media using a fixed-pitch character grid, like teletypes and terminals
- tv - Used for television-type devices
You can refer more about @media queries at w3schools link: About Media Queries
Lets do some modification when screen comes to width of 900px;
When screen width comes to 900px width, I hide the <aside> advertisement section. After that I set the main_articles <section> element width of 90% to fill the space and siteDecription <article> element width of 100% to fill the space inside <article> element.
Then I want to do some space modification when the screen gets up to 700px, actually this is not much important but for my scenario it looks more cleaner.
Here I have reduce the header height and font size once the screen width 700px. And reduce the width of description <div> to 50% so it looks more aligned.
Now I do the biggest part, do modifications when screen upto width of 500px; I want to display my page like this.
So now I don’t need all flexbox stuff. All I need is to do some basic adjustments. Where Image and Description <div> to take 90% width. As earlier I reduce the header height and font size. Then I hide the advertisement are. Then I set main_articles <section> to take width of 90% and siteDescription <article> to take width of 95% from main_articles width.
Also I remove the flexbox property from siteDescription <article> and make it as black view.
Finally I do make Image and Description <div> to take 90% width and remove flexbox-order property.
Now let's see styles for Mobile device.

Android (Galaxy s4 )
Windows Phone (920)
These styles are not much differ from styles that I used for layout that screen upto width of 500px.
Points of Interest
The most tricky part is how to handle flexbox. Try this link: FlexBox .
And try to find out Media Queries as well. Media Queries .
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Comments and Discussions
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

- What You Need to Know
- White Label Local SEO
- White Label SEO Reports
- Become a SEO Reseller
- Mobile App Marketing Agency
- Google Premier PPC Agency
- PPC Certified Professionals
- White Label Retargeting
- White Label Social Media
- White Label Reputation Management
- White Label Web Design
- White Label Branding
- White Label vs Private Label
- White Label Partnership FAQ
- White Label Solutions
- White Label Pricing
- White Label Software
- Daily Drive Podcast
- That! Company Newsletter
- That! Company’s Vision
- Open Positions

Improve Your Digital Agency Revenue Now
Making a fixed width website mobile friendly.
Last Updated on May 22, 2020

There will be a time when mobile overtakes desktop browsing. Since mobile has become so prominent in our everyday lives, there are times when we web designers need immediate results. If you’re one of those designers or programmers that just doesn’t have the time to convert a static website into a WordPress theme, one that’s mobile friendly, there is hope for you my friend. When it comes to viewing all of your content on mobile devices, you can accomplish this with a little CSS and HTML optimization.
http://www.smartinsights.com/wp-content/uploads/2017/03/Mobile-share-of-online-time-percent-2017.png
That! Company provides captivating and effective web design services for agencies world-wide. Learn More about our White Label Web Design Services and how we can help you and your clients create or improve their web presence. Get started today!
Globally, mobile devices account for more than half of the minutes spent online, according to comScore
Mobile friendly or responsive websites have the ability to configure the width of your website automatically, to fit the viewer’s device. Horizontal scroll bars are a no-no in responsive design. If you view your current website on a mobile device, and you see a scroll bar at the bottom of your phone or tablet, chances are your website (or elements of your site) isn’t mobile friendly. These horizontal scroll bars ruin the user’s experience and will most likely click the back button. People are so accustomed to scrolling vertically on their devices, so that when users find themselves having to scroll vertically as well as horizontally to view content, they become uncomfortable and leave the site.
It’s all-inclusive from this point going forward, design websites have CSS classes and ID’s for everything, and I mean everything.
From the header and menu div’s to the <p> tags in the footer. You’ll want to adjust the size of all these things based on screen width. Moving, sizing, and scaling are a lot easier when the element can be targeted via CSS.
CSS Media Queries are easier to use than you think.

The first time media queries were brought to my attention, I thought it was part of a programming language, and not a form of CSS. I was overcome by a sense of relief, when I found out what it truly was, and I’ve been using it ever since on ALL of my web projects.
@media only screen and (min-width: 100px) and (max-width: 699px) {
body { background-color : blue}
So the above code is simply saying that if the user’s screen is between 100px and 699px wide, then change the body’s background color to blue. This is where labeling every div and span in your site becomes crucial. Manipulating these elements for different screen widths become easier with proper tagging.
Css-tricks.com list many mobile widths for us as a quick reference.
/* ———– Galaxy S5 ———– */
/* Portrait and Landscape */
@media screen
and (device-width: 360px)
and (device-height: 640px)
and (-webkit-device-pixel-ratio: 3) {
/*****YOUR CODE HERE*****/
/* ———– HTC One ———– */ /* Portrait and Landscape */
/* ———– iPhone 5 and 5S ———– */
@media only screen
and (min-device-width: 320px)
and (max-device-width: 568px)
and (-webkit-min-device-pixel-ratio: 2) {
/* ———– iPhone 6 ———– */
and (min-device-width: 375px)
and (max-device-width: 667px)
There is much more media query code for Galaxy, HTC, and Apple phones. Additionally, the widths for Ipad, Galaxy and Nexus tablet widths are listed. All you have to do is input this code to your site’s CSS file and fill in the blank CSS brackets with your own code! Done!
Your site’s responsive, but it still doesn’t feel quite right

You may also deem certain elements as insignificant, ones that can found be on both desktop and mobile devices. . You can get rid of them using CSS:
#main-content .sidebar img { display : none}
These site adjustments have a tendency to make websites’ really long, compelling users to scroll a lot. This is where anchor text (for sighted users), or skip links (for screen readers) become your friend.
Anchor text is a clickable link that allows users to skip to a certain section of your web page, without having to scroll. It’s a handy method for mobile friendly sites. The code below is an example of anchor text linking.
<a href=”#skip”>Click here to skip to Main Content</a>
<a id=”skip”></a>Main content starts here
Skip Links are mainly used for screen readers and allow users with disabilities to bypass or skip over repetitive web content, like menu navigation, and get right to the information that interests them.
If skip links have a CSS rule of “display:none”, screen readers the link becomes “inaccessible”. So, a way around this is to position the links off screen, that way screen readers can still recognize your links, and allow them to skip through your newly mobile site.
.skip-link {
position: absolute !important;
top: -9999px !important;
left: -9999px !important;
So, that’s the quick and easy way to convert your fixed width website into a mobile friendly one. Also, don’t forget to set all your images to “width: 100%; height: auto.” Any images, div’s or spans that have a set width in pixels, will create to the horizontal scroll bar on mobile devices, so remember to set them to either percentages, or different widths for the varying devices using media queries.
It can be a bit time consuming, but trust me, once you learn it, you’ll use it forever. You’ll use this method on every website you’ll ever do, because even the newest WordPress or Joomla responsive themes and templates require a little doctoring to get them to look the way you want them to. That is especially true, with the ever-changing market for mobile devices.
-Izzak Hale, Senior Graphic Designer

IMAGES
VIDEO
COMMENTS
A "fixed-width" layout is one in which the layout of the page is contained within a wrapper that doesn't adjust its size when the width of the browser changes.
The key is the auto margin for left and right. This will horizontally align that 980px div in the center and keeping the width no matter what.
To make something centreed: use margins. Do not use align:center; This is outdated. Apply margin:0px auto 0px auto to whatever element you want
To do so, you have to declare a width property in your CSS code with some value in it. Whether it was 640, 800, or 1080 pixels, choosing one
So What is the Fix? ... You may sleep better knowing it starts with a page width setting. This problem arises when any element - not just the HTML
just like you set width: auto; you can add the property max-width: 1000px; for example (you can have both property's) you can even add a min-width. (min-height
Using max-width instead, in this situation, will improve the browser's handling of small windows. This is important when making a site usable on small devices:.
auto - This is default. · length - Defines the height/width in px, cm, etc. · % - Defines the height/width in percent of the containing block · initial - Sets the
So lets take a look at a effective way of make a web site screen size adjustable by using some great things in HTML and CSS. Lets look at what
From the header and menu div's to the <p> tags in the footer. You'll want to adjust the size of all these things based on screen width. Moving