Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div id="app">
</div>
              
            
!

CSS

              
                :root {
  --objectFit: none;
	 --containerWidth: 500px;
	 --containerHeight: 281px;
	
/* 	 Colors */
	--turquoise-dark: #0e8873;
	--turquoise-light: #59c5af;
	--yellow-light: #fcf9ec;
	--green: #67eaca;
	
/* 	font-size */
	--fs-s: 1.4rem;
	--fs-m: 1.6rem;
}

@media (max-width: 450px) {
	:root {
		--containerWidth: 100%;
	}
}

html {
	font-size: 62.5%;
}

body {
	font-family: 'Roboto', sans-serif;
 font-weight: 300;
	font-size: var(--fs-m);
	line-height: 1.5;
	background-color: var(--yellow-light);
	color: #212020;
}

h1, h2 {
 font-family: 'Roboto Slab', serif;
}

.wrapper {
  display: grid;
  grid-gap: 1.6rem;
  padding: 1.6rem;
	 max-width: 900px;
		margin: auto;
}

.image-container {
  width: var(--containerWidth);
  height: var(--containerHeight);
	 justify-self: center;
	 background-color: #212020;
	 border: 2px solid #212020;
}
.image {
  width: 100%;
  height: 100%;
  object-fit: var(--objectFit);
}

.image-width-container,
.image-aspect-ratio-container,
.object-fit-container,
.container-aspect-ratio {
	display: grid;
	grid-template-rows: 1fr 4rem;
	grid-gap: 0.8rem;
	font-weight: 500;
}

.form-container {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
	 grid-gap: 1.6rem;
}
.title {
	 margin: 0;
	 text-align: center;
	 line-height: 1.2;
	 color: var(--turquoise-dark);
}
.description {
	 margin: 0;
	 font-style: italic;
}

.select-field,
.input-field {
		background-color: var(--yellow-light);
	 border: 2px solid var(--turquoise-light);
		padding: 0.8rem;
	 min-width: 10rem;
}
.select-field:hover, 
.input-field:hover {
	border-color: var(--turquoise-dark)
}
	select {
  -webkit-appearance: none;
  -webkit-border-radius: 0px;
}

              
            
!

JS

              
                const objectFitOptions = ["fill", "cover", "contain", "none", "scale-down"]

function getMultiplier (aspectRatio) {
	let multiplier;
	 switch(aspectRatio) {
				case "16-9":
					return multiplier = 9/16
						break;
				case "3-4":
				return multiplier = 4/3
						break;
				case "3-2":
				return multiplier = 2/3
						break;
				case "4-3":
				return multiplier = 3/4
						break;
				case "1-1":
				return multiplier = 1/1
						break;
				default:
				return multiplier = 9/16
		}
}

const ObjectFit = ({onChange, value}) => {
  return (
    <div class="object-fit-container">
      <label htmlFor="object-fit">Object-fit</label>
    <select id="object-fit" class="select-field" value={value} onChange={onChange}>
      <option value="">Select</option>
      {objectFitOptions.map(option => <option value={option}>{option}</option> )}
    </select>
    </div>
  )
}

const ImageAspectRatio = ({onChange, imageAspectRatio}) => {
  return (
    <div class="image-aspect-ratio-container">
      <label htmlFor="image-aspect-ratio">Image Aspect ratio</label>
    <select id="image-aspect-ratio" class="select-field" value={imageAspectRatio} onChange={onChange}>
      <option value="16-9">16:9</option>
      <option value="3-4">3:4</option>
      <option value="1-1">1:1</option>
      <option value="3-2">3:2</option>
      <option value="4-3">4:3</option>
    </select>
  </div>
  )
}

 const PreviewContainer = ({imageWidth, imageAspectRatio, containerAspectRatio}) => {
	 const imageHeight = imageWidth*getMultiplier(imageAspectRatio);
  return (
      <div class="image-container">
        <img class="image" src={`https://source.unsplash.com/4w8-xhdcZoU/${imageWidth}x${imageHeight}`} alt="deer"/>
      </div>
  )
}
	
	const ImageWidth = ({imageWidth, onChange}) => {
		return (
			<div class="image-width-container">
				<label htmlFor="image-width">Image Width</label>
					<input id="image-width" class="input-field" type="number" value={imageWidth} onChange={onChange} />
			</div>
		)
	}
	
	const ContainerAspectRatio = ({containerAspectRatio, onChange}) => {
		return (
			<div class="container-aspect-ratio">
				<label htmlFor="container-aspect-ratio">Container Aspect ratio</label>
				<select id="container-aspect-ratio" class="select-field" value={containerAspectRatio} onChange={onChange}>
					 <option value="16-9">16:9</option>
      <option value="3-4">3:4</option>
      <option value="1-1">1:1</option>
      <option value="3-2">3:2</option>
      <option value="4-3">4:3</option>
				</select>
			</div>
		)
	}

class App extends React.Component {
  constructor(props) {
    super(props)
				this.preview = React.createRef();
    this.state = {
      objectFitValue: "none",
      imageAspectRatio: "3-4",
					 imageWidth: "400",
					 containerAspectRatio: "16-9"
    }
  }
  objectFitChange (e) {
				this.setState({objectFitValue: e.target.value })
				let root = document.documentElement;
				root.style.setProperty('--objectFit', e.target.value);
  }
  imageAspectRatioChange(e) {
    this.setState({imageAspectRatio: e.target.value })
  }
	
	 imageWidthChange(e) {
			this.setState({imageWidth: e.target.value })
		}
	containerAspectRatioChange(e) {
		 this.setState({containerAspectRatio: e.target.value })
		 		let root = document.documentElement;
		const containerWidthInPixel =    		getComputedStyle(root).getPropertyValue('--containerWidth');
		 const containerWidth = containerWidthInPixel.split('px')[0]
			root.style.setProperty('--containerHeight', `${containerWidth*getMultiplier(e.target.value)}px`);
		}
  
render() {
	return (
 	<div class="wrapper">
			<h1 class="title">Visualize object-fit with Aspect ratios</h1>
			<p class="description">Set the container aspect ratio which is a black box. Set the image aspect ratio and the image width, height of the image will be calculated based on the width and aspect ratio of the image.Play around with object-fit property how it behaves in different container and image aspect ratios. Container width for screen size below 500px is set be 100% of the width and above all screen sizes it is set to 500px.Thanks <a href="https://unsplash.com/@thkelley">Thomas Kelley</a> for the image which I used for this demo.</p>
			<form class="form-container">
				<ContainerAspectRatio 
					containerAspectRatio=	{this.state.containerAspectRatio} 
					onChange={(e) => this.containerAspectRatioChange(e) } />
				<ImageWidth imageWidth={this.state.imageWidth} 
			           onChange={(e) => this.imageWidthChange(e)} />
  		<ImageAspectRatio onChange={(e) => this.imageAspectRatioChange(e) } 
																			imageAspectRatio={this.state.imageAspectRatio} />
				<ObjectFit onChange={(e) => this.objectFitChange(e)} 
			          value={this.state.objectFitValue} />
		</form>
  <PreviewContainer 
			ref={this.preview}
			previewAspectRatio={this.state.containerAspectRatio} 
			imageWidth={this.state.imageWidth} 
			imageAspectRatio={this.state.imageAspectRatio}
			containerAspectRatio={this.state.containerAspectRatio}/>
 </div>
)
}
}

ReactDOM.render(<App />, document.getElementById("app"))
              
            
!
999px

Console