EduDev
EduDev

Reputation: 33

Making multiple paragraphs show up as one

I am trying to make a tool that calculates your how big of a skateboard you would need depending on your shoe size. Here is the code:

const shoeSizeInput = document.getElementById('shoeSize')
      const shoeSizeResult = document.getElementById('resultSize') 

shoeSizeInput.addEventListener('input', (event) => {
  const shoeSize = parseInt(event.target.value) 
  let boardSize = '?'

  switch (true) {
    case 0 <= shoeSize && shoeSize <= 7:
      boardSize = '7.75'
      break;
    case shoeSize === 8 || shoeSize === 9:
      boardSize = '8'
      break;
    case shoeSize === 10 || shoeSize === 11:
      boardSize = '8.25'
      break;
    case shoeSize === 12 || shoeSize === 13:
      boardSize = '8.38'
      break;
    case 14 <= shoeSize && shoeSize <= 20:
      boardSize = '8.5'
      break;
  }
  shoeSizeResult.textContent = boardSize 
})
<div class="board-tool">
    <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
    <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
    <p id="resultSize"></p><p>should be your ideal board size.</p>
  </div>
.shoe {
  width: 50px;
  height: 15px;
  border-radius: 5px;
  margin-left: 10px;
}
.board-tool {
  font-size: 1.5rem;
}

All of the code works but the issue is that the first paragraph, the label, and then the paragraph after that are all on different lines but I would like to organize it so that it looks like it is one paragraph for example: If your shoe size is: 12, 8.25 would be the ideal board size.

Upvotes: 1

Views: 54

Answers (3)

Dequog
Dequog

Reputation: 124

Step 1:

change .board-tool to display: flex

Step 2:

change .board-too to flex-wrap: wrap and align-items: center

Step 3: give your last paragraph a margin-left of 10px

Code:

CSS:

.shoe {
  width: 50px;
  height: 15px;
  border-radius: 5px;
  margin-left: 10px;
}

.board-tool {
  font-size: 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.resultLabel {
  margin-left: 10px;
}

HTML:

    <div class="board-tool">
    <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit
        your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
    <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
    <p id="resultSize"></p>
    <p class="resultLabel">should be your ideal board size.</p>
</div>

No change in the javascript

For more information on flexbox visit W3Schools: Flexbox

Upvotes: 1

fnostro
fnostro

Reputation: 4591

There are several way to address your concerns. But I think first is to gain a basic understanding of HTML elements.

<p> (paragraph) elements are block items, which means, by default, their width is 100% of their containing element and they have a default top and bottom margin intended to provide some default spacing between elements.

<span> elements are inline. They run contiguously one after another such that they would read as a paragraph.

However, things are much more advanced now and it is very possible, using styles and class rules, to redefine <p> to behave as <span> and vice versa, as you can see in the snippet below.

Though I do not suggest making this a habit, it can be done. The example div's following your code use dashed light grey borders so you see the default widths of block vs inline elements

const shoeSizeInput = document.getElementById('shoeSize')
const shoeSizeResult = document.getElementById('resultSize')

shoeSizeInput.addEventListener('input', (event) => {
  const shoeSize = parseInt(event.target.value)
  let boardSize = '?'

  switch (true) {
    case 0 <= shoeSize && shoeSize <= 7:
      boardSize = '7.75'
      break;
    case shoeSize === 8 || shoeSize === 9:
      boardSize = '8'
      break;
    case shoeSize === 10 || shoeSize === 11:
      boardSize = '8.25'
      break;
    case shoeSize === 12 || shoeSize === 13:
      boardSize = '8.38'
      break;
    case 14 <= shoeSize && shoeSize <= 20:
      boardSize = '8.5'
      break;
  }
  shoeSizeResult.textContent = boardSize
})
.shoe {
  width: 50px;
  height: 15px;
  border-radius: 5px;
  margin-left: 10px;
}

.board-tool {
  font-size: 1.5rem;
}
/* End OP CSS */

/* Begin Example CSS */

.d-inline {
  display: inline;
}

.d-block {
  display: block;
}

.m-none {
  margin: 0;
}

.mt {
  margin-top: 10px;
}

.mtb {
  margin: 10px 0;
}

.b {
  border: 1px solid green;
}

div.example>div {
  padding: 10px;
}

div.example>div>p,
div.example>div>span {
  border: 1px dashed #ccc;
}
<div class="board-tool">
  <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
  <p>If your shoe size is: <input id='shoeSize' type="number" class="shoe">
    <span id="resultSize"></span>
    <span>should be your ideal board size.</span>
  </p>
</div>

<div class="example">
  <div class="mt b">
    <p>paragraphs </p>
    <p>as default</p>
    <p>block items.</p>
  </div>

  <div class="mt b">
    <p class="d-inline m-none">paragraphs </p>
    <p class="d-inline m-none">styled as </p>
    <p class="d-inline m-none">spans.</p>
  </div>

  <div class="mt b">
    <span class="d-block mtb">span elements </span>
    <span class="d-block mtb">styled as </span>
    <span class="d-block mtb">paragraph elements.</span>
  </div>
</div>

Upvotes: 2

A Haworth
A Haworth

Reputation: 36426

p elements are 'paragraphs' so by default will start a newline.

You could just change those p elements into span elements.

const shoeSizeInput = document.getElementById('shoeSize')
const shoeSizeResult = document.getElementById('resultSize')

shoeSizeInput.addEventListener('input', (event) => {
  const shoeSize = parseInt(event.target.value)
  let boardSize = '?'

  switch (true) {
    case 0 <= shoeSize && shoeSize <= 7:
      boardSize = '7.75'
      break;
    case shoeSize === 8 || shoeSize === 9:
      boardSize = '8'
      break;
    case shoeSize === 10 || shoeSize === 11:
      boardSize = '8.25'
      break;
    case shoeSize === 12 || shoeSize === 13:
      boardSize = '8.38'
      break;
    case 14 <= shoeSize && shoeSize <= 20:
      boardSize = '8.5'
      break;
  }
  shoeSizeResult.textContent = boardSize
})
.shoe {
  width: 50px;
  height: 15px;
  border-radius: 5px;
  margin-left: 10px;
}

.board-tool {
  font-size: 1.5rem;
}
<div class="board-tool">
  <p>Most people pick their board size by prefrence but I will make a tool below to choose a board size that will fit your shoe size best. The most popular board sizes are 7.75, 8, 8.25, 8.38, and 8.5.</p>
  <label>If your shoe size is:</label><input id='shoeSize' type="number" class="shoe">
  <span id="resultSize">?</span><span> should be your ideal board size.</span>
</div>

Upvotes: 1

Related Questions