How to absolute position DIV of unknown width - html

My layout consists of 3 DIVs
The first DIVis a wrapper.
The second DIV is centered and uses max-width:980px; Otherwise it defaults to 100% width.
The third DIV is 200px wide and uses absolute position. right:-200pxand top:0px position it next to the first DIV
This layout works perfect but only because the last DIVhas a width of 200px. If that DIV had a variable width I couldn't use right:-200px and it wouldn't place correctly.
So my question is what would I do if the DIV with absolute position had a variable width? How would I place it next to the main DIV?
Here is my code.
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container"></div>
<div class="column_outside"></div>
</div>
</div>
CSS
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
right: -200px;
height: 500px;
width: 200px;
background-color: black;
}
FYI: the outer_container DIV allows column_outside to sit outside the screen if the browser is smaller than 980px wide.

Make it a child of the main and give it left: 100%;
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
left: 100%;
height: 500px;
width: 200px;
background-color: black;
}
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container">
<div class="column_outside"></div>
</div>
</div>
</div>
After a second thought, simply use left: 100% instead of right: -200px;
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
left: 100%;
height: 500px;
width: 200px;
background-color: black;
}
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container"></div>
<div class="column_outside"></div>
</div>
</div>

Very simple:
.column_outside {
right: 0px;
-webkit-transform: translateX(100%);
-moz-transform: translateX(100%);
transform: translateX(100%);
}
Demo https://jsfiddle.net/n4nq6Lxt/
No need to change your HTML structure.

You can use transform: translateX(100%); what it does is to move the element to the right of the amount of the width of the element itself.
right: 0;
transform: translateX(100%);

Related

How to I position something sticky so that it follows the screen in the center vertically

As you can see in my snippet I have almost acheived my desired effect, by using top: 50%;. But is not exactly centered on the screen. If I add transform: translateY(-50%); I do acheive centering, however now the red div is position too far up initially and doesnt go all the way to the bottom. Anyone got any ideas?
(also please note that I cant know the height of this red div because it is supposed to be an image first of all, and second I need it to be responsive meaning the height will change)
* {
margin: 0;
padding: 0;
}
.other_content {
height: 80vh;
width: 100%;
background-color: #eee;
}
section {
background-color: skyblue;
height: 300vh;
width: 100%;
position: relative;
}
.sticky {
position: sticky;
top: 50%;
/* transform: translateY(-50%); *//* DOES NOT WORK */
width: 90%;
margin-left: 5%;
background-color: red;
}
<div class="other_content"></div>
<section>
<div class="sticky">
<p>IMAGE WITH <br> UNKNOWN <br> HEIGHT <br> GOES HERE</p>
</div>
</section>
<div class="other_content"></div>
.sticky {
position: sticky;
top: 45%
/* transform: translateY(-50%); *//* DOES NOT WORK */
width: 90%;
margin-left: 5%;
background-color: red;
}
Just change the top percentage from 50% to 45%;
If I understand your question correctly, can you change the top to 40% to make it higher? 45% (should) perfectly align it in the middle.
* {
margin: 0;
padding: 0;
}
.other_content {
height: 80vh;
width: 100%;
background-color: #eee;
}
section {
background-color: skyblue;
height: 300vh;
width: 100%;
position: relative;
}
.sticky {
position: sticky;
top: 45%;
/* transform: translateY(-50%); *//* DOES NOT WORK */
width: 90%;
margin-left: 5%;
background-color: red;
}
<div class="other_content"></div>
<section>
<div class="sticky">
<p>IMAGE WITH <br> UNKNOWN <br> HEIGHT <br> GOES HERE</p>
</div>
</section>
<div class="other_content"></div>

hide the images going outside the div

