Reputation: 119
I have a color_wheel element I'm trying to keep within the boundaries of the parent div (speech_bubble_middle_bar).
I know the correct way to do this is to have an absolute positioned div within a relative positioned div, so I tried creating a parent container. I've been unable to get ANY relative positioned divs to stay centered on the screen though, and when the div is absolute positioned I can't get it to stay within the constraints of the relatively positioned parent div.
This is what I have so far, could someone help me position this div so that it stays within the grey colored div? In my actual code I have some other elements within speech-bubble-middle-bar so I need to avoid changing that div in any way that would affect other elements, but I can create a container div for color_wheel if needed.
Here's a working code-pen with the full code: https://codepen.io/TheNomadicAspie/pen/dyWVLyx
<div id="speech_bubble_middle_bar" class="speech-bubble-middle-bar">
<div id="color_wheel" class="color-wheel"></div>
</div>
.speech-bubble-middle-bar {
grid-row: 2/3;
padding-left: 1%;
padding-right: 1%;
padding-top: 1%;
padding-bottom: 1%;
height: 100%;
background-color: grey;
}
.color-wheel {
position: absolute;
left: 50%;
transform: translate(-50%, 0);
}
And this is how it looks now.
Upvotes: 1
Views: 50
Reputation: 2577
This is tricky because two things seem to be in tension with one another:
The colorpicker has a height greater than 300px. On some screens, the grey box will adaptively shrink to be less than 300px tall, and since the Iro Colorpicker tool is explicitly sized, it will not shrink and hence overflow out of the box. So, you have two options:
width
as one of the options.For example, option 2 can be accomplished rather cheaply using the following code, though perhaps you will need something more sophisticated.
var background_color_wheel = new iro.ColorPicker(color_wheel, {
width: 0.12 * window.innerHeight // 12% of the window's height
})
The code is a bit complex for me to dive into the specifics on how to accomplish either option; ultimately, it's a compromise between a statically sized element and a flexibly sized layout.
There's also an option 3, which happens to be my favorite though admittedly far less flashy.
input
tag can have type="color"
, which will open the user's native color picker. It's flexible, semantic, and accessible. See more on MDN: Input Color TypeUpvotes: 1