how to set float and non float element vertical align to bottom - html

I'm running this JSFIDDLE and I want to set the vertical-align property to bottom for both the float and non-float elements, but it doesn't work :
Here is my html code :
<div class="main">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
CSS :
div {
border: 1px solid;
}
.main {
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
vertical-align: bottom;
width: 20%;
}
.left {
height: 60px;
float: left;
background-color: red;
}
.right {
height: 40px;
float: right;
background-color: blue;
}
.middle {
height: 20px;
background-color: green;
display: inline-block;
}

Unless you're using with a table cells, vertical-align aligns the element with respect to adjacent elements, in particular text.
Read more about vertical alignment here:
http://phrogz.net/css/vertical-align/index.html
What you can do though is give your wrapper a width, height, and set it it to position: relative
After that you can use absolute positioning for your divs, set them to bottom: 0 they will then stick to the bottom of the wrapper.
Here is new CSS:
div {
border: 1px solid;
}
.main {
position: relative;
width: 100%;
height: 100px;
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
width: 20%;
}
.left {
height: 60px;
background-color: red;
position: absolute;
bottom: 0;
left: 0;
}
.right {
height: 40px;
background-color: blue;
position: absolute;
bottom: 0;
right: 0;
}
.middle {
height: 20px;
background-color: green;
position: absolute;
transform: translate(-50%, 0%);
left: 50%;
bottom: 0;
}
CODEPEN DEMO

Related

How do I get the red box on top of the gray box?

I have this HTML:
<div id="container">
<div id="content"></div>
<div id="close-button"></div>
</div>
and this CSS:
#container {
width: 50%;
}
#content {
width: 100%;
height: 50px;
background-color: gray;
}
#close-button {
float: right;
margin-left: 100%;
height: 50px;
width: 50px;
background-color: red;
}
JSFiddle: https://jsfiddle.net/Le53m70b/
How can I make the red box overlaid on top of the gray one, instead of being on a separate line? Note that the size of the container is not fixed, but regardless of its width, I'd like the gray box to cover 100% of it and the red box to be at its very right.
Ah, this finally works: https://jsfiddle.net/Le53m70b/1/
#container {
position: relative;
width: 50%;
}
#content {
width: 100%;
height: 50px;
background-color: gray;
}
#close-button {
position: absolute;
right: 0;
top: 0;
height: 50px;
width: 50px;
background-color: red;
}
You can use z-index property. Which is used to overlay an individual div over another div element.
#container{
width: 200px;
height: 200px;
position: relative;
margin: 20px;
}
#content{
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0.8;
}
#close-button{
z-index: 9;
margin: 20px;
}

CSS How to put 4 divs on each side of a parent div

