Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Help with Positioning and Media Queries

Is there a better method for positioning 2 objects in front of each other? Ideally I would like them to be fully responsive.

Additionally, I can NEVER seem to get Media Queries to work! What am I doing wrong? I've included my HTML and CSS markup.

Also note that I am just practicing/experimenting/trying to hone my skills here!

  <head>
    <meta charset="utf-8">
    <title>Ryan Decker|Designer</title>
    <link rel="stylesheet" href="css/normalize.css" type="text/css" media="screen">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
  </head>
<body>
<div id="front">
    <h1> HUGE SALE </h1>
    <h2> Buy some crap </h2>
</div>
<div id="back"></div>
body {font-size: 15px;
width: 100%;
height: 100%;}

#front { width: 30%;
height: 300px;
background: yellow;
z-index: 1;
margin: 80px auto 0px;
}

#front h1 {font-size: 3em;
padding-top: 90px;
margin-bottom: 5px;
text-align: center;}

#front h2 {font-size: 2em;
text-align: center;
margin-top: 0px;}

#back { width: 30%;
height: 500px;
background: black;
position: absolute;
left: 35%;
top: 10px;
z-index: -1;

}

@media screen and (max-width: 300px) {
    #front h1 {
        font-size: 2em;}
    }

4 Answers

Hey Ryan! haha

That is not your full code, is it? If it is, I hate to say that you're missing quite a bit from each file. The HTML is missing the html element entirely and the ending body element. The CSS is missing an ending curly brace for your media query. I guess that's not really quite a bit for the CSS or HTML really.

Also, I'm sure you realize that max-width, when used with media queries, is only for target widths of less than 300 px. You are not gonna be able to see to that by resizing the browser window since (unless you're running a 640 x 480 resolution on your computer, even then I don't know if it can resize down that far), the browser can only go down to about a 465 px width. You can use Chrome's neat tool (and Firefox has a similar one too) called "Device Mode" which will allow you to see different resolutions in action. To activate it, just press Ctrl + Shift + I (or Command + Shift + I on Mac) and press the mobile looking icon right next to the magnifying glass on the far left side of the toolbox that pops up.

You should also know that I don't think any device actually has a resolution of 300px so why you are targeting that width is beyond me lol I would instead change that 300px to 468px.

Marcus,

I did not post the html/opening body/closing body in my HTML example. But, they are there!

Are you sure I'm missing a curly brace in the CSS? There are 2 opening braces and 2 closing.

@media screen and (max-width: 300px) {
    #front h1 {
        font-size: 2em;}
    }

I had the max-width set to 300 just to test the code to see if it worked. I also tried the max-width at 500px and still, no adjustment in font-size when the screen was adjusted below 500px.....

They are there now that I've taken a closer look. It's hard to see them, though, since they are on the same line as the last property. It's really hard to read CSS when it's written like that, but if that's the way you like it, so be it.

But, your code, from what you've pasted thus far, works just fine. You should post your full code for both pages so that I can see what is going on. It is likely there is an error elsewhere.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ryan Decker|Designer</title>
    <link rel="stylesheet" href="css/normalize.css" type="text/css" media="screen">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
  </head>
<body>
<div id="front">
    <h1> HUGE SALE </h1>
    <h2> Buy some crap </h2>
</div>
<div id="back">
<h2> STARTS AT MIDNIGHT </h2>
</div>

</body>
</html>
body h1 {font-size: 15px;
}

#front { width: 30%;
height: 300px;
background: yellow;
z-index: 1;
margin: 200px auto 0px;
}

#front h1 {font-size: 4em;
font-family: impact;
padding-top: 90px;
margin-bottom: 5px;
text-align: center;}

#front h2 {font-size: 2em;
font-family: verdana;
text-align: center;
margin-top: 0px;}

#back { width: 30%;
height: 500px;
background: black;
position: absolute;
left: 35%;
top: 100px;
z-index: -1;
}

#back h2 {color: yellow;
text-align: center;
padding-top: 10px;}

@media screen and (max-width:300px) {#front h1 {font-size: 3px;}}

So, your code is working perfectly. There's nothing wrong with it except for the dreary format you've decided for whatever reason to use lol. I'm kidding!

But, I don't know that I've explained this yet, but testing that small of a width when using your browser window is not a good idea because your browser is only going to go down to a set width. This is usually somewhere between like 330px to 465px or more/less; it's dependent upon your platform and the browser you're using. Regardless, I can guarantee you that if you change your "max-width" property to something like 700px you will actually see the results when resizing the browser window. Otherwise, use Chrome's Device Mode as I said in my first answer! It will allow you to go as small or as big as you want the screen to be. You could even keep the media query at 300px and see what's happening when using Device Mode in Chrome.

Hahaha, give me a break on the formatting! I'm still learning!

Is there a better method you would suggest for putting two objects in front of each other like I've done? Just a square in front of a rectangle that is responsive?

It doesn't matter that you have two objects in front of each other so long as they scale proportionately to the viewport. If you want responsiveness, you need to convert any large pixel values into % values and change your font sizes from px to em. "px" units are fixed units meaning that regardless of the current resolution they are displayed on, they are that exact size. % and em units are responsive units, on the other hand, scale a ton better. % units will scale proportionately to the viewport so that they will keep their dimensions proportionate to where they originally start. "em" units will scale proportionate to the current font size. You can also use % units for your fonts if you want them to be precisely the size that you originally set them to.

There are two options for converting your px units into % units: option 1 is the long, hard way that you should just know how to do (just in case) and option 2 is the short, easy way. Which one do you prefer, Ryan?

I guess both ways! But, I do have some of font-size set to em units. They don't seem to scale like when I set n element to a % unit:

#front h1 {font-size: 4em;
font-family: impact;
padding-top: 90px;
margin-bottom: 5px;
text-align: center;}

