how to make div overlapped another div using html and css - html

I have two div container, one act as dropdown component, when dropdown clicked, it will not overlapped second component. I am trying using z-index and css position, also it didnt work, try another solution from so and googling but not found the answer, hope anyone can guide this. this is stackblitz I created, what have I tried:
html
<div>
<div class="div1" (click)="selectSavedCard()">
<div *ngIf='!hasSelected'>
<div>
<p>dropdown V</p>
</div>
</div>
<div *ngFor="let card of savedCards">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
<div *ngIf="show">
<div *ngFor="let card of savedCreditCards" (click)="selectDropdownCard(card)">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
<div class="div2">overlapped this div</div>
</div>
css
.div1 {
border: 1px solid black;
z-index: 1;
}
.div2 {
z-index: 2;
}
ts

You want the dropdown of div1 to overlap the div2 right?
There are multiple ways to do this, but the simplest one i can imagine is to simply put a position: absolute;
property on the dropdown div.
<div>
<div class="div1" (click)="selectSavedCard()" [(ngModel)]="selectedCard" ngDefaultControl>
<div *ngIf='!hasSelected'>
<div>
<p>dropdown V</p>
</div>
</div>
<div style="position: absolute;z-index: 100;background: #aaa;width: 100%;" *ngFor="let card of savedCards">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
<div style="position: absolute;z-index: 100;background: #aaa;width: 100%;" *ngIf="show">
<div *ngFor="let card of savedCreditCards" (click)="selectDropdownCard(card)">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
<div class="div2">overlapped this div</div>
</div>

From your stackblitz link behavior I think you just don't want the overlapped div to be pushed down when the dropdown is clicked.
You just need to add a wrapper around your dropdown and set the position of the dropdown content to be absolute.
I will edit your stackblitz app HTML and CSS to show this.
app.component.html
<hello name="{{ name }}"></hello>
<div class="container-div">
<div class="wrapp-dropdown">
<div class="div1" (click)="selectSavedCard()" [(ngModel)]="selectedCard" ngDefaultControl>
<div *ngIf='!hasSelected'>
<div>
<p>dropdown V</p>
</div>
</div>
<div *ngFor="let card of savedCards" class="dropdown-content">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
<div *ngIf="show" class="dropdown-content">
<div *ngFor="let card of savedCreditCards" (click)="selectDropdownCard(card)">
<div>
<p>{{card.viewValue}}</p>
</div>
</div>
</div>
</div>
<div class="div2">overlapped this div</div>
Then I updated your app.component.css to look like this
p {
font-family: Lato;
}
.container-div {
position: relative;
}
.div1 {
border: 1px solid black;
z-index: 3;
position: relative;
}
.wrap-dropdown {
position: relative;
}
.dropdown-content {
position: absolute;
}
b.div2 {
z-index: 2;
}

Related

Why does div display above div with display:flex

I have a div (divA) which has the style "display:flex" and another div (divB) without any positioning styles/properties which displays itself above divA even when divB comes after divA inside my HTML file.
(Someone who is relatively new to CSS so please excuse any poor/confusing terminology)
#login_or_signup {
display: flex;
top: 30%;
position: relative;
}
#terms_conditions {
text-align: center;
}
<div id="welcome_div">
<h1>Hi. Welcome to Website name</h1>
<p>Please Login or Sign up</p>
</div>
<div id="login_or_signup"> <!--(divA)-->
<div id="login_div" class="select_options borders">
<p>Login</p>
</div>
<div id="line"></div> <!-- displays grey line between divs -->
<div id="signup_div" class="select_options borders">
<p>Sign Up</p>
</div>
</div>
<div id="terms_conditions"> <!--(divB)-->
<p>Please view our T&C's</p>
</div>
By using 'top: 30%' you are forcing position of that div.
by using relative top:30% DOES NOTHING. If you used position:absolute it would reposition the element. Your code seems to work just fine.
#login_or_signup {
display: flex;
top: 30%;
position: relative;
}
#terms_conditions {
text-align: center;
}
<div id="login_or_signup">div a <!--(divA)-->
<div id="login_div" class="select_options borders">
<p>Login</p>
</div>
<div id="line"></div> <!-- displays grey line between divs -->
<div id="signup_div" class="select_options borders">
<p>Sign Up</p>
</div>
</div>
<div id="terms_conditions"> <!--(divB)-->
<p>div b</p>
</div>
#login_or_signup {
display: flex;
}
#terms_conditions {
text-align: center;
}
<div id="welcome_div">
<h1>Hi. Welcome to Website name</h1>
<p>Please Login or Sign up</p>
</div>
<div id="login_or_signup"> <!--(divA)-->
<div id="login_div" class="select_options borders">
<p>Login</p>
</div>
<div id="line"></div> <!-- displays grey line between divs -->
<div id="signup_div" class="select_options borders">
<p>Sign Up</p>
</div>
</div>
<div id="terms_conditions"> <!--(divB)-->
<p>Please view our T&C's</p>
</div>
I changed your CSS rules to fix the problem.
Removing position: relative; resolves the positioning issue.

