Two-column card layout for desktop and mobile - css

Here's my fiddle: https://jsfiddle.net/b6q7pmkg/
I'm trying to achieve two-column cards where it always stays two column regardless on mobile or desktop.
So something like this:
On desktop:
On mobile:
Here's what I have so far.. but not quite there..
.cardContainer {
max-width: 30rem;
}
css:
.root {
display: inline-block;
width: 100%;
border: 1px solid rgb(221, 221, 221);
color: rgb(90, 90, 90);
}
.link {
vertical-align: bottom;
text-decoration: none;
cursor: pointer;
}
.imageContainer{
display: inline-block;
vertical-align: top;
width: 40%;
}
image {
-o-object-fit: cover;
object-fit: cover;
width: 100%;
height: auto;
display: block;
}
.content {
display: inline-block;
vertical-align: top;
width: 60%;
padding: 1rem;
}
.large .title {
font: 400 1.1875rem/1.263 'Avenir Next W01', 'Helvetica Neue', 'Helvetica', 'sans-serif';
font-size: 1.1875rem;
line-height: 1.263;
letter-spacing: 0.0125rem;
font-weight: 700;
}
.body {
margin-top: 1rem;
color: rgb(139, 139, 139);
display: block;
}
.description {
margin-bottom: 0;
font-size: inherit;
margin-top: 1rem;
}

Here's a model with a basic css.
The important thing to know here is the double flexbox; one flexbox for card (img/paragraph), and one flexbox for all the cards, with a flex-wrap: wrap; to make sure they're doing their job right.
I use this flexbox cheat sheet to remember exactly how flexbox works :)
Here's a copy of the jsfiddle code :
section{
display: flex;
flex-wrap: wrap;
}
article{
display: flex;
flex-direction: row;
border: 1px solid black;
width: 300px;
}
<section>
<article>
<img src="http://lorempixel.com/150/100/" />
<p>
Some text.
</p>
</article>
<article>
<img src="http://lorempixel.com/151/100/" />
<p>
Some text.
</p>
</article>
<article>
<img src="http://lorempixel.com/150/101/" />
<p>
Some text.
</p>
</article>
</section>
edit: I just made an edit of the fiddle with grey text and vertically centered paragraphs :)

You can do this with CSS Grid as well:
Put all of your cards in a <div class="grid-container">, add the following lines in your CSS:
.grid-container{
display: grid;
grid-gap: 20px; /*that's optional, for the space between your cards*/
grid-template-columns: repeat(auto-fit, 300px);
}

Related

image goes out from grid and sizing issue

