0

i have a header-container and a quote-container within the body. I like the h1 element to be on the top left of the page and the quote-container in the center of the page.

I applied display:flex to the body and both header-container and quote-container get centered.

How can I center the quote-container only? Is it possible to achieve this using flexbox?

Below are the html and css files:

@import url("https://fonts.googleapis.com/css?family=Montserrat&display=swap");

html {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Montserrat, sans-serif;
  font-weight: 700;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-container {
  margin: 0;
  width: auto;
  max-width: 250px;
  text-align: center;
  margin-bottom: 10px;
  font-size: 1.8rem;
  margin-left: 100px;
  background-color: lightblue;
}

.quote-container {
  width: auto;
  max-width: 900px;
  padding: 20px 30px;
  border: 0.5px solid lightgray;
  border-radius: 10px;
}

.quote-text {
  font-size: 2.5rem;
}

.quote-author {
  margin-top:15px;
  font-size: 2rem;
  font-weight: 400;
  font-style: italic;
}

.button-container {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}

button {
  cursor: pointer;
  font-size: 1.2rem;
  width: auto;
  height: 2.5rem;
  border:none;
  border-radius: 10px;
  color: #fff;
  background: black;
  outline: none;
  padding: 0.5rem 1.8rem;
  box-shadow: 0 0.3rem rgba(121,121,121,0.65);
}

button:hover {
  filter:brightness(110%);
}

button:active {
  transform: translate(0,0.3rem);
  box-shadow: 0 0.1rem rbga(255,255,255,0.65);
}

.twitter-button:hover {
  color: #38a1f3;
}

.fa-twitter {
  font-size:1.5rem;
}

/* Loader */
.loader {
  border: 16px solid #f3f3f3; /* Light grey */
  border-top: 16px solid #333; 
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quotes</title>
    <link rel="icon" type="image/png" href="favicon.png">
    
    <!-- Old Favicon - Grab Icon From Google (Might be unsupported in the future) -->
    <!-- <link rel="icon" type="image/png" href="https://s2.googleusercontent.com/s2/favicons?domain=jacinto.design"> -->

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css"/>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="header-container">
        <h1>Quotes</h1>
    </div>
    <div class="quote-container" id="quote-container">
        <div class="quote-text" id="quote-text">
            <i class="fa fa-quote-left"></i>
            <span id="quote"></span>
        </div>
        <div class="quote-author" id="author">
            <span id = "author"></span>
        </div>
        <div class="button-container">
            <button class="twitter-button" id="twitter" title="Tweet This!" >
                <i class="fab fa-twitter"></i>
            </button>
            <button id="new-quote">New Quote</button>        
        </div>
    </div>

    <!-- Loader -->
    <div class="loader" id="loader"></div>

    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>

final output as below

enter image description here

Jag99
  • 634
  • 2
  • 9
  • 19
  • why is this question mark duplicate as its not. My layout and requirement is different from https://stackoverflow.com/questions/36191516/center-and-bottom-align-flex-items – Jag99 Feb 17 '21 at 22:45

1 Answers1

0

Is that how you want to display?

@import url("https://fonts.googleapis.com/css?family=Montserrat&display=swap");

html {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Montserrat, sans-serif;
  font-weight: 700;
  display: flex;
  flex-direction: column;
}

.header-container {
  margin: 0;
  width: auto;
  max-width: 250px;
  text-align: center;
  margin-bottom: 10px;
  font-size: 1.8rem;
  background-color: lightblue;
}

.quote-container {
  width: auto;
  max-width: 900px;
  padding: 20px 30px;
  border: 0.5px solid lightgray;
  border-radius: 10px;
  align-self: center;
  justify-self: center;
}

.quote-text {
  font-size: 2.5rem;
}

.quote-author {
  margin-top:15px;
  font-size: 2rem;
  font-weight: 400;
  font-style: italic;
}

.button-container {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}

button {
  cursor: pointer;
  font-size: 1.2rem;
  width: auto;
  height: 2.5rem;
  border:none;
  border-radius: 10px;
  color: #fff;
  background: black;
  outline: none;
  padding: 0.5rem 1.8rem;
  box-shadow: 0 0.3rem rgba(121,121,121,0.65);
}

button:hover {
  filter:brightness(110%);
}

button:active {
  transform: translate(0,0.3rem);
  box-shadow: 0 0.1rem rbga(255,255,255,0.65);
}

.twitter-button:hover {
  color: #38a1f3;
}

.fa-twitter {
  font-size:1.5rem;
}

/* Loader */
.loader {
  border: 16px solid #f3f3f3; /* Light grey */
  border-top: 16px solid #333; 
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quotes</title>
    <link rel="icon" type="image/png" href="favicon.png">
    
    <!-- Old Favicon - Grab Icon From Google (Might be unsupported in the future) -->
    <!-- <link rel="icon" type="image/png" href="https://s2.googleusercontent.com/s2/favicons?domain=jacinto.design"> -->

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css"/>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="header-container">
        <h1>Quotes</h1>
    </div>
    <div class="quote-container" id="quote-container">
        <div class="quote-text" id="quote-text">
            <i class="fa fa-quote-left"></i>
            <span id="quote"></span>
        </div>
        <div class="quote-author" id="author">
            <span id = "author"></span>
        </div>
        <div class="button-container">
            <button class="twitter-button" id="twitter" title="Tweet This!" >
                <i class="fab fa-twitter"></i>
            </button>
            <button id="new-quote">New Quote</button>        
        </div>
    </div>

    <!-- Loader -->
    <div class="loader" id="loader"></div>

    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>
Yadab
  • 1,767
  • 1
  • 10
  • 16