I wish to include 4 divs inside a parent div in the following manner:
I could use fixed position and set right/left/top/bottom = 0 accordingly for each child div if they were not inside in a div, but right now, I can't figure out how to do this.
Here you go, but I'm not sure how this will fare in responsiveness since the parent has fixed sizes, but the child div should be able to adapt if the parent changes size. Some css can be combined, but I separated them all for reference
.parent {
width: 300px;
height: 300px;
position: relative;
border: 1px solid black;
}
.div1 {
position: absolute;
bottom: 0;
right: 0;
width: 80%;
height: 20%;
background-color: green;
}
.div2 {
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 80%;
background-color: blue;
}
.div3 {
position: absolute;
top: 0;
left: 0;
width: 80%;
height: 20%;
background-color: red;
}
.div4 {
position: absolute;
bottom: 0;
left: 0;
width: 20%;
height: 80%;
background-color: brown;
}
<div class="parent">
<div class="div1">
DIV1
</div>
<div class="div2">
DIV2
</div>
<div class="div3">
DIV3
</div>
<div class="div4">
DIV4
</div>
</div>
Consider utilizing absolute positioning on nested div elements and offsetting their positions, within the containing element, appropriately and as required by declaring top, bottom, left and right properties respectively.
Code Snippet Demonstration
Note:
In the below demonstration, a containing element, with resizing properties, has been wrapped around the element in question, to demonstrate the responsive behaviour of this method.
Resize the element manually by clicking the icon, in the bottom-left corner of the containing element, and dragging vertically or horizontally.
* {
box-sizing: border-box;
font-family: arial;
}
.outer {
border: 3px solid black;
width: 100%;
height: 100%;
position: relative; /* required */
}
.outer-wrapper { /* purely for the sake of responsive demonstration */
padding: 10px;
resize: auto;
overflow: hidden;
border: 3px dashed gray;
width: 400px;
height: 400px;
}
.outer div {
position: absolute;
padding: 10px;
font-weight: bold;
font-size: 12px;
}
.outer div:nth-child(odd) {
width: 80%;
height: 20%;
}
.outer div:nth-child(even) {
width: 20%;
height: 80%;
}
.outer div:nth-child(1) {
background: #ed1c24;
left: 0;
top: 0;
}
.outer div:nth-child(2) {
background: #00a2e8;
right: 0;
top: 0;
}
.outer div:nth-child(3) {
background: #22b14c;
right: 0;
bottom: 0;
}
.outer div:nth-child(4) {
background: #b97a57;
left: 0;
bottom: 0;
}
<div class="outer-wrapper">
<div class="outer">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
</div>
</div>
It will be helpful to you
.parent{
position: relative;
width: 100%;
height: 100vh;
border: 1px solid black;
}
.parent>div{
position:absolute;
text-align:center;
}
.one{
background-color: green;
bottom: 0;
right: 0;
width: 80%;
height: 20%;
}
.two{
background-color: blue;
top: 0;
right: 0;
width: 20%;
height: 80%;
}
.three{
background-color: red;
top: 0;
left: 0;
width: 80%;
height: 20%;
}
.four{
background-color: brown;
bottom: 0;
left: 0;
width: 20%;
height: 80%;
}
<div class="parent">
<div class="one"> Div1</div>
<div class="two">Div2</div>
<div class="three">Div3</div>
<div class="four">Div4</div>
</div>

CSS: slide a div out to the right only to width of the parent div

