123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--Form Section-->
<section class="form-input">
  <div class="form-container rounded mt-4">
    <h1 class='text-center m-2'>Create your namecard</h1>
    <form class='mt-5 needs-validation' novalidate>

      <!--Name-->
      <div class="row">
        <div class="col">
          <div class="form-group">
            <label for="first-name">First name</label>
            <input type="text" class='form-control' id='first-name' placeholder='Mike' name='first-name' required>
            <div class="invalid-feedback">
              First name is required.
            </div>
          </div>
        </div>
        <div class="col">
          <div class="form-group">
            <label for="last-name">Last name</label>
            <input type="text" class='form-control' id='last-name' placeholder='Huang' name='last-name' required>
            <div class="invalid-feedback">
              Last name is required.
            </div>
          </div>
        </div>
      </div>

      <!--Photo Link-->
      <div class="form-group">
        <label for="photo-link">Link to your photo</label>
        <input type="url" class='form-control' id='photo-link' name='photo-link' placeholder='https://'>
        <div class="valid-feedback">
          Looks fine!
        </div>
      </div>

      <!--Introduction-->
      <div class="form-group">
        <label for="intro">Your introduction</label>
        <textarea class='form-control' id="intro" cols="30" rows="3" placeholder='Describe yourself in 200 words' maxlength="200" required></textarea>
        <div class="invalid-feedback feedback">
          Description is required.
        </div>
      </div>

      <!--Theme Choosing-->
      <div class="form-group">
        <span class>Preferred theme:</span>
        <div class="form-check mt-3 ml-4">
          <input type="radio" id="light" name="theme" class="form-check-input" value='light-theme' required>
          <label for="light" class='form-check-label'>Light Theme</label>
        </div>
        <div class="form-check mt-1 ml-4">
          <input type="radio" name='theme' class="form-check-input" id='dark' value='dark-theme' required>
          <label for="dark" class='form-check-label'>Dark Theme</label>
        </div>
        <div class="form-check mt-1 ml-4">
          <input type="radio" name='theme' class="form-check-input" id='both' value='both-theme' required>
          <label for="both" class='form-check-label'>I Want Both</label>
          <div class="invalid-feedback">Please choose a theme</div>
        </div>
      </div>

      <!--Submit Button-->
      <button class="btn btn-primary btn-block">Submit</button>
    </form>
  </div>
</section>

<!--Name Card Section-->
<section class='name-card my-5'>
  <!--Display name card-->
</section>
            
          
!
            
              * {
  box-sizing: border-box;
}

body,
html {
  height: 100%;
}

body {
  background-image: url("https://images.pexels.com/photos/414645/pexels-photo-414645.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
}

/*Form Section*/
.form-container {
  width: 60%;
  max-width: 800px;
  margin: 0 auto;
  padding: 15px;
  color: white;
  font-size: 20px;
  background: rgba(0, 0, 0, 0.55);
  -webkit-box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
  box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
}

.form-group {
  margin-bottom: 2rem;
}

.form-control,
.form-control:focus {
  padding-left: 0;
  font-size: 20px;
  color: rgb(255, 255, 255);
  background: transparent;
  border: 0;
  border-bottom: 1px solid gray;
  border-radius: 0;
}

.form-control:focus {
  padding-left: 10px;
  -webkit-box-shadow: 0px 0px 3px 2px rgba(255, 255, 255, 0.3);
  -moz-box-shadow: 0px 0px 3px 2px rgba(255, 255, 255, 0.3);
  box-shadow: 0px 0px 3px 2px rgba(255, 255, 255, 0.3);
}

.invalid-feedback {
  font-size: 90%;
}

.dark-theme {
  color: white;
  background: rgba(0, 0, 0, 0.7);
}

.light-theme {
  color: rgba(0, 0, 0, 0.71);
  background: rgba(255, 255, 255, 0.8);
}

.btn-block {
  width: 60%;
  margin: 50px auto 20px auto;
}

/*Name Card Section*/
.card-container {
  width: 60%;
  max-width: 800px;
  margin: 0 auto;
  padding: 15px;
  -webkit-box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
  box-shadow: 3px 3px 9px 2px rgba(0, 0, 0, 0.5);
}

div.my-img {
  height: 100%;
  padding-top: 33.333%;
  background-image: url("https://assets-lighthouse.s3.amazonaws.com/uploads/user/photo/1408/medium_GOPR0254.JPG");
  background-position: center;
  background-size: contain;
  background-repeat: no-repeat;
}
            
          
!
            
              let sections = document.querySelectorAll('section')
let form = document.querySelector('.needs-validation')
let firstName = document.getElementById('first-name')
let lastName = document.getElementById('last-name')
let link = document.getElementById('photo-link')
let intro = document.getElementById('intro')
let themes = document.getElementsByName('theme')

//trigger form submit to validate input
form.addEventListener('submit', formValidate)

//intro length real-time display
intro.addEventListener('input', displayIntroLength)

//validate input
function formValidate(event) {
  event.preventDefault()
  if (form.checkValidity() === false) {
    event.stopPropagation()
    form.classList.add('was-validated')
  } else {
    //number of card is based of user's theme choice 
    (themes[2].checked === true) ? createCards(2) : createCards(1)
    form.classList.remove('was-validated')
  }
}

//create cards: number is based of user's theme choice 
function createCards(num) {
  //create a title for card section
  sections[1].innerHTML = `<h1 class='text-center text-white m-2'>Your Name Card</h1>`

  //create card content for each card
  for (let i = 0; i < num; i++) {
    generateCardContent(i)
  }
  //reset the form after submit
  resetForm()
}

//generate card content for a card
function generateCardContent(cardIndex) {
  //create card inner html
  sections[1].innerHTML += `
    <div class="card-container rounded mt-4">
      <div class="row p-2">
        <div class="col-8">
          <h1 class="col p-0">${firstName.value} ${lastName.value}</h1>
          <p>${intro.value}</p>
        </div>
        <div class="col-4">
          <div class="my-img"></div>
        </div>
      </div>
    </div>
  `
  //update the image url in CSS if user entered
  if (link.value !== '') {
    let image = document.querySelectorAll('.my-img')[cardIndex]
    image.style.backgroundImage = `url('${link.value}')`
  }
  //create theme for the card
  createTheme(cardIndex)
}

//display input length info to user
function displayIntroLength(event) {
  let feedback = intro.nextElementSibling
  let introLength = this.value.length

  if (introLength < 200 && introLength > 0) {
    feedback.innerHTML = `You have entered ${introLength} words, ${200 - introLength} more words allowed`
    feedback.style.display = 'inherit'
    feedback.classList.add('text-success')
  } else if (introLength === 0) {
    feedback.innerHTML = 'Description is required.'
    feedback.classList.remove('text-success')
  } else {
    feedback.innerHTML = `You have entered ${introLength} words, reaching the limit`
  }
}

//create theme for the card
function createTheme(cardIndex) {
  let cardContainer = sections[1].children[cardIndex + 1]

  //clear all previously added class
  cardContainer.classList.remove('dark-theme')
  cardContainer.classList.remove('light-theme')

  //for light theme choice or first card
  if (themes[0].checked || (themes[2].checked && cardIndex === 0)) {
    cardContainer.classList.add('light-theme')
    //for dark theme chioce or second card
  } else {
    cardContainer.classList.add('dark-theme')
  }
}

//reset form input and validation data
function resetForm() {
  form.reset()
  intro.nextElementSibling.classList.remove('text-success')
  intro.nextElementSibling.innerHTML = 'Description is required.'
  intro.nextElementSibling.style.display = ''
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console