Stack two div then float left with CSS - css

I'd like to have 5 divs (1, 2, 3, ...) displayed like this:
+---+ +---+ +---+
| 1 | | | | 4 |
+---+ | 3 | +---+
| 2 | | | | 5 |
+---+ +---+ +---+
where 3's height is twice as mush as 1, 2, 4, 5's ones.
Is there a way to position them like this without wrapping 1, 2 and 4, 5 into divs ?
Here's my HTML markup :
<div class="AlaOracles">
<div class="AlaOracle">
<img src="http://mywebsite.ru/wp-content/uploads/shutterstock_132379904.jpg" alt="mange l'écran" />
<div>
<div class="Header">
<div>
<div>
Some text
</div>
</div>
</div>
<div class="Body">
<div>
<div>
Some text<br />
[button link="#" color="orange" size="medium" type="flat" shape="square" target="_parent" title=" Test "] Mode de cuisson [/button]
</div>
</div>
</div>
</div>
</div>
<div class="AlaOracle">
<img src="http://mywebsite.ru/wp-content/uploads/shutterstock_132379904.jpg" alt="mange l'écran" />
<div>
<div class="Header">
<div>
<div>
Some text
</div>
</div>
</div>
<div class="Body">
<div>
<div>
Some text
</div>
</div>
</div>
</div>
</div>
<div class="AlaOracle Bigger">
<img src="http://mywebsite.ru/wp-content/uploads/shutterstock_132379904.jpg" alt="mange l'écran" />
<div>
<div class="Header">
<div>
<div>
Some text
</div>
</div>
</div>
<div class="Body">
<div>
<div>
Some text
</div>
</div>
</div>
</div>
</div>
<div class="AlaOracle">
<img src="http://mywebsite.ru/wp-content/uploads/shutterstock_132379904.jpg" alt="mange l'écran" />
<div>
<div class="Header">
<div>
<div>
Some text
</div>
</div>
</div>
<div class="Body">
<div>
<div>
Some text<br />
[button link="#" color="orange" size="medium" type="flat" shape="square" target="_parent" title=" Test "] Mode de cuisson [/button]
</div>
</div>
</div>
</div>
</div>
<div class="AlaOracle">
<img src="http://mywebsite.ru/wp-content/uploads/shutterstock_132379904.jpg" alt="mange l'écran" />
<div>
<div class="Header">
<div>
<div>
Some text
</div>
</div>
</div>
<div class="Body">
<div>
<div>
Some text<br />
[button link="#" color="orange" size="medium" type="flat" shape="square" target="_parent" title=" Test "] Mode de cuisson [/button]
</div>
</div>
</div>
</div>
</div>
</div>

Using Flexbox
Compatibility: IE11 and all modern browsers. Safari is supported with the -webkit- prefix.
If the lack of legacy browser support is not a problem, this is a good way to achieve this flexibility without using wrappers or javascript.
Let's make this
The HTML
Couldn't be simpler:
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
The CSS
the body (or a single outer wrapper) is given display: flex, flex-direction: column and flex-wrap: wrap so that the flex items will wrap themselves into columns. It is given an appropriate min and max width and is centered with margin: 0 auto
the divs are given flex: 1 1 50% this tells the div to have a default value of 50% and allow it to grow and shrink, even with a pixel value.
the middle column is given flex: 1 1 100% and it will be twice as tall. It can easily be selected using just nth-child(3)
Here is an excellent guide over on CSS Tricks.
Complete Example
body {
height: 50vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin: 0;
max-width: 1000px;
min-width: 300px;
margin: 0 auto;
}
body > div {
background: #F00;
flex: 1 1 50%;
}
body > div:nth-child(odd) {
background: #F90;
}
body > div:nth-child(3) {
flex: 1 1 100%;
margin: 0 20px;
background: purple;
}
<div>top left</div>
<div>bottom left</div>
<div>middle</div>
<div>top right</div>
<div>bottom right</div>

