I'd like to partially overlap multiple HTML elements - say DIVs - as in the below image. The black-bordered rectangles (Hanafuda cards) represent the elements I want to overlap.
With Javascript I'm sure I could come up with something, but I'd like to know if there's a pure CSS solution. I considered relative positioning but the problem is that each card needs a bigger and bigger offset along the x-axis.
Ideally I'd like the degree of overlap to depend on how much space there is, so that the elements crowd together more when cramped, but that's secondary and I don't mind using JS to accomplish it.
You can achieve that using flex, making all cards except the last one adjust to the remaining space
Here is a fiddle of the below:
.container {
display: flex;
width: 300px;
}
.card-container {
flex: 1 0 0;
overflow-x: hidden;
}
.card-container:last-child {
flex: 0 0 auto;
}
.card {
background: linear-gradient(to right, #ccc 0%, #444 100%);
width: 100px;
height: 150px;
}
<div class="container">
<div class="card-container">
<div class="card"></div>
</div>
<div class="card-container">
<div class="card"></div>
</div>
<div class="card-container">
<div class="card"></div>
</div>
<div class="card-container">
<div class="card"></div>
</div>
<div class="card-container">
<div class="card"></div>
</div>
</div>
This can also be achieved using display: table, which currently enjoys greater browser compatibility over the newer, and wonderful, flex specification.
Compatibility: IE8+ and all modern browsers.
The outer div is given display: table
Each image is wrapped in a div with display: table-cell
table-layout: fixed allows the "cells" to overlap
The outer div can be kept flexible to allow the images to overlap more / less depending on the space they left with
Full Example
.cards {
display: table;
table-layout: fixed;
width: 50%;
max-width: 700px;
}
.cards > div {
display: table-cell;
width: 100px;
}
.cards > div > img {
display: block;
}
<div class="cards">
<div><img src="http://www.placehold.it/200x300" /></div>
<div><img src="http://www.placehold.it/200x300/FF0000" /></div>
<div><img src="http://www.placehold.it/200x300" /></div>
<div><img src="http://www.placehold.it/200x300/FF0000" /></div>
</div>
I found that the simplest solution was to use float: left combined with a negative right-margin:
.card {
--card-width: 100px;
margin-right: calc(25px - var(--card-width));
float: left;
width: var(--card-width);
height: calc(var(--card-width) * 1.5);
border: 1px solid black;
background: white;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
Related
I need your help!
The designer did that:
But I can't display the list like that !
I need to have a max width on my container!
How my horizontal list can be align on left side and not affected by the max width on the right side ? :'(
That is a conundrum. I would use position:absolute to get out of the edge of container. If the container is position:relative you wouldn't be able to do that. Play around with the width of the cards container to see what fits your needs because you can't really snap it to the right side exactly, you'd need to overflow it (so make body {overflow-x:hidden}).
body {
overflow-x: hidden;
}
.container {
margin: auto;
max-width: 400px;
border: 1px solid red;
}
.strip-right {
position: absolute;
height: 50px;
display: flex;
width: 100%;
justify-content: space-between;
}
.strip-placeholder {
height: 50px;
}
.card {
width: 50px;
height: 50px;
background: purple;
}
<body>
<div class="container">
<h2>hello world</h2>
<div class="strip-right">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<div class="strip-placeholder"></div>
<p>
other content 1
</p>
<p>
other content 2
</p>
<p>
other content 3
</p>
<p>
other content 4
</p>
</div>
</body>
Context:
I am trying to create 3 charts on 1 row (each has a minimum width) such that on window resize, the charts should also resize and may go to next row depending on the browser total width.
Problem:
Currently, I am missing something in the css because the chart is overflowing within the demo container below. The tooltip looks fine, but only half of the chart is seen in the container and both the axes are also hidden.
Has someone implemented something similar before? I want to understand how to load the charts in the div.
P.S. In the code below, highcharts-container is the inbuilt div which contains the charts. I am using the latest version of Highcharts and Angular 7.
My current html code -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-md4 demo>
<div class="demo-container">
<highcharts-chart [Highcharts]="Highcharts" [options]="options1" [callbackFunction]="cb1">
</highcharts-chart>
</div>
</div>
<div class="col-lg-4 col-md4 demo>
<div class="demo-container">
<highcharts-chart [Highcharts]="Highcharts" [options]="options2" [callbackFunction]="cb2">
</highcharts-chart>
</div>
</div>
<div class="col-lg-4 col-md4 demo>
<div class="demo-container">
<highcharts-chart [Highcharts]="Highcharts" [options]="options3" [callbackFunction]="cb3">
</highcharts-chart>
</div>
</div>
</div>
</div>
My css code (The main part) -->
.container-fluid{
width:100%
}
.demo{
margin: 20px 0;
min-width: 448px;
}
.demo-container{
position: relative;
border: 1px solid transparent;
box-shadow: 0 0 20px #1793f5;
width: 100%;
height: 100%;
}
.highcharts-container{
height: 100% !important;
width: 100% !important;
display: inline-block;
vertical-align: middle;
}
You could try to include the following CSS as a part of demo-container or container-fluid... one of it should do the deal for you.
height: 100% !important;
width: 100% !important;
Update
one of the harder challenges i have encountered, but add this to the style.css
and you have your contents dynamic inside its container.
.highcharts-background, .highcharts-root, .highcharts-container {
max-height: 100%;
max-width: 100%;
}
is this what you are looking for?
This was made using images of the graphs in the links you posted. I also used flexboxes which seems to be what you are looking for.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.container-fluid div {
width: auto;
border: 2px solid red;
min-width: 33%;
}
.container-fluid{
display: flex;
flex-wrap: wrap;
border: 2px solid blue;
justify-content: center;
}
<div class="container-fluid">
<div class="chartOne">
<img src="https://i.gyazo.com/161954d0841b7a398d5f0d63e1b2bcc4.png" alt="">
</div>
<div class="chartOne">
<img src="https://i.gyazo.com/161954d0841b7a398d5f0d63e1b2bcc4.png" alt="">
</div>
<div class="chartOne">
<img src="https://i.gyazo.com/161954d0841b7a398d5f0d63e1b2bcc4.png" alt="">
</div>
</div>
My issue is that I wanted side-by-side elements with borders, but I noticed without doing some margin-hack it was difficult to use the border property and it still didn't look right. However when I use outline or box-shadow, I get this alignment issue at the end.
.inner {
outline: 1px solid black;
width: 50%;
height: 50px;
float: left;
margin: 0;
display: inline-block;
box-sizing: border-box;
position: relative;
background: #fff;
}
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
It looks alright when there's an even number of elements but when I have this last element it looks odd. Some might suggest I just make it fit to the end which would be okay but the size can be configurable sometimes so this could be a common occurrence.
What is the proper way to achieve this where the last element lines up the border(or outline) correctly?
Because you're using outline to create your border, the outlines at the center are actually overlapping one another. When you get to the bottom where there is only one div the outline is not being overlapped and therefore looks misaligned. You could solve this issues by building it as a table:
.table {
width: 100%;
display: table;
border-collapse: collapse;
}
.column {
display: table-row;
}
.inner {
display: table-cell;
border: 1px solid black;
width: 50%;
height: 50px;
background: #fff;
}
<div class="table">
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
</div>
</div>
I have a container of various width, and various number of boxes float inside it. All the boxes have same width and height.
This is a demonstration.
https://jsfiddle.net/kghvmjb6/1/
I am looking for pure CSS solution if possible. Otherwise pure javascript (no jQuery) and CSS solutions are fine.
This example works fine with one line of floating boxes but fail with multiple lines, which is not I want.
https://codepen.io/alexandredees/pen/ojaFr
Instead of using float left, use display inline-block and in the parent div add text-align center, that will center the boxes in the container
Modified Fiddle
HTML
<div class="container">
<div class="">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="clear"></div>
</div>
</div>
CSS
.container {
border: 1px solid red;
width: 480px; text-align:center;
}
.container.wider {
width: 530px;
}
.box {
float: none;
width: 80px;
height: 80px;
margin: 10px;
background-color: #ddd;
display:inline-block;
}
How i can center div elements horizontally, so when i change the width of the browser, the last div go down with css?
So:
---||||-||||-||||---
--------||||--------
When i write:
<div style="float: left; position: relative; left: 50%;">
<div style="float: left; position: relative; left: -50%;">
<div style="width:315px; height:340px; float: left; margin-top: 10px; margin-bottom: 10px;">Text</div>
<div style="width:315px; height:340px; float: left; margin-top: 10px; margin-bottom: 10px;">Text</div>
...
</div>
</div>
Then after a element go down, all div elements go to the left side.
I would recommend using display: inline-block on the elements and then using text-align: center on the container to handle the centering you want:
I cleaned up your HTML but here is the basic HTML formatting with a container class and multiple (as many as you want) block class DIVs:
<div class="container">
<div class="block">Text</div>
<div class="block">Text</div>
<div class="block">Text</div>
</div>
The CSS modifies the display settings of the blocks and the text-alignment of the container:
div.block {
display: inline-block; /* this changes the elements to behave more like inline elements (think <span>s) */
width: 315px;
margin: 10px 0;
height: 340px;
}
div.container {
width: 100%;
text-align: center; /* this is the magic that centers the elements */
}
I put together a small demo that should help demonstrate this method: JSFIDDLE
Be Aware: a small 'quirk' exists with the display: inline-block CSS. it causes a small amount of space to occur between the elements. This can be removed multiple ways, my preferred methods being either using comments or wrapping the closing tags of the DIVs. (the issue is caused by the return/spaces between the elements in the HTML):
<div class="container">
<div class="block">Text</div><!--
--><div class="block">Text</div><!--
--><div class="block">Text</div>
</div>
<div class="container">
<div class="block">Text</div
><div class="block">Text</div
><div class="block">Text</div>
</div>
reference for the quirk behavior
Create a container <div> that is 100% of a given area. Then set each <div>'s width inside the container to be a % and float: left;. They'll stack next to each other until they do not fit and will break to the next line.
.container {
width: 100%;
}
.three {
width: 33%;
min-width: 225px;
float: left;
border: 1px solid black;
}
<div class="container">
<div class="three">
<p>Something</p>
</div>
<div class="three">
<p>Something</p>
</div>
<div class="three">
<p>Something</p>
</div>
</div>
Run the snippet.
You could use media queries to write different css code for different sizes:
Media Queries