Float left not working in div - html

i have the following HMTML/CSS:
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">NAME</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">öffnen</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>
The following set of CSS rules is defined:
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 100;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shooping-cart-body {
font-family: 'sans-serif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
float: left;
}
.cart-preview .body .shopping-cart-header{
font-family: 'IBMPlexSans-Bold.woff'
font-size: 45px;
margin-top: 40px;
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-right: 20px;
}
.product img {
width: 100%;
height: auto;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: left;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
position: absolute;
top: 80%;
height: 100%;
width: 100%;
background: gray;
}
.product-quantity-details .quantity{
float: left;
border: 2px solid black;
margin: 0 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 15px;
color: black;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
vertical-align: middle;
color: white;
top: 15%;
line-height: 14px;
bottom: 50px;
height: 20px;
width: 205px;
left: 25%;
}
.checkout .taxes{
position: absolute;
top: 5%;
}
.checkout .cart-total{
position: absolute;
top: 10%;
float: left;
}
.cart-total .value {
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
float: left;
}
.taxes .value {
float: right;
}
.taxes .label {
float: left;
}
JavScript:
function toggleClass() {
document.getElementById('shopping-cart-body').classList.toggle('open');
}
I don't get the content within the div product-details algined in one horizontal line next to the image. I tried using float: left and left:0. Can someone help me to get the content aligned next to the image and all starting letters are in one line horitontally?
I pasted it into a codepen:
https://codepen.io/seppl2202/pen/QVNdXd

Here is the link https://codepen.io/anon/pen/vzGgqq with the CSS .
The Css that needs to be modified is
.product-quantity-details{
display: flex;
flex-direction: column;
align-items: center;
}
.product-details{
display: inline-flex;
}
The inline-flex will align the items to a single line for .product-details and the align-items: center; should align the contents in the center perfectly, the color and quantity field.

Related

Fix div to bottom of parent

