How to set absolute element height 100% og parent [duplicate] - css

This question already has answers here:
::before and ::after position absolute acting like position fixed
(2 answers)
Closed 3 years ago.
I need to change height of absolute element to 100% of parent.
So what i need to do I tried many type of height with padding and line height
div{
height:300px
}
.arrow {
position: absolute;
/* left: 0; */
height: 100%;
display: flex;
align-items: center;
z-index: 102;
width: 100px;
justify-content: center;background: linear-gradient(to left,
rgba(255, 0, 0, 0), rgba(22, 23, 25, 1));
}
<div>
<div class="arrow"> <img src="/path"/> </div>
</div>

Just set position relative in parents div:
div{
height:300px;
position:relative;
}
.arrow {
position: absolute;
/* left: 0; */
height: 100%;
display: flex;
align-items: center;
z-index: 102;
width: 100px;
justify-content: center;background: linear-gradient(to left,
rgba(255, 0, 0, 0), rgba(22, 23, 25, 1));
}

Absolute elements go up the DOM tree and look for the next Element with a position (relative, or absolute) to position themselves.
So you div needs a position: relative; in order for the .arrow to use it for positioning and sizing.

Related

opacity to a div with "background-image" affects the children, I only want to affect the father [duplicate]

This question already has answers here:
How to set opacity in parent div and not affect in child div? [duplicate]
(7 answers)
I do not want to inherit the child opacity from the parent in CSS
(18 answers)
Closed 3 years ago.
I have a div that has a background-image, I would like its hover effect to change its opacity without affecting the child element, in this case the <p>. I have seen similar answers to my question, but having a background does not work the answers. opacity to an element with background affects the children, I only want to affect the father
How can I solve that?
<div class="container_img">
<p>
this is a text
</p>
</div>
.container_img{
position:relative;
border:1px solid red;
margin-top:-14px;
display: table;
margin-top:2px;
width: 709px;
height: 141px;
background-image: url("https://i.imgur.com/VBOZfaY.png");
background-image: url("https://i.imgur.com/VBOZfaY.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
.container_img:hover{
background-color: transparent;
opacity:0.5;
}
this is my code:
https://jsfiddle.net/t98mbxca/
In this case opacity is working as expected as it changes the alpha channel for the element and all of its contents. You may need another approach. Looks like you just want a green gradient button?
I changed the div to a button and removed the p tag in the HTML.
For the background you can use pure CSS to create a linear-gradient as a background-image using rgba for the colors so you can change the alpha (opacity) on hover.
.container_img {
height: 50px;
line-height: 50px;
padding: 0 60px;
border-radius: 10px;
font-size: 20px;
cursor: pointer;
background-image: linear-gradient(rgba(130, 167, 99, 1), rgba(102, 146, 63, 1));
}
.container_img:hover {
background-image: linear-gradient(rgba(130, 167, 99, 0.5), rgba(102, 146, 63, 0.5));
}
<button class="container_img">
this is a text
</button>
The easiest way, if you want to use an actual image, is to move your background image to a pseudo element.
.container_img {
position: relative;
border: 1px solid red;
margin-top: -14px;
display: table;
margin-top: 2px;
width: 709px;
height: 141px;
}
p {
position: absolute;
left: 50%;
font-size: 50px;
}
.container_img::before {
content: '';
background-image: url("https://i.imgur.com/VBOZfaY.png");
background-repeat: no-repeat;
background-size: 100% 100%;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.container_img:hover::before {
opacity: .5;
}
<div class="container_img">
<p>
this is a text
</p>
</div>

How to position an element to the LEFT of an other element?

What is the idiomatic way to position an element (X) to the left of an other element (inline-box 2), independent of its size, using standard CSS & HTML?
It is okay if it appears over other elements.
I have found a solution: to position an element to the left of another one, no matter their sizes, make it its child, and then absolute-position it right: 100%.
100% means the width of its parent, so 100% from the right will put it just left of it!
Using left: -100% wouldn't work, because it means: offset the left of the element left of the parent by the parent's width, however we want to offset by the width of the child element, itself.
CSS-only demo:
/* The important parts */
#box2 {
position: relative;
}
#x {
position: absolute;
right: calc(100% + 5px);
top: -1px;
}
/* Just styling */
#box1, #box2 {
border: 1px solid blue;
width: 200px;
margin-right: 10px;
display: inline-block;
}
#x {
border: 1px solid orangered;
width: 100px;
height: 150px;
}
<div id="container">
<div id="box1">box1</div>
<div id="box2">box2
<div id="x">X</div>
</div>
</div>