bgpattern, illustratewoman class images should be hidden outside the div but the image is visible outside the card div also. For me, the images only should be displayed inside the div but not outside. Box class image should display on the inner and outer side of the div.
CSS even though I tried overflow: hidden for card class but it hides all the images going outside but for me, box image should not be hidden I tried to add for bgpatternimg, illustratewoman it doesn't work.
card {
position: absolute;
background-color: white;
height: 70vh;
width: 130vh;
border-radius: 3%;
position: relative;
margin: auto;
top: 15vh;
}
.bgpatternimg {
overflow: hidden;
position: absolute;
left: -70vh;
top: -30vh;
width: 120vh;
}
.illustratewoman {
overflow: hidden;
position: absolute;
bottom: 10vh;
left: -10vh;
width: 60vh;
height: 50vh;
}
.boximg {
position: absolute;
width: 22vh;
bottom: 4vh;
left: vh;
overflow: visible;
}
<div class="card">
<div class="left">
<div class="bgpattern">
<img class="bgpatternimg" src="images/bg-pattern-desktop.svg" alt="bgpattern">
</div>
<div class="illustratewoman">
<img class="illuswoman" src="images/illustration-woman-online-desktop.svg" alt="illustratewoman">
</div>
<div class="box">
<img class="boximg" src="images/illustration-box-desktop.svg" alt="">
</div>
</div>
you're positioning absolute which means it'll make the position relative to the nearest static/relative positioned parent or grandparent. you're better off using another strategy without absolute positioning, then make your widths and heights 100%
change style file
hello you should change the style to this code
.card{
position:absolute;
background-color: white;
height:70vh;
width:130vh;
border-radius: 3%;
position:relative;
margin:auto;
top:15vh;
}
.bgpattern {
overflow: hidden;
position: absolute;
left: -70vh;
top: -30vh;
width: 120vh;
}
.illustratewoman {
width: 60vh;
position: absolute;
bottom: 10vh;
left: -10vh;
overflow: hidden;
}
.box {
position: absolute;
width: 22vh;
bottom: 4vh;
left: 28vh;
}

Aligning position absolute div to middle?

I have a parent div and a child div. The child div has the position: absolute property. It is already centered, but I'd like to align it to the middle of the parent div. How do I go about doing that? Here's my jsFiddle
HTML
<div id='parent'>
<div id='child'>
</div>
</div>
CSS
#parent {
position: relative;
width: 500px;
height: 300px;
background-color: red;
}
#child {
position: absolute;
width: 70px;
height: 70px;
background-color: blue;
left: 0;
right: 0;
margin: 0 auto;
border-radius: 50%;
}
The solution is to use transform: translate(-50%, -50%) on the child div, like so:
#child {
position: absolute;
width: 70px;
height: 70px;
background-color: blue;
left: 50%;
top: 50%;
border-radius: 50%;
transform: translate(-50%, -50%);
}
https://jsfiddle.net/jwoy7rxr/
This works because the transform positions the item based on a percentage from it's own point of origin.
Since the parent has a height based on px, you can safely use a simple margin top and bottom to centre the element.
#parent {
position: relative;
width: 500px;
height: 300px;
background-color: red;
}
#child {
position: absolute;
width: 70px;
height: 70px;
background-color: blue;
left: 0;
right: 0;
margin: 115px auto;
border-radius: 50%;
}
Here's the fiddle: https://jsfiddle.net/Lr3fLser/
You need to give the parent:
#parent {
width: 500px;
height: 500px;
background-color: red;
display: table-cell;
vertical-align: middle;
}
#child {
width: 70px;
height: 70px;
background-color: blue;
border-radius: 50%;
}
You need the display table-cell in order to use the vertical-align.
Then add align="center" to the parent div's:
<div align="center" id="parent">
<div id='child'>
</div>
</div>
I have the updated JSFiddle attached:
https://jsfiddle.net/o7pzvtj3/2/

Fixed div stuck into parent - CSS

