Position fixed relative to fixed parent - html

I have a fixed positioned DIV called Lightbox. My problem is that the close button doesn't stay on the top right, when I scroll the content.
How can I achieve that the close button stays on the top right corner?
Fiddle Link
.lightbox {
position: fixed;
background: white;
top: 50%;
left: 50%;
width: 600px;
height: 400px;
border: 1px solid black;
margin-left: -300px;
margin-top: -200px;
z-index: 10000;
overflow-y: auto;
}
.close-btn {
position: absolute;
top: 0;
right: 0;
background: black;
color: white;
font-weight: bold;
border-radius: 100%;
width: 20px;
height: 20px;
text-align: center;
}
.item {
width: 250px;
display: inline-block;
border: 1px solid blue;
height: 300px;
background: lightblue;
}
<div class="lightbox">
<div class="close-btn">x</div>
<div class="items">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
</div>

Make the item div scrollable instead of the lightbox, then the close button will stay absolutely positioned in the top right corner.
Here is the CSS that I changed:
.lightbox {
overflow: hidden;
}
.close-btn {
top: 5px;
right: 20px;
}
.items {
width: 600px;
height: 400px;
overflow-y: auto;
}
JSFiddle: http://jsfiddle.net/dgw8tj5r/4/

You can achieve a sticky button to your lightBox div by adjusting your HTML a bit and adding a container to your lightBox content:
<div class="lightbox">
<div class="close-btn">x</div>
<div class="container">
<div class="items">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
</div>
</div>
Then instead of the .lightbox div, you add your width, height, overflow properties on this new .container.
.container{
overflow-y: auto;
width: 600px;
height: 400px;
}
Now your close-btn will not be included in the scrolling part.
JSFiddle demo
EDIT: Benjamin's answer is a more efficient version since you actually already have a containing div: .items. You can use that instead of adding a new one.

Draw light box with proper position.
2.Add close button and container inside light box ans close button position where you need.
3.Finally added items inside container.
thats it.
thanks.
Fixed here
<div class="lightbox"><div class="close-btn">x</div>
<div class="lightboxdv">
<div class="items">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
</div>
</div>
.lightbox {
position: fixed;
background: white;
top: 50%;
left: 50%;
border: 1px solid black;
margin-left: -300px;
margin-top: -200px;
z-index: 10000;
overflow:hidden;
}
.lightboxdv{
width: 560px;
height: 400px;
overflow-y: auto;
}
.close-btn {
position: absolute;
top: 0;
right: 20px;
background: black;
color: white;
font-weight: bold;
border-radius: 100%;
width: 20px;
height: 20px;
text-align: center;
}
.item {
width: 250px;
display: inline-block;
border: 1px solid blue;
height: 300px;
background: lightblue;
}

All you need to do is change .close-btn position to fixed.
.close-btn {
position: fixed;
}
I hope it works!

Related

add div in fixed position with 100% height

