alex
alex

Reputation: 611

keep children div inside of parent div

Okay, hi, I feel like this is a very silly question, and I've found a lot of questions like it, but none of the answers seemed to work for me.

My issue is that have one div (taskbar) and inside of it another div (taskbar-bar) and I want taskbar-bar to stay within taskbar. I tried putting position on absolute and relative and it didn't seem to work at all, it's always underneath the taskbar div. I could push it up with top, but I don't feel like that's the way to go right now. I don't know, though, I'm honestly very new to CSS and HTML and still am learning.

Here's a jsfiddle of my code: https://jsfiddle.net/5zghzczs/3/

.taskbar {
    width: 100%;
    height: 40px;
    box-shadow: 0px 1px 0px 0px #C2C5CA inset, 0px 2px 0px 0px #FFF inset;
    background-color: #C2C5CA;
    position: absolute;
    left: 0;
    bottom: 0;
}
#taskbar-start {
    margin-top: 4px;
    margin-bottom: 2px;
    margin-left: 2px;
    width: 90px;
    height: 33px;
    background-color: #C2C5CA;
    cursor: pointer;
}
.taskbar-start-inactive {
    box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
}
.taskbar-start-active {
    box-shadow: -1px -1px 0px 0px #FFF inset, 1px 1px 0px 0px #000 inset, 2px 2px 0px 0px #868A8E inset;
}
.taskbar-start-frame-active {
    margin-top: 2px;
    margin-left: 2px;
    width: 84px;
    height: 27px;
    border-style: dotted;
    border-width: 1px;
    position: absolute;
}
.taskbar-start-logo {
    margin-top: 6px;
    margin-left: 3px;
    width: auto;
    height: 20px;
    -webkit-user-select: none;
}
.taskbar-start-text {
    margin-top: 10px;
    margin-left: 5px;
    display: inline;
    font-size: 12px;
    letter-spacing: -2px;
    -webkit-user-select: none;
    font-family: "Press Start 2P";
    position: absolute;
}
.taskbar-bar {
    height: 35px;
    width: 2px;
    background: green;
  margin-left: 100px;
}
<div class="taskbar">
   <div id="taskbar-start" class="taskbar-start-inactive">
            <div id="taskbar-start-frame">
                <img class="taskbar-start-logo" src="img/logo.png" /> 
                <div class="taskbar-start-text">Start</div>
            </div>
        </div>
  <div class="taskbar-bar"></div>

Upvotes: 1

Views: 4789

Answers (4)

Jeff Walters
Jeff Walters

Reputation: 4433

fit-content worked for me like a charm when dynamic text was forcing my modal wider.

max-width: fit-content;

More on fit-content https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content

Upvotes: 0

Mark R
Mark R

Reputation: 337

Your problem is that you are using margin-left, which is trying to give a margin in between one div and the other. Here is a new JSFiddle where I set the position to absolute, changed the margin-left to left, and added top: 0px; to set it to be at the top (overlaying the other div).

.taskbar-bar {
  position: absolute;
  top: 4px;
  left: 100px;
    height: 35px;
    width: 2px;
    background: green;
}

Upvotes: 1

Maarten van Tjonger
Maarten van Tjonger

Reputation: 1927

I think the best way to do this is to make the parent a flexbox container. This will position all children in a row.

display: flex; 

See https://jsfiddle.net/5zghzczs/7/

Read more about flexbox here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Upvotes: 1

sn3ll
sn3ll

Reputation: 1685

Change the "display" of taskbar-start and taskbar-bar to "inline-block"

.taskbar-start, .taskbar-start {
     display: inline-block;
}

Upvotes: 0

Related Questions