prevent div from collapsing when hiding or removing all child elements

I created a Vuetify app managing some card items. Before adding the actions / buttons I check the User's permissions. If some permissions are missing these buttons will not be rendered. I created an example here
https://codepen.io/anon/pen/RmMRQb?editors=1010
As you can see the second div collapses because no children is rendered. This problem is not related to Vuetify, so I will reproduce it with default HTML / CSS example.
.container {
background: red;
}
.box {
display: inline-block;
height: 32px;
width: 32px;
margin: 5px;
background: blue;
}
.notRendered {
display: none;
}
<div id="app">
<h1>Div with visible elements</h1>
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
<h1>Div with hidden elements</h1>
<div class="container">
<div class="box notRendered">
</div>
<div class="box notRendered">
</div>
<div class="box notRendered">
</div>
</div>
</div>
I don't want the div to collapse. I already found a solution here
JQuery: Prevent div from collapsing when element hides()
but would like to ask if there is a way to achieve it without using some hardcoded heights or selecting the element's height. I don't want to modify Vuetify's native elements, so maybe there is a trick when the action bar is empty (no children got rendered) and the bar would not collapse.
I have added a secondary class for the default/native container. I think this is the best/easiest approach.
.improved-container {
background: red;
min-height: 40px;
}
.box {
display: inline-block;
height: 32px;
width: 32px;
margin: 5px;
background: blue;
}
.notRendered {
display: none;
}
<div id="app">
<h1>Div with visible elements</h1>
<div class="container improved-container">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
<h1>Div with hidden elements</h1>
<div class="container improved-container">
<div class="box notRendered">
</div>
<div class="box notRendered">
</div>
<div class="box notRendered">
</div>
</div>
</div>

Overflow-y and position: absolute cause the offset when scroll bar is not at the top

Here is an example: https://jsfiddle.net/sunvom3a/
I have a list of items.
Basically a container with some text and a dropdown. The idea is when you hover over the text the dropdown should be directly below (kina like a tooltip).
.container {
overflow-y: scroll;
height: 100px;
border: solid 2px green;
}
.popup {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
display: none;
}
.item:hover .popup {
display: block;
}
<div class="container">
<div class="item"><span> Text1</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text2</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text3</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text4</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text5</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text6</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text7</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text8</span>
<div class="popup"></div>
</div>
<div class="item"><span> Text9</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text10</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text11</span>
<div class="popup"></div>
</div>
</div>
The first few elements that fit below the scrollbar of overflow-y (with my resolution these are first 5 items) work great:
but the rest are broken - when the scroll bar is moved this offset is added as a distance between the popup and the text:
Is there any way to get the consistent behavior for all items in the list?
This is exhibited in Chrome and Edge (Firefox works as expected). It is calculating the relative position based on the initial, out-of-view position.
You need to add this...
.item{
position: relative;
}
...to make the absolutely positioned element positioned relative to the hovered item.
Fiddle: https://jsfiddle.net/sunvom3a/1/
The downside to the above solution is that it moves the tooltip inside the container, making a portion of it likely to be out of view. On a side note, I'm not sure this is a great UI anyway since you are covering other options with your tooltip. I would recommend attaching it to the parent of your container (then you don't have to worry about the tooltip being out of view either.
Adding to your snippet...
body{
position: relative;
}
...will always put the tooltip in the top right corner of the body. This would be better done by adding a container for your scrolling container, but this is a mere example.
Fiddle: https://jsfiddle.net/sunvom3a/2/
just change position absolute to relative in popup class
jsfiddle: demo
.container {
overflow-y: scroll;
height: 100px;
border: solid 2px green;
}
.popup {
width: 100px;
height: 100px;
background-color: yellow;
position: relative;
display: none;
}
.item:hover .popup {
display: block;
}
<div class="container">
<div class="item"><span>Text1</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text2</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text3</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text4</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text5</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text6</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text7</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text8</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text9</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text10</span>
<div class="popup"></div>
</div>
<div class="item"><span>Text11</span>
<div class="popup"></div>
</div>
</div>