I'm pretty new to coding, and I'm trying to use a grid.
But I got stuck a little bit.
I have the following codePan project:
* {
box-sizing:border-box
}
body {
height:100vh;
width: 100%;
background-color: red;
padding:0;
margin:0;
}
#wrapper {
background-color:blue;
height:100%;
width:100%;
margin:0;
padding:0;
display: grid;
grid-template-areas:
"nav"
"content"
"footer" ;
grid-template-columns: 1fr;
grid-template-rows: .8fr 1fr .8fr ;
}
#nav {
background-color: yellow;
grid-area: "nav";
position: sticky;
top: 0;
}
#content {
background-color: pink;
grid-area: "nav";
display:flex;
flex-direction: column;
}
footer {
background-color: brown;
grid-area: "nav";
}
#box1 {
background-color:purple;
height:50%;
}
#box2 {
background-color: blue;
height:60%;
display:flex;
flex-direction: column;
}
.image {
height: auto;
width: 400px;
position: relative;
top:-150px;
}
button {
height: 90px;
width: 400px;
background-color: #262A58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
}
<div id="wrapper">
<div id="nav"> nav </div>
<div id="content">
<div id="box1">
<h1>flower</h1>
</div>
<div id="box2">
<img class="image" src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1" alt="">
<button> buy flower </button>
</div>
</div>
<footer>
footer
</footer>
</div>
what i want is the similar like on this picture:
picture
unfortunatly i can not postitioning, the contents in the Box2, becuase the button goes out completly out from grid.
I tried several options, but at the end it was always some issue with the content in the Box2.
if someone could give me an advice. Probably I'm using wrong way Grid...
Thank you!
Let your grid define the size of the areas, and use Flexbox to lay out your content.
You can absolutely achieve this layout with CSS Grid. The button was overflowing its content area because #box2 has a fixed height of 60% of the content grid area, but your .image and the button are taller than that. Basically your image pushes down the button.
You don’t need to set explicit heights on your content, because that’s taken care of the grid.
The white-blue background is a bit tricky, because it doesn’t align exactly with the edges of your image. But you can achieve this affect with a background gradient on your content container.
Check out this working solution as inspiration:
body {
height: 100vh;
padding: 0;
margin: 0;
}
#wrapper {
height: 100%;
width: 100%;
display: grid;
grid-template-areas:
'nav'
'content'
'footer';
grid-template-columns: 1fr;
grid-template-rows: 64px 1fr 96px;
}
#nav {
grid-area: nav;
position: sticky;
top: 0;
background-color: white;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}
#content {
grid-area: content;
display: flex;
flex-direction: column;
justify-content: center;
background: linear-gradient(to bottom, transparent 60%, #aad 60%);
align-items: center;
padding: 32px;
}
#content .wrapper {
display: flex;
flex-direction: column;
width: 400px;
}
footer {
grid-area: footer;
}
.image {
border: solid 16px #aad;
margin: 16px;
}
button {
padding: 8px 16px;
background-color: #262a58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
margin-top: 32px;
align-self: center;
}
<div id="wrapper">
<div id="nav">nav</div>
<div id="content">
<div class="wrapper">
<h1>flower</h1>
<img
class="image"
src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1"
alt="Flower"
/>
<button>buy flower</button>
</div>
</div>
<footer>footer</footer>
</div>
Grid is not really used to make a full webpage. Everything is automatically stacked on top of eachother and should not have a fixed height. The height should changed based on the content.

Align text vertically to middle not taking affect

I need to align text near an icon to middle.
See the admin word near the user icon in this sandbox:
https://codesandbox.io/s/fragrant-morning-267l5
I have this css, but it's not taking affect:
.HeaderToolbar {
background-color: black;
color: white;
fill: white;
width: 100%;
margin: 0 auto;
align-items: baseline;
vertical-align: middle;
/*white-space: nowrap;*/
}
I would suggest you to use elements like header, nav and div for layout along with flexbox instead of going for table to layout your header.
Please check following code. It shows basic mark up for header component with navigation and logo.
header {
width: 100vw;
height: 54px;
}
.header {
width: 100%;
max-width: 1000px;
margin: 0 auto;
display: flex;
align-items: center;
padding: 0 8px;
}
.logo {
flex: 1;
}
nav {
flex: 2;
display: flex;
justify-content: flex-end;
}
button {
padding: 8px 12px;
color: white;
border: none;
cursor: pointer;
}
.ghost {
color: black;
background: transparent;
}
<header>
<div class='header'>
<div class="logo">
<h2>Logo</h2>
</div>
<nav>
<button>Projects</button>
<button>Projects</button>
<button class='ghost'>Admin 😳</button>
<button>Profile</button>
</nav>
</div>
</header>
You need to add a class to your element and then add a css to your class:
display: flex;
justify-content: flex-start;
align-items: center;

Flexbox in css grid: how to position the flex items

