I've got a div containing configurable content (but likely paragraphs of text) and I've come across an issue animating it's height.
Here's my animation (nice and slowed down), which goes from a height 0 to it's calculated height based on it's content:
See the "hop" at the end?
After some investigation, I think I've worked out the issue. I'm animating the div that contains the html elements liket this (Angular 2, but it's not really relevant to the issue):
<div [#visibilityStateTrigger]="visibilityState" style="display: block">
<ng-content></ng-content>
</div>
ng-content is where the children go, the visibility state is the animation triggers. The important thing is it's a div, it contains children.
My assumption (new to web development) would be that the div would size to it's children's content. And it certainly seemed to until I animated it and saw this strange step and had a look at it in the chrome inspection:
Here's the div:
<div _ngcontent-8027-15="" style="display: block; opacity: 1;">
<p _ngcontent-8027-11="">
Dissertation I designed an electronic tuning device for a violin including a cost estimate and full circuit diagram of all components.
</p>
</div>
Here's it's content:
When contracted, it simply sets the height and opacity to 0:
<div _ngcontent-8027-15="" style="display: block;height: 0px;opacity: 0;">
<p _ngcontent-8027-11="">
Dissertation I designed an electronic tuning device for a violin including a cost estimate and full circuit diagram of all components.
</p>
</div>
However, it's the in-between stages where the problem is. I'm presuming due to the way it performs the animation.
I know, however, that it's not an issue if the height of the div matches the height of the content. Since I'm animating the parent div not the child components only the div's height is used in the animation.
I found an awkward solution: use padding instead of margin. However, this involves never using margin on any styles that I'm likely to ever want to put into this component - and re-styling every single html style to use padding instead of margin.
Is there a way of telling my div to size it'self based on child's full size, including margin?
Have you tried getting clientHeight of child onclick and apply it to the parents height?
this.on('click', function() {
var childHeight = parentDiv.children()[0].clientHeight;
parentDiv.style.height = childHeight + 'px';
});
I found a way of doing it, but it feels a little hacky. However, it plays happy with Angular 2's reluctance to play with the DOM.
I simply add an item after the child content. The item doesn't have any margin, but so long as it's present the parent div's height accomodates it.
Like this:
I've made the item 5 by 5, and red so the behaviour can be seen:
<div [#visibilityStateTrigger]="visibilityState" style="display: block">
<ng-content></ng-content>
<div style="background-color: red; width: 5px; height: 5px;"></div>
</div>
But so long as it's at least 1px by 1px and thus displayed, it all works fine.
Related
So when you put a word into a block container there is a small spacing between the very first pixel of the very first character ("1" in this case) and the left edge of the container. How do I remove it?
Setting padding to 0 doesn't help, as you can see from the picture:
Here is the sample to play with: https://codepen.io/alanklm/pen/oNWXerd?editors=1000
<div style="width: 300px;">
<div style="float:right; padding: 0; font-size:200px;">
15
</div>
</div>
I can use negative margins to compensate for this, but this would be an ugly solution, which will mess all the rest of css around the box and also for different fonts and different letter the amount will be different.
As Paulie_D commented, setting container so it dimensions matches geometric bounding box of its text (a vice versa text to 'touch its wrapper') is not universally possible in plain CSS, and generally even not quite desirable typography-wise.
You can super "hand-craft" style to match precise combination of font and text, but unless font used will be super "blocky" and with uniform metrics, it will work only for that single combination:
<body style="background: white; margin: 10vmin">
<div contenteditable style="display: inline-block;
font: 90vmin / 0.7em Times New Roman;
background-color: black; color: white;
text-indent: -.121em; word-spacing: -.317em;
">15 <!-- = space for negative word spacing --> </div>
</div>
(Notice those wacky text-indent & word-spacing values.)
Rendered outcomes in Firefox on Windows machine:
("correct"), ("wrong").
As you can see, metrics of (used) font are set for visual balance, not geometric boundaries.
Hypothetically, using JavaScript canvas to get geometric bounding box of text and applying data back to style is technically possible, but as seen in the "correct" example above, even then the "rightmost" pixel of the top arm of the "5" glyph outreaching its bottom arch is probably not the right boundary to take.
You might try a combination of absolute positioning and media queries.
Set the parent div to position: relative and the child to position: absolute, then set the left property to a negative number, like left: -25px
You'll likely need to use media queries to adjust the left property value as you scale down.
I am attempting to style a bootstrap panel on a page for a small checklist app. Users can input as many 'to do' items as they so choose and see their list grow and grow.
In order to prevent the entire page from scrolling, I'd like to allow the list to scroll, inside the panel element.
I have set a max-height: 350px, but to me, this feels a little "hacktastic". I can't seem to find a solid solution using flexbox and I'm unsure if % values are the way to go either.
Is there a sure fire, silver bullet solution for this problem (my gut feeling is no, but I'll remain positive)?
HTML:
<div class="ChecklistContainer">
{{ input loop to handle user input }}
<div>
CSS:
.ChecklistContainer {
max-height: 350px;
overflow: auto;
}
I think it's fine to use a max-height in order to achieve the desired look and feel. We use it all the time in our production softwares.
I'm creating a site with a horizontal navbar in which the buttons are designed as elements, making them easy to differentiate, and they individually light up when you a:hover over them. Here's a link: http://allpropestmanagement.net/commercial2.html
Obviously not a finished product.
My current problem involves that big purple field on the far right of the navbar, the one that's not a button. That too is an element, but with hover disabled and a whole load of nonbreaking spaces to pad it. That's the problem. I would like that purple field to extend all the way to the right end (with a tiny margin, like it does on the left side). The trouble with nbsp, as you can imagine, is that there's a finite number of them, and they don't scale. So if the navbar is the perfect length on my computer with, say, 16 nbsps, on someone else's machine it won't reach all the way and on yet another person's it will reach too far.
The html looks like this:
<div id="navmenu">
<form>
Home
Commercial
Meet The Pro
Contact
<a id="farright" style="border-top-right-radius:25px;">
<i> "We'll get the job done right!"
</i></a>
</form>
</div>
I feel odd saying this, but the css is kind of bulky and I'm having trouble formatting this post. Perhaps I'll add it in a few minutes once this post is visible, but the css file is "smithmicropurple.css".
Anyway, I would like a way to stretch that element so it always fits correctly, or if not, some other method that achieves the same effect. I have already tried setting widths individually for each element and that doesn't appear to work.
I like to do these types of things to "help" others (rarely, if I'm lucky), but also to help me learn more about html/css.
So I've given it the old college try with this FIDDLE.
HTML
<div class='holderdiv'>
<a href='#'>One</a>
<a href='#'>Two</a>
<a href='#'>Three</a>
<a href='#'>Four</a>
<a href='#'>We'll Get the Job Done Right!</a>
</div>
I won't post the CSS because it's pretty long. It's in the fiddle.
Please don't consider this a "real" answer. Perhaps just something to think about.
Semantically, I am not sure why the parent is a form element, i'd suggest changing that to a HTML5 <nav> element. (assuming you're using HTML5, of course)
The approach taken here is to set the child elements to display:table-cell, and give the targeted element, #farright a width of 100% to fill the remaining space. Also, text-align:center will effectively center all the child elements. No need for %nbsp;
#navmenu {
font-size: 14pt;
margin: 5px 0 0 5px;
}
#navmenu form {
width: 940px;
}
#navmenu form > a {
display: table-cell;
white-space: nowrap;
text-align:center;
}
#navmenu #farright {
width:100%;
}
I wanted to make a site with the grid system. ( I have made one already, so I know/knew how it works )
This is my custum grid: http://grids.heroku.com/grid.css?column_width=100&column_amount=8&gutter_width=15
And now comes the problem: When I try to put prefix_1 as a class nothing happen it just stays in the same place. And I have one prefix_1 grid_3 and one grid_4. so it is 1+3+4=8. My grid is 8 columns wide so it has to fit together easily, but it doesn't.
http://jsfiddle.net/gekkeabt/5LA82/
<div class="container_8">
<span id="download" class="prefix_1 grid_3"><b>Download</b> Leasy CMS</span>
<span id="download" class="grid_4"><b>Download</b> Macha Webserver</span>
<span id="about" class="prefix_2 grid_4 suffix_2"><b>About</b> Me</span>
</div>
I checked my code 100 times. But I can't find where it went wrong.
Maybe someone can help me out?
Thanks!
EDIT:
I solved the prefix and suffix problem by defining the class in another div outside of the span.
So it looks like:
<div class="prefix_1 grid_3"><span id="download">The content....</span></div>
Try removing the
padding:10px 50px 10px 50px;
line from your #download,#about CSS declaration (or at least the horizontal padding of 2 x 50px).
It's that extra padding that makes the elements wider than one row.
Here's how it looks without that line: http://jsfiddle.net/WHG4u/
Please add this css to your /css/custom.css to make the page stop jumping each time the slide changes
.avia-content-slider-active .slide-entry-wrap { height: 275px; }
Having seen advice seemingly change over the years regarding use of empty DIVs (ie. <DIV CLASS="somediv"></DIV>) I'm confused as to the current thinking over whether or not to use when a DIV will have no inner HTML.
I can find no definitive confirmation over whether we can rely on all modern browsers to display background color and image correctly at the specified width & height when there is no inner HTML, so I'm thinking maybe we can't rely on it - yet it's such a seemingly basic area.
I have even seen suggestions that empty DIVs should never be used - but do specs really state it is 'wrong' to have empty DIVs, or is it just unreliable? (I've tried finding reference to them, but maybe I'm using the wrong terms).
To illustrate, here are 5 areas where I would normally use an empty DIV, in the absence of any recommended alternative:
as a placeholder for content which will subsequently be fetched by XHR calls
as a way to manually create space in a layout
where an image is defined in CSS (as a background image, but will effectively be foreground)
where the text will come from the CSS using .somediv:after{content:SOMETEXT}
where CSS is used to display graph bars etc using solid background color
Maybe there are different answers for each of these, which might explain the complexity over this issue.
I have, of course, tried discovering already, but for example the SO question Is necessary to show an empty <div>? suggests to me there is a huge amount of "IMHO", "probably", "seems to work" in this area. I would expect that by now that some official consensus has been reached on best practice.
So.. should I use and if so should I set font-size to the same as the smaller of DIV width/height to ensure that space is filled in all browsers? Are there any other CSS tricks to ensure this will work in all browsers?
The browser is not going to discard or forget your container just because it does not have any contents (yet).
If you want the container to have a specific placeholder shape, then you might give it min-height, min-width, height and width and make sure it's display: block;.
If you are still unsure, you can fill it with a spacer.gif/png without padding and margin.
http://jsfiddle.net/APxNF/1/
Short answer. Yes, browsers will render the div even if there is no content.
Long answer, That might now always be the case. I have worked in the web for 8 years now and never had to use these, but here they are anyway.
jsFiddle demo
HTML
<div class="empty1"></div>
<div class="empty2"></div>
<div class="empty3"></div>
CSS
.empty1 {
background: #FBB829;
height: 100px;
width: 100px;
}
.empty2:before {
content: "\00a0";
}
.empty2 {
background: #FF0066;
height: 100px;
width: 100px;
}
.empty3 {
background: #F02311;
min-height: 1px;
height: 100px;
width: 100px;
}
Sources:
Experience
Empty div with 2px width and background color doesnt show with height as 100%
http://csscreator.com/node/36023