Center responsive div with unknown height and enable full vertical scroll

I'm trying to center a div with an unknown height.
I can't find a solution that allows scroll to the top of the div when the viewport height is less than the div height.
HTML
<div>
<p>This will be hidden when <br />
window_height < div_width</p>
<br />
<br />
<br />
How to make it scroll to the top?
</div>
CSS
body {
background: grey;
}
p{
background: green;
}
div {
position: absolute;
left: 50%;
top: 50%;
box-sizing: border-box;
transform: translate(-50%, -50%);
max-width: 500px;
width:100%;
height: 700px; /* Unknown*/
padding: 20px;
background: red;
color: white;
text-align: center;
}
http://codepen.io/Koopa/pen/GpypdX
Thanks
The reason you can't scroll to the top of the div is because the transform property with negative values positions the div off-screen on smaller screens.
In this demo transform is disabled:
http://codepen.io/anon/pen/wKpMyM
Also, when you apply absolute positioning to an element you take it out of the normal flow of the document. This means it is ignored by its container. Hence, the body and html element have zero height.
In this demo the body has a green border (which is totally collapsed):
http://codepen.io/anon/pen/RWxrod
To make your layout work, you can give the body a minimum height (so it can expand along with the div) and, instead of centering with absolute positioning, use a flexbox.
CSS
html { height: 100%; } /* necessary for percentage heights to work */
body {
background: grey;
border: 10px solid green; /* for demo purposes */
min-height: 100%; /* allow body to expand with children */
display: flex; /* establish flex container */
justify-content: center; /* center div horizontally, in this case */
align-items: center; /* center div vertically, in this case */
}
p {
background: green;
}
div {
/* REMOVE
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%); */
box-sizing: border-box;
max-width: 500px;
width:100%;
height: 700px; /* Unknown*/
padding: 20px;
background: red;
color: white;
text-align: center;
}
DEMO: http://codepen.io/anon/pen/OyzMvV
Note that flexbox is supported by all major browsers, except IE 8 & 9.

Z-index not working on pseudo elements

I have looked at other answers but was not able to specifically find an example. If i am using a :before or :after pseudo element as an overlay with an absolute position, is it possible to change the z-index of elements inside the parent container to bring them above the overlay. I have created a basic example, any help would be much appreciated as having a hard time understanding how to get the z-index to work correctly in this situation:
Example of the structure would be as follows:
<div class="page-banner">
<div class="container">
<h2 class="page-title">Shop</h2>
</div>
CSS as follows:
.page-banner {
position: relative;
z-index: 1;
height: 100%;
background-image: url('http://placehold.it/1920x500'); background-size: cover;
}
.page-banner .page-title {
z-index: 150;
}
.page-banner:after {
position: absolute;
left: 0;
content:"";
top: 0;
width: 100%;
background: rgba(0, 0, 0, 0.7);
height: 100%;
z-index: 1;
}
http://jsfiddle.net/webidia/co9u3vqa/1/
Set negative z-index to :after to put in under inner elements (.page-title).
.page-banner:after {z-index: -1}
http://jsfiddle.net/co9u3vqa/5/

Center a position:fixed element

