I still do not understand the float lesson


#1

In the 10. Float lesson, I still do not understand why the questions move around when you set the answers to float left. Can someone please explain?

https://www.codecademy.com/courses/learn-css-display-positioning/lessons/css-display-positioning/exercises/css-float?action=lesson_resume&course_redirect=learn-css


#2

please include a link to the exercise, so we know which exercise you talk about


#3

Sorry about that! I edited my original post.


#4

i can’t access the exercise yet, i haven’t gotten time for the new course.

Can you share the code?

the point of float: left is to move the element to the most left possible, see:

https://developer.mozilla.org/en-US/docs/Web/CSS/float


#5

.question {
  text-align: center;
  position: relative;
  top: 40px;
}

.answer {
  border: 1px solid #466995;
  margin: 20px;
  display: inline-block;
  height:150px;
  width:200px;
  float: left;
}

The thing I do not understand is that the answer boxes do not just move left. When I put the browser in full screen, they move up and they are all lined up together. Is that due to the inline-block display?


#6

when i said code, i meant full code (both html and css), so i can fiddle with it to understand your problem

you have the problem in front of you, i don’t, so i need to get a clear picture of it. It might be clear to you, not to me :wink:

it might be possible that there is no more room to move to the left?


#7
body {
  background-color: #FFF;
  margin: 0 auto;
}

header {
  background-color: #466995;
  border-bottom: 1px solid #466995;
  position: fixed;
  width: 100%;
  z-index: 10;
}

ul {
  margin: 30px auto;
  padding: 0 20px;
  text-align: center;
}

li {
  color: #FFF;
  font-family: 'Oswald', sans-serif;
  font-size: 16px;
  font-weight: 300;
  text-transform: uppercase;
  display: inline-block;
  width: 80px;
}

li:hover {
  color: #DBE9EE;
}

h1 {
  color: #466995;
  font-family: 'Oswald', sans-serif;
  font-size: 32px;
  font-weight: 300;
  text-transform: uppercase;
}

h2 {
  color: #333;
  font-family: 'Varela Round', sans-serif;
  font-size: 26px;
  font-weight: 100;
  margin: 0 auto 20px auto;
}

h3 {
  color: #466995;
  font-family: 'Oswald', sans-serif;
  font-size: 18px;
  text-align: center;
  font-weight: 700;
  text-transform: uppercase;
  padding: 30px;
}

h4 {
  color: #466995;
  font-family: 'Oswald', sans-serif;
  font-size: 18px;
  font-weight: 300;
  letter-spacing: 2px;
  text-align: center;
  text-transform: uppercase
}

p {
  color: #333;
  font-family: 'Varela Round', sans-serif;
  font-size: 18px;
}

footer {
  background-color: #DBE9EE;
  text-align: center;
  height: 100px;
}

.welcome {
  background-color: #DBE9EE;
  box-sizing: border-box;
  padding: 40px;
  text-align: center;
  width: 100%;
  position: relative;
  top: 50px;
}

.question {
  text-align: center;
  position: relative;
  top: 40px;
}

.answer {
  border: 1px solid #466995;
  margin: 20px;
  display: inline-block;
  height: 150px;
  width: 200px;
  float:left;
}

.answer:hover {
  background: #C0D6DF;
  color: #FFF;
}


<!DOCTYPE html>
<html>
<head>
  <title>Please Participate in Our Survey!</title>
  <link href="https://fonts.googleapis.com/css?family=Oswald:300,700|Varela+Round" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

  <header>
    <ul>
      <li>Question 1</li>
      <li>Question 2</li>
      <li>Question 3</li>
      <li>Question 4</li>
      <li>Question 5</li>
      <li>Question 6</li>
    </ul>
  </header>
  
  <div class="welcome">
    <h1><strong>Welcome</strong> to our survey!</h1>
    <p>We're looking forward to getting your answers so we can make sure our products and services are the best they can be!</p>
  </div>

  <div class="question">
    <h4>Question 1</h4>
    <h2>I like participating in physical activity such as running, swimming, or biking.</h2>

    <div class="answer">
      <h3>Disagree</h3>
    </div>

    <div class="answer">
      <h3>Neutral</h3>
    </div>

    <div class="answer">
      <h3>Agree</h3>
    </div>
  </div>

  <div class="question">
    <h4>Question 2</h4>
    <h2>I try to keep up to date with the latest fashion in active wear.</h2>

    <div class="answer">
      <h3>Disagree</h3>
    </div>

    <div class="answer">
      <h3>Neutral</h3>
    </div>

    <div class="answer">
      <h3>Agree</h3>
    </div>
  </div>

  <div class="question">
    <h4>Question 3</h4>
    <h2>I purchase clothing online regularly.</h2>

    <div class="answer">
      <h3>Disagree</h3>
    </div>

    <div class="answer">
      <h3>Neutral</h3>
    </div>

    <div class="answer">
      <h3>Agree</h3>
    </div>
  </div>

  <div class="question">
    <h4>Question 4</h4>
    <h2>I try to buy goods that are designed and/or manufactured in my home country.</h2>

    <div class="answer">
      <h3>Disagree</h3>
    </div>

    <div class="answer">
      <h3>Neutral</h3>
    </div>

    <div class="answer">
      <h3>Agree</h3>
    </div>
  </div>

  <div class="question">
    <h4>Question 5</h4>
    <h2>I look to famous athletes when trying to choose what to wear when training.</h2>

    <div class="answer">
      <h3>Disagree</h3>
    </div>

    <div class="answer">
      <h3>Neutral</h3>
    </div>

    <div class="answer">
      <h3>Agree</h3>
    </div>
  </div>
  <footer>
    <h3>Thanks for taking our survey!</h3>
  </footer>

</body>
</html>

Sorry! I thought the code was too long so I wasn’t sure… :persevere:

Thank you for being so patient with me.


#8

Perhaps adding a “clear” after the questions will help?

.question:after{
  display:block;
  content:'';
  clear:both; 
}

The way it is now, you’re not clearing the float which means all the “floats” will just pile up. Additionally, the use of float makes the inline-block rule pointless, assuming what I linked to is what you’re looking to do.


#9

@eldonmcguinness is right, by setting to float: left, if there is still room left the elements will go inline, you need to apply a clearance to keep them each on there own “line”


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.