How to avoid scrolling when resizing the page? - html

I have two divs that stack horizontally on each other.
Each div occupies 100% of the view-port.
.section{
width:100%;
height:100%;
}
In each div there is an anchor to the other div.
<div class="first section ">
<a name="first-section"> </a>
<p>Click here to go to the second section</p>
</div>
<div class="second section">
<p>Click here to go to the first section</p>
</div>
<a name="second-section"> </a>
My goal is to show the content of only one div each time. But when one is on the second section div and resize the page, the two divs are showned when the page is resized to its initial size.
How can I avoid scrolling up when the page is resize?
I have body{overflow:hidden;}. Thanks.

You can set the div to position:fixed, so it's always relative to the viewport. Then hide all the divs except the first one by default, and show the one when it's on :target.
jsFiddle example
html, body { height: 100%; }
body { margin: 0; }
.section {
width: 100%;
height: 100%;
position: fixed;
}
.section:target {
visibility: visible;
}
.first {
background: lightblue;
}
.second {
background: lightgreen;
visibility: hidden;
}
<div class="first section" id="first-section">
<p>Click here to go to the second section</p>
</div>
<div class="second section" id="second-section">
<p>Click here to go to the first section</p>
</div>
Or, use z-index with different values, and move the div on top when it's on :target.
jsFiddle example
html, body { height: 100%; }
body { margin: 0; }
.section {
width: 100%;
height: 100%;
position: fixed;
}
.section:target {
z-index: 3;
}
.first {
background: lightblue;
z-index: 2;
}
.second {
background: lightgreen;
z-index: 1;
}
<div class="first section" id="first-section">
<p>Click here to go to the second section</p>
</div>
<div class="second section" id="second-section">
<p>Click here to go to the first section</p>
</div>
Note, both approaches require each div to have a solid background defined.

Related

Sticky position + Flex alignment anchors at top

When using position sticky and a align-self: start; to avoid flex stretching--the content grows upwards instead of pushing down. Is there a workaround or a different way of implementing this where the content pushes down instead of up.
To test.
Scroll down to when the sticky position stops being sticky.
Click on the 2nd purple box
You will see that the content grows upwards instead
of pushing the page down.
How can I push the page down instead?
If you remove align-self:flex-start; then it works as intended and pushes down but the orange background is too long. It should only match the content.
Codepen: link
let toggle = document.querySelectorAll('.toggle')
toggle.forEach((t) => {
t.addEventListener('click', () => {
t.style.height = '500px'
})
})
html {
color: white;
}
section {
background: red;
width: 100%;
display: flex;
}
.content {
background: blue;
height: 2000px;
width: 100%;
}
.sticky {
position: sticky;
align-self:flex-start;
top: 0;
background: orange;
width: 100%;
}
.toggle {
background: purple;
height: 300px;
width: 50px;
}
.more {
background: green;
height: 400px;
width: 100%;
}
<div class="more">
spacer
</div>
<section>
<div class="content">
<p>Content</p>
</div>
<div class="sticky">
<p> sticky </p>
<div class="toggle">
<p>Top A</p>
<p>row A2</p>
<p>row A3</p>
</div>
<div class="toggle">
<p>Top B</p>
<p>row B2</p>
<p>row B3</p>
</div>
</div>
</section>
<div class="more">
spacer
</div>

Page jump to middle of section?

I'm trying to jump to a section of a web page but...
I want to jump to the center of it (rather than have the top of the page be the top of the section)
Is there any way to do this?
I was thinking if there was some kind of #section + 50px kind of thing?
Because you wanted pure HTML, based on your tag, all I can come up with is this.
Create an absolute element.
Place it in the middle of the section.
Point the anchor to said element.
html {
scroll-behavior: smooth;
}
section {
position: relative; /* to make absolute child elements stay within it */
min-height: 100vh;
}
section.yellow {
background-color: yellow;
}
section.brown {
background-color: brown;
}
section.purple {
background-color: purple;
}
.middle.element {
position: absolute;
height: 0px;
top: 50%;
width: 100%;
}
.dashed-line.middle.element {
border-top: 1px dashed;
}
<ul>
<li>Yellow</li>
<li>Brown</li>
<li>Purple</li>
</ul>
<section class="yellow">
<div class="dashed-line middle element"><a id="yellow"></a></div>
</section>
<section class="brown">
<div class="middle element"><a id="brown"></a></div>
</section>
<section class="purple">
<div class="middle element"><a id="purple"></a></div>
</section>
<section></section>

How to calculate distance of element in CSS only?

