I want the div with red background to resize according to the browser width while keeping its aspect ratio as 16:9 but not exceed the height of 300px. I also want the text written in the div to align at the center; both vertically and horizontally. Through the following CSS code, the div is resizing according to the aspect ratio and the text is horizontally aligned. But the div is not limiting its height to 300px and the text is not vertically aligning.
.container {
background-color: red;
position: relative;
width: 100%;
max-height: 300px;
padding-top: 56.25%;
/* 16:9 Aspect Ratio */
display: table;
}
.text {
display: table-cell;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center;
vertical-align: middle;
font-size: 20px;
color: white;
}
<h2>Maintain Aspect Ratio 16:9</h2>
<div class="container">
<div class="text">This is some text...</div>
</div>