html - Center container AND use negative margin -
i styling figures.
i want make when image smaller text column, figure shrinks size , centers, i'm using:
div { width: 20em; margin: 0 auto; background-color: #f8f8f8; } figure { display: table; margin: 1rem auto; padding: 2rem 2rem; background-color: #eee; } figure img { max-width: 100%; margin: 0 auto; }
<div><figure> <img src="https://placehold.it/100x100" alt="100x100 placeholder"> </figure> <figure> <img src="https://placehold.it/400x100" alt="400x100 placeholder"> </figure></div>
the problem want figure bleed outside column when image size of column. i'd use:
div { width: 20em; margin: 0 auto; background-color: #f8f8f8; } figure { display: table; margin: 1rem -2rem; padding: 2rem 2rem; background-color: #eee; } figure img { max-width: 100%; margin: 0 auto; }
<div><figure> <img src="https://placehold.it/100x100" alt="100x100 placeholder"> </figure> <figure> <img src="https://placehold.it/400x100" alt="400x100 placeholder"> </figure></div>
but solution removes margin: auto;
centering
i'd appreciate css solutions, js , jquery if there's no other way
set figure
inline-table
, apply text-align:center
parent.
body { background: lightgreen; } div { width: 20em; margin: 0 auto; background-color: #f8f8f8; background: cornflowerblue; text-align: center; } figure { display: inline-table; margin: 1rem -2rem; padding: 2rem 2rem; background-color: #eee; } figure img { max-width: 100%; margin: 0 auto; }
<div> <figure> <img src="https://placehold.it/100x100" alt="100x100 placeholder"> </figure> <figure> <img src="https://placehold.it/400x100" alt="400x100 placeholder"> </figure> </div>
Comments
Post a Comment