Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling elements like an image

Tags:

html

css

When I for example have an image in :

<img src="10px_image.png" alt="Some image" style="width: 100%" />

Its height automatically gets scaled according to width the image gets (you can see that when you resize the browser window for example). Is there any way to do the same with other elements in html ?

like image 235
user2394156 Avatar asked Jul 23 '26 18:07

user2394156


1 Answers

Solution #1

You could use the new css viewport units vw, vh etc to pull this off.

FIDDLE

div
{
    width: 40vw;
    height: 20vw;
    background: pink;
}

CSS3 has some new values for sizing things relative to the current viewport size: vw, vh, and vmin. One unit on any of the three values is 1% of the viewport axis. "Viewport" == browser window size == window object. If the viewport is 40cm wide, 1vw == 0.4cm.

1vw = 1% of viewport width 1vh = 1% of viewport height 1vmin = 1vw or 1vh, whichever is smaller 1vmax = 1vw or 1vh, whichever is larger (css-tricks post)

Support: IE 9+, Firefox 19+, Chrome 20+, Safari 6+

Reference: w3c, mozilla


Solution #2

FIDDLE

Markup

<div class="container">
    <div class="outer r4x3">
        <div class="inner">
        </div>
    </div>
</div>

CSS

/* container defines margins and width */
.container {
    margin: 60px 120px 0;
}

/* outer container will define aspect ratio */
.outer {
    position: relative;
    width: 100%;
}

.outer.r4x3 {
    padding-top: 75%; /* "height" will be 3/4 of width */
}
/* inner container positioned absolutely and holds content */
.outer .inner {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    outline: 1px solid grey;
    background: pink;
}
like image 132
Danield Avatar answered Jul 25 '26 12:07

Danield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!