Responsive break out of parent element

Developer
Size
6,593 Kb
Views
34,408

How do I make an responsive break out of parent element?

IMO The easiest way to make elements break out of their parent container - No padding problems, no keeping track of regular content, not bound to percentages and all elements adapts individually.. What is a responsive break out of parent element? How do you make a responsive break out of parent element? This script and codes were developed by Jakob-e on 04 July 2022, Monday.

Responsive break out of parent element Previews

Responsive break out of parent element - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Responsive break out of parent element</title> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="initial-scale=1" />
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width, minimal-ui, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <main> <article> <header class="breakout"> <h1>Responsive break out of parent element</h1> </header> <div class="breakout"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/68939/ae.jpg" /> </div> <div class="breakout"> <blockquote> <p>We cannot solve our problems with the same thinking we used when we created them</p> </blockquote> <p>Albert Einstein</p> </div> <h2>The problem</h2> <p> Making a child element break out of its parent container is a thing that almost always causes a world of pain, a messy stylesheet and a bad developer-designer relationship. </p> <p>In general you are left with one of two choices: <ul> <li>Make the widths larger than 100%, try to counter the wrapping padding and add media queries to fix the negative margins when the content starts to overflow the viewport. </li> <li>Target all the nested elements individually (.container > *), create a faux or pseudo-element background on the wrapper and don't forget the media queries. </li> </ul> </p> <h2>The Solution</h2> <p>Rather than focusing on the parent and or trying keep track of all the child elements – take the evil wrapper pill and swallow your pride. <a href="#visual">Visual explanation</a> </p> <code> &lt;main&gt; &lt;article&gt; &lt;h1&gt;Lorem ipsum&lt;/h1&gt; &lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt; <em>&lt;div class="breakout"&gt; &lt;img src="image.jpg" /&gt; &lt;/div&gt;</em> &lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt; &lt;/article&gt; &lt;/main&gt; </code> <strong>The wrapper:</strong> <code> /** * 1 Position the wrapper relative to the parent element * 2 Make the wrapper fit the content height * 3 Set the width to match the width of the viewport (100vm) * 4 Push the wapper to the center of the parent element * * Note! Remember to add html,body { overflow-x:hidden; } to * prevent horizontal scrolling * * Note! If the left and right padding of your wrapper are * not the same – adjust the .breakout margin-left * <em>breakout-margin-left: (wrapper-padding-right - wrapper-padding-left)/2</em> */ .breakout { position:relative; display:table; width:100vw; left:50%; } </code> <strong>The content:</strong> <code> /** * Generic content style * 1 Position the content relative to the wrapper * 2 Center the content * 3 Pull the content -50% to the left and hereby * back to the center of the page */ .breakout > * { position:relative; margin-left:auto; margin-right:auto; left:-50%; } /** * Custom content style * Note! min-width will cause cropping */ .breakout > img { display:block; width:100%; max-width:960px; min-width:320px; } </code> <h2>The bug and the fixes:</h2> <p>Sadly browsers sucks at handling vm units - especially mobile ie6 incarnated (Safari on iOS7)... and once again we have to <s>turn to JavaScript.</s> make workarounds</p> <p><em>As of October 19, 2015 <strong>91%</strong> of iDevices are running iOS8+</em></p> <strong>Media Query Fix:</strong> <p>Benjamin Morel made <a class="inline" href="//gist.github.com/BenMorel/e9e34c08360ebbbd0634" target="_blank">a mixin to handle viewport units on iOS 7</a> that produces the following queries:</p> <code> @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 768px) and (device-height: 1024px) and (orientation: portrait) { .breakout { width: 768px; } } @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 768px) and (device-height: 1024px) and (orientation: landscape) { .breakout { width: 1024px; } } @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 320px) and (device-height: 480px) and (orientation: portrait) { .breakout { width: 320px; } } @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 320px) and (device-height: 480px) and (orientation: landscape) { .breakout { width: 480px; } } @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 320px) and (device-height: 568px) and (orientation: portrait) { .breakout { width: 320px; } } @media only screen and (-webkit-min-device-pixel-ratio: 1) and (device-width: 320px) and (device-height: 568px) and (orientation: landscape) { .breakout { width: 568px; } } </code> <strong>JavaScript Fix:</strong> <code> &lt;script&gt; function vwFix(){ var ww = window.innerWidth + 'px'; var el = document.getElementsByClassName('breakout'); for(var i=0,l=el.length;i&lt;l;++i){ el[i].style.width = ww; } }; vwFix(); window.addEventListener('resize', vwFix); // ...or if you prefer jQuery $(window).on('resize',function(){ $('.breakout').width(window.innerWidth); }).trigger('resize'); &lt;/script&gt; </code> <div class="breakout"> <figure id="visual"> <figcaption>For the visually oriented</figcaption> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/68939/breakoutofparent_1.png" /> </figure> <div> <footer class="breakout"> <p>Please comment :-) <br> Note! Because of the iframe on codepen this page might not show correctly unless in debug view. </p> </footer> </article> <div class="tools"> <a href="#" class="trans">CSS Transitions:</a> <a href="#" class="sizes">Random max-widths</a> <a href="#" class="reset">Reset</a> <a href="http://codepen.io/jakob-e/debug/zjAHe/" class="full" target="_blank">Full page</a> </div>
</main> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Responsive break out of parent element - Script Codes CSS Codes

