2

I created a sample for float right in CSS. However, I have a problem with the order and space between elements. This is the result:

enter image description here

The gray dots should be one right side of and even to the green dots.

Any thought? Thanks,

This is my code sample: https://jsfiddle.net/dalenguyen/mfj78LL5/

.green-dot {
  color: #20b08f;
}
.gray-dot {
  color: grey;
}
.professional-skills .green-dot,
.professional-skills .gray-dot {
  float: right;
  font-size: 1.5rem;
}
<div class="professional-skills">
  <h2>Professional Skills</h2>
  <p>Web Design <span class="green-dot">&#x25cf; &#x25cf; &#x25cf; &#x25cf; &#x25cf;</span></p>
  <p>Design <span class="green-dot">&#x25cf; &#x25cf; &#x25cf;</span><span class="gray-dot">&#x25cf; &#x25cf;</span></p>
</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Dale Nguyen
  • 1,930
  • 3
  • 24
  • 37

5 Answers5

4

The CSS float property was never designed for building layouts.

It was designed for wrapping text around images.

Because CSS didn't offer a better layout system, floats (and tables, inline-block and absolute positioning) have been used as hack workarounds.

But now that CSS3 flex and grid modules have wide browser support, and they are specifically designed for building layouts, you should consider using them.

section {
  display: flex;
}

section > * {
  margin: 0;
  align-self: center;  /* 1 */
}

section > p {
  margin-right: auto;  /* 2 */
}

.green-dot {
  color: #20b08f;
  font-size: 1.5rem;
}

.gray-dot {
  color: grey;
  font-size: 1.5rem;
}
<article class="professional-skills">

  <h2>Professional Skills</h2>

  <section>
    <p>Web Design</p>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
  </section>

  <section>
    <p>Design</p>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="gray-dot">&#x25cf;</span>
    <span class="gray-dot">&#x25cf;</span>
  </section>

</article>

Notes:

  1. Learn about the align-self property here:

  2. Learn about auto margins here:

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1

You can update the following:

.professional-skills .green-dot, .professional-skills .gray-dot
{
  float: right;
  font-size: 1.5rem;
  padding-right:5px;
}

and it will work but it's better to keep each circle in a sperate DIV.

Mhd.Jarkas
  • 406
  • 2
  • 5
  • 11
1

When using float:right; the element that is the furthest is always the one that is present before in the HTML code. If you want your gray dots to be right to your green dots, you have to insert them first in your HTML.

Also, as said before, you will probably want to split up your dots into separate containers, that way you will have a complete control over spacing (instead of using spaces between your dots). See Mhd.Jarkas's answer.

Hope it helps!

1

The uneven spacing is caused by float: right;, in your example there are 2 spans in the second row compare to only 1 span in the first row, and floating removes the white space rendering between the tags.

You can wrap every single dot into a span, that will fix the spacing issue, and be aware of the reversed order that caused by floating.

.green-dot {
  color: #20b08f;
}

.gray-dot {
  color: gray;
}

.professional-skills .green-dot,
.professional-skills .gray-dot {
  float: right;
  font-size: 1.5rem;
  margin-left: 5px;
  margin-top: -5px;
}
<div class="professional-skills">
  <h2>Professional Skills</h2>
  <p>
    Web Design
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
  </p>
  <p>
    Design
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="green-dot">&#x25cf;</span>
    <span class="gray-dot">&#x25cf;</span>
    <span class="gray-dot">&#x25cf;</span>
  </p>
</div>
Stickers
  • 75,527
  • 23
  • 147
  • 186
0

Try this

.green-dot,.green-dot-1 {
  color: #20b08f;
}
.gray-dot,.gray-dot-1 {
  color: grey;
}

.professional-skills .green-dot, .professional-skills .gray-dot{
  float: right;
  font-size: 1.5rem;
}
#dots{
font-size: 1.5rem;
float : right;
}
#abc{
float: left;
}
#clear {
clear: both;
}
.green-dot-1{
padding-right: 5px;
}
<div class="professional-skills">
  <h2>Professional Skills</h2>
  <p>Web Design <span class="green-dot">&#x25cf; &#x25cf; &#x25cf; &#x25cf; &#x25cf;</span></p>
  <p id="abc">Design </p>
<div id="dots">  
  <span class="green-dot-1">&#x25cf; &#x25cf; &#x25cf;</span><span class="gray-dot-1">&#x25cf; &#x25cf;</span>
  </div>
  <div id="clear"></div>
</div>
</div>
Nimish
  • 1,006
  • 1
  • 7
  • 19