1

i try to fit span in another span but for some reason it doesnt work.

I have already tried display: flex and display: flexbox.

JSFiddle

html,
body {
  height: 100%;
  width: 100%;
}

body {
  margin: 0;
  background-color: #f0f0f0;
}

#midbox {
  display: block;
  position: absolute;

  width: 90%;
  height: 85%;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);

  background-color: #7d7c7d;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}

#preview {
  display: block;
  position: relative;

  height: 100%;
  width: 35%;

  background-color: #525052;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}

#preview p {
  margin: 0;
  padding-top: 3%;
  font-size: 2em;
  opacity: 0.7;

  color: #f0f0f0;
  font-family: 'Lato', sans-serif;
  text-align: center;
}

#cont {
  display: block;
  position: relative;

  height: 100%;
  width: 35%;

  background-color: green;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}
<span id="midbox">
  <span id="preview">
    <p>Preview</p>
  </span>
  <span id="cont">
    <p>Why does this apper under the box and not on the left?</p>
  </span>
</span>

Expectation:

Content of span with green background should be inside the lightgray box.

Output:

Content of span with green background is outside of any box.

0

3 Answers 3

0

Can you try adding float:left; to both your #preview span and #cont span. Float left forces two elements to stay on the left side.

#preview, #cont{
  float:left;
}

I updated your fiddle. Try the fiddle here: https://jsfiddle.net/edy0whkp/

0

Short Answer

You're setting display: block to your span elements. This turns them into block elements which start on a new line and they expand the size of their parent container. Changing the display to dispay: inline-block will cause the elements to not start a new line and to only take up as much space as they need.

https://jsfiddle.net/xypntkc0/

More details

In the JSFiddle I changed the parent element to be a div instead. Setting it to a div makes it a block component so you also don't need the display: block when it's a div. It's bad practice to place block elements inside of inline elements (you have a paragraph tag as a child tag to your span tags) So I would actually change all your span tags to divs

I also changed the position to position: relative on the parent component. You typically only want to set absolute to the children elements inside of a parent component that has position: relative. The parent is set as relative so that their absolute positioned children get positioned relative to the parent.

Even more detail

If you want to align elements next to each other inside of a container, a good tool to use is flexbox. You can set display: flex to the parent element to mark it as a flex container. Then the children will automatically be set as flex items and will render side by side and boom, you're done. https://jsfiddle.net/vkru8wg7/

0

It looks like you're trying to make #preview and #cont sit side by side within #midbox. If this is the case, simply make #midbox {display: flex;}

body {
  margin: 0;
  background-color: #f0f0f0;
}

#midbox {
  display: flex;
  position: absolute;
  width: 90%;
  height: 85%;
  top:0; right:0; bottom:0; left:0;
  margin:auto;
  background-color: #7d7c7d;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}

#preview {
  display: block;
  position: relative;
  width: 35%;
  background-color: #525052;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}

#preview p {
  margin: 0;
  padding-top: 3%;
  font-size: 2em;
  opacity: 0.7;
  color: #f0f0f0;
  font-family: 'Lato', sans-serif;
  text-align: center;
}

#cont {
  display: block;
  position: relative;
  width: 35%;
  background-color: green;
  box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.75);
}
<span id="midbox">
  <span id="preview">
    <p>Preview</p>
  </span>
  <span id="cont">
    <p>Why does this apper under the box and not on the left?</p>
  </span>
</span>

If you want to learn about flexbox there's a great article at https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Not the answer you're looking for? Browse other questions tagged or ask your own question.