overflow:scroll doesn't fill div / changes div size - html

I have this code:
<div style="position:absolute;left:0px;top:0px;width:17px;height:395px;background-color:white;border:1px solid black;">
<div style="position:inherit;;width:inherit;height:inherit;overflow:scroll;"></div>
<button style="position:absolute;left:0px;top:139px;width:17px;height:73px;background-color:white;border:1px solid black;"></button>
</div>
If you look at the generated HTML, you will see that there is a white square that is not filled by the scrollbar on the bottom. How does this happen? When I inspect the element, the overflow:scroll div does not have 395px, but 378px. When I correct it to 395px the scrollbar will fill the parent div visually, but there will be an overflow.
What is happening here?

This is simply the space for the horizontal scrollbar. You can make it visible by increasing your elements width:
.bar {
position: absolute;
left: 0px;
top: 0px;
width: 80px;
height: 395px;
background-color: white;
border: 1px solid black;
}
.inner {
position: inherit;
width: inherit;
height: inherit;
overflow: scroll;
}
button {
position: absolute;
left: 0px;
top: 139px;
width: 17px;
height: 73px;
background-color: white;
border: 1px solid black;
}
<div class="bar">
<div class="inner"></div>
<button></button>
</div>
You can avoid that space by using overflow-y instead of the more general overflow:
.bar {
position: absolute;
left: 0px;
top: 0px;
width: 17px;
height: 395px;
background-color: white;
border: 1px solid black;
}
.inner {
position: inherit;
width: inherit;
height: inherit;
overflow-y: scroll;
}
button {
position: absolute;
left: 0px;
top: 139px;
width: 17px;
height: 73px;
background-color: white;
border: 1px solid black;
}
<div class="bar">
<div class="inner"></div>
<button></button>
</div>
In general it's a good idea to seperate styles from markup. It's easier to avoid problems if not using inline styles.

Related

CSS: How to overlap circle element properly

What I want to do is to cover circle element with square. But I can still see circle border.
When I inspect the element, the child element size doesn't include the parent's border (118px x 118px) so I tried to remove box-sizing: border-box;. Even though child element size is 120px x 120px, the same thing still happens.
How can I cover the circle properly?
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>
The content itself starts within the border, not at the end of the border. As such you have to position the element out of the content area. Instead of using top, right, bottom, left you could simply use inset:
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
inset: -1px;
background-color: white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>
You can cover the circle properly by adding a border also to the square. and moving it a bit.
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
top: -1px;
left: -1px;
width: 100%;
height: 100%;
background-color: rgba(255,255,255,0.5);
border: 1px solid white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>

Is it possible to generate an HTML div with CSS ::after pseudo element

This may be a stupid question, but I have to clarify this fact. So this is my concern. I can style two div elements to look like below.
.element-container{
display: flex;
position: relative;
width: 300px;
height: 100px;
}
.element{
z-index:1;
width: 100%;
height: 100%;
position: absolute;
background-color: Transparent;
background-repeat:no-repeat;
border-radius: 20px;
display: inline-block;
border: 2px solid black;
}
.element-shadow{
z-index: -1;
top: 10%;
left: 4%;
border-radius: 20px;
background-color: yellow;
height: 100%;
width: 100%;
position: absolute;
}
<div class="element-container">
<div class="element"></div>
<div class="element-shadow"></div>
</div>
my question is can we do the same using ::after pseudo element. Basically can we add an html element after some other element rendered in to DOM (make the shadow effect after element is created, so someone does not need to concern about the actual size of the element when use it somewhere if the shadow element created with the same styles but with ::after pseudo element)
#Telary's answer is acceptable with this upper part of the question(original question) But now it directs me to another question, I was try to did the same with an <button>, but it does not work as expected. what did I miss here? Below code is my new problem
.but{
position: absolute;
background-color: Transparent;
background-repeat:no-repeat;
cursor:pointer;
outline:none;
border-radius: 500px;
display: inline-block;
border: 2px solid black;
color: black;
font-size: 250%;
padding: 20px 100px;
}
.but:after{
content:'';
z-index: -1;
top: 8%;
left: 3%;
border-radius: 500px;
display: inline-block;
background-color: rgba(140,122,230,1);
position: absolute;
}
<button class="but">GO</button>
Is it because I removed the outer <div> element?
You can use the code below to achieve the needed effect:
.element-container{
display: flex;
position:relative;
width: 300px;
height: 100px;
z-index: 1;
}
.element{
width: 100%;
height: 100%;
position: absolute;
background-color: Transparent;
background-repeat:no-repeat;
border-radius: 20px;
display: inline-block;
border: 2px solid black;
}
.element:after{
content:'';
display: inline-block;
top: 10%;
left: 4%;
border-radius: 20px;
background-color: yellow;
height: 100%;
width: 100%;
position: absolute;
z-index: -1;
}
<div class="element-container">
<div class="element"></div>
</div>
You need to remove z-index in ".element" selector, to put it on the top of "shadow" layer.

Overlapping box elements

