css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              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;
  }
}
            
          
!
            
              function isBG(element,filename){
  var bg = window.getComputedStyle(element).backgroundImage
  return (bg.indexOf(filename) !== -1)
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console