I am trying to add an element on all the 4 sides of the div.
I don't want to use box-sizing as I have to apply event listeners on the elements that I would place on all the 4 sides.
I am able to add it to the left and right but how can I add it on all the 4 sides? And this also is not an elegant way.
.box {
width: 100px;
height: 100px;
background: yellow;
text-align: center;
font-size: 0.8rem;
position: relative;
}
#sideEl {
background-color: red;
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
#sideEl2 {
background-color: red;
position: absolute;
right: 0;
bottom: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
<div class="box">
<div id="sideEl">
<div id="sideEl2">
</div>
I had referred Placing 4 html elements in every corner of a div . But not able to get an idea how to place them along side borders
I swapped the height and width parameters for horizontally aligned elements so they are showed as intended. Also you should only be using one of those option ( top right bottom left ) to align element to one of four sides. Here's the code:
.box {
width: 100px;
height: 100px;
background: yellow;
text-align: center;
font-size: 0.8rem;
position: relative;
}
#sideEl {
background-color: red;
position: absolute;
top: 0;
height: 4px;
width: 100%;
cursor: w-resize;
}
#sideE2 {
background-color: brown;
position: absolute;
left: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
#sideE3 {
background-color: blue;
position: absolute;
right: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
#sideE4 {
background-color: green;
position: absolute;
bottom: 0;
height: 4px;
width: 100%;
cursor: w-resize;
}
<div class="box">
<div id="sideEl"></div>
<div id="sideE2"></div>
<div id="sideE3"></div>
<div id="sideE4"></div>
</div>
Add the two other divs and use :
a class to identify (style in other words) a side, let's call it side, that'll be used by the divs that act as sides. This class holds mutual styles for the sides like the background-color and the position.
two additional classes :
side-h which is used by the sides that are horizontal (top and bottom sides). It holds the width and height for these specific sides as they do share the same values. Also, these sides have cursor: n-resize rule for a vertical cursor.
side-v which is used by the sides that are vertical (right and left sides). It holds the width and height for these specific sides as they do share the same values. Also, these sides have cursor: w-resize rule for an horizontal cursor.
the #side-top and #side-left hold the same values for top and left rules.
the #side-bottom and #side-right hold the same values for bottom and left rules.
.box {
width: 100px;
height: 100px;
background: yellow;
text-align: center;
font-size: 0.8rem;
position: relative;
}
.box .side {
position: absolute;
background-color: red;
}
.box .side.side-h {
width: 100%;
height: 4px;
cursor: n-resize;
}
.box .side.side-v {
width: 4px;
height: 100%;
cursor: w-resize;
}
#side-top, #side-left {
top: 0;
left: 0;
}
#side-right, #side-bottom {
bottom: 0;
right: 0;
}
<div class="box">
<div class="side side-h" id="side-top"></div>
<div class="side side-v" id="side-right"></div>
<div class="side side-h" id="side-bottom"></div>
<div class="side side-v" id="side-left"></div>
</div>
the ordering of the elements inside .box can be changed without affecting the final result.
.box {
width: 100px;
height: 100px;
background: yellow;
text-align: center;
font-size: 0.8rem;
position: relative;
}
#sideTop {
background-color: red;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 4px;
cursor: w-resize;
}
#sideRight {
background-color: red;
position: absolute;
top: 0;
right: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
#sideBottom {
background-color: red;
position: absolute;
right: 0;
bottom: 0;
width: 100%;
height: 4px;
cursor: w-resize;
}
#sideLeft {
background-color: red;
position: absolute;
left: 0;
top: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
<div class="box">
<div id="sideTop"></div>
<div id="sideBottom"></div>
<div id="sideRight"></div>
<div id="sideLeft"></div>
</div>
I hope you may get the idea...
.main-container {
width: 400px;
height: 400px;
background-color: #ccc;
}
.child {
float: left;
}
.child:nth-child(odd) {
height: calc(100% - 50px);
width: 50px;
background-color: blue;
}
.child:nth-child(even) {
height: 50px;
width: calc(100% - 100px);
background-color: red;
}
.child:nth-child(4) {
width: 100%;
}
<div class="main-container">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Related
I'm trying to make a button that looks like this:
https://postimg.cc/HcG72DCf
and this:
https://postimg.cc/1nkXSpz2
Here is my codepen to try & copy it:
https://codepen.io/assilem4791/pen/KKBoLOm
.buttonContainer {
position: relative;
height: 200px;
width: 500px;
background-color: lightgray;
border-radius: 40px;
align-items: center;
}
.buttonstripes {
position: absolute;
background-color: gray;
height: 5%;
width: 100%;
left: 0;
z-index:1;
}
#stripe1 {
top: 30%;
}
#stripe2 {
top: 40%;
}
#stripe3 {
top: 50%;
}
#stripe4 {
top: 60%;
}
#stripe5 {
top: 70%;
}
.offAir {
postion: absolute;
height: 80%;
width: 70%;
background-color: lightyellow;
border-radius: 40px;
font-size: 50px;
align: center;
z-index: 2;
}
<div class="buttonContainer">
<div class="buttonstripes" id="stripe1"></div>
<div class="buttonstripes" id="stripe2"></div>
<div class="buttonstripes" id="stripe3"></div>
<div class="buttonstripes" id="stripe4"></div>
<div class="buttonstripes" id="stripe5"></div>
<div class="offAir"> <p>OFF AIR</p>
</div>
I'm having trouble with making the stripes sit behind the offAir div.
Any help would be appreciated!
I thought putting a div inside another div with a z-index would work.
I'd like to have an image with a div that covers the image exactly. I can get the div to overlay the image by using position: relative in the parent and position: absolute for the div, but background-color fills out the padding in the parent so they aren't overlayed properly.
Here's a snippet that demonstrates the problem.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
I'm able to get it pretty close with some calc()'s to subtract the padding. This almost works, but the div fills out a little too much at the bottom. I'd like to not have a bunch of hardcoded values for padding anyway, so I wouldn't really like this solution even if it did work entirely.
Here's a snippet that shows the calc() approach.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: calc(100% - 2 * 10px);
height: calc(100% - 2 * 10px);
border-radius: 13px;
left: 10px;
top: 10px;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
This snippet does things a slightly different way, putting the img inside the overlay div and putting the actual green, lower opacity overlay as the overlay div's after pseudo element.
This way you don't have to build in any knowledge of the parent's padding.
.parent {
position: relative;
padding: 10px;
width: 40%;
background: red;
height: fit-content;
}
.image {
width: 100%;
border-radius: 13px;
top: 0;
left: 0;
}
.overlay {
position: relative;
padding: 0;
width: fit-content;
height: fit-content;
}
.overlay::after {
content: '';
position: absolute;
background-color: green;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
padding: 0px;
}
<div class="parent">
<div class="overlay"> <img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156"></div>
</div>
When using HTML5, browser adds some padding to the bottom of the img tag. This can be avoided by making the image a block element. So just adding display: block to class .image and then it good.
And btw, to define witdh/height of an absolute element, beside calc() you can also define 4 values top, right, bottom, left of it.
:root {
--custom-padding: 10px;
}
.parent {
position: relative;
padding: var(--custom-padding);
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
display: block;
}
.overlay {
position: absolute;
background-color: red;
border-radius: 13px;
bottom: var(--custom-padding);
right: var(--custom-padding);
left: var(--custom-padding);
top: var(--custom-padding);
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
Consider the following jsfiddle:
https://jsfiddle.net/0fwhmhLe/
html markup:
<div class="city-losangeles-bg">
<div class="user-container user-container-losangeles">
<div class="user-pic user-pic-losangeles"></div>
<div class="user-name-container">
<p class="user-name">User Name</p>
<div class="user-name-mask"></div>
</div>
<hr class="underline">
<div class="ellipse-container">
<div class="ellipse ellipse-losangeles-1"></div>
<div class="ellipse ellipse-losangeles-2 ellipse-with-left-margin"></div>
<div class="ellipse ellipse-losangeles-3 ellipse-with-left-margin"></div>
</div>
</div>
<p class="user-text user-text-losangeles">Some text that needs to be below the user-container div, based on the position and height of user-container</p>
</div>
css:
.city-losangeles-bg
{
width: 100%;
height: 1230px;
top: 0px;
background-color: orange;
position: relative;
}
.user-container
{
position: relative;
width: 206px;
height: 192px;
background-color: green;
}
.user-container-losangeles
{
left: 41%;
top: 25px;
}
.user-pic
{
position: relative;
width: 73px;
height: 73px;
left: -36.5px;
margin-left: 50%;
border-radius: 50%;
border: none;
}
.user-pic-losangeles
{
background-color: red;
}
.user-name-mask
{
position: relative;
width: inherit;
height: inherit;
top: 0;
}
.user-name
{
position: relative;
font-family: Ariel;
font-size: 28px;
text-align: center;
width: 100%;
/*top: -6px;*/ /*so text hides properly under color bar reveal animation */
}
.underline
{
position: absolute;
width: 178px;
top: 138px;
left: 14px;
margin-top: 0;
margin-bottom: 0;
}
.ellipse-container
{
position: absolute;
width: 126px;
height: 30px;
top: 162px;
left: 40px;
}
.ellipse
{
position: relative;
width: 30px;
height: 30px;
float: left;
border-radius: 50%;
border: none;
}
.ellipse-with-left-margin
{
margin-left: 18px;
}
.ellipse-losangeles-1
{
background-color: #4574b4;
}
.ellipse-losangeles-2
{
background-color: #71c8ca;
}
.ellipse-losangeles-3
{
background-color: #e6dddd;
}
.user-text
{
position: relative;
margin-top: 0; /* 100 */
font-family: Ariel;
font-size: 26px;
text-align: center;
line-height: 50px;
color: #848484;
}
.user-text-losangeles
{
margin-left: 29%;
width: 50%;
}
I can't figure out how to make the paragraph tag user-text user-text-losangeles always be below the div user-container user-container-losangeles. I thought they should automatically stack and if I changed user-container-losangeles's top property that user-text-losangeles would get bumped down as well.
Someone tell me what obvious mistake I am making please!!
You can use padding-top: 25px; on the container (.city-losangeles-bg) instead of the top:25px; of .user-container-losangeles
https://jsfiddle.net/y8pocwsn/1/
The reason: With position:relative and a topsetting an element is simply moved down from its original position, but the subsequent elements are NOT moved. The space reserved for the element is still the space it would occupy with top: 0 , which is the same as if that element would have position: static
I have a kind of "range display", where I use elements to display the current position within a range. See the example https://jsfiddle.net/juwxdb5m/ or the following code.
HTML:
<h1>Range display with fixed sizes (works correctly)</h1>
<div class="my-fixed-frame">
<div class="my-fixed-chart">
<div class="my-fixed-point" style="bottom:0%;left:0%;"></div>
<div class="my-fixed-point" style="bottom:50%;left:50%;"></div>
<div class="my-fixed-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
<h1>Range display with relative sizes (works incorrectly)</h1>
<div class="my-relative-frame">
<div class="my-relative-chart">
<div class="my-relative-point" style="bottom:0%;left:0%;"></div>
<div class="my-relative-point" style="bottom:50%;left:50%;"></div>
<div class="my-relative-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
CSS:
.my-fixed-frame {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.my-fixed-chart {
background-color: silver;
display: inline-block;
bottom: 8px; left: 8px; right: 8px; top: 8px;
margin: auto;
position: absolute;
}
.my-fixed-point {
background-color: lime;
height: 16px;
margin-bottom: -8px;
margin-left: -8px;
position: absolute;
width: 16px;
}
.my-relative-frame {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.my-relative-chart {
background-color: silver;
display: inline-block;
bottom: 25%; left: 25%; right: 25%; top: 25%;
margin: auto;
position: absolute;
}
.my-relative-point {
background-color: lime;
height: 50%;
margin-bottom: -25%;
margin-left: -25%;
position: absolute;
width: 50%;
}
When I use fixed sizes, I can implement the design as desired. The "point" elements are within the parent element, respectively within its frame.
But I didn't found a solution, when I use relative sizes for the child elements.
Maybe this is what you want:
https://jsfiddle.net/xoq95xaa/
The main changes are that I took the green squares out of the inner container (which is what you kind of did using negative margins in the first version), removed any margins, inserted a forth element (reacting to your comment), changed the size to 25% width and height and changed the bottom and left values to 25% steps (0, 25, 50, 75).
I found a solution which works as desired, see also https://jsfiddle.net/juwxdb5m/1/.
HTML:
<h1>Range display with relative sizes</h1>
<div class="range-display">
<div class="range-cocoon">
<div class="range-point" style="bottom:0%;left:0%;"></div>
<div class="range-point" style="bottom:33.333%;left:33.333%;"></div>
<div class="range-point" style="bottom:66.666%;left:66.666%;"></div>
<div class="range-point" style="bottom:100%;left:100%;"></div>
</div>
</div>
CSS:
.range-display {
background-color: gray;
height: 90px;
position: relative;
width: 160px;
}
.range-cocoon {
background-color: silver;
bottom: 0;
height: 75%;
left: 0;
position: absolute;
width: 75%;
}
.range-point {
background-color: lime;
height: 33.333%;
position: absolute;
width: 33.333%;
}
How can I insert an element between and its child and its grandson?
I have a markup like this:
<div id="main">
<div id="img-container">
<img id="img">
</div>
</div>
And the styles are:
#main {
margin-top: 300px;
position: relative;
z-index: 1;
}
#img-container {
margin-top: -150px;
position: relative;
z-index: 0;
}
#img {
display: block;
position: relative;
z-index: 2;
}
Now the order must be
img-container
main
img
How it works now:
How it is expected to work:
(Thanks to #ralph.m for images)
You can really just get that visual effect without having to reorder layers etc. You can reverse the styles on those elements to get that appearance. Or you could do something even simpler like this:
#main {
position: relative;
background: #e7e7e7;
width: 600px;
padding: 0 50px;
margin: 50px;
}
#main::after {
content: '';
width: 500px;
height: 200px;
position: absolute;
z-index: -1;
left:50%;
margin-left: -250px;
top: -50px;
background: #30353b;
}
#img-container {
position: relative;
width: 400px;
top: -20px;
margin: 0 auto;
}
<div id="main">
<div id="img-container">
<img src="https://unsplash.it/400/200">
</div>
</div>
Question isn't clear, but are you just looking for something like this? (It basically involves replacing margin-top with top on the img-container.)
#main {
margin-top: 100px;
position: relative;
z-index: 1;
background: #e7e7e7;
width: 500px;
padding: 0 40px;
}
#img-container {
top: -50px;
position: relative;
z-index: 0;
background: #30373b;
width: 400px;
padding: 40px;
}
#img {
display: block;
position: relative;
z-index: 2;
}
<div id="main">
<div id="img-container">
<img id="img" src="https://unsplash.it/400/200">
</div>
</div>