Вы можете сделать это с полем, но хитрость заключается в том, чтобы учесть его и внутри свойства left.
.MessageBox {
/* it matters */
position: relative;
left: calc(50% - 20px);
transform: translateX(-50%);
max-width: 600px;
margin:0 20px;
/* just decorations */
background: lightblue;
padding: 6px 8px;
}
<div class="MessageBox"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>
С помощью CSS переменных вы можете легко настроить его:
.MessageBox {
--gap:20px;
/* it matters */
position: relative;
left: calc(50% - var(--gap));
transform: translateX(-50%);
max-width: 600px;
margin:0 var(--gap);
/* just decorations */
background: lightblue;
padding: 6px 8px;
}
<div class="MessageBox"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>
Также можно сделать это с учетом только ширины:
.MessageBox {
--gap:20px;
/* it matters */
position: relative;
margin:auto;
max-width: 600px;
width:calc(100% - 2*var(--gap));
/* just decorations */
background: lightblue;
padding: 6px 8px;
box-sizing:border-box;
}
<div class="MessageBox"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>