How to set all checkboxes and its labels on left side one after another line?

Viewed 826

I am learning HTML and CSS. I am trying to design a webpage to practice HTML and CSS. I am trying to add multiple checkboxes with labels in new lines form left side. But I couldn't.

I want to design a page like the below image: enter image description here

But the checkboxes is not going to left. Here is my design below. enter image description here

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Project 05</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="headings">
            <h1 >freeCodeCamp Survey Form</h1>
            <h3><em>Thank you for taking the time to help us improve the platform</em></h3>
        </div>
        <div class="form-container">
            <label for="">Name</label>
            <input type="text" required>
            <label for="">Email</label>
            <input type="email" required>
            <label for="">Age (optional)</label>
            <input type="number" required>
            <label for="">Which option best describes your current role?</label>
            <select name="" id="">
                <option disabled> Select an option</option>
                <option class="container" value="challenges">Challenges</option>
                <option class="container" value="projects">Projects</option>
                <option class="container" value="community">Community</option>
                <option class="container" value="opensource">Opensource</option>
            </select>
            <label for="">What is your favorite feature of freeCodeCamp?</label>
            <label class="checkboxes"><input type="checkbox"> One</label>
            <label class="checkboxes"><input type="checkbox"> Two</label>
            <label class="checkboxes"><input type="checkbox"> Three</label>
        </div>
    </div>
</body>
</html>

CSS

body {
    background: linear-gradient(rgba(68, 28, 179, 0.753), rgba(68, 28, 179, 0.753)), url(./survey-form-background.jpeg) no-repeat center center fixed, rgba(68, 28, 179, 0.753);; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
.container {
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.4;
    font-family: 'Poppins', sans-serif;
}
.headings {
    color: white;
    text-align: center;
}
.form-container {
    background: rgba(27, 27, 50, 0.8);
    margin-left: 18rem;
    margin-right: 18rem;
    padding: 2em;
    text-align: left;
    border-radius: 5px;
}
label {
    color: white;
    display: flex;
    font-size: 1.125rem;
    margin-bottom: 10px;
}
input {
    width: 100%;
    height: 1.5rem;
    padding: 0.375rem 0.01rem;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    margin-bottom: 30px;
}
select {
    width: 100%;
    height: 2.4rem;
    padding: 0.375rem 0.01rem;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    margin-bottom: 30px;
}
.checkboxes {
    text-align: left;
}

How can I do that? Please help me how can I do that design with CSS.

2 Answers

Just as @StefanBob mentioned, the checkboxes aren't aligned to the left side of the form because you have width: 100% declared on all of the <input> elements. I see you want to keep 100% width for the name/email/age fields, so you can just add width: auto to the checkbox <input>'s by using .checkboxes input { width: auto } in your stylesheet.

If you want it to mimic the look of the checkboxes included in your image, you can add an explicit width on the checkbox inputs like width: 4ch. Lastly, to make your form responsive, you can remove the 18rem left and right margins on .form-container and replace it with margin: 0 auto. This way the left/right margins are auto calculated by the viewport. To achieve the same width for the form you had with the left/right 18rem margins, just utilize max-width. Now your form is responsive and viewable for all viewport widths.

.form-container {
  background: rgba(27, 27, 50, 0.8);
  margin: 0 auto;
  max-width: 65ch; /* max width of form */
  padding: 2em;
  text-align: left;
  border-radius: 5px;
}

.checkboxes {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.checkboxes input {
  /* width: auto; */
  width: 4ch; /* define an explicit width */
  margin-bottom: 0;
  margin-left: 0;
  margin-right: .25rem; /* controls the space between checkbox and text */
}

body {
    background: linear-gradient(rgba(68, 28, 179, 0.753), rgba(68, 28, 179, 0.753)), url(./survey-form-background.jpeg) no-repeat center center fixed, rgba(68, 28, 179, 0.753);; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
.container {
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.4;
    font-family: 'Poppins', sans-serif;
}
.headings {
    color: white;
    text-align: center;
}
.form-container {
    background: rgba(27, 27, 50, 0.8);
    margin: 0 auto;
    max-width: 65ch; /* max width of form */
    padding: 2em;
    text-align: left;
    border-radius: 5px;
}
label {
    color: white;
    display: flex;
    font-size: 1.125rem;
    margin-bottom: 10px;
}
input {
    width: 100%;
    height: 1.5rem;
    padding: 0.375rem 0.01rem;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    margin-bottom: 30px;
}
select {
    width: 100%;
    height: 2.4rem;
    padding: 0.375rem 0.01rem;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    margin-bottom: 30px;
}
.checkboxes {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.checkboxes input {
  /*width: auto;*/
  width: 4ch; /* define an explicit width */
  margin-bottom: 0;
  margin-left: 0;
  margin-right: .4rem; /* controls the space between checkbox and text */
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Project 05</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="headings">
            <h1 >freeCodeCamp Survey Form</h1>
            <h3><em>Thank you for taking the time to help us improve the platform</em></h3>
        </div>
        <div class="form-container">
            <label for="">Name</label>
            <input type="text" required>
            <label for="">Email</label>
            <input type="email" required>
            <label for="">Age (optional)</label>
            <input type="number" required>
            <label for="">Which option best describes your current role?</label>
            <select name="" id="">
                <option disabled> Select an option</option>
                <option class="container" value="challenges">Challenges</option>
                <option class="container" value="projects">Projects</option>
                <option class="container" value="community">Community</option>
                <option class="container" value="opensource">Opensource</option>
            </select>
            <label for="">What is your favorite feature of freeCodeCamp?</label>
            <label class="checkboxes"><input type="checkbox"> One</label>
            <label class="checkboxes"><input type="checkbox"> Two</label>
            <label class="checkboxes"><input type="checkbox"> Three</label>
        </div>
    </div>
</body>
</html>

Welcome learning html. There are indeed some issues in your code you could do better. I only refer to the last part you asked for.


//##### HTML (part)

<!-- 
Semantic: this is not a label, it is a (sub)heading
Change tag to a 'h?' and style it
-->

<h2>What is your favorite feature of freeCodeCamp?</h2>

<!--
Your inputs are general styled to have 100% width.
So following elements move to next line.
No good/clean solution.
Here: checkboxes are inputs and now move your elements in unwanted direction.
And they have margin bottom.
Quick workarround here: 
Overwrite css for checkboxes
Add linebreaks at the end of the line to move following labels/checkboxes to next line
-->

<label class="checkboxes"><input type="checkbox"> One</label><br>
<label class="checkboxes"><input type="checkbox"> Two</label><br>
<label class="checkboxes"><input type="checkbox"> Three</label><br>


//#### CSS - overwriting general stylings

input[type=checkbox] {
    width: auto;
    height: inherit;
    margin-bottom: 0;
}

Related