@import url(http://fonts.googleapis.com/css?family=Lato:300,400,400italic);
*, *:before, *:after { box-sizing: border-box; margin: 0; padding: 0;
}
header, main, footer, nav, aside, article { display: block; font-family: 'lato', sans-serif;
}
h1 { font-size: 1.8em; margin-bottom: 0; font-weight: 300; text-transform: uppercase;
}
h2 { font-size: 1.5em; margin-bottom: 0;
}
p { font-size: 1em; margin-bottom: 1.5em; font-weight: 300;
}
a { font-size: 400; display: block; font-style: italic; color: #ab58b1; text-decoration: none;
}
a:hover { text-decoration: underline; color: #ce9dd2;
}
html, body { line-height: 1.5; width: 100%; overflow-x: hidden; background: SlateGray;
}
article { background: white; max-width: 700px; margin: 0 auto; padding: 2.5em 2.5em 0;
}
@media (max-width: 580px) { article { padding: 1.5em 1.5em 0; }
}
.breakout { position: relative; display: table; width: 100vw; left: 50%;
}
.breakout > * { position: relative; margin-left: auto; margin-right: auto; left: -50%;
}
header h1 { max-width: 800px; background: #f1f1f1; color: #808080; padding: 0.6em 2em; text-align: center;
}
.breakout > img { display: block; display: block; width: 100%; max-width: 960px; min-width: 320px;
}
blockquote { text-align: center; font-size: 1.8em; max-width: 800px; background: #f1f1f1; padding: 1em 2.5em 2em; color: #808080;
}
@media (max-width: 580px) { blockquote { padding: 1em 2em 2em; font-size: 1.1em; }
}
blockquote:before { font-family: Georgia, serif; content: '\201C'; position: absolute; top: 0.1em; left: 0.2em; font-size: 3em; color: #ccc; border-left: 2px;
}
blockquote p { margin: 0;
}
blockquote + p { max-width: 800px; text-align: right; padding-right: 1.5em; top: -2em; font-style: italic; color: #ccc;
}
footer p { max-width: 700px; background: #657382; color: white; padding: 1.5em 4em;
}
@media (max-width: 580px) { footer p { padding: 1.5em 1.5em; }
}
figure { border: 10px solid #f1f1f1; text-align: center; padding-top: 20px; max-width: 820px; background: white;
}
figure img { width: 100%; display: block;
}
code { margin-bottom: 1.5em; background: #f1f1f1; display: block; white-space: pre-wrap; font-size: 0.75em; font-family: "Courier New", Courier, "Lucida Sans Typewriter", "Lucida Typewriter", monospace;
}
ul { padding-left: 1em;
}
.tools { background: #282828; position: fixed; width: 100%; top: 0; z-index: 20; text-decoration: none;
}
.trans,
.sizes,
.reset,
.full { margin: 0 10px; color: white; text-decoration: none; display: block; float: left;
}
.trans:after { content: ' OFF';
}
.full { float: right;
}
main.transitionsenabled .trans:after { content: ' ON';
}
main.transitionsenabled .breakout,
main.transitionsenabled .breakout > * { -webkit-transition: all .5s; transition: all .5s;
}
.inline { display: inline;
}

Responsive break out of parent element - Script Codes JS Codes

/*$(window).on('resize',function(){ $('.breakout').width(window.innerWidth);
}).trigger('resize');
*/
// Just some quickly added tools
$('.trans').on('click',function(e){ e.preventDefault(); $('main').hasClass('transitionsenabled') ? $('main').removeClass('transitionsenabled') : $('main').addClass('transitionsenabled');
})
$('.sizes').on('click', function(e){ e.preventDefault(); $('.breakout').each(function(){ var width = (Math.round(Math.random() * 1000) + 200) + 'px'; $('> *',$(this)).css( { 'max-width' : width } ); console.log(width) })
});
$('.reset').on('click', function(e){ e.preventDefault(); $('.breakout > *').removeAttr('style')
})
Responsive break out of parent element - Script Codes
Responsive break out of parent element - Script Codes
Home Page Home
Developer Jakob-e
Username jakob-e
Uploaded July 04, 2022
Rating 4.5
Size 6,593 Kb
Views 34,408
Do you need developer help for Responsive break out of parent element?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jakob-e (jakob-e) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!