1
I have a div that her width and height is according to the content, and I need to leave her in the center of the page, I’m having trouble with the max-height
, it just doesn’t work, the min-height
works perfectly, when I take the display:table
the max-height
works, only I lose the centralization of the div, someone knows how to fix it ?
.miniatura
{
display:table;
margin:2% auto 0 auto;
padding:2% 2%;
max-width:500px;
max-height:390px;
width:auto;
height:auto;
background:green
}
<div class="miniatura">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
Put the code on the following platform and share the question so we can help: https://jsfiddle.net
– Tiago Martins Peres 李大仁
here it is. if you take the
display:table
works perfectly https://jsfiddle.net/hdzns75u/– goio
visually what is your goal?
– Tiago Martins Peres 李大仁
my goal is, to leave the width of it according to the content but not passing of
500px
, and the same height having at most390px
and it centralized, withposition:absolute
sure what I want, only that I lose other properties of my layout– goio
Bruno here has no less than 20 responses of how to center elements on the page, you came to take a look? https://answall.com/questions/2817/qual-a-melhor-forma-de-centralizar-um-elemento-vertical-e-horizontalmente
– hugocsl
@hugocsl my problem is the
max-height
that does not work withdisplay:table
, as my width is also according to the contents of the div, having at most500px
, I just need the div to be responsive as its content respects themax-width, max-height
, if I take thedisplay
it works, then I center withposition:aboslute
, but mess up my layout all– goio
Dude you don’t need to display:table, you can center in 20 different ways, with Flex, with Grid, With Transform:Translate, use display:table for this is a thing of the past. and use position:Bsolute tb is not necessary... I’ll make an example for you and you test in your project to see if you break the layout.
– hugocsl
Dear I gave a quick read here on other questions in STOF in English, and it is not possible to put height on a table, unless this table is within a div that has set height and set overflow, but then you create a paradox, pq for height to work you need the display:block of div, but to align you need the display:table of table.... So somehow I think the Flex was the best option for you in this case...
– hugocsl