- WordPress Mobile Web Development:Beginner's Guide
- Rachel McCollin
- 518字
- 2021-08-13 18:06:20
Time for action—making our site fluid
So, let's see what code we can change to turn the Carborelli's site into a fluid one. We'll remove any code that doesn't need to be changed as follows:
/* main layout */ body { width: 90%; max-width: 940px; padding: 10px 1%; } /* Structure the header */ #site-title { width: 35%; } #site-title img { width: 60%; } #header-right { width: 60%; } #header-right img { margin: 10px 0 10px 1%; } #header-right .CTA { padding: 20px 2% 20px 4%; margin: 20px 0; } #header-right .CTA img { width: 20%; } #access { width: 100%; } /*Structure the content*/ #content { width: 65%; } /*alternative styling for pages with no sidebar*/ .page-template-onecolumn-page-php #content, .single-attachment #content { width: 100%; } /* Structure the sidebars */ #primary, #secondary { width: 32%; } /* Structure the footer area */ #copyright { width: 50%; } #credits { width: 50%; }
What just happened?
The following list shows what's happened:
- All of those pixel widths have been replaced by percentages. The math to calculate each element's percentage width is as follows:
(width of element / width of containing element) * 100
I haven't been entirely precise with my percentages. I prefer to use round number percentages that fit within our layout than to match them up exactly to the original pixels. After all, as soon as someone resizes his/her browser window, those original pixel widths become irrelevant.
- Some extra styling on the
<body>
element has set a maximum width as follows:body { width: 90%; max-width: 940px; }
What this does is set the width of the
<body>
element to be90%
of the width of the browser window, except in the case of very large screens, where the<body>
element stops expanding at940px
of width, our original measurement. This avoids long lines of text running from left to right or large areas of white space within the layout.
To summarize what we did, we took the layout styling for the Carborelli's site and changed width settings to percentages instead of pixels to make the layout fluid. We also defined a maximum width for our site to avoid it getting too wide on very large screens. There was a bit of math involved but it wasn't too onerous!
Have a go hero
Now try it yourself. Perform the following steps:
- Open up the stylesheet for your site and find the layout styling.
- Identify all the settings for the width of elements.
- Apply
max-width
to your site's<body>
. - Work out on what percentage widths all the internal elements should have.
- Amend the CSS to apply widths instead of pixels.
- Open the site in your browser window and see what happens. It should resize according to the size of the window.
But it gets better. This layout still isn't going to look too good on very small screens. We need to set some specific styling for mobile devices, which is where media queries come in.