Mark Boulder
Mark Boulder

Reputation: 14287

Removing full width from flexbox centered div

How does one remove the full width from this flexbox centered div or its child?

Ie. this:

enter image description here

Should be:

enter image description here

.main {
    color: white;
    background: blue;
    position: relative;
    height: 300px;
    padding: 10px;
}

.wrapper {
    display: flex;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    position: absolute;
    width: calc(100% - 20px);
    height: calc(100% - 20px);
}

.wrapper p {
    color: #000;
    background: lightgrey;
}
<div class="main">
    <div class="wrapper">
        <p>Please press Enter to continue installation</p>
    </div>
    <p>Welcome to Windows 95</p>
</div>

Upvotes: 3

Views: 2316

Answers (2)

Alex Char
Alex Char

Reputation: 33218

You can set margin: 0 auto to p element inside .wrapper:

.main {
  color: white;
  background: blue;
  position: relative;
  height: 300px;
  padding: 10px;
}
.wrapper {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  position: absolute;
  width: calc(100% - 20px);
  height: calc(100% - 20px);
}
.wrapper p {
  color: #000;
  background: lightgrey;
  margin: 0 auto;/*add margin 0 auto*/
}
<div class="main">
  <div class="wrapper">
    <p>Please press Enter to continue installation</p>
  </div>
  <p>Welcome to Windows 95</p>
</div>

Upvotes: 2

JakeParis
JakeParis

Reputation: 11210

You can add auto to the left and right margins of the <p> to center it, and specify a width less than 100% to make it not full width .

.main {
    color: white;
    background: blue;
    position: relative;
    height: 300px;
    padding: 10px;
}

.wrapper {
    display: flex;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    position: absolute;
    width: calc(100% - 20px);
    height: calc(100% - 20px);
}

.wrapper p {
    color: #000;
    background: lightgrey;
    max-width: 50%;
    margin: 0 auto;
}
<div class="main">
    <div class="wrapper">
        <p>Please press Enter to continue installation</p>
    </div>
    <p>Welcome to Windows 95</p>
</div>

Upvotes: 1

Related Questions