1

I want to display a variable number of elements with content in perfect circles, well aligned. For that I use Bulma and its columns and box, like this:

<div class="columns is-centered is-vcentered is-8 is-variable">
  <div class="column is-2">
    <div class="box has-text-centered">
       A
    </div>
  </div>
  <div class="column is-2">
    <div class="box has-text-centered">
       B
       more text in this element
     </div>
  </div>
</div>

Making the boxes round is easy

.box {
  border-radius: 50% 50%;
}

.box::after {
  content: "";
  display: block;
  padding-bottom: 84.4%;
}

And the ::after Element trick seems to be the way to go to achieve square elements of relative size (I had to use 83.4% because a .column.is-2 is roughly 16.6% wide). However, as soon as the boxes receive content, the squariness is lost.

I tried other ::before and ::after tricks (e.g. with display:table) to no avail. I also tried to set the position of the after element absolutely.

How can I make the columns (or probably rather the boxes?) square?

Fiddle at: https://jsfiddle.net/2tukdp8w/

Of course it would be great if all Bulma-comfort (responsiveness, column-gaps, ...) would be preserved.

Felix
  • 4,510
  • 2
  • 31
  • 46
  • the content inside need to be positionned using position:absolute – Temani Afif Jun 27 '20 at 16:41
  • check the end of this answer: https://stackoverflow.com/a/10441480/8620333 – Temani Afif Jun 27 '20 at 16:42
  • 1
    https://jsfiddle.net/sheriffderek/Lbjcw8gp/ – sheriffderek Jun 27 '20 at 16:43
  • "display a variable number of elements with content in perfect circles" - this shouldn't have been closed. That other answer - (could be helpful) - but is not an answer to this specific question. – sheriffderek Jun 27 '20 at 16:44
  • @sheriffderek I quickly checked your fiddle and it looks like this would have become the accepted answer. I will look into it post-weekend. Thanks a lot. Also, I agree that the other answer doesnt seem to adress my point but don't know what do to about the closure. – Felix Jun 27 '20 at 20:49
  • 1
    @sheriffderek I casted a reopen vote. If the question is reopened, please post your answer, and I will accept it. Although it doesnt fully build on top of bulmas columns, it works the way I'd like it to work. I'd add some centering to the list, though - makes it look nicer. – Felix Jun 28 '20 at 12:19
  • I'd love to hear more about why you like Bulma. We have a CSS specific discord: https://discord.gg/pFc6XmH if you ever need help or want to talk about CSS stuff. : ) – sheriffderek Jun 28 '20 at 14:14
  • I also updated that jsFiddle with a grid exmaple. – sheriffderek Jun 28 '20 at 14:18

1 Answers1

1

You might want a list - or maybe not, depending on the meaning of the content. I appreciate that you are trying to use less elements and instead a pseudo element, but consider a custom-element as a way to 'not use' an unsemantic 'div' or something - and win in both ways.

<ul class="thing-list">
  <li class="thing">
    <circle-component>
      <span>Text in circle</span>
    </circle-component>
  </li>
  
  <li class="thing">
    <circle-component>
      <span>Longer text in circle</span>
    </circle-component>
  </li>
  
  <li class="thing">
    <circle-component>
      <span>Longer text in circle</span>
    </circle-component>
  </li>
  
  <li class="thing">
    <circle-component>
      <span>Longer text in circle</span>
    </circle-component>
  </li>
</ul>

From there - there are many ways to do this. You could use the height: 0; padding-bottom: 50%; type trick, but it might cause you more trouble than being squishy is worth in this case.

Here is the setup for using flex-box / and also a grid alternative. Don't be afraid to use custom-elements! They are easy to read - and well, it's 2020! : )

I'm sure that Bulma has some neat conventions, but in this case I see no reason to use any framework. CSS in 2020 provides us a really lovely set of options.

/* reset + project setup */
* {
  box-sizing: border-box; /* look this up if you don't already use it https://www.paulirish.com/2012/box-sizing-border-box-ftw/ */
}


/* components FIRST */
circle-component {
  display: block; /* custom components are inline by default */
  width: 120px;
  /* one of the few places I would EVER set an explicit height or width */
  height: 120px; 
  border-radius: 50%;
  border: 1px solid red;
  display: flex;
  flex-direction: column; /* one way to do that... (center) */
  align-items: center;
  justify-content: center;
  padding: 10px;
  text-align: center;
}


/* then... context */
.thing-list {
  display: flex; /* flex version */
  flex-direction: row;
  flex-wrap: wrap; /* let it break to the next line */
}

.thing-list.grid-version {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); /* depends on what you want to happen */
  border: 1px solid blue;
}

.thing-list .thing {
  padding: 10px; /* maybe */
}



/* you could totally use position absolute for the text if you want - but it's nice to not need that - and let the text flow naturally */

fiddle: https://jsfiddle.net/sheriffderek/Lbjcw8gp/

A few thoughts on flex-box patterns to help solidify them: Almost all of the UI layout comes down to just this

sheriffderek
  • 8,848
  • 6
  • 43
  • 70