I would like to make a position: fixed; popup box centered to the screen with a dynamic width and height. I used margin: 5% auto; for this. Without position: fixed; it centers fine horizontally, but not vertically. After adding position: fixed;, it's even not centering horizontally.
Here's the complete set:
.jqbox_innerhtml {
position: fixed;
width: 500px;
height: 200px;
margin: 5% auto;
padding: 10px;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="jqbox_innerhtml">
This should be inside a horizontally
and vertically centered box.
</div>
How do I center this box in screen with CSS?
If your div has a known width and height, then you basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.
position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */
Or, if your div has a dynamic/undefined width and/or height, then instead of the margin, set the transform to the negative half of the div's relative width and height.
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Or, if your div has at least a fixed width and you don't care about centering vertically and old browsers such as IE6/7, then you can instead also add left: 0 and right: 0 to the element having a margin-left and margin-right of auto, so that the fixed positioned element having a fixed width knows where its left and right offsets start. In your case thus:
position: fixed;
width: 500px;
margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */
left: 0;
right: 0;
Again, this works only in IE8+ if you care about IE, and this centers only horizontally not vertically.
I want to make a popup box centered to the screen with dynamic width and height.
Here is a modern approach for horizontally centering an element with a dynamic width - it works in all modern browsers; support can be seen here.
Updated Example
.jqbox_innerhtml {
position: fixed;
left: 50%;
transform: translateX(-50%);
}
For both vertical and horizontal centering you could use the following:
Updated Example
.jqbox_innerhtml {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You may wish to add in more vendor prefixed properties too (see the examples).
Or just add left: 0 and right: 0 to your original CSS, which makes it behave similarly to a regular non-fixed element and the usual auto-margin technique works:
.jqbox_innerhtml
{
position: fixed;
width:500px;
height:200px;
background-color:#FFF;
padding:10px;
border:5px solid #CCC;
z-index:200;
margin: 5% auto;
left: 0;
right: 0;
}
Note you need to use a valid (X)HTML DOCTYPE for it to behave correctly in IE (which you should of course have anyway..!)
Add a container like:
div {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
text-align: center;
}
Then put your box into this div will do the work.
Edit: as mentioned in the comments, the inner content needs to be set to display: inline-block assuming there're two divs like:
<div class="outer">
<div class="inner">
content goes here
</div>
</div>
Then the CSS for the inner needs to be:
.outer {
position: fixed;
text-align: center;
left: 0;
right: 0;
}
.inner {
display: inline-block;
}
Together with the outer div having a left: 0; right:0; and text-align: center this will align the inner div centered, without explicitly specifying the width of the inner div.
Just add:
left: calc(-50vw + 50%);
right: calc(-50vw + 50%);
margin-left: auto;
margin-right: auto;
Center fixed position element
(the simple & best way I know)
position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%));
For centering it horizontally & vertically (if height is same as width)
position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%), calc(50vh - 50%));
Both of these approaches will not limit centered element's width less than viewport width, when using margins in flexbox, inside centered element
#modal {
display: flex;
justify-content: space-around;
align-items: center;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
inside it can be any element with diffenet width, height or without.
all are centered.
This solution does not require of you to define a width and height to your popup div.
http://jsfiddle.net/4Ly4B/33/
And instead of calculating the size of the popup, and minus half to the top, javascript is resizeing the popupContainer to fill out the whole screen...
(100% height, does not work when useing display:table-cell; (wich is required to center something vertically))...
Anyway it works :)
left: 0;
right: 0;
Was not working under IE7.
Changed to
left:auto;
right:auto;
Started working but in the rest browsers it stop working!
So used this way for IE7 below
if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {
strAlertWrapper.css({position:'fixed', bottom:'0', height:'auto', left:'auto', right:'auto'});
}
I used vw (viewport width) and vh (viewport height). viewport is your entire screen. 100vw is your screens total width and 100vh is total height.
.class_name{
width: 50vw;
height: 50vh;
border: 1px solid red;
position: fixed;
left: 25vw;top: 25vh;
}
You can basically wrap it into another div and set its position to fixed.
.bg {
position: fixed;
width: 100%;
}
.jqbox_innerhtml {
width: 500px;
height: 200px;
margin: 5% auto;
padding: 10px;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="bg">
<div class="jqbox_innerhtml">
This should be inside a horizontally and vertically centered box.
</div>
</div>
I just use something like this:
.c-dialogbox {
--width: 56rem;
--height: 32rem;
position: fixed;
width: var(--width);
height: var(--height);
left: calc( ( 100% - var(--width) ) / 2 );
right: calc( ( 100% - var(--width) ) / 2 );
top: calc( ( 100% - var(--height) ) / 2 );
bottom: calc( ( 100% - var(--height) ) / 2 );
}
It centers the dialog box both horizontally and vertically for me, and I can use different width and height to fit different screen resolutions to make it responsive, with media queries.
Not an option if you still need to provide support for browsers where CSS custom properties or calc() are not supported (check on caniuse.)
This one worked the best for me:
display: flex;
justify-content: center;
align-items: center;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
To fix the position use this :
div {
position: fixed;
left: 68%;
transform: translateX(-8%);
}
simple, try this
position: fixed;
width: 500px;
height: 300px;
top: calc(50% - 150px);
left: calc(50% - 250px);
background-color: red;
One possible answer:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>CSS Center Background Demo</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
div.centred_background_stage_1 {
position: fixed;
z-index:(-1 );
top: 45%;
left: 50%;
}
div.centred_background_stage_2 {
position: relative;
left: -50%;
top: -208px;
/* % does not work.
According to the
http://reeddesign.co.uk/test/points-pixels.html
6pt is about 8px
In the case of this demo the background
text consists of three lines with
font size 80pt.
3 lines (with space between the lines)
times 80pt is about
~3*(1.3)*80pt*(8px/6pt)~ 416px
50% from the 416px = 208px
*/
text-align: left;
vertical-align: top;
}
#bells_and_wistles_for_the_demo {
font-family: monospace;
font-size: 80pt;
font-weight: bold;
color: #E0E0E0;
}
div.centred_background_foreground {
z-index: 1;
position: relative;
}
</style>
</head>
<body>
<div class="centred_background_stage_1">
<div class="centred_background_stage_2">
<div id="bells_and_wistles_for_the_demo">
World<br/>
Wide<br/>
Web
</div>
</div>
</div>
<div class="centred_background_foreground">
This is a demo for <br/>
<a href="http://stackoverflow.com/questions/2005954/center-element-with-positionfixed">
http://stackoverflow.com/questions/2005954/center-element-with-positionfixed
</a>
<br/><br/>
<a href="http://www.starwreck.com/" style="border: 0px;">
<img src="./star_wreck_in_the_perkinnintg.jpg"
style="opacity:0.1;"/>
</a>
<br/>
</div>
</body>
</html>
Try using this for horizontal elements that won't center correctly.
width: calc (width: 100% - width whatever else is off centering it)
For example if your side navigation bar is 200px:
width: calc(100% - 200px);
This works wonderfully when you don't know the size of the thing you are centering, and you want it centered in all screen sizes:
.modal {
position: fixed;
width: 90%;
height: 90%;
top: 5%; /* (100 - height) / 2 */
left: 5%; /* (100 - width) / 2 */
}
What I use is simple. For example I have a nav bar that is position : fixed so I adjust it to leave a small space to the edges like this.
nav {
right: 1%;
width: 98%;
position: fixed;
margin: auto;
padding: 0;
}
The idea is to take the remainder percentage of the width "in this case 2%" and use the half of it.
Had this problem so I concluded that using a (invisible) container is the best option (based on answer #Romulus Urakagi Ts'ai). To make it with flexbox:
.zoom-alert {
position: fixed;
justify-content: center;
display: flex;
bottom: 24px;
right: 0;
left: 0;
z-index: 100000;
width: 100%;
&__alert {
flex: 0 0 500px;
padding: 24px;
background-color: rgba(212, 193, 105, 0.9);
border: 1px solid rgb(80, 87, 23);
border-radius: 10px;
}
}
(the syntax is SCSS but can be easily modified to pure CSS)
Center element of a div with the property of
position:fixed
Html and Css code
.jqbox_innerhtml {
position: fixed;
width:100%;
height:100%;
display: flex;
justify-content: space-around;
align-items: center;
left: 0;
top: 0;
width: 100%;
height: 100%;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="jqbox_innerhtml">
This should be inside a horizontally
and vertically centered box.
</div>
Another simple solution is to set the width of the element to fit-content and set the left and right to 0px;
width: fit-content;
position: fixed;
left: 0px;
right: 0px;
This is useful if you don't know the width of the element.
The only foolproof solution is to use table align=center as in:
<table align=center><tr><td>
<div>
...
</div>
</td></tr></table>
I cannot believe people all over the world wasting these copious amount to silly time to solve such a fundamental problem as centering a div. css solution does not work for all browsers, jquery solution is a software computational solution and is not an option for other reasons.
I have wasted too much time repeatedly to avoid using table, but experience tell me to stop fighting it. Use table for centering div. Works all the time in all browsers! Never worry any more.

Resources