Maybe something like this?
div.one {
background-color: #f00;
width: 200px;
height: 200px;
}
div.two {
background-color: #0f0;
width: 200px;
height: 200px;
}
div.group-one {
float: left;
}
div.group-two {
float: left;
}
div.three {
background-color: #00f;
width: 200px;
height: 400px;
}
div.group-three {
float: left;
}
div.four {
background-color: #ff0;
width: 200px;
height: 200px;
}
div.five {
background-color: #0ff;
width: 200px;
height: 200px;
<div class="group-one">
<div class="one"></div>
<div class="two"></div>
</div>
<div class="group-two">
<div class="three"></div>
</div>
<div class="group-three">
<div class="four"></div>
<div class="five"></div>
</div>

Related

Push the right aligned element down when the first left element is too long

I'm trying to achieve this, where brown gets pushed down when pink is too long, and pink is dynamically sized based on its text content.
I was only able to achieve this using javascript and two templates, if pink length is over X use second template, but I'm wondering if there's a way to achieve this using CSS only?
I tried meddling with grid auto-fill/auto-fit with minmax, float, flex with wrap, but I couldn't get to the desired result with these.
.parent {
width: 170px;
}
.flex {
display: flex;
}
div {
outline: 2px solid rgba(255, 0,0, 0.3);
}
<div>
<p>scenario A</p>
<div class="parent flex">
<div>
<div class="one">A short title</div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
<div>
<p>scenario B</p>
<div class="parent">
<div class="one">Testing a very long title</div>
<div class="flex">
<div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
</div>
<p>can a component achieve both a and b with only css?
I found a solution, it requires having fixed height in the first row, and the right side button will basically overflow when needed.
Will wait a bit before accepting my own solution in case someone came with a better one.
.container {
width: 300px;
display: flex;
gap: 10px;
padding: 16px;
font-size: 14px;
font-family: Arial;
}
.text {
flex: 1;
min-width: 0;
}
.first-row {
height: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.image {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #444;
}
.title {
flex-grow: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<h2>Long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test title test title</div>
<button>Visit store</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Short title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Short title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Very long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test titleTest title test titleTest title test title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>

Getting alternative columns to have text on top in css

I'm trying to create a page where I have image 1 on top and the text at the bottom and image 2 at the bottom and the text on top. I'm not sure how to go about doing it this way.
In my JSFIDDLE I would like "Text 1" to be at the bottom. "Text 2" on top and "Text 3" at the bottom.
my html
<div class="gallery_wrapper">
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/QuLaaLb.jpg">
</div>
<div class="gallery_title">
<h2>
Text 1
</h2>
</div>
</div>
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/vFEg6ef.jpg">
</div>
<div class="gallery_title">
<h2>
Text 2
</h2>
</div>
</div>
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/QuLaaLb.jpg">
</div>
<div class="gallery_title">
<h2>
Text 3
</h2>
</div>
</div>
JSFIDDLE
This should help you get started. You can use flex and pseudo-properties to achieve this.
.gallery_wrapper {
display: flex;
}
.gallery {
background: #333333;
}
.gallery:nth-child(even) {
display: flex;
flex-direction: column-reverse;
}
.gallery_title {
color: #fff
}
<div class="gallery_wrapper">
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/vFEg6ef.jpg">
</div>
<div class="gallery_title">
<h2>
Text 1
</h2>
</div>
</div>
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/vFEg6ef.jpg">
</div>
<div class="gallery_title">
<h2>
Text 2
</h2>
</div>
</div>
<div class="gallery">
<div class="gallery_image">
<img src="http://i.imgur.com/vFEg6ef.jpg">
</div>
<div class="gallery_title">
<h2>
Text 3
</h2>
</div>
</div>
</div>
You can use flexbox for your requirement.
In flexbox there are two properties with which you can solve your issue. Either using order or using flex-direction. But flex-direction is for parent and order is for child. So you can use any of the properties to solve your problem.
In this example snippet, I use order.
.item {
display: flex;
flex-direction: column;
}
.text {
font-size: 20px;
}
.image {
display: flex;
width: 200px;
height: 200px;
background-image: url('https://files.allaboutbirds.net/wp-content/themes/html5blank-stable/images/blue-winged-warbler.jpg');
background-size: cover;
background-repeat: no-repeat;
}
.item:nth-child(2n) .text {
order: 2;
}
<section>
<div class="item">
<span class="text">Text 1</span>
<span class="image"></span>
</div>
<div class="item">
<span class="text">Text 2</span>
<span class="image"></span>
</div>
<div class="item">
<span class="text">Text 3</span>
<span class="image"></span>
</div>
</section>
You can add the following CSS to achieve this.
.gallery{
position: relative;
}
.gallery_title{
position: absolute;
bottom: 5px;
left: 0;
}
.gallery:nth-child(even) .gallery_title{
bottom: auto;
top: 5px;
}
Check the link here jsfiddle

CSS #Media Print - horizontal spacing of inline elements

Hi have been working on an AngularJS POS system what needs to print receipts after making a sale. I display a sales summary and use ng-print to print the div containing the sales summary. I do the usual with display:none for all other elements and then the rest is display:block. There are a some rows that need to print in a line so I set them to inline but I cannot get spacing between these or even align.
This is an image of the sales summary and the spacing for Desc, Price and Qty.
Here is the html for this:
<div id="sales-summary">
<div layout="column" class="sales-summary-headers" style="text-align: center" style="margin: 3px">
<div class="print-address">{{transaction.store.name}}</div>
<div class="print-address">{{transaction.store.address1}}</div>
<div class="print-address">{{transaction.store.address2}}</div>
<div class="print-address">{{transaction.store.address3}}</div>
<div class="print-address">{{transaction.store.contact}}</div>
<br/>
<div class="print-address">You were served by: {{transaction.employee.firstName}}</div>
</div>
<div class="sales-summary-headers">
<div style="text-align: center"> Sales Summary</div>
</div>
<md-divider></md-divider>
<div layout="row" class="print-row">
<div class="print-left" flex="65"><p>Desc</p></div>
<div class="print-mid" flex="15"><p>Qty</p></div>
<div class="print-right" layout-align="center end"><p>Price</p></div>
</div>
<md-divider></md-divider>
<div layout="row" class="print-row" ng-repeat="item in saleItems">
<div class="print-left" flex="65">{{item.product.description}} </div>
<div class="print-mid" flex="15">{{item.quantity}} </div>
<div class="print-right" layout-align="center end">{{item.unitTotalDisplayPrice | currency:"€"}} </div>
<br/>
</div>
<md-divider></md-divider>
<md-divider></md-divider>
<div layout="row" class="print-row">
<div flex="80">Subtotal</div>
<div layout-align="center end">{{ subTotal | currency:"€" }}</div>
</div>
<div layout="row" class="print-row">
<div flex="80">Tax</div>
<div layout-align="center end">{{ tax | currency:"€" }}</div>
</div>
<div layout="row" class="print-row">
<div flex="80">Total</div>
<div layout-align="center end">{{ total | currency:"€" }}</div>
</div>
<md-divider></md-divider>
<div style="text-align: center" class="sales-summary-headers">
<div>Payments</div>
</div>
<md-divider></md-divider>
<div layout="row" class="print-row" ng-repeat="payment in payments">
<div flex="80">{{ payment.type }}</div>
<div layout-align="center end">{{ payment.amount | currency:"€" }}</div>
<br/>
</div>
<md-divider></md-divider>
<div layout="row" class="print-row">
<div flex="80">Change</div>
<div layout-align="center end">{{ transaction.changeValue | currency:"€"}}</div>
</div>
<div>
<div>
Some message for the store...
</div>
</div>
<br/>
<div class="print-row" style="text-align: center" style="margin: 3px">
<h4>Till: {{ transaction.till.name }}</h4>
</div>
</div>
<div layout="row" >
<md-button ng-print print-element-id="sales-summary">
Print
</md-button>
<md-button ng-click="email()">
Email
</md-button>
<span flex></span>
<md-button ng-click="finishTransaction()" >
Done
</md-button>
</div>
It prints only the elements within id="sales-summary".
I have tried to set spacing in CSS but nothing works
Here is code of the CSS. It is a little dirty as I have added in background colors and text color to try isolate elements and see what space the take up on the print preview.
#media print {
body * {
display:none;
}
md-sidenav * {
display: none !important;
}
section * {
display: none !important;
}
#printSection, #printSection * {
display: block;
}
#printSection {
position:absolute;
left:0;
right: 0;
top:0;
}
.print-row *{
width:100%;
display: inline !important;
color: green;
/*background-color: pink;*/
margin: 2px;
}
.print-row div *{
display: inline !important;
}
.print-left{
text-align: left;
width:65% !important;
background-color: blue;
color: black;
}
.print-mid p{
width:65% !important;
background-color: green;
color: white;
}
.print-row{
background-color: yellow;
width:100% !important;
/*font-size: 0pt;*/
}
.print-left{
text-align: left;
width:65% !important;
/*font-size: 12pt;*/
}
.print-mid{
width:15% !important;
text-align: right;
/*font-size: 12pt;*/
}
.print-right{
width:20% !important;
text-align: right !important;
/*font-size: 12pt;*/
}
Here is what prints. No spacing between the desc, Qty Price.
Any help would be greatly appreciated.
I think the reason is the rule with this selector:
.print-row *{
It defines all these elements as inline elements, by which their width setting becomes meaningless. Try to set this to inline-block

Images do not align and are different heights

I have a problem with images displaying in different heights and text underneath not aligning.
this is the mark up, I have a six row containers to display 6 images
<div class="row">
<div class="posts">
<img src="images/bond.jpg" alt="quantum of solace">
<h3 class="title">Quantum of Solace</h3>
<p class="post-info">PG13 | 106min</p>
</div>
</div>
I am setting each post to 14% coz there are 6 of them allowing for 2.5% margin right. I tried to wrap image in a div and set that to overflow hidden, but didn't work.
.row {
width: 100%;
}
.posts {
width: 14%;
float: left;
margin-right: 2.5%;
}
.posts img {
width: 100%;
max-width: 100%;
border-radius: 0.5em;
}
If you can change the disposition of the nodes, you could align the text below the images using: display:table. See the next example:
.table {
display: table ;
}
.row {
display: table-row ;
}
.header, .title, .post-info {
display: table-cell ;
width: 14%;
padding: 0 1.25% 0.25% 1.25% ;
vertical-align: top ;
}
.header img {
max-width: 100%;
border-radius: 0.5em;
}
<div class="table">
<div class="row">
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/en/2/2d/Quantum_of_Solace_-_UK_cinema_poster.jpg" alt="quantum of solace">
</div>
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/en/6/66/E_t_the_extra_terrestrial_ver3.jpg" alt="E.T. the Extra-Terrestrial ">
</div>
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/en/a/a2/Star_Wars_The_Force_Awakens_Theatrical_Poster.jpg" alt="Star Wars VII">
</div>
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/en/8/8e/DisneyCheshireCat.jpg" alt="Alice in Wonderland">
</div>
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/en/6/6c/XFilesMoviePoster.jpg" alt="The X-Files">
</div>
<div class="header">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Poster_-_Gone_With_the_Wind_01.jpg/440px-Poster_-_Gone_With_the_Wind_01.jpg" alt="Gone with the Wind">
</div>
</div>
<div class="row">
<h4 class="title">Quantum of Solace</h4>
<h4 class="title">E.T. the Extra-Terrestrial </h4>
<h4 class="title">Star Wars: Episode VII The Force Awakens</h4>
<h4 class="title">Alice in Wonderland</h4>
<h4 class="title">The X-Files: Fight the Future</h4>
<h4 class="title">Gone with the Wind</h4>
</div>
<div class="row">
<p class="post-info">PG13 | 106min</p>
<p class="post-info">G | 115min</p>
<p class="post-info">PG13 | 136min</p>
<p class="post-info">G | 187min</p>
<p class="post-info">PG13 | 121min</p>
<p class="post-info">G | 238min</p>
</div>
</div>

How do I float these fieldsets?

I am working with an existing system, and am trying to modify the CSS in order to arrange the three columns correctly.
What css changes do I need to make in order to display the third column correctly?
View in this JSFiddle
CSS
.test .repeater {
border: none;
margin: 0;
}
.test .indent1 .wizard-parentcontrol .controlkl {
width: 33%;
float: left;
display: block;
}
.test .wizard-controls .indent1 .control:first-child {
margin-top: 17px;
}
.test .indent1 .wizard-parentcontrol .popupbrowser {
width: 30%;
float: left;
border: 1px solid red;
display: block;
}
HTML
<div class="test wizard-parentcontrol">
<div>
<div>
<fieldset></fieldset>
</div>
<div>
<div>
<fieldset>
<div>
<div class="repeater indent1">
<div class="wizard-parentcontrol">
<div>
<div>
<div class="controlkl">Column 1</div>
</div>
</div>
<div>
<div>
<fieldset>
<div id="p0t2c4dpopupbrowserControl" class="popupbrowser">Column 2</div>
</fieldset>
</div>
<div>
<div class="">
<p class="ctrlinvalidmessage"></p>
<fieldset>
<div id="p0t2c5dpopupbrowserControl" class="popupbrowser">Column 3</div>
</fieldset>
</div>
</div>
</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
</div>
</div>
Check If you can do the following changes.
I just added class
.wizard-parentcontrol div
{
float:left;
}
and removed the hardcoded width's of 33% and 30% from your code.
Check the demo

Resources