i have the following HTML/CSS:
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">NAME</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">öffnen</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>
CSS:
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 100;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shooping-cart-body {
font-family: 'sans-serif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
float: left;
}
.cart-preview .body .shooping-cart-header{
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-right: 20px;
}
.product img {
width: 100%;
height: auto;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: right;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
height: 100%;
width: 100%;
}
.product-quantity-details .quantity{
border: 2px solid black;
margin: 0 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 15px;
color: black;
width: auto;
height: auto;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
color: white;
bottom: 50px;
height: 20px;
width: 205px;
left: 25%;
}
.taxes {
position: relative;
margin-bottom: 5px;
left: 0;
}
.cart-total {
position: absolute;
bottom: 100px;
width: 100%;
}
.taxes {
position: absolute;
bottom: 300px;
width: 100%;
}
.cart-total .value {
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
float: left;
}
.taxes .value {
float: right;
}
.taxes .label {
float: left;
}
.cart-preview.cart-overview {
width: 100%;
position: inherit;
}
.cart-preview.cart-overview .body {
display: block;
position: inherit;
width: 100%;
}
.cart-preview .header > :first-child {
float: left;
}
.cart-preview .header > :last-child {
float: right;
}
.cart-preview .header::after,
.cart-preview .cart-totals > div::after {
clear: both;
content: "\A0";
}
.cart-preview .body {
border: 1px solid #808080;
padding: 2px;
}
The JavaScript simply toggles the .open class for the body.
Now I want to stick the last div to the bottom of the body, so the product-container div is scrollable on overflow and the checkout is always visible.
However, neither the scrolling nor the chechkout divs position are working.
Could someone help me with these two things?
Please try this and provide feedback so, that answer can be improved more.
.cart-preview .body {
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 100;
background-color: #fff;
transition: right 1s linear;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shooping-cart-body {
font-family: 'sans-serif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
float: left;
}
.cart-preview .body .shooping-cart-header{
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
max-height: 400px;
height: auto;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-right: 20px;
}
.product img {
width: 100%;
height: auto;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: right;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.product-quantity-details .quantity{
border: 2px solid black;
margin: 0 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 15px;
color: black;
width: auto;
height: auto;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.cart-preview .body .checkout>button {
position: relative;
background: black;
text-align: center;
color: white;
height: 20px;
width: 205px;
}
.taxes {
position: relative;
}
.cart-total .value {
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
float: left;
}
.taxes .value {
float: right;
}
.taxes .label {
float: left;
}
.cart-preview.cart-overview {
width: 100%;
position: inherit;
}
.cart-preview.cart-overview .body {
display: block;
position: inherit;
width: 100%;
}
.cart-preview .header > :first-child {
float: left;
}
.cart-preview .header > :last-child {
float: right;
}
.cart-preview .header::after,
.cart-preview .cart-totals > div::after {
clear: both;
content: "\A0";
}
.cart-preview .body {
border: 1px solid #808080;
padding: 2px;
}

Place horizontal line in middle between two divs

i have the following HTML/CSS/JS:
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">This is a very long test name</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">open</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
<hr>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>
CSS:
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 101;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shopping-cart-body {
font-family: 'IBMPlexSerif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
margin-top: 20px;
margin-left: 20px;
font-size: 30px;
float: left;
}
.cart-preview .body .shopping-cart-header{
font-family: 'IBMPlexSans';
font-size: 45px;
margin-top: 40px;
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-right: 10px;
}
.product img {
width: 100%;
height: auto;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: left;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
position: absolute;
top: 80%;
height: 100%;
width: 100%;
background: white;
}
.product-quantity-details .quantity{
float: left;
text-align: center;
border: 2px solid black;
margin-right: 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 33px;
color: black;
}
.cart-preview .product{
margin-top: 10px;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.price-open .product-price:after{
content: '€';
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
vertical-align: middle;
border: none;
color: white;
top: 13%;
line-height: 14px;
bottom: 50px;
height: 40px;
width: 205px;
left: 25%;
}
.checkout .taxes{
position: absolute;
width: 100%;
top: 5%;
}
.checkout .cart-total{
position: absolute;
width: 100%;
top: 10%;
float: left;
}
.cart-total .value {
margin-right: 20px;
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
margin-left: 20px;
float: left;
}
.taxes .value {
margin-right: 20px;
float: right;
}
.taxes .label {
margin-left: 20px;
float: left;
}
.taxes>hr{
margin-top: 30px;
margin-left: 20px;
margin-right: 20px;
border-color: black;
}
.product-quantity-details{
align-items: center;
display: inline-flex;
}
.product-details{
display: flex;
flex-direction: column;
align-items: flex-start
}
and JavaScript:
function toggleClass() {
document.getElementById('shopping-cart-body').classList.toggle('open');
}
Now I want to achieve to have the <hr> within the div with the class checkout to be placed always at the middle of the space between the taxes and the total sum.
How can I achieve this? I tried height: 50% and top: 50%.
I summed this up in a codepen:
https://codepen.io/anon/pen/EeyEPg
Update:
I just learned how to achieve the effect with a div, so i need to align this new div between the two other divs, so I updated the codepen.
The <hr> tag has a different meaning in HTML5 than previous versions of HTML. W3Schools has a very good description of this, but basically it used to be a visual element while now it's a structural element. It symbolizes a thematic break between two different sections of content.
Based on your question, I'm assuming you're trying to achieve the visual effect. You don't need the <hr> tag for this at all! Just use border-bottom like so:
div {
border-bottom: 1px solid #000;
padding-bottom: 10px;
}
I suggest to use border-bottom instead of an <hr> tag, like the example:
https://codepen.io/RACCH/pen/gdMeGQ
function toggleClass() {
document.getElementById('shopping-cart-body').classList.toggle('open');
}
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 101;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shopping-cart-body {
font-family: 'IBMPlexSerif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
margin-top: 20px;
margin-left: 20px;
font-size: 30px;
float: left;
}
.cart-preview .body .shopping-cart-header{
font-family: 'IBMPlexSans';
font-size: 45px;
margin-top: 40px;
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-right: 10px;
}
.product img {
width: 100%;
height: auto;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: left;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
position: absolute;
top: 80%;
height: 100%;
width: 100%;
background: white;
}
.product-quantity-details .quantity{
float: left;
text-align: center;
border: 2px solid black;
margin-right: 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 33px;
color: black;
}
.cart-preview .product{
margin-top: 10px;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.price-open .product-price:after{
content: '€';
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
vertical-align: middle;
border: none;
color: white;
top: 13%;
line-height: 14px;
bottom: 50px;
height: 40px;
width: 205px;
left: 25%;
}
.checkout .taxes{
position: absolute;
/* width: 100%; */
top: 5%;
}
.checkout .cart-total{
position: absolute;
width: 100%;
top: 10%;
float: left;
}
.cart-total .value {
margin-right: 20px;
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
margin-left: 20px;
float: left;
}
.taxes .value {
/* margin-right: 20px; */
float: right;
}
.taxes .label {
/* margin-left: 20px; */
float: left;
}
.taxes>hr{
margin-top: 30px;
margin-left: 20px;
margin-right: 20px;
border-color: black;
}
.taxes {
border-bottom: 1px solid black;
margin: 0px 22px;
width: calc(100% - 48px);
padding-bottom: 2px;
}
.product-quantity-details{
align-items: center;
display: inline-flex;
}
.product-details{
display: flex;
flex-direction: column;
align-items: flex-start
}
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">This is a very long test name</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">open</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>

Align divs along image vertically

I have the following HTML/CSS/JS summed up in a codepen:
https://codepen.io/anon/pen/gdwLGm
HTML:
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">This is a very long test name</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">open</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
<hr>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>
CSS:
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 101;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shopping-cart-body {
font-family: 'IBMPlexSerif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
margin-top: 20px;
margin-left: 20px;
font-size: 30px;
float: left;
}
.cart-preview .body .shopping-cart-header{
font-family: 'IBMPlexSans';
font-size: 45px;
margin-top: 40px;
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-left: 20px;
margin-right: 10px;
}
.product img {
width: 100%;
height: auto;
}
.name-header{
text-align: left;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: left;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
position: absolute;
top: 80%;
height: 100%;
width: 100%;
background: white;
}
.product-quantity-details .quantity{
float: left;
text-align: center;
border: 2px solid black;
margin-right: 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 33px;
color: black;
}
.cart-preview .product{
margin-top: 10px;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
vertical-align: middle;
border: none;
color: white;
top: 13%;
line-height: 14px;
bottom: 50px;
height: 40px;
width: 205px;
left: 25%;
}
.checkout .taxes{
position: absolute;
width: 100%;
top: 5%;
}
.checkout .cart-total{
position: absolute;
width: 100%;
top: 10%;
float: left;
}
.cart-total .value {
margin-right: 20px;
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
margin-left: 20px;
float: left;
}
.taxes .value {
margin-right: 20px;
float: right;
}
.taxes .label {
margin-left: 20px;
float: left;
}
.taxes>hr{
margin-top: 30px;
margin-left: 20px;
margin-right: 20px;
border-color: black;
}
.product-quantity-details{
align-items: center;
display: inline-flex;
}
.product-details{
display: flex;
flex-direction: column;
align-items: flex-start
}
#blockcart-modal {
position: fixed;
background-color: hsla(0, 0%, 100%, .1);
top: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center
}
#blockcart-modal > div {
padding: 20px;
display: inline-block;
min-width: 50%;
min-height: 400px;
margin-top: 200px;
text-align: left;
background-color: #fff;
z-index: 100;
border: 1px solid #ccc
}
and the JavaScript (only contains one function):
function toggleClass() {
document.getElementById('shopping-cart-body').classList.toggle('open');
}
So the goal is to align the items right of the picuture. The name (div with class name-header should always be at the 'top' level of the picture, the quantity (div with class product-quantity-details should be at the 'half' and the price (div with class price-open should be at the bottom. The alignment within the elements is fine, just the positioning is not working the way I want.
What I tried: I tried using position: absolute; for the divs to align and then using top:0; for the name, bottom:0; for the price and top: 50%; for the quantity details. This was not really working.
Can someone help me here?
Here is my solution, Open the snippet in full screen mode
I increased the height of the image to 300px for testing purpose only.
justify-content:space-between; will align the items by distributing space equally between children.
In your case name will be on top, product details in middle and price at the bottom
Add this to ur CSS
.product-details{
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content:space-between;
}
function toggleClass() {
document.getElementById('shopping-cart-body').classList.toggle('open');
}
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
visibility: visible;
position: fixed;
height: 100%;
top: 0;
width: 400px;
z-index: 101;
background-color: #fff;
transition: right 1s linear;
right: -400px;
}
.cart-preview .body.open {
visibility: visible;
transition: right 1s linear;
right: 0px;
}
.cart-preview .body .shopping-cart-body {
font-family: 'IBMPlexSerif';
width: 100%;
text-align: center;
}
.cart-preview .body .close{
margin-top: 20px;
margin-left: 20px;
font-size: 30px;
float: left;
}
.cart-preview .body .shopping-cart-header{
font-family: 'IBMPlexSans';
font-size: 45px;
margin-top: 40px;
text-align: center;
}
.cart-preview .body .products-container {
position: relative;
height: 100%;
width: 100%;
margin-top: 15px;
overflow: auto;
}
.product {
display: flex;
}
.product>div {
width: 50%;
}
.product .prodcut-image {
margin-left: 20px;
margin-right: 10px;
}
.product img {
width: 100%;
height: 300px; /*For testing purpose*/
}
.name-header{
text-align: left;
}
.cart-preview .body .products-container>.product-image {
position: absolute;
width: 50%;
left: 0;
}
.cart-preview .body .products-container>.product-details {
position: absolute;
width: 50%;
float: left;
}
.cart-preview .body .products-container .color-circle:before {
content: ' \25CF';
font-size: 30px;
}
.cart-preview .body .checkout {
position: absolute;
top: 80%;
height: 100%;
width: 100%;
background: white;
}
.product-quantity-details .quantity{
float: left;
text-align: center;
border: 2px solid black;
margin-right: 10px;
background: white;
width: 36px;
height: 36px;
font-size: 15px;
line-height: 33px;
color: black;
}
.cart-preview .product{
margin-top: 10px;
}
.product-quantity-details .quantity:after{
content: 'x'
}
.cart-preview .body .checkout>button {
position: absolute;
background: black;
text-align: center;
vertical-align: middle;
border: none;
color: white;
top: 13%;
line-height: 14px;
bottom: 50px;
height: 40px;
width: 205px;
left: 25%;
}
.checkout .taxes{
position: absolute;
width: 100%;
top: 5%;
}
.checkout .cart-total{
position: absolute;
width: 100%;
top: 10%;
float: left;
}
.cart-total .value {
margin-right: 20px;
float: right;
}
.cart-total .value:after {
content:'€'
}
.cart-total .label {
margin-left: 20px;
float: left;
}
.taxes .value {
margin-right: 20px;
float: right;
}
.taxes .label {
margin-left: 20px;
float: left;
}
.taxes>hr{
margin-top: 30px;
margin-left: 20px;
margin-right: 20px;
border-color: black;
}
.product-quantity-details{
align-items: center;
display: inline-flex;
}
.product-details{
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content:space-between;
}
#blockcart-modal {
position: fixed;
background-color: hsla(0, 0%, 100%, .1);
top: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center
}
#blockcart-modal > div {
padding: 20px;
display: inline-block;
min-width: 50%;
min-height: 400px;
margin-top: 200px;
text-align: left;
background-color: #fff;
z-index: 100;
border: 1px solid #ccc
}
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">
</a>
</div>
<div class="body" id="shopping-cart-body">
<div class="close">X</div>
<ul>
</ul>
<div class="shopping-cart-header">CART</div>
<div class="products-container">
<div class="product">
<span class="prodcut-image"><img src="https://via.placeholder.com/250x100"></span>
<div class="product-details">
<div class="name-header">This is a very long test name</div>
<div class="product-quantity-details">
<span class="quantity">QTY</span>
<span class="color-circle"></span>
<span class="color">COLOR</span>
</div>
<div class="price-open">
<span class="product-price">XX.XX</span>
<span class="product-link">open</span>
</div>
</div>
</div>
</div>
<div class="checkout">
<div class="taxes">
<span class="label">Taxes</span>
<span class="value">0</span>
<hr>
</div>
<div class="cart-total">
<span class="label">Total</span>
<span class="value">0</span>
</div>
<button>Checkout</button>
</div>
</div>
</div>
</div>
Add these two things too
.cart-preview .header {
float: left;
}
.cart-preview .body {
float: left;
}

CSS class for fly in transition for javaScript

I need your help using CSS3 transitions. I have some contend I want to fly in from the right to its defined position when hovering. This can be achieved using the CSS in the :hover rule.
However, I want to call this effect from a JavaScript function, so I need your help.
Here is the Code:
#blockcart-wrapper .cart-preview .header {
border: none;
}
#blockcart-wrapper .cart-preview .header .cart-icon {
width: 25px;
height: 28px;
}
.cart-preview {
float: right;
position: relative;
}
.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
text-decoration: none;
color: inherit;
}
.cart-preview .header {
display: block;
font-weight: bold;
border: 1px solid #808080;
padding: 5px;
cursor: pointer;
background-color: #fff;
}
.cart-preview .body {
display: none;
width: 400px;
background-color: #fff;
right: 0;
}
.cart-preview:hover .body {
display: block;
position: absolute;
}
.cart-preview.cart-overview {
width: 100%;
position: inherit;
}
.cart-preview.cart-overview .body {
display: block;
position: inherit;
width: 100%;
}
.cart-preview .header > :first-child {
float: left;
}
.cart-preview .header > :last-child {
float: right;
}
.cart-preview .header::after,
.cart-preview .cart-totals > div::after {
clear: both;
content: "\A0";
}
.cart-preview .body {
border: 1px solid #808080;
padding: 2px;
}
.cart-preview ul {
margin: 0;
padding: 0;
margin-top: 20px;
margin-bottom: 20px;
}
.cart-preview li {
list-style: none;
margin-bottom: 15px;
}
.cart-preview li > * {
display: inline-block;
vertical-align: top;
}
.cart-preview li .product-quantity {
color: #666;
width: 10%;
}
.cart-preview .product-quantity::after {
content: 'x';
}
.cart-preview li .product-name {
width: 50%;
}
.cart-preview li .product-price {
width: 20%;
}
.cart-preview li .remove-from-cart {
text-indent: 100%;
display: inline-block;
overflow: hidden;
vertical-align: middle;
text-decoration: none;
color: inherit;
font-weight: bold;
width: 2em;
white-space: nowrap;
float: right;
}
.cart-preview li .remove-from-cart::before {
content: 'X';
text-indent: 0px;
border: 1px solid #ccc;
border-radius: 100%;
width: 1em;
height: 1em;
padding: 2px;
text-align: center;
background-color: #333;
color: #fff;
float: left;
}
.cart-preview .cart-totals .label {
float: left;
}
.cart-preview .cart-totals .value {
float: right;
}
.cart-preview .cart-totals > div {
clear: both;
border-bottom: 1px solid #ccc;
}
.cart-preview .cart-totals > div:not(:last-child) {
margin-bottom: 5px;
}
.cart-totals .label {
font-weight: bold;
}
#blockcart-modal {
position: fixed;
background-color: rgba(255,255,255,0.1);
top: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center;
}
#blockcart-modal > div {
padding: 20px;
display: inline-block;
min-width: 50%;
min-height: 400px;
margin-top: 200px;
text-align: left;
background-color: #fff;
z-index: 100;
border: 1px solid #ccc;
}
<div id="blockcart-wrapper">
<div class="blockcart cart-preview">
<div class="header">
<a rel="nofollow" href="#">
<img class="cart-icon" src="https://fakeimg.pl/50x50/?text=cart+icon">
</a>
</div>
<div class="body">
<ul>
</ul>
<div class="cart-subtotals">
<div class="products">
<span class="label">Partial sum</span>
<span class="value">0</span>
</div>
<div class="">
<span class="label"></span>
<span class="value"></span>
</div>
<div class="shipping">
<span class="label">Shipping</span>
<span class="value">0</span>
</div>
<div class="">
<span class="label"></span>
<span class="value"></span>
</div>
</div>
<div class="cart-total">
<span class="label">Total sum</span>
<span class="value">0</span>
</div>
</div>
</div>
</div>
So I want to achieve, that the div with class body flies in from the right to its determined position using an own class so I can toggle the class by a JavaScript if a certain external event is triggered and the class should only contain the transition effect, so the browser does the 'reverse transition' when the hover is stopped or the class is removed by a JavaScript function.
Can you help me here?
The css should be just like you would do if using :hover, just instead of :hover add a class to the selector (a.hovered), and use js mouseover and mouseout events to toggle this class.
CSS -
div.body.hovered {
right: 300px; /*or however you meant to do the transition*/
}
JS -
var elements = document.getElementsByClass('body');
elements.addEventListener("mouseover", function( event ) {
this.classList.add('hovered');
});
elements.addEventListener("mouseout", function( event ) {
this.classList.remove('hovered');
});

