Am I using flex correctly in this situation? - html

Say I want to a container of courses that would look something like this
Is this bad making essentially each part of the "course" it's own flexbox?
.course-container {
display: flex;
flex-direction: column;
}
.course-options {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.course-title {
display: flex;
flex-direction: row;
justify-content: center;
}
.course-grade {
display: flex;
flex-direction: row;
justify-content: flex-end;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
Course 1
</div>
<div class="course-grade">
Grade: 0.00%
</div>
</div>
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
Course 2
</div>
<div class="course-grade">
Grade: 0.00%
</div>
</div>
</div>

You don't need to define display: flex on each inner element, you can use align-self instead.
.course-container,
.course {
display: flex;
flex-direction: column;
}
.course {
width: 300px;
border: 1px solid black;
padding: 10px;
height: 100px;
margin: 5px;
justify-content: space-between;
}
.course-options {
display: flex;
justify-content: space-between;
}
.course-title {
align-self: center;
}
.course-grade {
align-self: flex-end;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
Course 1
</div>
<div class="course-grade">
Grade: 0.00%
</div>
</div>
<div class="course">
<div class="course-options">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
</div>
<div class="course-title">
Course 2
</div>
<div class="course-grade">
Grade: 0.00%
</div>
</div>
</div>

There is nothing wrong with how you set it up, and whether it is the best is very much dependent on how a course box should behave.
With what I know now, you could optimize that code, which I would, and achieve the same result.
Here I removed all inner wrappers and used auto margins, which got an upgrade with Flexbox, that make it easy to align items within its parent.
The benefit with a structure like this, besides being having a lot less markup, you have endless ways to reorder the items based on content or screen sizes.
Stack snippet
.course-container {
display: flex;
flex-direction: column;
}
.course {
display: flex;
flex-wrap: wrap;
align-items: flex-start; /* align to top, and prevent from stretch */
width: 250px;
height: 120px;
margin: 5px;
border: 1px solid lightgray;
}
.course .fa-trash-o,
.course .grade {
margin-left: auto; /* push trash and grade to the right */
}
.course .grade {
margin-top: auto; /* push to bottom */
}
.course .title {
flex-basis: 100%; /* take full width, make it wrap on a row of its own */
margin: auto 0; /* center vertically */
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="course-container">
<div class="course">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
<a class='title' href="">Course 1</a>
<a class='grade' href="">Grade: 0.00%</a>
</div>
<div class="course">
<i class='fa fa-pencil-square-o fa-lg' aria-hidden="true"></i>
<i class='fa fa-trash-o fa-lg' aria-hidden="true"></i>
<a class='title' href="">Course 2</a>
<a class='grade' href="">Grade: 0.00%</a>
</div>
</div>

This situation does NOT require flexbox. Flexbox still has less than 90% browser support worldwide (unprefixed), so I would not use it yet, unless you have absolutely no alternative.
This is a proper solution that works in all browsers:
.card {
text-align: center;
position: relative;
width: 100%;
max-width: 300px;
height: 100px;
line-height: 100px;
border: 1px solid black;
margin: 5px;
}
.card > * {line-height: 1.4em; position: absolute;}
.card > .edit {left: 0; top: 0;}
.card > .delete {right: 0; top: 0;}
.card > .grade {right: 0; bottom: 0;}
<div class="card">
edit
delete
<span class="grade">grade</span>
course 1
</div>
<div class="card">
edit
delete
<span class="grade">grade</span>
course 2
</div>

Related

How To Make Social Icons Responsive HTML CSS

i am developing my portfolio website but i came into an error can anyone just help me.
i need to make my social icons responsive.
Here The The Image For Desktop Version.
I need To make it responsive and direction to row for mobile version. Please Help Me.
As You Can See Here The Social Icons Are Not Responsive.
index.html
```
<!-- home -->
<section class="home bd-grid" id="home">
<div class="max-width">
<div class="home-content">
<div class="text-1">Hello, Myself</div>
<div class="text-2">Piyush Shrivastava</div>
<div class="text-3">And I'm a <span class="typing"></span></div>
Download CV
</div>
</div>
<div class="social">
<ul>
<li>
<div class="circle">
<i class="fa fa-facebook-f" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-instagram" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-youtube" aria-hidden="true"></i>
</div>
</li>
</ul>
</div>
</section>```
Styles.css
.home{
display: flex;
flex-wrap: wrap;
background: url("/img/banner.jpg") no-repeat center;
height: 100vh;
color: #fff;
min-height: 500px;
background-size: cover;
background-attachment: fixed;
font-family: 'Ubuntu', sans-serif;
}
.home .max-width{
width: 100%;
display: flex;
}
.home .max-width .row{
margin-right: 0;
}
.home .home-content .text-1{
font-size: 27px;
}
.home .home-content .text-2{
font-size: 75px;
font-weight: 600;
margin-left: -3px;
}
.home .home-content .text-3{
font-size: 40px;
margin: 5px 0;
}
.home .home-content .text-3 span{
color: #32de84;
font-weight: 500;
}
.home .home-content a{
display: inline-block;
background: #32de84;
color: #fff;
font-size: 25px;
padding: 12px 36px;
margin-top: 20px;
font-weight: 400;
border-radius: 6px;
border: 2px solid #32de84;
transition: all 0.3s ease;
}
.home .home-content a:hover{
color: #32de84;
background: none;
}
.social{
position: absolute;
top: 35%;
right: 5%;
color: #fff;
/* background-color: #ffffff; */
}
.social ul{
list-style: none;
}
.social li{
margin-bottom: 20px;
text-align: center;
}
.circle{
background-color: transparent;
border: 2px solid white;
border-radius: 200px;
height: 35px;
width: 35px;
}
.social i{
padding-top: 7px;
height: 30px;
width: 30px;
color: #ffffff;
}
#media (max-width: 947px) {
.home .home-content .text-2 {
font-size: 60px;
}
}```
Try thinking of your problem the other way around.
Layout shifts like this are much easier to deal with when working with a 'mobile first' approach. (style the mobile version and then override at a min-width media query to style the desktop version, rather than styling the desktop perfectly and then trying to fold everything down to mobile.)
I've knocked up a quick demo of what should work for you.
This is NOT a direct replication of or fix of your existing code, just a demo of the methods you could use, but hopefully from this demo and the snippets, you should understand what is happening and be able to apply some of these methods to your own code.
The method is entirely done with flex properties.
The socials are no longer absolutely positioned and the unnecessary social wrapper div is removed.
I've added border colors and padding just to more obviously show you whats happening to the box model when you resize your browser.
To view the demo, click 'run code snippet' - but make sure to then also click the 'full page' option otherwise it'd only show you the mobile layout.
Here's the JSFiddle where I created the demo incase it's useful: CLICK TO VIEW FIDDLE
.contentWrapper {
border: 1px solid pink;
padding: 10px;
}
.mainContent {
border: 1px solid blue;
padding: 10px;
}
.social {
border: 1px solid green;
padding: 10px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: row;
gap: 10px;
}
.circle {
background-color: red;
border: 2px solid white;
border-radius: 200px;
height: 35px;
width: 35px;
}
#media screen and (min-width: 948px) {
.myWrapper {
display: flex;
min-height: 100vh;
flex-direction: row;
}
.contentWrapper {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center
}
.mainContent {
width: calc(100% - 55px);
max-width: 1000px;
margin: 0 auto;
}
.social {
display: flex;
flex-direction: column;
justify-content: center;
}
.social ul {
display: flex;
flex: 0 0 55px;
flex-direction: column;
}
}
<section class="myWrapper">
<div class="contentWrapper">
<div class="mainContent">
<div class="text-1">Hello, Myself</div>
<div class="text-2">Piyush Shrivastava</div>
<div class="text-3">And I'm a <span class="typing"></span></div>
Download CV
</div>
</div>
<ul class="social">
<li>
<div class="circle">
<i class="fa fa-facebook-f" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-instagram" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-youtube" aria-hidden="true"></i>
</div>
</li>
</ul>
</section>
To make icons in row
we can do it using #media in the approach
APPROACH
Use flexbox to make your icons in a row
and use grid to make everything in a row when you are on a big screen
you can also set width and then add margin auto to make things centre but using justify-content center is better ---> not told in this answer
<!-- home -->
<section class="home bd-grid" id="home">
<div class="max-width">
<div class="home-content">
<div class="text-1">Hello, Myself</div>
<div class="text-2">Piyush Shrivastava</div>
<div class="text-3">And I'm a <span class="typing"></span></div>
Download CV
</div>
</div>
<div class="social">
<ul class="icons"> // give class icons to make it a flexbox
<li>
<div class="circle">
<i class="fa fa-facebook-f" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-instagram" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</li>
<li>
<div class="circle">
<i class="fa fa-youtube" aria-hidden="true"></i>
</div>
</li>
</ul>
</div>
</section>```
***css***
.icons{
display: flex;
flex-direction: column;
justify-content: center;
}
#media screen and (min-width: 800px //your mobile screen width) {
.icons {
display: flex;
justify-content:center;
}
.home{
display:grid;
width: 100vw;
gap: 20px;
text-align:center;
grid-template-column:2fr 2fr;
}

Vertical aligning items in CSS which are next to each other [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 1 year ago.
I have 2 icons in divs and 1 link which works as a button
.A {
width: 50px;
height: 50px;
border: 1px solid black;
display: inline-flex;
justify-content: center;
align-items: center;
margin: 5px;
}
a {
text-decoration: none;
}
.B {
width: 100px;
height: 50px;
}
/* when i change font-size for example to 10px then the element moves down a bit*/
.A span {
text-transform: uppercase;
color: black;
font-size: 10px;
}
<script src="https://kit.fontawesome.com/4254229804.js" crossorigin="anonymous"></script>
<a href="#">
<div class="A">
<i class="fab fa-facebook-f"></i>
</div>
</a>
<a href="#">
<div class="A">
<i class="fab fa-youtube"></i>
</div>
</a>
<a href="#">
<div class="A B">
<span>sign up</span>
</div>
</a>
here is a link to codepen https://codepen.io/hanzus/pen/GRrMbbm
Everything is centered when a font size of span element of "sign up" link is 16px, but when I change it for example to 10px then link moves a bit down and it is not on the same line with other divs with facebook and youtube links.
How do I get these links on the same line when I change a font-size of 1 element?
Wrap in flex:
.container {
display: flex;
}
<div class="container">
<a href="#">
<div class="A">
<i class="fab fa-facebook-f"></i>
</div>
</a>
<a href="#">
<div class="A">
<i class="fab fa-youtube"></i>
</div>
</a>
<a href="#">
<div class="A B">
<span>sign up</span>
</div>
</a>
</div>
Codepen
I know I am a bit a late to the party, but it is possible to make it simpler:
.container {
display: inline-flex;
width: 300px;
height: 50px;
}
.container a {
width: 33%;
margin: 5px;
border: 1px solid black;
display:inline-flex;
justify-content:center;
align-items:center;
font-size: 16px;
}
<div class="container">
<a href="#">
<i class="fab fa-facebook-f"></i>
</a>
<a href="#">
<i class="fab fa-youtube"></i>
</a>
<a href="#">
<span>sign up</span>
</a>
</div>
You should only add "flex-direction: column;" for your CSS code.
.container{
display: flex;
flex-direction: column;
}
.A {
width:50px;
height: 50px;
border: 1px solid black;
display:inline-flex;
justify-content:center;
align-items:center;
margin: 5px;
}
a {
text-decoration:none;
}
.B {
width: 100px;
height: 50px;
}
.A span {
text-transform: uppercase;
color:black;
font-size: 10px;
}

Why isn't my flex grow working even with a defined flex basis?

I want the 3 boxes to fill the height of the screen in 1:4:1 ratio.
*{
margin:0;
padding:0;
}
.container{
display: flex;
flex-direction:column;
align-items: center;
height:100100%;
width:100%;
}
.title{
background:yellow;
flex-basis:30px;
flex-grow:1;
}
.box{
background:green;
flex-basis:120px;
flex-grow:4;
}
.buttons{
background:red;
flex-basis:30px;
flex-grow:1;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
<div class="title">
Random Quote Generator
</div>
<div class="box">
This is Where Author and Quote go.
</div>
<p class="blank"></p>
<div class="buttons">
<a class="button"><i class="fa fa-twitter"></i></a>
<a class=button><i class="fa fa-tumblr"></i></a>
<a class='button'><i class="fa fa-chevron-right"></i></a>
</div>
</div>
You're using percentage heights. That requires you to define a height on the parent element. It gets tricky. See here: Working with the CSS height property and percentage values
Instead, just use height: 100vh, which is much simpler and easier.
.container {
display: flex;
flex-direction: column;
align-items: center;
height: 100vh; /* NEW */
width: 100%;
}
.title {
background: yellow;
flex: 1 0 30px;
}
.box {
background: green;
flex: 4 0 120px;
}
.buttons {
background: red;
flex: 1 0 30px;
}
* {
margin: 0;
padding: 0;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
<div class="title">
Random Quote Generator
</div>
<div class="box">
This is Where Author and Quote go.
</div>
<p class="blank"></p>
<div class="buttons">
<a class="button"><i class="fa fa-twitter"></i></a>
<a class=button><i class="fa fa-tumblr"></i></a>
<a class='button'><i class="fa fa-chevron-right"></i></a>
</div>
</div>

How to achieve this effect? its about borders/lines

https://codepen.io/anon/pen/NpoevM
http://imgur.com/a/OQ3cu
I want to have the small lines to separate the boxes. I was thinking about how to make the lines like in the picture. I was thinking using borders or span but having trouble
#third {
height: 55%;
width: 40%;
display: flex;
flex-direction: column;
float: right;
}
.t-row {
height: 100%;
display: flex;
}
.tbox {
width: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fa {
font-size: 5em !important;
}
<section id="third">
<div class="hr-lines">
<hr class="icon-sep">
</div>
<div class="t-row">
<div class="tbox tb-one">
<i class="fa fa-home" aria-hidden="true"></i>
<h1 class="t-text">Home</h1>
</div>
<div class="tbox tb-two">
<i class="fa fa-info-circle" aria-hidden="true"></i>
<h1 class="t-text">About</h1>
</div>
</div>
<div class="t-row">
<div class="tbox tb-three">
<i class="fa fa-book" aria-hidden="true"></i>
<h1 class="t-text">Work</h1>
</div>
<div class="tbox tb-four">
<i class="fa fa-envelope" aria-hidden="true"></i>
<h1 class="t-text">Hire</h1>
</div>
</div>
</section>
check below snippet. I have used :after for all four div and used border to create the + sign.
#third {
height: 55%;
width: 40%;
display: flex;
flex-direction: column;
float: right;
}
.t-row {
height: 100%;
display: flex;
}
.tbox {
width: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fa {
font-size: 5em !important;
}
.tb-one,
.tb-two,
.tb-three,
.tb-four {
position: relative;
}
.tb-one:after,
.tb-two:after,
.tb-three:after,
.tb-four:after {
position: absolute;
content: " ";
border: 1px solid #000;
width: 20px;
height: 20px;
display: block;
}
.tb-one:after {
bottom: 0;
right: 0;
border-top: none;
border-left: none
}
.tb-two:after {
bottom: 0;
left: 0;
border-top: none;
border-right: none
}
.tb-three:after {
top: 0;
right: 0;
border-bottom: none;
border-left: none
}
.tb-four:after {
top: 0;
left: 0;
border-bottom: none;
border-right: none
}
<section id="third">
<div class="hr-lines">
<hr class="icon-sep">
</div>
<div class="t-row">
<div class="tbox tb-one">
<i class="fa fa-home" aria-hidden="true"></i>
<h1 class="t-text">Home</h1>
</div>
<div class="tbox tb-two">
<i class="fa fa-info-circle" aria-hidden="true"></i>
<h1 class="t-text">About</h1>
</div>
</div>
<div class="t-row">
<div class="tbox tb-three">
<i class="fa fa-book" aria-hidden="true"></i>
<h1 class="t-text">Work</h1>
</div>
<div class="tbox tb-four">
<i class="fa fa-envelope" aria-hidden="true"></i>
<h1 class="t-text">Hire</h1>
</div>
</div>
</section>
In your code add four classes with border property of div.
<style>
#third {
height: 55%;
width: 40%;
display: flex;
flex-direction: column;
float: right;
}
.t-row {
height: 100%;
display: flex;
}
.tbox {
width: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fa {
font-size: 5em !important;
}
.left-border{ border-left : 1px solid;}
.right-border{ border-right : 1px solid;}
.bottom-border{ border-bottom : 1px solid;}
.top-border{ border-top: 1px solid;}
</style>
<section id="third">
<div class="hr-lines">
<hr class="icon-sep">
</div>
<div class="t-row">
<div class="tbox tb-one right-border bottom-border">
<i class="fa fa-home" aria-hidden="true"></i>
<h1 class="t-text">Home</h1>
</div>
<div class="tbox tb-two left-border bottom-border">
<i class="fa fa-info-circle" aria-hidden="true"></i>
<h1 class="t-text">About</h1>
</div>
</div>
<div class="t-row">
<div class="tbox tb-three top-border right-border">
<i class="fa fa-book" aria-hidden="true"></i>
<h1 class="t-text">Work</h1>
</div>
<div class="tbox tb-four top-border left-border">
<i class="fa fa-envelope" aria-hidden="true"></i>
<h1 class="t-text">Hire</h1>
</div>
</div>
</section>
You can use a '+' inside a span and position / style it
#third {
height: 55%;
width: 40%;
display: flex;
flex-direction: column;
float: right;
position:relative;
}
.t-row {
height: 100%;
display: flex;
}
.tbox {
width: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fa {
font-size: 5em !important;
}
.lines { position:absolute; font-size:150px; top:17%; left:40% }
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet">
<section id="third">
<span class="lines">+</span>
<div class="t-row">
<div class="tbox tb-one">
<i class="fa fa-home" aria-hidden="true"></i>
<h1 class="t-text">Home</h1>
</div>
<div class="tbox tb-two">
<i class="fa fa-info-circle" aria-hidden="true"></i>
<h1 class="t-text">About</h1>
</div>
</div>
<div class="t-row">
<div class="tbox tb-three">
<i class="fa fa-book" aria-hidden="true"></i>
<h1 class="t-text">Work</h1>
</div>
<div class="tbox tb-four">
<i class="fa fa-envelope" aria-hidden="true"></i>
<h1 class="t-text">Hire</h1>
</div>
</div>
</section>

Inline-blocks overlapped by flexbox in Chrome

I have strange behavior of inline blocks in nested flexbox.
This happens in Chrome.
I use green block for spacing, nested .row need to show/hide all it's( content, if remove display:flex(or .row) from it -- .spacer not work.
Can anyone help?
body {
font-size: 20px;
width: 50%;
}
.row {
display: flex;
box-sizing: border-box;
flex-direction: row;
}
.spacer {
flex: 0 0 15px;
text-align: center;
background-color: #AEA;
}
.flex {
flex: 1;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"/>
<body>
<div class="row">
<span>label</span>
<span class="spacer">|</span>
<div class="row">
<i class="fa fa-clock-o"></i>
<span>date</span>
<span class="spacer"></span>
<span>date2</span>
</div>
<span class="flex"></span>
<span class="fa fa-arrow-down"></span>
</div>
</body>