I try to understand position and values absolute and relative, but I have one problem.
Why, when I use the "right" attribute, my "A" point changes position when the resolution changes, but when it applies the "left" attribute, everything stays in place well, even when I change the resolution?
Please check on different resolutions:
- Left:
- Right:
Code:
- Left:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
left: 100px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
Right:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
right: 447px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
My question is why this is happening, why the use of "right" is so unstable?
Because the absolute position is "relative" to .map div that it has no fixed width (
it changes as the browser size changes, from min 350px to max 700px -
inherits this rule from the parent main). So the .map's right border continuously changes its position. The left border, on the other hand, "remains steady" so the h1 never change its position.
I try to explain better with an image:
Related
About Us and Contact Us are my two sections. The contact us section's background property is working well. However, I now want to add a background picture to both divs' parent wrappers. Therefore, the background image may be seen above both divs. I am unable to identify the issue that is preventing me from displaying the background image.
index.html:
<div class="aboutus-contact-us-wrapper" >
<div class="about-us-section">
// content here
</div>
<div class="contact-us">
// content here
</div>
</div>
style.css:
.aboutus-contact-us-wrapper {
width: 100%;
position: relative;
background-image: url('./images/ourboardvector.png');
background-size: 100%;
background-position: right;
background-size: contain;
}
.contact-us {
width: 100%;
height: 100vh;
background-color: #181f2b;
position: relative;
border-bottom: 20px;
color: #ffffff;
position: relative;
}
.about-us-section {
position: relative;
width: 100%;
height: 100vh;
background-color: #F1F1F1;
font-family: 'Lato' !important;
}
Set z-index of both child divs to -1.
Z Index ( z-index ) is a CSS property that defines the order of overlapping HTML elements. Elements with a higher index will be placed on top of elements with a lower index. Note: Z index only works on positioned elements ( position:absolute , position:relative , or position:fixed )
.aboutus-contact-us-wrapper {
width: 100%;
position: relative;
background-image: url('./images/ourboardvector.png');
background-size: 100%;
background-position: right;
background-size: contain;
}
.contact-us {
width: 100%;
height: 100vh;
background-color: #181f2b;
position: relative;
border-bottom: 20px;
color: #ffffff;
position: relative;
z-index: -1;
}
.about-us-section {
position: relative;
width: 100%;
height: 100vh;
background-color: #F1F1F1;
font-family: 'Lato' !important;
z-index: -1;
}
<div class="aboutus-contact-us-wrapper">
<div class="about-us-section">
// content here
</div>
<div class="contact-us">
// content here
</div>
</div>
I have been trying to get the header image to be shorter, however, I cannot figure out how to. Here is the HTML:
<div class="header">
<img src="images/header_sea(3).jpg" width="99%" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
Here is the CSS:
html, body {
height: 100%;
width: 99%;
text-align: center;
}
.title{
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header{
position: relative;
}
.header_image{
opacity: 0.55;
height: 40%;
width: 99%;
}
.header_title{
position:absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
I tried adjusting the height percentage in .header_image, but the image doesn't get shorter when I change the value.
The header needs a size associated with it. Otherwise the image has nothing to be "40%" of since the header is just using auto sizing.
Relevant code
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
Another good practice is to use semantic elements when possible, so consider using <header> instead of a div with a class of header.
html,
body {
height: 100%;
width: 99%;
text-align: center;
}
.title {
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
.header_image {
opacity: 0.55;
height: 100%;
width: 99%;
}
.header_title {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
<header class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</header>
try removing the width attribute from the image (inline) and change the width in .header_image
I did it for you below
<div class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg"
class="header_image"
alt="sea_sky"
/>
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
.header_image{
opacity: 0.55;
height: 40%;
width: 70%;
}
I'm working on a projet (HTML and CSS page) in which I have a navbar at the top of the page, a main container below the navbar, and a menu at the left.
The menu is hidden, and when I move the mouse to the left edge of the window, it appears, and overlaps the main container.
If I scroll the page down, the navbar scrolls, and the menu moves up until it reaches the top. Then it stops and keeps at this place.
I managed to achieve it, more or less. But I still have a problem.
To illustrate my project in a simple way, I took some basic code I found on css-tricks.com website and just modified it a bit to show my problem.
Here is the code :
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra,
#wrapper {
display:flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div class="extra"></div>
<br />
<div id="wrapper">
<div id="sticky">
sticky
</div>
<div id=brol>
This part should be overlapped by the sticky element
</div>
</div>
<br />
<div class="extra"></div>
Here, the 'extra' div is my navbar, the main container is the grey part, and the sticky element is the menu.
What I would like is that the main container (the grey part) is really using the full width and height, meaning that the text in it should appear at the top-left corner and be party overlapped by the sticky div.
How can I achieve that?
I have used z-index for overlapping content:
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
z-index: 2;
}
.extra,
#wrapper {
width: 75%;
margin: auto;
background-color: #ccc;
z-index:1;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#brol{
margin-top: -100px;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
// added .sticky-parent
.sticky-parent {
width: 0;
}
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra, #wrapper {
display: flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper {
height: 2000px;
}
}
<div class="sticky-parent">
<div id="sticky">
sticky
</div>
</div>
<div id="brol">
This part should be overlapped by the sticky element
</div>
You can achieve this behavior by wrapping the sticky div inside a absolute positioned one. this will make the rest use the full available space.
Don't forget to add position:relative to it's parent, in your simplified case #wrapper to make sure it wont take the full width & height of the document or first relative parent it finds
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
#mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.extra,
#wrapper {
position: relative;
display:flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div class="extra"></div>
<br />
<div id="wrapper">
<div id="mask">
<div id="sticky">
sticky
</div>
</div>
<div id=brol>
This part should be overlapped by the sticky element
</div>
</div>
<br />
<div class="extra"></div>
How would I position a div to take up 100% of the page height even when the page has a scroll bar and I scroll to the bottom. My div is a modal and is positioned under a navbar, I set the min-height to 100vh, but it doesn't seem to expand to the bottom of the page when I scroll down. I'm using position absolute not fixed because, I want it to be relative to the navbar.
<header className ={styles.header}>
<span className={styles.header__item} onClick={() => handleTab(1)}>Library</span>
<div className={styles.img} onClick={()=>handleOptions()}>
</div>
<input className={styles.search} type ="text" placeholder="Search"></input>
<PlayerModal open={playerOpen}/>
</header>
.modalStyles{
min-height: 100vh;
width: 26vw;
position: absolute;
left: 74%;
top: 100%;
background-color: whitesmoke;
}
you can use position sticky instead of absolute and set top of this At the bottom of the nav.
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 70px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra,
#wrapper {
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div id="wrapper">
<div id="sticky">
sticky
</div>
</div>
<br />
<div class="extra"></div>
I am new to CSS and HTML, and I am working on my final project for school.
I am trying to absolutely position some text "Welcome" to a div I've made. For some reason it won't position in relation to the div, I've looked it over 10 times and can't figure out why.
I want the "Welcome" text to sit at the bottom of the welcome div, however when I put bottom:0px; into the CSS, it doesn't position according to its parent container and instead goes 0px from the top of the whole screen.
Here's the code:
#wrapper {
height: 1000px;
margin: 0 auto;
background-image: url(images/background.jpg);
background-size: 100% 100%;
}
#header {
height: 150px;
position: relative;
background-color: red;
}
#welcome {
position: absolute;
bottom: 0;
width: 420px;
height: 100px;
background-color: green;
}
.w {
height: 150px;
position: absolute;
font-size: 64px;
left: 20px;
bottom: 0px;
color: #fff;
}
<div id="wrapper">
<header id="header">
<div id="welcome">
<p class="w">Welcome</p>
</div>
<nav id="main nav"></nav>
</header>
</div>
You are very close. Take the height away from the .w p tag and remove its margin as well:
#wrapper {
height: 1000px;
margin: 0 auto;
background-image: url(images/background.jpg);
background-size: 100% 100%;
}
#header {
height: 150px;
position: relative;
background-color: red;
}
#welcome {
position: absolute;
bottom: 0;
width: 420px;
height: 100px;
background-color: green;
}
.w {
/*height: 150px;*/
margin: 0;
position: absolute;
font-size: 64px;
left: 20px;
bottom: 0px;
color: #fff;
}
<div id="wrapper">
<header id="header">
<div id="welcome">
<p class="w">Welcome</p>
</div>
<nav id="main nav"></nav>
</header>
</div>
The problem, as CalvinNunes pointed out, is that you have a height set on .w div. And, p elements have margin and line-height values by default. You need to remove the margin and set the line-height to 1 or less (.5 makes the text touch the bottom of the green box).
#wrapper {
height: 1000px;
margin: 0 auto;
background-image: url(images/background.jpg);
background-size: 100% 100%;
position: relative;
}
#header {
height: 150px;
background-color: red;
position: absolute;
width: 100%;
}
#welcome {
position: absolute;
bottom: 0;
width: 420px;
height: 100px;
background-color: green;
}
.w {
position: absolute;
font-size: 64px;
left: 20px;
bottom: 0px;
color: #fff;
margin: 0;
line-height: 1;
}
<div id="wrapper">
<header id="header">
<div id="welcome">
<p class="w">Welcome</p>
</div>
<nav id="main nav">
</nav>
</header>
</div>
<!-- End of wrapper-->
If you use absolute on something, related dom element should be relative, absolute or fixed, depending on your needs.
Also check if your absolute element doesn't have some unneeded margins etc.
But in your usage case i don't think that there is absolute needed. you can use bigger paddings for parent element top. Also this can be achieved using flex-end, which will allow dynamic text input.