I've stuck at the problem with this
.autoHeightParent {
width: 500px; /* just for example */
position: relative;
}
.absolutePosChild {
/* absolute is required to make pretty drag and drop transitions with interpolating left property */
position: absolute;
display:flex;
flex-direction: column;
border: 1px solid green;
}
.absolutePosChild div {
width: 100%;
}
<div class="autoHeightParent" style="height:170px;">
<div class="absolutePosChild" style="left: 0px; width: 100px;">
<div>
Child content
</div>
</div>
<div class="absolutePosChild" style="left: 100px; width: 150px;">
<div>
Child content with nesting
</div>
<!-- !! NESTING -->
<div class="autoHeightParent">
<div class="absolutePosChild" style="left: 0px; width: 50px;">
<div>
We don't know this height too
</div>
</div>
<div class="absolutePosChild" style="left: 50px; width: 100px;">
<div>
G.Child 2
</div>
</div>
</div>
</div>
</div>
<div style="border: 1px solid red">
I need to automatcally calculate height of the box above to make THIS box right after it. For now you can see hardcoded 170px height.
</div>
How do you guys solve problems like this?
I have tried to solve it with JavaScript but my solution looks wrong.
function setRootHeight() {
var root = document.getElementById("root");
var allChilds = root.getElementsByTagName("*");
var rootRect = root.getBoundingClientRect();
var maxChildHeight = 0;
_.each(allChilds, function(item) {
var nodeRect = item.getBoundingClientRect();
var nodeHeightRelativeToRoot = ( nodeRect.top + nodeRect.height ) - rootRect.top;
if ( nodeHeightRelativeToRoot > maxChildHeight) maxChildHeight = nodeHeightRelativeToRoot;
});
root.style.height = maxChildHeight + "px";
}
setInterval(setRootHeight, 300);
.autoHeightParent {
width: 500px; /* just for example */
position: relative;
}
.absolutePosChild {
/* absolute is required to make pretty drag and drop transitions with interpolating left property */
position: absolute;
display:flex;
flex-direction: column;
border: 1px solid green;
}
.absolutePosChild div {
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
Test content Above
<div id="root" class="autoHeightParent">
<div class="absolutePosChild" style="left: 0px; width: 100px;">
<div>
Child content
</div>
</div>
<div class="absolutePosChild" style="left: 100px; width: 150px;">
<div>
Child content with nesting
</div>
<!-- !! NESTING -->
<div class="autoHeightParent">
<div class="absolutePosChild" style="left: 0px; width: 50px;">
<div>
We don't know this height too
</div>
</div>
<div class="absolutePosChild" style="left: 50px; width: 100px;">
<div>
G.Child 2
</div>
</div>
</div>
</div>
</div>
<div style="border: 1px solid red">
Visualization of that height was calculated
</div>
It uses setInteval to check if any of nested children changed it's height and then recalculate height of the parent box. Actually i need to find better solution because of performance and code beauty.
Snippets very simplified compared to real recalculations.
I am free to use flexbox or any tool that can solve this
I've solved this issue with relative positioning.
For my complex "absolute position" animations i made function that translates absolute to relative.
.autoHeightParent {
width: 500px; /* just for example */
position: relative;
display: flex;
}
.absolutePosChild {
/* relative position here must do absolutes' job here */
position: relative;
display:flex;
flex-direction: column;
border: 1px solid green;
}
.absolutePosChild div {
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="autoHeightParent">
<div class="absolutePosChild">
<div>
Child content
</div>
</div>
<div class="absolutePosChild">
<div>
Child content with nesting
</div>
<!-- !! NESTING -->
<div class="autoHeightParent">
<div class="absolutePosChild">
<div>
We don't know this height too
</div>
</div>
<div class="absolutePosChild">
<div>
G.Child 2
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You see that parent box automatically resizes fine.
But now i can use translate3d to any child and parent box keeps it's size and position.
Related
In col-2, I am trying to center the "WHAT" text over the image.
<div class="row">
<div class="col-2">
<div class="stackParent">
<img class="stack-Img" src="img/base.png">
<div class="stack-Txt">
<div class="fourWsText stack-Txt-child">WHAT</div>
</div>
</div>
</div>
<div class="col-10">
...Variable length content...
</div>
</div>
As we resize the browser, the row height will change so that everything in col-10 fits. I am using object-fit:contain to keep the image proportions correct. I need the "stack-Txt" div to resize with the image so that the text will stay centered over the image
Or maybe you know of a better way to achieve this?
Current Idea:
Put WHAT in another child div, make flex, center text with justify-content/align-items
&
JS addEventListener to window resize, get the img div and set nextSibling size equal to this.
These were the css classes at some point... but the only important thing is that the img doesn't overflow the col-2 or the height (which is variable based on the length of col-10)
.stackParent {
position: relative;
object-fit: contain;
max-width: inherit;
height: 20vh;
}
.stack-Txt {
position: absolute;
text-align: center;
width: 100%;
max-height: 15vh;
min-height: 15vh;
z-index: 4;
}
.stack-Txt-child {
}
.stack-Img {
position: absolute;
object-fit: contain;
max-width: inherit;
min-height: 15vh;
max-height: 15vh;
top: 0%;
left: 0%;
z-index: 3;
}
*Note: I also have media queries to resize text, but this shouldn't make a difference.
If you are using Bootstrap 5 you can just use the built-in classes to achieve so.
Apply .position-relative to the main div parent
Apply .img-fluid to <img /> to make image responsive
Apply .position-absolute, .top-50, .start-50, translate-middle to stack-Txt to center it vertically and horizontally over the image.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="row">
<div class="col-2">
<div class="stackParent position-relative">
<img class="stack-Img img-fluid" src="https://source.unsplash.com/random">
<div class="stack-Txt position-absolute top-50 start-50 translate-middle">
<div class="fourWsText stack-Txt-child text-white">WHAT</div>
</div>
</div>
</div>
<div class="col-10">
...Variable length content...
</div>
</div>
If you don't, just edit your CSS as follow:
.stackParent {
position: relative
}
/* make image responsive */
.stack-Img {
max-width: 100%;
height: auto;
}
.stack-Txt {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: white;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="row">
<div class="col-2">
<div class="stackParent">
<img class="stack-Img" src="https://source.unsplash.com/random">
<div class="stack-Txt">
<div class="fourWsText stack-Txt-child">WHAT</div>
</div>
</div>
</div>
<div class="col-10">
...Variable length content...
</div>
</div>
The text "what" is in center over the image in any screen resolution.
.stackParent{
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
.stack-Img{
position: absolute;
margin: 0;
min-height: 15vh;
max-height: 15vh;
}
.stack-Txt{
z-index: 99;
}
<div class="row">
<div class="col-2">
<div class="stackParent">
<img class="stack-Img" src="img/base.png">
<div class="stack-Txt">
<div class="fourWsText stack-Txt-child">WHAT</div>
</div>
</div>
</div>
<div class="col-10">
...Variable length content...
</div>
</div>
I want to create a web site that contains a network graph. I used vis.js for this. If you hover over an image of a person the image should flip and show some information on the background. I created a JavaScript file that creates a tree out of a json file dynamically. Here is a picture of the result:
As you can see the images are visible outside the box which should not be the case. They should not be displayed outside of the borders.
Here is my html-code:
<div id="fullpage">
<div class="section " id="section0">
...
</div>
<div class="section" id="section1">
...
</div>
<div class="section" id="section2">
<div class="intro">
...
</div>
<div id="media">
...
</div>
</div>
<div class="section active" id="section3">
<div class="slide" id="slide1">
<div id="network_container_1">
<div id="network"></div>
</div>
</div>
<div class="slide active" id="slide2">
<div id="network_container_2">
</div>
</div>
</div>
Here is the CSS:
#network{
width: 100%;
height: 100%;
left: 0;
top: 0;
}
#network_container_bewohner{
width: 100%;
height: 100%;
left: 0;
top: 0;
border:1px black solid;
}
#network_container_freunde{
width: 100%;
height: 100%;
left: 0;
top: 0;
}
.overlay {
position: absolute;
padding: 4px;
z-index: 0;
}
#slide1{
z-index:0;
}
#slide2{
z-index:1;
}
The images are located as #block1, #block2, #block3, etc. as class overlay inside of #network_container_1.
As you can see I tried to mess around with the z-index but it doesn't work.
Okay I found the solution. The problem was the absolute position.
See here: Position absolute and overflow hidden
I am currently working on an application where I have a notification panel with different cattegories. The categories are divided by images and dashed lines, but how can I set the height of that dashed line to auto so it changes its height automatically when an new notification is added.
Here is a FIDDLE which represents the problem.
Normally I have this as my html:
<div class="departure-wrapper">
<div class="ui-grid-a">
<div class="ui-block-a">
<span class="departure-img">
<img class="image" src="img/icons/car.png">
</span>
</div>
<div class="ui-block-b">
<h4>Vertrek</h4>
<div class="departure-notification-append"></div>
</div>
</div>
</div>
Where I append with js the new added notifications to the departure-notification-append class
if (category === 'vertrek'){
departureHtml = `
${departureHtml}
<div class='notification-item' style='${style}'>
<div class='ui-grid-a notification-grid'>
<div class='ui-block-a'>
<img class='notification-image' id='${id}' src='${imgPath}'>
</div>
<div class='ui-block-b'>
<span class='notification-text'>${shortmessage}</span>
</div>
</div>
</div>`;
$('.departure-notification-append').empty().prepend(departureHtml);
}
So my question is how can I set the dashed lines to height auto, when I do now the height of the dashed line is 1px or something. I'll hope someone can help me out on this.
use :afrer on parent div like below
.departure-wrapper:after {
content: '';
display: block;
position: absolute;
top: 88px;
left: 60px;
width: 0;
height: 100%;
border: 1px dashed white;
}
try this http://jsfiddle.net/7uygyvwo/
I have these 3 container divs and inside it textdiv is floated left and image div is floated right. I want to set the height of the image as per the height of the text. Is there anyway to do this using jQuery? Or should I give different class names to each div and set the height?
<div class='containerr'>
<div class='textdiv'></div>
<div class='imagediv'></div>
</div>
<div class='containerr'>
<div class='textdiv'></div>
<div class='imagediv'></div>
</div>
<div class='containerr'>
<div class='textdiv'></div>
<div class='imagediv'></div>
</div>
try using this snippet
$('.containerr').each(function(){
$this = $(this);
var textHeight = $this.find(".textdiv");
var imageHeight = textHeight.height();
$(".imagediv").css("height", imageHeight);
});
CSS3 Flexbox can help with difficult layout challenges that were once difficult or impossible with floats alone:
.container {
display: flex;
flex-wrap: wrap;
}
.textdiv, .imagediv {
display: flex;
}
HTML
<div class="containerr">
<div class="textdiv"><p>Smapmle text.</p></div>
<div class="imagediv"></div>
</div>
<div class="containerr">
<div class="textdiv"><p>Smapmle text.</p><p>Smapmle text.</p></div>
<div class="imagediv"></div>
</div>
<div class="containerr">
<div class="textdiv"><p>Smapmle text.</p></div>
<div class="imagediv"></div>
</div>
CSS
.containerr {
display:block;
width:100%;
border: 1px solid black;
position: relative;
}
.textdiv {
display: inline-block;
width: 50%;
border-right: 1px solid blue;
}
.imagediv {
position: absolute;
display:block;
right:0;
top:0;
width: 50%;
height: 100%;
background-color:red;
}
I've been messing with this code for awhile now and I'm stuck. I've done a lot of coding by hand but just now ramping up on the new things with HTML5, CSS3, and Javascript.
I'm trying to make a simple menu that slides up and down. I have a div that contains everything. The div has overflow set to hidden so when the menu is out of the viewing box, it can't be seen. It can then animate in and out of the view box.
I've been trying to do it with a Javascript function but it sounds like it's easier to just use CSS3 transitions? Any advice?
My Javascript code is below. I can't quite figure out how to do it with CSS3 transitions. Any advice would be much appreciated.
Html
<header>
<nav>
<div id="mobileMenu" class="mobileMenu">
<div id="mobileMenuWrapper" class="mobileMenuWrapper">
<div style="height: 100px; width: 100%;">
<div style="height: 100px; width: 100%; background-color: black; color: white;">
Menu option<br>Menu option<br>Menu option
</div>
</div>
<div style="position: relative; height: 50px; width: 100%; left: 50px;">
<div style="height: 50px; width: 125px; background-color: black; color: white; text-align: center;">Menu</div>
</div>
</div>
</div>
</nav>
</header>
Javascript
var startPosition = -100;
var endPosition = 0;
var speed = 2;
function moveMenuDown(){
// Get the element
menu = document.getElementById("mobileMenuWrapper");
// Grab the element's current CSS top position
currentPosition = Number(menu.style.top.substr(0,(menu.style.top.length-2)));
// Compare the position and move it
if(currentPosition <= endPosition){
// I'm stuck about the line below...how can I attach a CSS3 transition here? Or should I?
menu.style.MozTransition = ???;
// Here's my original code where I move the element manually
menu.style.top = (currentPosition + speed) + 'px';
moveMenuDown();
}else{
}
}
Updated entire HTML/CSS/JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Menu test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
.mobileMenu {
position: absolute;
height: 150px;
width: 250px;
top: 0px;
left: 50px;
overflow: hidden;
}
#mobileMenuWrapper {
margin-top:-100px;
transition: margin-top 0.5s ease;
}
#mobileMenuWrapper.show {
margin-top:0px;
}
</style>
<script type="text/javascript">
function moveMenuDown(){
menu = document.getElementById("mobileMenuWrapper hide");
if(menu.className=="mobileMenuWrapper hide"){
menu.className = menu.className.replace('hide','show');
}else{
menu.className = "mobileMenuWrapper hide";
}
}
</script>
</head>
<body>
<header>
<nav>
<div id="mobileMenu" class="mobileMenu">
<div id="mobileMenuWrapper" class="mobileMenuWrapper hide">
<div style="height: 100px; width: 100%;">
<div style="height: 100px; width: 100%; background-color: black; color: white;">
Menu option<br>Menu option<br>Menu option
</div>
</div>
<div style="position: relative; height: 50px; width: 100%; left: 50px;">
<div style="height: 50px; width: 125px; background-color: black; color: white; text-align: center;">Menu</div>
</div>
</div>
</div>
</nav>
</header>
</body>
</html>
HTML: one additional class:
<header>
<nav>
<div id="mobileMenu" class="mobileMenu">
<div id="mobileMenuWrapper" class="mobileMenuWrapper hide">
<div style="height: 100px; width: 100%;">
<div style="height: 100px; width: 100%; background-color: black; color: white;">
Menu option<br>Menu option<br>Menu option
</div>
</div>
<div style="position: relative; height: 50px; width: 100%; left: 50px;">
<div style="height: 50px; width: 125px; background-color: black; color: white; text-align: center;"><a id="move" href="javascript:moveMenuDown();">Menu</a></div>
</div>
</div>
</div>
</nav>
</header>
CSS: (very simple)
#mobileMenuWrapper{
margin-top:-100px;
transition: margin-top 0.5s ease;
}
#mobileMenuWrapper.show{
margin-top:0px;
}
*just added class and transition.
JS, much simpler:
function moveMenuDown(){
menu = document.getElementById("mobileMenuWrapper");
if(menu.className=="mobileMenuWrapper hide"){
menu.className = menu.className.replace('hide','show');
}
else {
menu.className = "mobileMenuWrapper hide";
}
}
('toggle' functionality added)
Fiddle: http://jsfiddle.net/8u0eka5x/