I'm having some CSS issues I hope someone here can help with. I basically am trying to set a group of inline divs that slide out to the right, expanding to the width of the parent div containing them. As you can see from the jsfiddle I have (https://jsfiddle.net/0o9bw101/), the divs expand to the width of the parent div, instead of expanding only to the rightmost border of the parent div. If anyone can help I'd be quite thankful. Thank you in advance!
Here is the CSS I'm using in case you want to see it here:
.container {
width: 70%;
height: 100px;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
color: black;
}
.greyDiv:hover {
transition: 2s;
width: 70%;
position: absolute
}
Try this
.container {
width: 70%;
height: 100px;
background-color: black;
position:relative;
overflow:hidden;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 100%;
position: absolute;
}
<div class='container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
EDIT:
The trick is to add another box inside main container.
DEMO: https://jsfiddle.net/0o9bw101/3/
<div class='container'>
<div class='invisible_container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
</div>
previous answer:
It's hard to do when you mix parent's with in % with children margins in px.
Also having parent's position set to something other than default helps a bit.
Here is working example for you:
.container {
width: 70%;
height: 100px;
background-color: black;
position: absolute;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
margin-left: 2%;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 96%;
position: absolute
}
DEMO: https://jsfiddle.net/0o9bw101/2/
This might not be quite what you were going for, but here elements will grow to the full width of the container (left to right) regardless of it's starting position using 2 extra elements per object.
The .inner is used to grow the background to the full width
The .placeholder is used to keep the other elements from collapsing left
#keyframes grow {
from {width: 0%;}
to {width: 92%;}
}
.container {
width: 70%;
height: 100px;
position: relative;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: black;
display: inline-block;
margin: 20px 4%;
vertical-align: top;
position: relative;
z-index: 2;
}
.inner {
width: 0%;
height: 100%;
display: none;
background-color: transparent;
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
.placeholder {
display: none;
background-color: transparent;
height: 60px;
width: 60px;
margin: 20px 4%;
}
.greyDiv:hover {
width: 100%;
position: absolute;
left: 0;
margin: 20px 0;
background-color: transparent;
z-index: 4;
}
.greyDiv:hover + .placeholder {
display: inline-block;
}
.greyDiv:hover .inner {
display: inline-block;
left: 4%;
width: 100%;
background-color: white;
animation-name: grow;
animation-duration: 2s;
animation-fill-mode: forwards;
z-index: 5;
}
<div class='container'>
<div class='greyDiv'>1a<div class="inner">1x</div></div><div class="placeholder"></div>
<div class='greyDiv'>2a<div class="inner">2x</div></div><div class="placeholder"></div>
<div class='greyDiv'>3a<div class="inner">3x</div></div><div class="placeholder"></div>
<div class='greyDiv'>4a<div class="inner">4x</div></div><div class="placeholder"></div>
</div>

:after not working as expected

In the code below, I want to add a line after the div. I have used :after but the line comes before the div. Please help me on this.
div {
background-color: red;
width: 300px;
height: 300px;
}
div:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div></div>
You can set this using position
div {
background-color: red;
width: 300px;
height: 300px;
position: relative;
}
div:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
<div></div>
The name ::after for the pseudo-element can be a little misleading. It doesn't come after the element the selector is targeting, but inside it, after all the other content.
So your code is equivalent to this:
#div1 {
background-color: red;
width: 300px;
height: 300px;
}
#div2 {
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div id="div1">
<div id="div2"></div>
</div>
(and because your div has no content, it doesn't matter whether you use ::before or ::after).
So you either have to use positioning to move the blue line to be (visually) after the div, or you could apply your ::after to the parent element of the div, which may or may not do what you want:
#inner {
background-color: red;
width: 300px;
height: 300px;
}
#outer:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div id="outer">
<div id="inner"></div>
</div>
You can set line by using below code
<style type="text/css">
div {
background-color: red;
width: 300px;
height: 300px;
position: relative;
}
div::after {
content: " ";
background-color: blue;
display: block;
position: absolute;
height: 2px;
width: 120px;
left: 100%;
}
</style>
<div></div>
you can make the margin-top property of the after selector equal to your required div height,and add float:left;
div {
background-color: red;
width: 300px;
height: 300px;
float:left;
}
div:after {
content: '';
background-color: blue;
height: 2px;
width: 120px;
margin: 300px auto 0;
display: block;
}
<div></div>

css responsive square with overflow

i have a responsive square with text inside. i want the square to keep his proportions on all sizes, unless the text inside is overflow, than i want the square to be taller.
now, the text is just hidden.
http://jsfiddle.net/38Tnx/2253/
<div class='square-box'>
<div class='square-content'>
<div>
<span>text</span>
</div>
</div>
</div>
.square-box{
position: relative;
width: 50%;
overflow: hidden;
background: #4679BD;
}
.square-box:before{
content: "";
display: block;
padding-top: 100%;
}
.square-content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
color: white;
}
.square-content div {
display: table;
width: 100%;
height: 100%;
}
.square-content span {
display: table-cell;
text-align: center;
vertical-align: middle;
color: white
}
Replace your .square-box css with the following code
.square-box{
position: relative;
width: 50vw;
height: 50vw;
overflow: hidden;
background: #4679BD;
}
JSFIDDLE DEMO
Try this. Hope it helps.
.square-box{
position: relative;
width: 50%;
/*overflow: hidden;*/
/*background: #4679BD;*/
}
.square-box:before{
content: "";
display: block;
padding-top: 100%;
}
.square-content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
color: white;
}
.square-content div {
display: table;
width: 100%;
height: 100%;
background:#4679BD; /* moved */
}
.square-content span {
display: table-cell;
text-align: center;
vertical-align: middle;
color: white
}