Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (20.3k points)

Is it possible to set the same height as the width (ratio 1:1)?

Example

+----------+

| body     |

| 1:3      |

|          |

| +------+ |

| | div  | |

| | 1:1  | |

| +------+ |

|          |

|          |

|          |

|          |

|          |

+----------+

CSS

div {

  width: 80%;

  height: same-as-width

}

1 Answer

0 votes
by (40.7k points)

Try using the code given below:

In HTML:

<div id="container">

    <div id="dummy"></div>

    <div id="element">

        some text

    </div>

</div>

 In CSS:

#container {

    display: inline-block;

    position: relative;

    width: 50%;

}

#dummy {

    margin-top: 75%; /* 4:3 aspect ratio */

}

#element {

    position: absolute;

    top: 0;

    bottom: 0;

    left: 0;

    right: 0;

    background-color: silver /* show me! */

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...