#front h2 {font-size: 2em;
font-family: verdana;
text-align: center;
margin-top: 0px;}

As I said, em units scale proportionate to the current font size. Most of the time, 1 em = 16px. But, depending on accessibility options that are in place from the user, platform, browser, and screen resolution, this default pixel value could be different. The "em" units however, do not scale dynamically when you resize the browser. These units are relative to the current font size of the browser. If you want them to scale dynamically, they must be % units, as well.

Now, moving on, I'm going to put Option 2 into its own answer and focus this one solely on Option 1. Option 1 is to use the following formula to calculate the % unit you'd like based upon your current screen resolution. This is actually not even the most accurate solution because you should use your page viewport width/height instead of screen resolution (which I will show you with my option 2). You can get your page viewport width/height, though, by adding the "Window Resizer" extension into Chrome.

Now, this isn't the most accurate method of converting to % because you will be off by a noticeable amount of pixels. I HIGHLY recommend using my option 2 because it is shorter, easier, and more precise.

So, in order to convert pixel units to % units, you use the following formula: "(target / context) * 100 = % unit". target is the current pixel value that the element is set to. The context is either your screen resolution width/height or document offset width/height (depending on whether you are calculating width or height). You will get the most accuracy by using offset width/height which is generally calculated via JavaScript. Here is an example:

I don't know what system you use, but most of the time, it's likely either Windows or a Mac (I use Linux, although not too many people do). I don't know how to retrieve the current screen resolution on a Mac, but on Windows 7, it's as simple as right clicking the desktop and choosing "Screen Resolution". Let's say your current resolution is 1280 x 1024. This means your width is 1280px and your height is 1024px of the entire screen. And let's say we have a box that has its width property set to 300px. To convert it, plug the respective values into the formula: (300px / 1280px) * 100" which equals "23.4375%". And you would do that for each element.

And, this is option 2. Simply copy and paste this file into an HTML file like "whatever_you_want_to_name_me.html". Run this on your local computer or on Workspaces or wherever in a MAXIMIZED window and BAM! You're done. All you do is plug in your numbers, choose whether you're dealing with width or height and click "Convert!"

You can stick with the default of 10 decimal place precision if you'd like or you can change the default loading precision by just simply changing the value located in Enter decimal precision:&nbsp; <input type="text" id="prec" value="10"> to whatever number you desire.

Just a couple considerations, though: 1) If you like to switch between computers for development, you should stick to just one until you've converted all of your present pixel units over to % because different resolutions cause different results and you'll get layout malfunctions. 2) If you switch your resolution while you are in the process of converting over your pixel values, it's gonna cause some layout malfunctions. Again, different resolutions cause different results.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Percentage Converter</title>
</head>

<body>
<h2>This converter uses your screen resolution to create a responsive unit from pixels.</h2>
<h2><strong>MAKE SURE YOUR WINDOW IS MAXIMIZED!</strong></h2>
Enter dimension to convert:&nbsp; <input type="text" id="initDim">px
<br><br>
Choose width or height:&nbsp; <select name="dim" id="dim">
<option value="Width">Width</option>
<option value="Height">Height</option>
</select>
<br><br>
Enter decimal precision:&nbsp; <input type="text" id="prec" value="10">
<br><br>
New responsive value: &nbsp;<input type="text" readonly="readonly" id="finalDim">
<br><br>
<button onclick="javascript:convertDim();">Convert!</button>
<script type="text/javascript">
function convertDim() {
var initDim = document.getElementById("initDim");
var finalDim = document.getElementById("finalDim");
var initDimVal = Number(initDim.value);
var finalDimVal = Number(finalDim.value);
var prec = document.getElementById("prec");
var precVal = Number(prec.value);
var sel = document.getElementById("dim");

if (sel.options[sel.selectedIndex].value == "Width"){
var screenWidth = document.body.offsetWidth;
finalDimVal = ((initDimVal / screenWidth) * 100).toFixed(precVal);
finalDim.value = finalDimVal + "%";
}
else{
var screenHeight = screen.availHeight;
finalDimVal = ((initDimVal / screenHeight) * 100).toFixed(precVal);
finalDim.value = finalDimVal + "%";
}
}
</script>
</body>
</html>