I am new to using flexbox to position items within a css grid layout. The grid layout is three large cells 10%, 35% and 55% of viewport. In the second grid cell (marked "b"), I use flexbox to position a header and a subheader.
My problem is that I am not able to accurately position the header and subheader within the grid cell "b". Also, the header is takes up too much space in the vertical direction, thereby pushing the subheader too low.
Here is the html code:
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Welcome to Project</title>
<link href="css/main.css" rel="stylesheet" type="text/css">
</head>
<body class="site">
<div class="grid">
<div class="a">
<div class="a_left">
<div>Logo for Project</div>
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
</div>
<div class="b">
<div class="b_left">
<h1>This is the main header, it's longer than the subheader</h1>
</div>
<div class="b2_left">
<h3>This is the subheader</h3>
</div>
</div>
<div class="c">
<h2>This is grid-template-row c</h2>
</div>
</div>
</body>
</html>
Here is the css code:
#charset "utf-8";
body{
background-color: black;
margin-top: 0;
margin-bottom: 0;
margin-left: 0;
margin-right: 0;
}
.grid {
display: grid;
width: 100vw;
height: 100vh;
grid-template-rows: 10% 35% 55%;
}
.grid > * {
background-color: darkgray;
color: white;
padding: 2em;
}
.a{
display: grid;
background-color: lightgray;
font-family: sans-serif;
color: green;
font-size: 16pt;
}
.a_left{
display: flex;
text-align: left;
vertical-align: left;
flex-wrap: nowrap;
justify-content: space-between;
}
.a_right{
display: flex;
height: 100%;
flex-wrap: nowrap;
justify-content: right;
vertical-align: right;
}
.b{
display: grid;
background-color: white;
font-family: sans-serif;
color: blue;
font-size: 16pt;
}
.b_left{
display: flex;
flex-flow: row;
flex-grow: 0;
text-align: left;
vertical-align: left;
flex-wrap: nowrap;
justify-content: left;
height: 0px;
max-height: 0px;
flex-direction: row;
align-items: flex-start;
}
.b2_left{
display: flex;
flex-flow: row;
flex-grow: 0;
text-align: left;
vertical-align: left;
flex-wrap: nowrap;
justify-content: left; /*space-between*/
height: 0px;
max-height: 0px;
flex-direction: row;
align-items: flex-start;
}
.c{
display: grid;
background-color: black;
font-family: sans-serif;
color: black;
font-size: 16pt;
}
li {
display: inline;
}
site-nav{
margin-top: 0px;
}
.topnav {
align-content: right;
justify-content: center;
overflow: hidden;
background-color: #333;
height: 100%
}
.topnav a {
float: left;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
h1{
color: blue; /*deepskyblue;*/
font-family: sans-serif;
font-size: 28pt;
text-indent: 30px;
height: 0vh; /*0px*/
width: auto;
}
h2{
color: lightgray; /*deepskyblue;*/
font-family: sans-serif;
font-size: 12pt;
text-indent: 12px;
}
h3{
color: gray;
font-family: sans-serif;
font-size: 18pt;
text-indent: 85px;
}
a:link { color: green; }
a:visited { color: blue; }
a:hover { color:red; }
.site{
max-width: 100%;
max-height: 100%;
display: grid;
}
My questions are: (1) how can I precisely position the starting point of each of the header (within b_left) and subheader (within b2_left), and (2) how do I set the max height for the flex container that holds each of the two.
I have done a lot of research on this, and I have mixed and matched properties, but flexbox and grid both have a lot of properties and I still haven't found the right combo.
Thanks very much for any help.
EDIT: one possibility is to use position: absolute for each, and on the subheader set top: 150px, but I hope there is any way to do this without absolute positioning.
(1) how can I precisely position the starting point of each of the header (within b_left) and subheader (within b2_left:
The starting point is given with the properties of justify-content (start, between, end).
Second, I dont get it correctly, do you want to position the header above the subheader with flexbox?
If yes, maybe you want to set flex-direction: column; because this put all elements inside one above another.
In this link you can find more information about justify-content an its posible values
how do I set the max height for the flex container that holds each of the two.
I'm not sure if flexbox admit max height configuration, because its height is calculated depending on container's size.
Hope this can help you.

align inner div in vertical, horizontal center against the parent div without line breaking for the following span text element

If I didn't make parent container be inline-block style
The inner arrow will be aligned in the center position which is I expected.
However, there will be a line-break for the following text.
If I make the parent container be inline-block style
HTML
<div class="queue-view-entry-line" name="Name">
<div class="mycompany-document" style="/* display: inline-block; */">
<div class="arrow-right">
</div>
</div>
<span class="entry-label">File Name</span><span class="entry-value">Planned Payment Dates 2017
</span>
</div>
CSS rules
div{
.mycompany-document{
background: #F7F7F7;
border: 1px solid #AAAAAA;
left: 64px;
top: 64px;
width: 32px;
height: 32px;
vertical-align: middle;
border-radius: 5px;
letter-spacing: 1px;
text-align: center;
display: table-cell;
.arrow-right{
margin: auto;
vertical-align: middle;
display:inline-block;
width: 0.4em;
height: 0.4em;
border-right: 0.2em solid black;
border-top: 0.2em solid black;
transform: rotate(45deg);
}
}
}
I recommend you give flexbox a try. It will quickly be your best friend!
I didn't feel like wrestling with your HTML, so I created a new example to show how you could achieve the desired effect.
Check out this fiddle.
https://jsfiddle.net/omucfbzh/
<div class="box">
<h2>Documents</h2>
<div class="others">
<div class="arrow-container">
<div> > </div>
</div>
<p>Planned Payment Dates 2017</p>
</div>
</div>
.box {
background-color: orange;
display: flex;
flex-direction: column;
padding: 7.5px;
}
.others {
display: flex;
}
.arrow-container {
background-color: grey;
border-radius: 5px;
height: 50px;
width: 50px;
margin-right: 5px;
display: flex;
align-items: center;
justify-content: center;
}

Vertical align (inline, middle) divs with image and text

I just want to align two divs next to each other and aligning the content vertically middle in each. Any help could save my mental health. Here is my code:
.main-kozossegitag-container {
display: block;
width: 100%;
height: 100%;
}
.main-kozossegitag-text1 {
display: inline-block;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
}
.main-kozossegitag-nev {
font-size: 2em;
}
.main-kozossegitag-title {
font-size: 1em;
}
.main-kozossegitag-visszhang {
font-size: 1em;
}
.main-kozossegitag-image1 {
display: inline-block;
width: 39%;
}
.profilkep {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid rgba(255,255,255,0.5);
box-shadow: 1px 1px 5px rgba(0,0,0,0.3);
}
<div class="main-kozossegitag-container">
<div class="main-kozossegitag-text1">
<h3 class="main-kozossegitag-nev">Rita</h3>
<p class="main-kozossegitag-title">CEO</p>
<p class="main-kozossegitag-visszhang">Lorem ipsum dolor sit amet.</p>
</div>
<div class="main-kozossegitag-image1">
<img src="http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/szabo_rita.jpg" alt="Szabó Rita" class="profilkep">
</div>
</div>
As you see the two divs are next to each other, but I can not align the text vertically in the middle :(
.main-kozossegitag-container {
display: table;
width: 100%;
height: 100%;
}
.main-kozossegitag-text1 {
display: table-cell;
vertical-align : middle;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
}
.main-kozossegitag-nev {
font-size: 2em;
}
.main-kozossegitag-title {
font-size: 1em;
}
.main-kozossegitag-visszhang {
font-size: 1em;
}
.main-kozossegitag-image1 {
display: table-cell;
vertical-align : middle;
width: 39%;
}
.profilkep {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid rgba(255,255,255,0.5);
box-shadow: 1px 1px 5px rgba(0,0,0,0.3);
}
<div class="main-kozossegitag-container">
<div class="main-kozossegitag-text1">
<h3 class="main-kozossegitag-nev">Rita</h3>
<p class="main-kozossegitag-title">CEO</p>
<p class="main-kozossegitag-visszhang">Lorem ipsum dolor sit amet.</p>
</div>
<div class="main-kozossegitag-image1">
<img src="http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/szabo_rita.jpg" alt="Szabó Rita" class="profilkep">
</div>
</div>
I added display : table; to your main container (main-kozossegitag-text1) and display : table-cell; vertical-align : middle to your both sub-div. Those properties let them have the same behaviour as a table and cells that contains vertically aligned content. I just messed up with some left/right margin but the rest seems to work.
Add Vertical-align to the second Div.
.main-kozossegitag-image1 {
display: inline-block;
width: 39%;
vertical-align: middle;
}
Please see my fiddle here.
I used CSS3 flex-box model. I changed properties for these classes:
.main-kozossegitag-container {
display: flex;
align-items: stretch;
flex-flow: row wrap;
width: 100%;
height: 100%;
border: 1px solid red;
}
.main-kozossegitag-text1 {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: flex-end;
align-content: flex-end;
box-sizing: border-box;
width: 60%;
padding-right: 20px;
}
More on CSS3 flex-box here.
Just add a float-left to this div :
.main-kozossegitag-text1{
display: table-cell;
vertical-align : middle;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
float: left;
}

Resources