Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Responsive Art-Directed Photos</h1>
<h2>Using Element Queries</h2>

<p>By using Element Queries, with a little helper function named <code>isBG()</code> we can write repsonsive art direction rules about where we want the focal point of an image to be, and then using Element Queries we can check which elements have that image applied to them as a CSS background image, check the element's own aspect ratio, and reposition the background image inside whatever element it is set on to ensure that the desired focal point of the image is always visible.</p>

<p>I added all of the <code>border-color</code> just so you could see which query was applying to the image :) You can feel free to remove that!</p>

<hr>

<!-- Family Demo -->
<div class="family" style="width: 100px; height: 600px;"></div>
<div class="family" style="width: 100px; height: 400px;"></div>
<div class="family" style="width: 100px; height: 200px;"></div>
<div class="family" style="width: 100px; height: 100px;"></div>
<div class="family" style="width: 200px; height: 100px;"></div>
<div class="family" style="width: 400px; height: 100px;"></div>
<div class="family" style="width: 600px; height: 100px;"></div>

<!-- Toronto Demo -->
<hr>
<div class="toronto" style="width: 100px; height: 600px;"></div>
<div class="toronto" style="width: 100px; height: 400px;"></div>
<div class="toronto" style="width: 100px; height: 200px;"></div>
<div class="toronto" style="width: 100px; height: 100px;"></div>
<div class="toronto" style="width: 200px; height: 100px;"></div>
<div class="toronto" style="width: 400px; height: 100px;"></div>
<div class="toronto" style="width: 600px; height: 100px;"></div>

<!-- Refugees Demo -->
<hr>
<div class="refugees" style="width: 100px; height: 600px;"></div>
<div class="refugees" style="width: 100px; height: 400px;"></div>
<div class="refugees" style="width: 100px; height: 200px;"></div>
<div class="refugees" style="width: 100px; height: 100px;"></div>
<div class="refugees" style="width: 200px; height: 100px;"></div>
<div class="refugees" style="width: 400px; height: 100px;"></div>
<div class="refugees" style="width: 600px; height: 100px;"></div>

<!-- About Demo -->
<hr>
<div class="about" style="width: 100px; height: 600px;"></div>
<div class="about" style="width: 100px; height: 400px;"></div>
<div class="about" style="width: 100px; height: 200px;"></div>
<div class="about" style="width: 100px; height: 100px;"></div>
<div class="about" style="width: 200px; height: 100px;"></div>
<div class="about" style="width: 400px; height: 100px;"></div>
<div class="about" style="width: 600px; height: 100px;"></div>

<!-- Help Demo -->
<hr>
<div class="help" style="width: 100px; height: 600px;"></div>
<div class="help" style="width: 100px; height: 400px;"></div>
<div class="help" style="width: 100px; height: 200px;"></div>
<div class="help" style="width: 100px; height: 100px;"></div>
<div class="help" style="width: 200px; height: 100px;"></div>
<div class="help" style="width: 400px; height: 100px;"></div>
<div class="help" style="width: 600px; height: 100px;"></div>

<!-- Contact Demo -->
<hr>
<div class="contact" style="width: 100px; height: 600px;"></div>
<div class="contact" style="width: 100px; height: 400px;"></div>
<div class="contact" style="width: 100px; height: 200px;"></div>
<div class="contact" style="width: 100px; height: 100px;"></div>
<div class="contact" style="width: 200px; height: 100px;"></div>
<div class="contact" style="width: 400px; height: 100px;"></div>
<div class="contact" style="width: 600px; height: 100px;"></div>
              
            
!

CSS

              
                div {
  margin: 5px;
  background-size: cover;
}
hr {
  margin: 3em 0;
}
.family {
  background-image: url('http://freshstarttoronto.com/img/family.jpg');
}
.toronto {
  background-image: url('http://freshstarttoronto.com/img/toronto.jpg');
}
.refugees {
  background-image: url('http://freshstarttoronto.com/img/refugees.jpg');
}
.about {
  background-image: url('http://freshstarttoronto.com/img/about.jpg');
}
.help {
  background-image: url('http://freshstarttoronto.com/img/help.jpg');
}
.contact {
  background-image: url('http://freshstarttoronto.com/img/contact.jpg');
}

/* Family Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 15% 25%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 20% 25%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 18% 25%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 0% 25%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 0% 30%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'family.jpg')? '$this' : ''") {
    background-position: 0% 40%;
  }
}

/* Toronto Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 49% 100%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 49% 100%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 49% 100%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 100% 50%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 100% 45%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'toronto.jpg')? '$this' : ''") {
    background-position: 100% 40%;
  }
}

/* Refugees Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 51% 100%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 51% 100%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 51% 100%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 0% 50%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 0% 55%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'refugees.jpg')? '$this' : ''") {
    background-position: 0% 60%;
  }
}

/* About Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 30% 100%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 40% 100%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 45% 100%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 0% 35%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 0% 30%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'about.jpg')? '$this' : ''") {
    background-position: 0% 27%;
  }
}

/* Help Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 35% 100%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 42% 100%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 45% 100%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 0% 65%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 0% 60%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'help.jpg')? '$this' : ''") {
    background-position: 0% 50%;
  }
}

/* Contact Demo */
@element div and (max-aspect-ratio: 1/1) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 35% 100%;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 30% 100%;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 27% 100%;
  }
}
@element div and (min-aspect-ratio: 1/1) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 27% 100%;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 25% 90%;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  eval("isBG($it,'contact.jpg')? '$this' : ''") {
    background-position: 20% 85%;
  }
}

/* Borders for Debugging */
@element div and (max-aspect-ratio: 1/1) {
  $this {
    border: 1px solid yellow;
  }
}
@element div and (max-aspect-ratio: 1/2) {
  $this {
    border: 1px solid orange;
  }
}
@element div and (max-aspect-ratio: 1/4) {
  $this {
    border: 1px solid red;
  }
}

@element div and (min-aspect-ratio: 1/1) {
  $this {
    border: 1px solid lime;
  }
}
@element div and (min-aspect-ratio: 2/1) {
  $this {
    border: 1px solid blue;
  }
}
@element div and (min-aspect-ratio: 4/1) {
  $this {
    border: 1px solid indigo;
  }
}
              
            
!

JS

              
                function isBG(element,filename){
  var bg = window.getComputedStyle(element).backgroundImage
  return (bg.indexOf(filename) !== -1)
}
              
            
!
999px

Console