1

What I'm trying to accomplish here is having some content, in this case the word 'NO' (font-size big enough for the idea of course) and surround it with Master Yodas quote as some kind of 'border'.

#boxContent {
  font-size: 55px;
  font-weight: normal;
}

body {
  text-align: center;
  font-size: 10px;
  font-weight: bold;
}

#bodyFormat {
  margin: 100px;
  padding: 10px;
  background-color: salmon;
  width: 250px;
  height: 125px;
}

.boxSides {
  display: inline-block;
  border: solid 2px darkGreen;
  /*height: 100px*/
}

#LeftBorder {
  transform: rotate(-90deg);
  /*margin: 40px 0 0 0;*/
}

#RightBorder {
  transform: rotate(90deg);
  /*margin: -80px 0 0 100px;*/
}

#bottomBorder {
  transform: rotate(180deg);
}
<div id=bodyFormat>
  <!--Close Box top-->
  <div>DO OR DO NOT</div>

  <!--Close Box left side-->
  <span class="boxSides" id="LeftBorder">BE WITH YOU</span>

  <!--Inside Box-->
  <span id="boxContent">NO</span>

  <!--Close Box right side-->
  <span class="boxSides" id="RightBorder">MAY THE FORCE</span>

  <!--Close Box bottom-->
  <div id="bottomBorder">THERE IS NO TRY</div>
</div>

```

This is the closest I got, but you can guess it's not close enough.

This is how it looks (if you wonder, yes it's cut out)

I'd like it to be real tight, border-like, more or less in this dimensions (font-size)

I already tried changing properties from <span> to <div> to <p> with display changed from block, inline-flex/grid/block back and forth. Just as changing padding and margin values back and forth. Every other constellation got me to something like this:

Doesn't look bad but not what I want for now.

The only thing I found in search was this: How to create Border with text

And this answer: https://codepen.io/mrigankvallabh/pen/LYNYoMq

But first of all, I don't really get what is happening in the code (if someone is eager enough, feel free to explain :) And secondly, the parts I do understand show me it's a solution for this particular problem. Having a border around a paper, oriented rather on the outside, not the inside.

I read about fieldset and legend too, as well as moving text-blocks around on absolute onto a border with changing background but don't see an approach that would make this work, since there shall not be a line around the content.

And another thing, I wrote the solid border for the sides out of curiosity because the background (#bodyFormat - salmon) seems to stretch as wide as the text before transformation (rotation) would have been, if you catch my drift. But it looks like the box around the border text is shaped around content not consuming the space of rotation. Changing margins or width just shuffles everything again. (For width it changes exactly at 218px if you wonder too). How do I eliminate the background excess?

  • What you're missing is that `#bodyFormat` needs to be a `position: relative` parent and the border elements need to be `position: absolute` child elements. The pen you posted shows how to handle that. Check out [MDN: positioning](https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Positioning)... – Rene van der Lende Aug 21 '23 at 15:20
  • Thanks a lot for the exercise! So doing it now didn't work. Will go over the exercise and do it again. – Ras_Al_Ghul Aug 21 '23 at 15:39

1 Answers1

0

I was able to achieve what you're going for using CSS grid for layout, and wrapping the sides in p tags with position: absolute and setting parents' position: relative- to unlink the parent width from the length of the text. I also set a lot of things to display: flex to make centering content easier and changed all spans to divs for consistency.

Initial grid layout: enter image description here It often helps to color-code things in these situations to make it more clear what's going on. This problem is totally solvable without using grid, but I find it makes it easier to conceptualize.

Here's a visual of why I wrapped the side text with p tags: enter image description here As you can see, the whole div block is rotating, throwing off the layout of the grid.

So instead we make them child p elements, position them absolute, position parents relative, rotate, and voila:

enter image description here

(I should also point out that at this point I changed the grid layout a bit to make the top and bottom only in the center and left and right the full height, but it's the same idea.)

Now we just have to resize the bodyFormat div to taste and mess with the left and right distances of the p elements and everything should fall into place:

enter image description here

I should also note that I changed some font sizes to make everything big enough to work with and help it to fit together better, but feel free to tweak all of those numbers until it looks the way you want. The structure should hold up. I left the color-coding commented out, which should help with this.

#boxContent {
  font-size: 150px;
  font-weight: normal;
/*   background-color: green; */
  grid-column: 2;
  grid-row: 2;
  display: flex;
  align-items: center;
  height: .75em;
}

body {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
}

#bodyFormat {
  padding: 10px;
  background-color: salmon;
  width: 13em;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr 1fr;
}

.boxSides {
  display: flex;
  align-items: center;
  position: relative;
}

#LeftBorder {
  grid-column: 1;
  grid-row: 1 / 4;
/*   background-color: blue; */
}

#LeftBorder p {
  transform: rotate(-90deg);
/*   background: white; */
  position: absolute;
  width: 9em;
  left: -4em;
}

#RightBorder p {
  transform: rotate(90deg);
/*   background: white; */
  position: absolute;
  width: 9em;
  right: -4em;
}

#RightBorder {
  grid-column: 3;
  grid-row: 1 / 4;
/*   background-color: red; */
}
.topBorder {
  grid-column: 2;
  grid-row: 1;
/*   background-color: orange; */
}
#bottomBorder {
  grid-column: 2;
  grid-row: 3;
/*   background-color: yellow; */
}
<div id=bodyFormat>
  <!--Close Box top-->
  <div class="topBorder">DO OR DO NOT</div>

  <!--Close Box left side-->
  <div class="boxSides" id="LeftBorder"><p>BE WITH YOU</p></div>

  <!--Inside Box-->
  <div id="boxContent">NO</div>

  <!--Close Box right side-->
  <div class="boxSides" id="RightBorder"><p>MAY THE FORCE</p></div>

  <!--Close Box bottom-->
  <div id="bottomBorder">THERE IS NO TRY</div>
</div>
  • Is it bad manners to say thanks in comments now? Anyway you helped me twice, you have a solution including a little lecture and forcing me to read about grids so thanks^3 and have a good day/evening/night. – Ras_Al_Ghul Aug 21 '23 at 19:35
  • No prob! Not sure what the powers that be think about it but I appreciate it haha. Likewise! – Tyler Seppala Aug 21 '23 at 19:37
  • Only got time to revisit this thing today, I've got a question. Since you used 'display:flex' on the side divs anyway, couldn't we just use '

    ' and turn and tweak? And why do you use 'em'?

    – Ras_Al_Ghul Aug 23 '23 at 20:15
  • If you look at the 2nd picture in my answer, you'll see that the problem is the height of the side divs are set by the height of the boxContent div (since it has the tallest content), and even when they're rotated they keep that height— pushing them away from the center. Even though it now looks like a width, it's still considered a height. Nesting that text in a child element (doesn't really have to be p, could be anything) allows us to decouple the text height from the flow of the grid it's a part of, but keep its relative position (thus bringing it closer in). – Tyler Seppala Aug 24 '23 at 22:01
  • As for em vs px, see this: https://stackoverflow.com/questions/609517/why-em-instead-of-px – Tyler Seppala Aug 24 '23 at 22:01
  • Oh boy, I just realized that you must've simply turned off the background-color values for the parent on the sides at the last img. I was wondering how you shrank the hight and width properties to the childs. Thanks a lot. – Ras_Al_Ghul Aug 25 '23 at 10:52
  • Sure thing! The colors are still there, they're just fully eclipsed by the child elements. – Tyler Seppala Aug 25 '23 at 15:54