I've stumbled upon an interesting problem while playing with my website.
So I've got a containing column with a fixed width. In this I have a number (two for now, don't know whether it'll matter) of images in one row. The images' size and aspect ratio are undetermined and not equal to each other. Now, I'm looking to resize them, so that their height is equal, and their combined width is equal to that of the parent container, while keeping their aspect ratio. And also resize the container height accordingly, for that matter.
Hope I made myself clear.
I'm sure it's possible with JavaScript or similar, but I'm mainly interested in if there's a solution using CSS.
Thanks
I think that's impossible, and even if it were possible, it would be the darkest css hack i've ever seen (besides it would imply using not-very-compatible CSS). It's way easier and recommendable doing that with Javascript. (EDIT: for a jQuery solution see this: http://jsfiddle.net/martinschaer/aJtdb/)
But, for fun I did this: http://jsfiddle.net/martinschaer/cGZrF/
It's the closest you can do with CSS3 for what you need. There's a small gap that fills empty vertical space, but all the images are resized with CSS to fit in the container.
.container{
width: 600px;
margin: 10px;
background-color: #000;
display: box;
box-orient: horizontal;
box-pack: center;
box-align: center;
}
.imgwrapper {
box-flex: 1;
}
img{
max-width: 100%;
height: auto;
display:block;
}
See the jsfiddle (http://jsfiddle.net/martinschaer/cGZrF/) for the full code compatible with mozila and webkit.
EDIT: for a jQuery solution see this: http://jsfiddle.net/martinschaer/aJtdb/
I'm not too experienced with JavaScript yet, but I have one addition to make for any late comers (like myself)…
I noticed that using the provided JSFiddle in Martin's answer on multiple rows compresses the images so I'd like to add that this:
var rows = 0; // set row count for multiple 'imageWrapper' divs
$('.imageWrapper').each(function(){
rows +=1;
});
and then use rows like this:
$('.imageWrapper img').each(function(){
this.width = $(this).data('width') * (container_width / container_width_temp) * rows - 8;
});
the - 8 is because I found that the last image in the row tends to overflow the container and get pushed into a row by itself.
I'm still looking for the perfect answer to this question myself, but this got me a little closer.
Related
So, I have a table with rows where the row height is way bigger than it needs to be. In every browser but Edge, I can fix the problem with display:contents, but in Microsoft Edge, which doesn't support this feature at this time, I have to find an alternative. Can someone please point me in the right direction?
Try adding the following code to your CSS after [your element] that is using display: contents;
`#supports not (display: contents) { /* workaround because Edge doesn't support display: contents; */
a.remove {
display: inline;
}
}`
Replace a.remove with your element name.
This feature is not yet implemented by edge browser and IE.
Basically there are two ways the break the flex-row
1)Page-break(Works only in Mozilla)
2) display: contents;(Works only in Mozilla,Chrome).
So if know number of rows and have repeated instances to break could use display:block and inline to get the required result.
But in dynamically repeating rows and number of tiles available its not possible to break the flex box.
Kindly report Edge browser to implement display: contents.
https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/10938981-implement-the-box-generation-keywords-from-css-dis
which is very important feature and missed out.
I try to check your code with Edge and check the result.
I made change in line below and set 'height: 30 px' instead of 300 px.
<tbody data-link="row" class="rowlink" style="overflow-y: auto; overflow-x: hidden; height: 30px; display: contents;">
Now it is displaying the rows properly in all major browsers including Edge.
Below is the testing result with Edge.
Click here to see the image
I think this can be the easiest alternative to display the output properly in Edge.
Regards
Deepak
I want to set the left postition of n-th div to (n-1)*250px, for e.g:
1st child: left = 0px
2nd child: left = 250px
...
is it possible to do so in css? I am using Javascript to set this. Thanks.
The CSS3 calc() method comes to mind, but it doesn't support using the index (n) as an operand, so that will not work.
Recommended solution: You could potentially design your layout such that the widths of each of the elements is 250px. Give each of the elements display: inline-block or float: left and they'll line up as you intend. If the width of the content of the elements needs to be larger than 250px, ensure overflow: visible (default value) is set on the elements and allow the content to overflow. Without more information, this should achieve the effect you are intending.
However, if you need to use a more direct method of positioning, you should stick with JavaScript to set the position of these elements. Likely, you'll want to take into account screen width, element width, and more, and CSS will leave you unable to do so.
Take a look at this JSFiddle for inspiration. If you post a sketch of what you're looking to achieve, I can help you further.
You can use:
div:nth-of-type(an+b)
// or
div:nth-child(an+b)
to address your divs.
div{
position:absolute;
}
div:nth-child(2){
left: 250px;
}
div:nth-child(3){
left: 500px;
}
without preprocessor you need to write every rule by hand because there is no possibility for a dynamic way when setting the left property.
Another possiblity (depending on what you really want to do) would be to introduce nesting and set padding-left:250px. But that only works if you can alter your markup accordingly.
Javascript probably is the easiest way here.
Definitely having one of those WTF moments right now.
I'm trying to implement a design where the main element goes off canvas when you carry on scrolling up. Responsively, of course.
I figured it would be pretty easy to do.
main{
padding-bottom: 100%;
}
Which, I figured would create space underneath the element equal to 100% of it's height. It didn't. Then, after some element inspecting, and some reading up on the spec, I found this horror:
"The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well."
-W3C
This blew my mind. I found this question asking why and although I'm not satisfied with the answer over there, I'm not going to duplicate the question.
What I want to know is how can I get around this bizarreness?
From one of the other comments on that question, I investigated trying to change the flow of the element in question to vertical.
"According to dev.w3.org/csswg/css3-box/#the-margin-properties it states 'Note that in a horizontal flow, percentages on ‘margin-top’ and ‘margin-bottom’ are relative to the width of the containing block, not the height (and in vertical flow, ‘margin-left’ and ‘margin-right’ are relative to the height, not the width).' So it goes both ways." – Adam Sweeney
So I tried adding writing-mode: vertical-rl; but that doesn't seem to change anything. Not really sure why because I figured it might at least rotate my text or something but no change.
So yeah, I'm starting to think this might not be possible in CSS at all but that, to me, is just mental.
Thanks for any suggestions.
.main {
position: relative;
bottom: 100%;
}
Just a thought.
Ok I've solved my problem with a pseudo element, so I'll post how I did it here in case anyone has the exact same problem but I'll leave the broader issue of getting percentage based padding & margins to work as expected unsolved as yet.
I wanted to create a space under the main element that was equal to the height of the main element.
So, I did this:
<div id="wrapper">
<main>Content</main>
</div>
#wrapper{
height: 100%;
width: 100%;
}
main:after{
height: 100%;
width: 100%;
position:absolute;
}
I have a %-based grid with a fixed-width (for the moment). The code is based off of this css-tricks article: http://css-tricks.com/dont-overthink-it-grids/
Works great until I have a column that has multiple responsive images in it that are the same size and need to be stacked next to each other (floated). Because of padding issues and what-not, I can not get all three images to come out the same width and height, despite the fact that they start that way. The last one is always taller. Here is a codepen showing the issue: http://codepen.io/joshmath/pen/dEuIv
Any help with this would be really appreciated. I've run into this issue before and always just end up hacking my way through it on a case-by-case basis. Thanks!
For whatever reason, if you remove the padding-right: 0 style from the last element, it fixes the issue.
It looks like you're trying to add spacing between the elements using padding. What I tried instead using the Chrome dev tools was to use a margin instead of padding, and then slightly reducing the width of your elements to around 29.5%. That seemed to work.
just add the following to your css. set the size to whatever you like and all images within your grid will remain that size, if they need to grow / shrink use height/width percents instead.
.grid img
{
width: 350px;
height: 400px;
}
I have several divs on a page that all have the same width but different heights. They are all in one div, the #note1PreviewDiv. They all share the class .note, which has the following css code (among other):
.note{
width: 160px;
padding: 10px;
margin: 10px;
background: #e3f0ff;
float: left;
}
I thought with float: left; they would all automatically align so that they are well aligned among each other.
Here's a preview of what it looks like:
Current state http://posti.sh/img/ist.png
And here's what the positioning should be like:
Desired state http://posti.sh/img/soll.png
I think you get the idea. Somehow it seems to me the height of the leftmost div pushes the other divs in the second row to the right - but that's only guessing.
Thanks for your help!
Charles
You're not going to be able to do this easily with CSS only.
CSS3 has a new feature called column layout, but browser support is not great. IE9 and below don't support it.
See http://designshack.net/articles/css/masonry/ and the last example for CSS3 solution.
Have a look at these js / jQuery options for easier implementation and browser support:
masonry
isotope
vanilla masonry which doesn't need jQuery.
wookmark
The kind of lay out you want is really difficult (not possible?) without going for a column based approach and adding additional block elements to represent each column. This obviously won't work with a flexible number of columns if you want a dynamic layout based on screen size.
That said, you could always use JavaScript to dynamically place elements into columns, and get it to match the screen size.
Is the height of the parent container given a fixed value? If it is, try setting the height of the parent container to auto, and the overlow propery to hidden.