avoid divs from going on top of each other

I'm trying to make a portfolio page. I've created a section with divs nested inside. The first two on the top of the section were set to relative and they work. I've tried to set the rest to relative and they show on top of the first two divs. Help!
#portfolio {
width: 650px;
background-color: white;
margin-left: 75px;
margin-top: 120px;
margin-right: 40px;
margin-bottom: 200px;
padding: 15px;
float: left;
border: 1px solid #dddddd;
overflow: auto;
clear: both;
}
#blog {
position: relative;
float: left;
width: 40%;
}
#blog img{
float: left;
width: 100%;
margin-right: 10px;
position: absolute;
}
#blog p {
margin: 0;
position: absolute;
top: 125px;
color: white;
background-color: #41AAA5;
width: 100%;
padding-top: 7px;
padding-bottom: 7px;
text-align: center;
font-size: 20px;
}
#hangman {
position: relative;
float: right;
width: 40%;
}
#hangman img{
float: left;
width: 100%;
position: absolute;
}
#hangman p {
margin: 0;
position: absolute;
top: 125px;
color: white;
background-color: #41AAA5;
width: 100%;
padding-top: 7px;
padding-bottom: 7px;
text-align: center;
font-size: 20px;
}
#playlist {
position: relative;
float: left;
width: 40%;
}
#playlist img {
}
#playlist p {
}
<section id="portfolio">
<div id="blog">
<img src="assets/images/icon1.jpg">
<p>Blog</p>
</div>
<div id="hangman">
<img src="assets/images/icon2.jpg">
<p>Hangman Game</p>
</div>
<div id="playlist">
<img src="assets/images/icon3.jpg">
<p>Playlist</p>
</div>
<div id="maps">
<img src="assets/images/icon4.jpg">
<p>Map Page</p>
</div>
<div id="pets">
<img src="assets/images/icon5.jpg">
<p>Pets</p>
</div>
</section>
The positon:absolute on your img tags in blog and hangman divs was causing this problem. Postion:absolute element do not pick height so thats why everything was overlapping.
Here is your updated code:
#portfolio {
width: 650px;
background-color: white;
margin-left: 75px;
margin-top: 120px;
margin-right: 40px;
margin-bottom: 200px;
padding: 15px;
float: left;
border: 1px solid #dddddd;
overflow: auto;
clear: both;
}
#blog {
position: relative;
float: left;
width: 40%;
}
#blog img {
float: left;
width: 100%;
margin-right: 10px;
}
#blog p {
margin: 0;
position: absolute;
top: 125px;
color: white;
background-color: #41AAA5;
width: 100%;
padding-top: 7px;
padding-bottom: 7px;
text-align: center;
font-size: 20px;
}
#hangman {
position: relative;
float: right;
width: 40%;
}
#hangman img {
float: left;
width: 100%;
}
#hangman p {
margin: 0;
position: absolute;
top: 125px;
color: white;
background-color: #41AAA5;
width: 100%;
padding-top: 7px;
padding-bottom: 7px;
text-align: center;
font-size: 20px;
}
#playlist {
position: relative;
float: left;
width: 40%;
}
#playlist img {}
#playlist p {}
<section id="portfolio">
<div id="blog">
<img src="https://dummyimage.com/600x400/000/fff&text=blog">
<p>Blog</p>
</div>
<div id="hangman">
<img src="https://dummyimage.com/600x400/f00/fff&text=hangman">
<p>Hangman Game</p>
</div>
<div id="playlist">
<img src="https://dummyimage.com/600x400/0f0/fff&text=playlist">
<p>Playlist</p>
</div>
<div id="maps">
<img src="https://dummyimage.com/600x400/00f/fff&text=maps">
<p>Map Page</p>
</div>
<div id="pets">
<img src="https://dummyimage.com/600x400/ff0/fff&text=pets">
<p>Pets</p>
</div>
</section>