I am trying to create a sticky header and above that sticky header, I have one div which has dynamic content.
I am not sure how much should I give to top:???? as top div height is not fixed.
is there any way to calculate sticky div position from the top or is there any way to count top:?? from the previous element
I am looking for pure CSS solution
.container {
height: 200px;
overflow: scroll;
}
.content {
background-color: gray;
}
.header {
position: sticky;
top: 10px;
background-color: red;
}
.footer {
top: 10px;
background-color: yellow;
height: 400px;
}
<div class="container">
<div class="content"> runtime example text </div>
<div class="header"> header text </div>
<div class="footer"> footer text </div>
</div>

Move element out of div using css

Effectively, I'm trying to move/position the inner paragraph element (p) from within the .box div (blue box) to be positioned above the .box div (blue box) in the fiddle below:
html, body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
My expected output is:
html,
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
.contents {
position: relative;
top: -50px;
}
<div class="container">
<h4>Box heading</h4>
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
<div class="box">
<div class="contents"></div>
</div>
</div>
However, in the above snippet, I have physically placed the paragraph elements above the blue box (.box div). Is there a way to essentially move the paragraph element from within the blue box above it using css.
I've tried using position attribute or giving the .contents a negative margin, however, this doesn't add space for the text to go above and causes overlapping issue with the heading and/or the blue box.
Note: The paragraphs within the box can be of any length, so I don't know the height and thus the offset needed for the paragraph.
You can consider position:relative for the content element and its container and use the same amount of pixel:
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
position: relative;
top: 110px;
}
.contents {
position: relative;
top: -110px;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
Or consider translate for a more dynamic way:
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
transform:translateY(40%); /*use 100% if height auto*/
}
.contents {
transform:translateY(-100%);
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
You could remove the styles from .box on mobile and add them to .content::after. This will allow for any amount of content in the paragraphs and grow accordingly.
html, body {
margin: 0;
}
.box,
.contents::after {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
/* Mobile only styles */
.box {
height: auto;
width: auto;
background: none;
}
.contents::after {
content: '';
display: block;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>

A better way to float text on top of an image with varying container widths

I'm redesigning a site and the different sections (header, banner image, main, etc.) have a background that stretches all the way across, however the content is contained to a certain width and that box is centered.
However, in the design the "banner image" (which is a image below the header but above the main content) will extend beyond the width of the rest of the content. At first this was easy until a need arose to have text on top of the banner image, and that text would need to line up with the rest of the text.
I cannot use CSS background image because on some pages the banner image area will be a slider, which requires tags.
I have a working solution, but it seems clunky and I was hoping to find a better method: http://jsfiddle.net/PkStg/10/
HTML:
<div class="header">
<div class="content-wrapper">
header text
</div>
</div>
<div class="banner">
<div class="content-wrapper">
<div class="banner-text-outer">
<div class="banner-text-inner">
<h2>banner text header</h2>
<p>banner text paragraph</p>
</div>
</div>
</div>
<div class="banner-image-wrapper">
<img src="http://www.brokenbowlakeguide.com/rainbow-trout-1.jpg" />
</div>
</div>
<div class="main-content">
<div class="content-wrapper">
main content text
</div>
</div>
CSS:
.header, .banner, .main-content { width: 100%; }
.header { background: red;}
.banner { background: green; }
.main-content { background: yellow; }
.content-wrapper {
margin: 0 auto;
max-width: 300px;
}
.banner-text-outer {
position: relative;
}
.banner-text-inner {
position: absolute;
top: 10px;
}
.banner-image-wrapper {
margin: 0 auto;
max-width: 400px;
min-width: 300px;
font-size: 0;
}
.banner-image-wrapper img {
width: 100%;
}
I know that you wanted to not use background-image, but here is a solution which uses that for anyone else who sees the page.
Perhaps your slider could make use of the background-image?
This should do it:
jsFiddle
HTML
<body>
<div class="header">
<div class="content-wrapper">
header text
</div>
</div>
<div class="banner">
<div class="content-wrapper">
<div class="banner-text-outer">
<div class="banner-text-inner">
<h2>banner text header</h2>
<p>banner text paragraph</p>
</div>
</div>
</div>
</div>
<div class="main-content">
<div class="content-wrapper">
main content text
</div>
</div>
</body>
CSS
.header, .banner, .main-content { width: 100%; }
.header { background: red;}
.banner { background: green; }
.main-content { background: yellow; }
.content-wrapper {
margin: 0 auto;
max-width: 300px;
}
.banner {
background: green url("http://www.brokenbowlakeguide.com/rainbow-trout-1.jpg") no-repeat;
background-size: contain;
background-position: center;
min-height: 150px;
}