As you can see from this fiddle:
http://jsfiddle.net/t1h3aauh/2/
I'm through a problem that I've never been before. I'm working with Drupal CMS and it generates a lot of the markup you need to style.
Given the use case, I have a MODAL box that are wrapped into a lot of divs and, like all MODALS, it need to be FIXED positioned. But, when I do this, the behavior is very much like absolute positioning. It get stuck in place and inherit all the .wrap div dimensions.
Thanks for the help.
Edit
The code:
HTML
<header class="sticky">log and menu</header>
<main>
<section class="test">
<div class="wrap">
<div class="myEl">
<!--HERE BE SOME SCROLLABLE ELEMENTS-->
<div.class="iWannaBeScrollable">i'm scrollable</div>
<!--HERE BE THE FIXED ONE-->
<div class="modal">as you can see, this should be FIXED, but appears to be stuck into the parent.</div>
</div>
</div>
</section>
</main>
CSS
body{
margin: 0;
padding: 0;
}
.sticky{
float: left;
position: fixed;
height: 40px;
width: 100%;
background-color: green;
left: 0;
top: 0;
z-index: 10;
}
.wrap {
background-color: #333;
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-ms-transform: translateX(-50%);
-o-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
height: auto;
max-width: 1200px;
float: left;
position: relative;
left: 50%;
}
main {
height: 1535px;
width: 100%;
float: left;
position: relative;
.test {
width: 100%;
height: auto;
padding: 72px 0;
float: left;
position: relative;
&::before{
content: "";
width: 100%;
height: 82%;
top: 21%;
background-color: #fafafa;
position: absolute;
}
.myEl{
float: left;
position: relative;
height: 300px;
width: 100%;
.modal {
position: fixed;
height: 100%;
width: 100%;
background-color: rgba(255,0,255,.5);
left: 0;
top: 0;
z-index: 100;
}
}
}
}
Why is there
transform: translateX(-50%);
on the .wrap element?
I think thats causing your problem...
IIRC: In my Drupal days, anytime you needed to fix an element, you wanted to call the top most parent.
So in this case, you would want to apply postioned:fixed it to .wrap.
.wrap{
position:fixed;
}
Here's why:
You are styling Modal with the fixed, so it is staying fixed within it's parent. You're parent/grandparent is styled as position:relative. Which makes .wrap and .myEl scrollable, while the modal is fixed within the scrollable div.
This makes it appear as if it is position:absolute;

How to resize to fit and center image in a position absolute div?

I need an image to be resized to fit in inside a div. This div must, necessarely, no matter what, be an position: absolute; div. Apart from the image have 100% from its greatest dimension, it should be centered in the other way.
I could resize to fit it, but can't center. I tried to make it inline and use vertical-align, but it didn't work.
Since code worth more than words, check my fiddle example.
This is the code from the jsfiddle:
CSS:
.relative {
width: 400px;
height: 400px;
position: relative;
<!-- Next is not important, only to display better -->
display: block;
background-color: green;
border: 3px solid yellow;
margin-bottom: 10px;
}
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
}
img {
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}
HTML:
<div class="relative">
<div class="absolute">
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"/>
</div>
</div>
<div class="relative">
<div class="absolute">
<img src="http://us.123rf.com/400wm/400/400/pashok/pashok1101/pashok110100126/8578310-vertical-shot-of-cute-red-cat.jpg"/>
</div>
</div>
you may put the image to background instead of an img tag.
<div class="absolute">
<img src="//upload.wikimedia.org/wikipedia/commons/5/52/Spacer.gif">
</div>
.absolute {
background-image: url(http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
however, if you can set a fixed height for the div, you can use this:
.absolute { line-height:360px; }
.absolute img { vertical-align:middle; }
Only for semi-new browsers:
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Absolutely position all the things!
transform still needs browser prefixes I hear. -webkit- works for me.
http://jsfiddle.net/rudiedirkx/G9Z7U/1/
Maybe I did not understand the question…
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
line-height:350px; //new
}
img {
position:relative;
display:inline-block; // new
vertical-align:middle; // new
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}