I need to add two div in top and bottom of fixed div,so I create fixed position panel in left, then add first div with h-100 class(height 100%). But Now, when I add second div in panel, I cant see this div in result.
.vertical-side {
width: 250px;
z-index: 1001;
background: #fbfaff;
bottom: 0;
margin-top: 0;
position: fixed;
top: 0;
border-right: 1px solid #e9e9ef;
}
.second {
width: 250px;
height: 100px;
border-top: 1px solid #e9e9ef;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="vertical-side">
<div class="first h-100">
First div
</div>
<div class="second">
second div
</div>
</div>
Demo HERE
I nee to this:
You can do this with flex utilities. Demo: here
.vertical-side {
width: 250px;
z-index: 1001;
background: #fbfaff;
bottom: 0;
margin-top: 0;
position: fixed;
top: 0;
border-right: 1px solid #e9e9ef;
}
.second {
width: 250px;
height: 100px;
border-top: 1px solid #e9e9ef;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="vertical-side d-flex flex-column">
<div class="first h-100 text-center">
First Div
</div>
<div class="second d-flex align-items-center justify-content-center">
Second Div
</div>
</div>
I think this is what your expecting, as per your description, you can adjust the height & width as per your need
.vertical-side {
width: 250px;
z-index: 1001;
background: #fbfaff;
bottom: 0;
margin-top: 0;
position: fixed;
top: 0;
border-right: 1px solid #e9e9ef;
}
.first {
width: 250px;
height: 100px;
background:blue;
border-top: 1px solid #e9e9ef;
}
.second {
width: 250px;
height: 100px;
background:Red;
border-top: 1px solid #e9e9ef;
}
<div class="vertical-side">
<div class="first">
First Div
</div>
<div class="second">
Second Div
</div>
</div>
Your 2nd div is under your first div, which is 100% of its parent height. that means the 2nd div is out of the viewport. use flex/grid for your parent div (vertical-side). you'll find the 2nd div. use a height for your first div. that will also solve the issue.
In HTML, You are basically building in layers - So your second div was butted up against your first div, whereas you wanted it at the bottom. By putting in a third one in between the two as you can see with my spacer div it essentially provides you with margin between your top and bottom divs that you can still use and work with later on.
CSS
.vertical-side {
width: 250px;
z-index: 1001;
background: #fbfaff;
bottom: 0;
margin-top: 0;
position: fixed;
top: 0;
border-right: 1px solid #e9e9ef;
}
.top {
width: 250px;
height: 100px;
border-bottom: 1px solid #e9e9ef;
}
.spacer {
height: 45%;
}
.bottom {
margin-top: 100%;
width: 250px;
height: 100px;
border-top: 1px solid #e9e9ef;
}
HTML
<div class="vertical-side">
<div class="top">
<h1>First div</h1>
</div>
<div class="spacer">
<h3>Spacer</h3>
</div>
<div class="bottom">
<h1>second div</h1>
</div>
</div>
Image of how it looks
Edit any specifics you need/want
you have in class .vertical-side : bottom: 0; and top: 0;
remove bottom: 0; perhaps this helps
alternatively you can use grid and change the height here: grid-template-rows: 1fr 60px;
or grid-template-rows: 1fr 1fr for automatic height.
.vertical-side {
display: grid;
grid-template-rows: 1fr 60px;
width: 250px;
height: 100vh;
}
.first {
display: grid;
align-items: center;
justify-content: center;
background: grey;
}
.second {
display: grid;
margin-top: 2px;
align-items: center;
justify-content: center;
background: green;
}
<div class="vertical-side">
<div class="first">
first
</div>
<div class="second">
second
</div>
</div>

How can I center this image inside of this div?

How can I center this image that I have in this div in a way that it won't move the 'line' div? I want the line to be touching the top of the square too.
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
Here is one way to prevent it from disrupting the flow layout of your container:
you can make the container a position of relative, and the image a position of absolute, positioned off the top and left by 50%, then transform it so that the center of the image is in the center position.
You could also just make the image a background-image of the div instead of using an image element, which may be easier to manipulate.
.black {
background: black;
}
.square {
position: relative;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
</div>
I'm not sure I understand your exact desired end goal. But, if I understand correctly, you could create a flex parent to justify the image, and then position the line absolutely within that. See -
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
background-color: red;
position: absolute;
left: 0;
top: 0;
bottom: 0
}
<div class="square black">
<div class="line"></div>
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
You can just use these css for .square and .image
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
position: relative;
}
.image {
height: 60px;
width: 60px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You can easily center a image by using CSS position absolute. By making the position of square black class "absolute" and apply to properties "top: 45%;" and "left: 47%" . By applying this your problem will be definitely solve.
.black {
background: black;
}
.square {
display: flex;
align-item: center;
justify-content: center;
width: 200px;
height: 500px;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
</div>
.black {
background: black;
}
.square {
position: absolute;
top: 45%;
left: 47%;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top:50%;
left:50%;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>

How to Center a Group of Relative Positioned Divs Inside another Relative Positioned Div?

I'm trying to center a group of relative positioned divs dynamically inside of a parent div that is also relative positioned.
The parent div is 620px wide, and the child divs are each 200px wide. There can be 1 to 3 child divs per line, thus what I meant by trying to center the group of child divs within the parent div dynamically. For example, if there is only 1 child div, that child div should be centered in the parent div, or if there are only 2 child divs, those child divs should be centered in the parent div.
I would use inline block for the child divs, however inside the child divs there are also divs that are absolute positioned to the child divs, so inline-block wouldn't work for the absolute positioning.
This is my css, or you can see a working example here: https://jsfiddle.net/y3ghpkvs/
.parentClass {
position: relative;
width: 620px;
height: 500px;
background-color: gray;
}
.childClass {
position: relative;
width: 200px;
height: 400px;
float: left;
margin-left: 5px;
background-color: white;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
I can't seem to figure out how to center the 2 childClass divs inside the parentClass div. Anyone have any tips?
Solution 1: Using Flex
Try to use flex css. Using flex it will be easy for you align the items vertically or horizontally center to the parent.
Use justify-content: center; to align the divs center.
Updated Fiddle
.parentClass {
width: 620px;
background-color: gray;
display: flex;
justify-content: center;
margin: auto;
flex-wrap: wrap;
}
.childClass {
width: 200px;
height: 400px;
background-color: white;
position: relative;
margin: 3px;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
<div class="parentClass">
<div class="childClass">
<div class="insideChildDiv1">George</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">CEO</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
</div>
Solution 2: Using display inline-block
.parentClass {
width: 620px;
background-color: gray;
margin: auto;
text-align: center;
}
.childClass {
width: 200px;
height: 400px;
background-color: white;
position: relative;
margin: 4px 0;
display: inline-block;
margin-left: 2px;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
<div class="parentClass">
<div class="childClass">
<div class="insideChildDiv1">George</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">CEO</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
</div>
I hope this will help you!

CSS overflow and white-space not working

Current Situation
Using the following code I show a couple of divs floated to the left.
* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
}
.container {
width: 100%;
height: 100%;
}
.header {
height: 80px;
position: fixed;
width: 100%;
background: green;
}
.inner-container {
position: absolute;
top: 80px;
left: 0px;
right: 0px;
bottom: 0px;
overflow: auto;
white-space: nowrap;
}
.column {
height: 500px;
width: 150px;
background: red;
float: left;
margin: 5px;
}
<div class="container">
<div class="header">
</div>
<div class="inner-container">
<div class="column">
</div>
<div class="column">
</div>
<div class="column">
</div>
</div>
</div>
Current result:
Problem
What I want is that the red boxes don't wrap within its container. I want both, a vertical and horizontal scroll bar if the space is not enough. For the vertical scrollbar it works. What am I missing?
JSFiddle: https://jsfiddle.net/brainchest/j6zh400v/
A fix I found was to change the .column from being a float: left to display: inline-block. This treats each column as a "word" (like a word in text) and thus the white-space: no-wrap; applies. Otherwise, the float: left changes the way the element gets positioned.
Edited Fiddle: https://jsfiddle.net/9bo4f5pv/
Use display: flex on the parent, then flex: 0 0 150px on the columns.
* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
}
.container {
width: 100%;
height: 100%;
}
.header {
height: 80px;
position: fixed;
width: 100%;
background: green;
}
.inner-container {
position: absolute;
top: 80px;
left: 0px;
right: 0px;
bottom: 0px;
overflow: auto;
display: flex;
}
.column {
height: 500px;
flex: 0 0 150px;
background: red;
margin: 5px;
}
<div class="container">
<div class="header">
</div>
<div class="inner-container">
<div class="column">
</div>
<div class="column">
</div>
<div class="column">
</div>
</div>
</div>

Positioning the content of a div on another div

I don't understand why the float: right doesn't work on the other box.
Anyone who can help me about this?
This is my code:
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
This is the resulting image of my code:
This is the resulting image I want to achieve:
Because of position: absolute on bottom-boxes so you need to add width: 100%
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
width: 100%;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
But here is better solution using flexbox
.main-box {
height: 200px;
width: 200px;
display: flex;
flex-direction: column;
justify-content: space-between;
background: black;
}
.row {
display: flex;
justify-content: space-between;
}
.box {
width: 50px;
height: 50px;
background: red;
}
<div class="main-box">
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
</div>
Here's a working fiddle
When you put absolute position on a container, you have to specify also top, right and left property with bottom property to set a width and a height of it.
.bottom-boxes{
position:absolute;
bottom: 0;
left: 0;
right: 0;
}
In this case, left: 0; and right: 0; are equivalent to width: 100%; and top: 0 and bottom: 0; are equivalent to height: 100%;
When you don't specify a value, by default it's "auto;"
float won't work on an absolutely positioned element - you need to give top or bottom and right or left parameters to it (the default setting is top: 0; and left: 0;, i.e. the upper left corner of the parent element).