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 class="contents">
	<p>この親要素の横幅はPC画面で600px、スマホ画面で80%です。テキスト部分は親要素と同じ横幅なのですが、写真は全画面表示になっております。</p>

 	<div class="image"><img src="https://pulpxstyle.com/wp-main/common/codepen/post21-codepen01.jpg" alt="Calc Margin Demo image01"></div>
 
	<p>写真も本来はPC画面で600px、スマホ画面で80%表示されるはずなのですが、calc()を使うことで全画面表示にすることが出来ます。写真を印象的に表現したいページに使えますね。</p>

	<p>これは、テキスト部分の外側にある余白の値を計算して、写真をその分左右に広げることにより全画面表示をしてます。</p>

	<p>文章の横半分の値を『50%』で、全画面の横半分の値を『50vw』で出すことができるので、それらを使って文章横にある余白の値をcalc()で計算します。</p>

	<div class="image"><img src="https://pulpxstyle.com/wp-main/common/codepen/post21-codepen02.jpg" alt="Calc Margin Demo image02"></div>

	<p>計算式は、calc((50vw - 50%)* -1)</p>

	<p>『-1』をかけているのはネガティブマージンを値を出して、その値で写真を左右に広げるから。仮にテキスト部分の外側にある余白が左右それぞれ200pxだとしたら、marginの左右の値を『-200px』にすることで画像部分の要素が左右に200px分ずつ広がります。</p>

	<p>そのため『-1』をかけてネガティブマージンにしてます。</p>

	<p>それを簡略化させるため、実際の記述は『calc(50% - 50vw)』にして-1を掛けなくてもネガティブマージンの値を求められるようにします。</p>

	<div class="image"><img src="https://pulpxstyle.com/wp-main/common/codepen/post21-codepen03.jpg" alt="Calc Margin Demo image03"></div>

	<p>この部分的な全画面表示は写真や画像だけではなく、さまざまな要素に適用することができます。</p>
</div>
              
            
!

CSS

              
                .contents {
	margin-right: auto;
	margin-left: auto;
	width: 600px;
}

@media not all and (min-width: 600px){
	.contents {
		width: 80%;
	}
}

.contents p {
	margin-bottom: 50px;
}

.image {
	margin-right: calc(50% - 50vw);
	margin-left: calc(50% - 50vw);
	margin-bottom: 50px;
}

@media not all and (min-width: 600px){
	.contents p {
		margin-bottom: 30px;
	}

	.image {
		margin-bottom: 30px;
	}
}

.image img {
	display: block;
	width: 100%;
	height: auto;
}

@media not all and (min-width: 600px){
	.image img {
		height: 200px;
		object-fit: cover;
	}
}
              
            
!

JS

              
                
              
            
!
999px

Console