CSS Nth-Child 3n hierarchy structure

With how my HTML is structure I am having hard time selecting 3n child. It doesn't seem even notice the 3n selector of class heroLetter, but if I use the 1n child selector the code notices the class, but it also selects every div. I am not sure how to call the 3n child selector with this structure of classes I have made.
Code:
.heroLetter {
float: left;
width: 48%;
margin-top: 150px;
text-align: center;
font-size: 600px;
color: #f5543a;
position: relative;
}
.windowWrapper .section .heroLetter:nth-child(3n) {
float: left;
width: 48%;
margin-top: 150px;
text-align: center;
font-size: 200px;
color: #f5543a;
position: relative;
}
<div class="wrapper">
<div id="section1" class="windowWrapper">
<div class="section group">
<h1 class="introH1">
<span class="Grand">GRAND</span>
<span class="Stand">STAND</span>
</h1>
<p class="introP">A new font.</p>
scroll down
</div>
</div>
<div id="section2" class="windowWrapper">
<div class="section group">
<div class="col span_6_of_12">
<h1>STORY</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
G
</div>
</div>
</div>
<div id="section3" class="windowWrapper">
<div class="section group">
<div class="col span_6_of_12">
<h1>PROCESS</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
S
</div>
</div>
</div>
<div id="section4" class="windowWrapper form">
<div class="section group">
<div class="col span_6_of_12">
<h1>BEAM</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
<div class="circle"></div>
a
</div>
</div>
</div>
</div>
</div>
The nth-child refers to children of the same parent only.
You could adjust your code to refer to the outer most common element.
Some css like this should help you:
.windowWrapper:nth-child(4n) .section .heroLetter {}
In this case it's the 4th windowWrapper since it contains your 3rd heroLetter

Set margins of a div based on another div that in not the parent

JSFIDDLE: http://jsfiddle.net/5nd6bexc/1/
I am trying to position a div (the contact us form) next to the contactus button, that is not the parent.
I know how to achieve that if the div is a parent(for instance the contactus is already placed in the page using that)
the best i got is this:
.managed-form {
background-color:#5B8F22;
width: 311px;
height: 369px;
z-index:99;
float: right;
margin: -75% 0% 0% 55%;
position:absolute;
display:none;
}
but it's not right because it positioning according to the parent div
HTML CODE
...........
...........
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 top-area">
<div class="managed-area row">
<div class="block-wrapper sliderblock">
<div class="block">
<div class="owl-carousel">
<div class="item slide">
<img src="/xcf.png" class="slide-image" />
<div class="slide-content">
<h2>Slide 1</h2>
<div class="managed-content">
<p>This is slide 1</p>
</div>
</div>
</div>
<div class="item slide">
<img src="cfv.png" class="slide-image" />
<div class="slide-content">
<h2>Slide 2</h2>
<div class="managed-content">
<p>This is slide 2</p>
</div>
</div>
</div>
</div> <span class="ContactForm" id="ContactForm">CONTACT US ∨ </span>
</div>
<div class="color-stripe color-stripe-2"></div>
</div>
</div>
.............
.............
.............
<div class="managed-form">
<div class="ContactForm-Heading">Contact Us</div>
<div class="ContactForm-Subheading"> <div class="managed-form">
<div class="ContactForm-Heading">Contact Us</div>
<div class="ContactForm-Subheading"></div>
<hr />
<div >
<hr />
<div >
......
</div>
Well, if the button has a fixed width and you want the space between the button and the form to be a fixed width, you just add them. The example code below would leave a 100px space between the 2 elements.
.button: {
position: absolute;
right: 0;
width: 20px;
}
.form {
position: absolute;
right: 120px;
}
On the other hand, if you want the space between the button and the form to be a percentage, you should do the following:
.button: {
position: absolute;
right: 0;
width: 20px;
}
.form {
margin-right: 20%;
position: absolute;
right: 20px;
}