Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sizing width of an element as a percentage of its height or vice versa in a fluid design? [duplicate]

Im making a responsive design, which has to keep the proportions of its elements (height to width) no matter what the screen size is, so i don't know the size in pixels for any of the elements and i can work only in %.

I can set either the width or the height as a % of the browser size, but the i have no idea how to set the other property value.

That using only CSS, making JS calculate the values is not an option.

like image 480
Bjorkata Avatar asked Jul 10 '13 11:07

Bjorkata


People also ask

Is used to set the width size of an element?

The CSS height and width properties are used to set the height and width of an element. The CSS max-width property is used to set the maximum width of an element.

What is element width?

The width property sets the width of an element. The width of an element does not include padding, borders, or margins! Note: The min-width and max-width properties override the width property.

How would you set the width of an element to be 100 %?

If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

How do I change the width of a container in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.


1 Answers

I came across this problem last year and found an obscure solution after some tedious researching. Unfortunately it involves wrapper DIVs. The structure is simple - you have a parent DIV which contains the contents container and another DIV that sets the proportions. You set the margin-top of the 'proportion' DIV as percent of the width of the parent... Example:

#parent {
    position: relative;
}
.proportions {
    margin-top: 75%; /* makes parent height to 75% of it's width (4:3)  */
}
.container { /* contents container with 100% width and height of #parent */
    position: absolute;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
}

http://jsfiddle.net/twpTU/

like image 123
Alexander Ivanov Avatar answered Oct 10 '22 11:10

Alexander Ivanov