Let's say I have a container that looks like this:
<div class="playing-card">
<p>Ace of Spades</p>
</div>
What I want to do is style the playing-card container to always have an aspect ratio of 5 / 7. If the container is too short, I want the playing card to be constrained by its height. If the container is too narrow, then the playing card should be constrained by its width.
Is something like this possible with CSS? I know this can be done with object-fit on replaced elements (such as images and videos), but my actual use case specifically needs to be a div with normal HTML content.
Related
When showing the element in fullscreen mode, I need the element (highlighted with the red rectangle) to be centered if the width is smaller than the screen(container). To achieve this, I used tailwindcss flex overflow-x-scroll justify-center (item 1).
If the width is bigger than the screen(container), we want the element to be aligned at the left edge. To achieve this, I need to remove flex from classes (item 2).
My question is how can I make it work for both cases: narrow and wide.
Your question is not clear but if you want to center the element in whatever the size of the container, just follow this code it may help:
<div class="lg:w-[85%] md:w-[90%] sm:w-[95%] mx-auto">
<div class=""></div>
</div>
it means on the large screen the width of the div is 85% of the body & the margin in the x-direction is auto or you can apply those classes directly to the targeted element and so on for the medium & small screen sizes. it's a basic CSS fundamentals but applied with tailwindcss
The beauty of the <img>-tag is that it occupies layout vertically based on its aspect ratio when you only set a width. Cool!
Now I want the image to do and behave just like that, until there is no more room vertically.
And as to my understanding flex-shrink does exactly that.
In the following codepen the .flexboxs height is hardcoded to simulate a height restrction (in my project this would be another flex-container in a full-height viewport scenario)
https://codepen.io/katerlouis/pen/RwrWorr
Now why does the image not shrink below on a aspect ratio greater than 1.85 – it's easier to debug it here: https://cdpn.io/katerlouis/debug/RwrWorr/dGMXWKBbVKnk – use the responsive resizing in devtools to change the viewport for instance to 770x415 and decrease the height of the viewport to see it won't shrink.
To make things more clear: Here is what happens for a viewport of 770x300
And here's the desired outcome.
I would maaaybe understand if this is just not possible because no css code controls the height of the image? but why does flex-grow work then?
I have a project that involves having a sidebar that floats over an image. The sidebar is set to position: absolute to keep it over the image and to help it scale along with it when the screen size changes.
Here is a codepen that basically recreates what I'm working on: https://codepen.io/gojiHime/pen/JmYqaz
The issue I'm having is with controlling the size of the contents within the wrapper container. I want the preview div to scale along with the wrapper container. Currently, it does not work as expected in that the preview div does not start scaling as the width and height change for wrapper and for thumbs-inner. The thumbs-inner div scales correctly for the most part, but the bottom of div is cut off so you can't see the bottom of the scroll bar in smaller screens.
I know I set overflow: hidden on wrapper but without it the content in preview would extend outside of it as the height of wrapper changed.
So, I'm looking for ideas on how to fix the aforementioned issues. wrapper must stay absolutely positioned and the thumbs-inner div needs to have a vertical scrolling feature, so I can't do anything with those. I don't think setting a height makes sense for wrapper since it needs to scale responsively in height and width.
EDIT: Not sure how much this will help but this is a screenshot of what the layout of everything should look like: enter image description here
The Kraftmaid logo, full-size thumbnail and the text below it (which are in the .preview div in the codepen) have to be visible at all times when changing the screensize.
I'm not sure if this is exactly what you're looking for, but generally for responsive layouts you would want to avoid fixed dimensions, such as specific widths set in x number of pixels.
This shows your code with responsive layouts for .wrapper and .thumbs-inner (note that I haven't addressed any content issues within those two divs since I have no idea what your intended layout is):
https://codepen.io/anon/pen/ZqrZaj
Note that:
I've switched the two layout divs to use box-sizing: border-box; which will allow you to use pixels for margin and padding but still use percentages for width.
I've removed width from .wrapper and switched to percentage based absolute left and right declarations - if you modify these values, the layout should still work.
I've added borders to make the layout more obvious.
I am trying to resize an image using only CSS, the problem is I don't know it's dimensions.
What I have tried so far is putting the into a and then making the image have 105% width. The idea was that the containing div would have no size other than it's contents, but this is only make the image the size of the next ancestor that does have an explicit size.
In order to resize something in CSS you have to either give it an exact pixel value or base the size on something else.
If you were to set an image to have a width of 105% of its container then that container must have some width for you to use. If it's a regular div with no styling applied then its width will be the full width of that divs parent and your img will be 105% of that.
If the div holding the img is floated then it will be getting its width from its contents (aka the img). This won't work because you can't have two elements getting their widths from each other. One of them has to be constrained somehow.
I've coded myself into a corner or I am overseeing something obvious here. I have a semi-fluid CSS layout that is designed like this:
header - 100% width at all times, contains a x-repeated background image
container - fluid (960px to 1200px, centered, contains two columns)
footer - 100% width at all times, contains a x-repeated background image
In almost all cases this works fine.
In summary, the design as a whole scales to any width, yet the content part only to 1200px at a maximum. However, since this concerns a photo site, sometimes images are wider than the container width of 1200px and the image breaks out of it. This is perfectly fine, I want the full image to be shown. However, I want the header and footer to scale to the widest element, in this case the image. This is not happening and is particularly troublesome when I resize the window to a width less than the image and then scroll to the right using the horizontal scrollbar: it leaves a clear gap on both the header and footer whilst I want them to stretch to at least the image/content width.
Simply setting the width to 100% is not enough as that concerns the viewport, not the content width. I can forcefully use min-width with a large value for the header and footer, but that leaves a horizontal scrollbar in normal resolutions. I could hide that scrollbar using overflow:hidden but that would chop of content and not display a scrollbar when the window is small.
To make a long story short, I guess what I want is that this layout would work as a table would work: if one column's content is wider than its size, it pushes all other rows to that same width. The largest width determines the total width. I prefer a solution without javascript, but am thinking it is either not possible or I am overseeing something very obvious?
100% width sets the element's width to 100% of the width of the element it is contained in. In your case, it seems this is the window itself (or the body element). To make the header and footer divs (I'm assuming you're using divs here) scale with the image, they will probably need to either be included in the same div that the image is in, or inside a div that the image div is also in, assuming that div is scaling to the correct width (don't assume it is scaling to fit the image).
However, in many cases using a table for your layout can be much cleaner, and will handle the type of horizontal scaling you're looking for without needing to resort to css hacks.
To make a long story short, I guess
what I want is that this layout would
work as a table would work
display: table on the common container of these elements, and display: table-row or table-cell on its children. This will not work in IE6, but clever things can be done with its CSS expression() hack to simulate this.
I would rather suggest, however, that you not set a maximum width at all, and allow the design to flow better according to the user's desired window size.