Please look at the image to understand what I am talking about. I have three box elements that look like what is displayed in the picture. What I want is for the green box to only be displayed overlapping the yellow and not displayed over overlapping the red. The green box needs to reside overlapping both but only visible over the yellow area. Ive tried using z-index, position and opacity in every different manner I could think of, but yet to come up with a solution.
link to image
<div id="one" ></div>
<div id="two" >
</div><div id="three" ></div>
#one{
border: solid 1px black;
width: 300px;
height: 300px;
background-color: red;
position: absolute;
}
#two{
margin-left: 50px;
border: solid 1px black;
width: 200px;
height: 200px;
background-color: yellow;
position: absolute;
}
#three{
border: solid 1px black;
width: 100px;
height: 100px;
background-color: green;
position: relative;
}
It's impossible to have elements overlap one layer and then go underneath another layer like you are asking. I know there is some art term for this.
Anyways here is the closest solution is to just fake it and have the green box inside the yellow box:
.outer {
width: 300px;
height: 300px;
position: relative;
outline: 1px solid black;
}
.green {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: -50px;
outline: 1px solid black;
background-color: green;
z-index: 3;
}
.yellow {
width: 200px;
height: 200px;
position: absolute;
left: 0;
right: 0;
top: 0;
margin: auto;
outline: 1px solid black;
background-color: yellow;
z-index: 2;
overflow: hidden;
}
.red {
width: 300px;
height: 300px;
position: absolute;
left: 0;
top: 0;
outline: 1px solid black;
background-color: red;
z-index: 1;
}
<div class="outer">
<div class="yellow">
<div class="green"></div>
</div>
<div class="red"></div>
</div>

Align link to bottom of div and center it

I'm trying to make a link stick to the bottom center of a div and have it be centered.
So far I've come up with this:
http://jsfiddle.net/r494Lx0r/2/
div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red;
}
div.container div.text {
position: absolute;
bottom: 0px;
border: solid 1px black;
}
Now how do I make it so that it's centered? I've tried adding text-align:center; and margin:0 auto; to the container but neither of those do anything.
Does anyone know how to do this?
UPDATE add text-algin: center to the parent to center the anchor and set border: solid 1px black; to your anchor:
div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red;
}
div.container div.text {
position: absolute;
bottom: 0px;
right: 0;
left: 0;
text-align: center;
}
a{border: solid 1px black;}
<div class="container">
<div class="text">
Google.com
</div>
</div>
Add Width: 100% and text-align: center
div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red;
}
div.container div.text {
position: absolute;
bottom: 0px;
text-align: center;
width:100%;
border: solid 1px black;
}
<div class="container">
<div class="text">
Google.com
</div>
</div>
or left: 0;, right: 0; and text-align: center;
div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red;
}
div.container div.text {
position: absolute;
bottom: 0px;
left: 0;
right: 0;
text-align: center;
border: solid 1px black;
}
<div class="container">
<div class="text">
Google.com
</div>
</div>
or you can combine `margin-left: 50%;` and `transform: translate(-50%)`
div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red
}
div.container div.text {
position: absolute;
bottom: 0px;
border: solid 1px black;
margin-left: 50%;
-webkit-transform: translate(-50%);
-moz-transform: translate(-50%);
transform: translate(-50%)
}
<div class="container">
<div class="text">
Google.com
</div>
</div>
display:block;
margin:auto;
makes elements centered. So you could edit your code to become:
div.container div.text {
bottom: 0px;
border: solid 1px black;
display:block;
margin:auto;
}
.text{ width: 100%; text-align: auto; }
The text wrapping div will then be as wide as its container, so text align will work as expected. The reason text-align isn't working for you on your current code is because the "text" div is only as wide as the link, therefore centering its contents does nothing.
PROVIDED the link is the bottom/last element in the div-
add this to the div:
text-align: center; //centers the text
and then set the link to:
margin-top: auto; // pushes the text down to the bottom
worked in my case.
Simple and quick, but only works provided your link is the last element in the div.

Alignment with relative and absolute positioning

How could I center the blue box inside the red one ?
I see that the left side of the blue box is exactly in the middle of the red box, but I would like to center the whole blue box, not its left side. The dimensions of the boxes are not constant. I want to align regardless of boxes dimensions. Example to play with here. Thanks !
HTML:
<div id="rel">
<span id="abs">Why I'm not centered ?</span>
</div>
CSS:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}
If you're able to change the <span> tag to a <div>
<div id="rel">
<div id="abs">Why I'm not centered ?</div>
</div>
Then this piece of CSS should work.
#rel {
position: absolute;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center; }
#abs {
width: 300px;
height: 200px;
border: 1px solid blue;
margin: auto;
margin-top: 50px; }
I think it's better to use more automation for the enclosed box as less changes would be needed should you change the size of the container box.
You could add left:50px to #abs if that's all you want...
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
left:50px;
}
If you are going to define dimensions like that (200px x 300px and 300px x 400px), here's how it can be centered:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 49px 0 0 49px;
}
You can check at my solution here at http://jsfiddle.net/NN68Z/96/
I did the following to the css
#rel {
position: relative;
top: 10px;
left: 20px;
right: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
display: table-cell;
vertical-align: middle;
}
#abs {
display: block;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 0 auto;
}
This should work
#abs {
position: absolute;
left: auto;
right: auto;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}