How to keep icons aligned strictly inside div? - html

I am making a sidebar and am new to css. I created a div which represents a closed sidebar. It is supposed to only show the icons. Unfortunately the icons come in a misaligned manner inside the div based on their size. How do I fix this?
.sidenav {
height: 492px;
width: 300px;
background-color: #db3d44;
}
.data-icon {
font-size: 45px;
color: black;
opacity: 0.5;
float: left;
margin-left: 9px;
margin-top: 5px;
margin-bottom: 10px;
}
.hamburger {
background-color: transparent;
border-color: transparent;
color: white;
margin-left: 10px;
margin-top: 4px;
font-size: 25px;
}
.hamburger:hover {
color: black;
}
.sidenav-closed {
width: 65px;
float: left;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="sidenav-closed sidenav">
<button class="hamburger data-disappear">☰</button>
<div class="icons-only">
<div class="data-icon">
<i class="fa fa-home"></i>
</div>
<div class="data-icon">
<i class="fa fa-car"></i>
</div>
<div class="data-icon">
<i class="fa fa-home"></i>
</div>
</div>
</div>
The car icon is misaligned here. What's the solution?

You could try to align all the icons to the center so your .data-icon class could look like this:
.data-icon {
font-size: 45px;
color: black;
opacity: 0.5;
display: block;
width: 100%;
text-align: center;
}

Your div elements doesn't have set width and height in CSS - so the icons are strictly aligned inside them.
If you want to vertically center them and learn something new use flexbox:
.icons-only {
display: flex;
flex-direction: column;
align-items: center;
}
and
.data-icon {
...
margin-left:0
}
Your car icon is also a little bit bigger than house - you can a little change it size by add new class to it or use this:
.data-icon:nth-child(2) {
font-size: 40px;
}
This CSS code will take second (=2) element with class .data-icon and set different font size for it

The icons are inline elements so they will be left aligned by default. Add in that the icons are not that same size (this is normal), you get an uneven alignment.
To remedy this, add text-align: center to .icons-only.
Note: Given the layout in the example, it does not appear necessary to float .data-icon to the left.
.sidenav {
height: 492px;
width: 300px;
background-color: #db3d44;
}
.icons-only {
text-align: center;
}
.data-icon {
color: rgba( 0, 0, 0, 0.5 );
font-size: 45px;
}
.hamburger {
background-color: transparent;
border-color: transparent;
color: white;
margin-left: 10px;
margin-top: 4px;
font-size: 25px;
}
.hamburger:hover {
color: black;
}
.sidenav-closed {
width: 65px;
float: left;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="sidenav-closed sidenav">
<button class="hamburger data-disappear">☰</button>
<div class="icons-only">
<div class="data-icon">
<i class="fa fa-home"></i>
</div>
<div class="data-icon">
<i class="fa fa-car"></i>
</div>
<div class="data-icon">
<i class="fa fa-home"></i>
</div>
</div>
</div>

Related

How do I move text without moving everything else?

I would like to move the words "Social Media", above the two icons as a title. The problem I'm having is moving the title without moving the icons and the book button.
Could you please help?
.icons{
font-size: 50px;
color: blue;
padding-top: 250px;
padding-left: 110px;
}
.book {
padding-left: 43.5%;
}
.book button {
background-color: #F6B2A2;
border: none;
color: black;
text-decoration: none;
font-size: 16px;
height: 50px;
width: 100px;
}
.book a {
color: black;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet"/>
<div class="socialm">
<h2>Social Media</h2>
</div>
<div class="icons">
<i class="fab fa-facebook-square"></i>
<i class="fab fa-instagram"></i>
</div>
<div class="book">
<button>Book</button>
</div>
You can add a display: block and a display: inline-block for your icons in your css:
.socialm {
display: block;
}
.icons{
display: inline-block;
font-size: 50px;
color: blue;
padding-top: 250px;
padding-left: 110px;
}
.book {
display: block;
padding-left: 43.5%;
}
.book button {
background-color: #F6B2A2;
border: none;
color: black;
text-decoration: none;
font-size: 16px;
height: 50px;
width: 100px;
}
.book a {
color: black;
}
Add another div that wraps both the header tag and the icons. Add your padding to that div to keep the two pieces together and move them both at the same time.
.top{
padding-left: 43.5%;
}
.icons{
font-size: 50px;
color: blue;
padding-top: 250px;
}
.book {
padding-left: 43.5%;
}
.book button {
background-color: #F6B2A2;
border: none;
color: black;
text-decoration: none;
font-size: 16px;
height: 50px;
width: 100px;
}
.book a {
color: black;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<div class="top">
<div class="socialm">
<h2>Social Media</h2>
</div>
<div class="icons">
<i class="fab fa-facebook-square"></i>
<i class="fab fa-instagram"></i>
</div>
</div>
<div class="book">
<button>Book</button>
</div>

Show/Hide contents by clicking an icon using CSS (no javascript)

Is it possible to use something similar to toggle on/off or show/hide but rather than using panels or accordion menu, use icon-blocks instead?
There are 4 icons arranged in a row, clicking each icon should display the contents below the icon row, but only one content should be displayed at any given time.
I'm looking for CSS/HTML solution only - no javascript.
Icons are arranged in a row, using grid
<!--display icons in a row, 4 columns-->
<div class="quarter">
<span class="icon1"></span>
</div>
Below the 4th 'quarter' div is where the contents should be displayed.
Edit: Final Code
I replaced the position: absolute with float to display all the text below the icons.
.iconBlock{
display: inline-block;
margin: 20px;
padding: 0px 40px 15px 40px;
cursor: pointer;
outline: none !important;
}
.iconBlock a{
font-size: 40px;
color: #000;
}
.contentBlock {
display: none;
height: auto !important;
opacity: 0;
}
.iconBlock:hover a{
color: #444;
}
.iconBlock:focus a{
color: #ff4455;
}
.iconBlock:focus + .contentBlock{
opacity: 1;
display: block;
float: left;
top: 0;
left: 0;
}
I'd recommend using javascript. But here's how to do it without js:
.icon{
display: inline-block;
margin: 20px;
cursor: pointer;
outline: none;
}
.icon i{
font-size: 40px;
color: #000;
}
.content{
position: absolute;
opacity: 0;
}
.icon:hover i{
color: #444;
}
.icon:focus i{
color: #ff4455;
}
.icon:focus + .content{
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="quarter">
<i class="fa fa-cog"></i>
<div class="content">Content for icon 1</div>
<i class="fa fa-trash"></i>
<div class="content">Content for icon 2</div>
<i class="fa fa-pencil"></i>
<div class="content">Content for icon 3</div>
<i class="fa fa-user"></i>
<div class="content">Content for icon 4</div>
</div>

Image not centering in bootstrap

Having tried the solutions suggested in the other posts, I'm left to post my novice code in which the embedded image simply will not centre and I haven't a clue why. The image should appear in the middle of a login screen/box; it did for the guy who did the demo, yet I've had to alter his CSS to make it marginally better. Still, the image will not centre. Does anyone have any insight?
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Standard Meta Tags -->
<!-- Bootstrap & Related Links-->
<link rel="stylesheet" href="static/styles.css">
<title>Project</title>
</head>
<body>
<!-- cont is the entire box; changing its CSS changes its position on the page -->
<div class="cont">
<!-- box is the area that contains the image, username, and pass fields, but not the button bar and login buttons -->
<div class="box">
<!-- Creates the entire top row with close button and three circular buttons
<div class="row top">
<div class="left">
<i class="fa fa-times close"></i>
</div>
<div class="right">
<i class=" fa fa-circle but one"></i>
<i class=" fa fa-circle but two"></i>
<i class=" fa fa-circle but three"></i>
</div>
</div>
-->
<div class="row middle sg">
<div class="row pic sg">
<div class="col-md-4 col-md-offset-4">
<img src="static/fleur.jpeg" alt="fleur-de-lis" class="photo">
</div>
</div>
<form action="#" class="form-horizontal form">
<div class="input-group y">
<span class="input-group-addon"><i class="fa fa-user use"></i></span>
<input type="text" class="form-control" placeholder="Username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa faunlock-alt use"></i></span>
<input type="password" class="form-control" placeholder="Password">
</div>
</form>
</div>
<div class="row base sg">
<h2 class="text-center login">Login</h2>
</div>
</div>
</div>
</body>
</html>
CSS
html, body {
width: 100%;
height: 100%;
}
.cont {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: #2B2B35;
width: 400px;
border-radius: 10px;
}
.top {
width: 100%;
background-color: #24242E;
margin: 0;
border-radius: 10px 10px 0 0;
padding: 0 15px;
}
.left {
float: left;
}
.right {
float: right;
}
.close {
padding: 18px 0;
font-size: 20px;
color: #fff;
}
.but {
padding: 18px 0 18px 5px;
font-size: 20px;
}
.but:hover {
cursor: pointer;
}
.one {
color: #F4CB61;
}
.two {
color: #DB5594;
}
.three {
color: #6451E8;
}
.photo {
width: 200px;
border-radius: 50%;
}
.sg {
display: block;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: center;
}
.pic {
margin: 40px 0 30px 0;
}
.form {
padding: 0 40px 40px 40px;
}
.login {
padding: 12px 0;
margin: 0;
}
.base {
background-color: #3FA752;
border-radius: 0 0 10px 10px;
color: #fff;
}
.base:hover {
cursor: pointer;
}
.base h2{
font-weight: 600;
font-size: 26px;
}
.user {
color: #ccc;
}
.y {
padding-bottom: 15px;
}
input[type=text],
input[type=password] {
background: 0, 0;
border: 0;
box-shadow: none;
border-radius: 0;
border-bottom: 1px solid #9446B6;
}
.input-group-addon {
background: 0 0;
border: 0;
border-radius: 0;
border-bottom: 1px solid #9AA6B6;
}
.use {
color: #9AA6B6;
}
input[type=text],
input[type=password]:focus{
box-shadow: none !important;
color: #FF3F3F;
}
Updated:
Removing these classes class="col-md-4 col-md-offset-4" and adding a class to center the image fixes the problem.
html, body {
width: 100%;
height: 100%;
}
.cont {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: #2B2B35;
width: 400px;
border-radius: 10px;
}
.top {
width: 100%;
background-color: #24242E;
margin: 0;
border-radius: 10px 10px 0 0;
padding: 0 15px;
}
.left {
float: left;
}
.right {
float: right;
}
.close {
padding: 18px 0;
font-size: 20px;
color: #fff;
}
.but {
padding: 18px 0 18px 5px;
font-size: 20px;
}
.but:hover {
cursor: pointer;
}
.one {
color: #F4CB61;
}
.two {
color: #DB5594;
}
.three {
color: #6451E8;
}
.text-center {
text-align: center;
}
.photo {
width: 200px;
border-radius: 50%;
}
.sg {
display: block;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: center;
}
.pic {
margin: 40px 0 30px 0;
}
.form {
padding: 0 40px 40px 40px;
}
.login {
padding: 12px 0;
margin: 0;
}
.base {
background-color: #3FA752;
border-radius: 0 0 10px 10px;
color: #fff;
}
.base:hover {
cursor: pointer;
}
.base h2{
font-weight: 600;
font-size: 26px;
}
.user {
color: #ccc;
}
.y {
padding-bottom: 15px;
}
input[type=text],
input[type=password] {
background: 0, 0;
border: 0;
box-shadow: none;
border-radius: 0;
border-bottom: 1px solid #9446B6;
}
.input-group-addon {
background: 0 0;
border: 0;
border-radius: 0;
border-bottom: 1px solid #9AA6B6;
}
.use {
color: #9AA6B6;
}
input[type=text],
input[type=password]:focus{
box-shadow: none !important;
color: #FF3F3F;
}
<html lang="en">
<head>
<!-- Standard Meta Tags -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Bootstrap & Related Links-->
<link rel="stylesheet" href="static/styles.css">
<title>Project</title>
</head>
<body>
<!-- cont is the entire box; changing its CSS changes its position on the page -->
<div class="cont">
<!-- box is the area that contains the image, username, and pass fields, but not the button bar and login buttons -->
<div class="box">
<!-- Creates the entire top row with close button and three circular buttons
<div class="row top">
<div class="left">
<i class="fa fa-times close"></i>
</div>
<div class="right">
<i class=" fa fa-circle but one"></i>
<i class=" fa fa-circle but two"></i>
<i class=" fa fa-circle but three"></i>
</div>
</div>
-->
<div class="row middle sg">
<div class="row pic sg">
<div class="text-center">
<img src="https://placeimg.com/200/200/nature" alt="fleur-de-lis" class="photo">
</div>
</div>
<form action="#" class="form-horizontal form">
<div class="input-group y">
<span class="input-group-addon"><i class="fa fa-user use"></i></span>
<input type="text" class="form-control" placeholder="Username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa faunlock-alt use"></i></span>
<input type="password" class="form-control" placeholder="Password">
</div>
</form>
</div>
<div class="row base sg">
<h2 class="text-center login">Login</h2>
</div>
</div>
</div>
</body>
</html>
By default images are inline-block elements, they follow the regular flow and should be centered with text-align (set in the parent).
Images can be set to display as block elements, to center these the value of width must be set (amount, percent,...), and both left and right margins set to auto.
.container {
width: 100%;
}
.tex-center {
text-align: center;
}
#inline-example {
display: inline-block; /* Default value*/
}
#block-example {
display: block;
max-width: 200px;
margin: 0 auto 0 auto;
}
<h3>Inline centered image</h3>
<div class="container tex-center"><img id="inline-example" src="https://placeimg.com/200/200/nature"></div>
<h3>Block centered image</h3>
<div class="container"><img id="block-example" src="https://placeimg.com/200/200/people"></div>
How to find out similar CSS issues. Using Chrome's devtools (or similar) we can see there's a margin in the left pushing the image. col-md-4 col-md-offset-4 are both Bootsrap classes used for a grid setup, trying and removing them gives us the solution.
Try this:
.photo {
width: 200px;
border-radius: 50%;
display: block;
margin: auto;
}
To center an image in a div, you should normally set the display to block and margin to auto on the img.
If you are talking about this image <img src="static/fleur.jpeg" alt="fleur-de-lis" class="photo"> then you should add to your 'photo' class:
display:block;
margin: 0 auto;
Make sure other CSS wont prevent those lines to work and your image should centre itself within your col-element.
Your image is in column with offset, remove column classes (event whole div) and all will work well with:
.photo {
display: block; // defining element as block
margin: 0 auto; // and auto side margin
width: 200px;
border-radius: 50%;
}
Example:
https://codepen.io/themeler/pen/QqNpWv

Resizable button with an image

I want to create a button like this one :
but bigger, resizable and to function properly on bootstrap.
Here's what I've done so far:
I've tried with display: inline-block; on download-btn-icon and download-btn-text then I wanted to center the .fa using http://www.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/ but the .fa displays way out of download-btn.
This is the html:
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
<div class="download-btn">
<div class="download-btn-icon">
<i class="fa fa-cloud-download fa-3x"></i>
</div>
<div class="download-btn-text">
<h3>Download Client</h3>
<h5>Get the latest full updated client.</h5>
</div>
</div>
</div>
and this is the less:
.download-btn {
width: 100%;
height: 70px;
background-color: #000;
.download-btn-icon {
display: inline-block;
float: left;
width: 25%;
height: 100%;
border-right: 1px solid #fff;
.fa{
padding-top: 14px;
padding-left: 23px;
}
}
.download-btn-text {
display: inline-block;
width: 75%;
float: right;
padding-left: 15px;
}
}
I'm clueless I don't know how to align the divs properly, I need some advice.
You can use display: table-cell and vertical-align: middle to achieve this effect. This solves the alignment issue and forces both elements to have the same height.
h3, h5 {
margin: 0;
line-height: 1.5em;
}
.download-btn-icon,
.download-btn-text {
display: table-cell;
background: #222;
vertical-align: middle;
padding: 7.5px 15px;
color: #999;
}
.download-btn-icon {
border-right: 1px solid #444;
}
<div class="download-btn">
<div class="download-btn-icon">
<i class="fa fa-cloud-download fa-3x"></i>
</div>
<div class="download-btn-text">
<h3>Download Client</h3>
<h5>Get the latest full updated client.</h5>
</div>
</div>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

HTML, CSS: Append inline-element to the right of a centered inline-element

(The title might sound stupid but I don't know how to put it more sophisticated.)
As you can see in the follwing screenshots, I have 3 stacked divs.
<div class="col-xs-3">
<div class="center-horizontal-inline">
<a class="upvote" href="#" data-id="iZheLNnewWtMhPTQd">
</div>
<div class="score">
115
<span class="badge score-diff vertical-align"> +5 </span>
</div>
<div class="center-horizontal-inline">
</div>
The one in the middle contains the number (115). The other two the vote-arrows. For the one containing the number, I want to add a "badge" (in bootstrap context) right next to the number, let's say to the right. You can see my attempt shining through the colored overlay.
The goal is to leave the number centered respectively to the arrow-icons, while placing the badge with an offset (or "right next to") respectively to the number.
My attempt was to set a float: right; for the badge, but as you can see, the number has an offset now. When I try position: absolute; on the badge, there is no offset (as wanted), but I can't get the badge right next to the number, I can only attach it to the right border because I don't have the number as positioning-reference anymore.
Hope my explanation was clear enough. I also didn't know how to search for that problem...
EDIT 1:
As I want it to look like (positioning-wise):
Here we go, using relative and absolute positions together:
DEMO: http://jsfiddle.net/1qbo7uwn/
HTML
<div class="score">
<span class="score-wrap">
<span class="score-main">123</span>
<span class="score-diff">8</span>
</span>
</div>
CSS
.score {
width: 80px;
border: 2px solid blue;
padding: 10px 0;
text-align: center;
}
.score-wrap {
display: inline-block;
position: relative;
}
.score-main {
border: 2px solid green;
}
.score-diff {
position: absolute;
border: 2px solid red;
left: 100%;
top: -2px;
}
Added some span tags in the HTML.
EDIT: vertical align of everything, by setting line height.
http://jsfiddle.net/1qbo7uwn/1/
As you said you are okay with HTML modification. Here's my attempt:
Demo Fiddle
HTML
<div class="wrap">
<div class="vote up">
<i class="fa fa-angle-up"></i>
</div>
<div class="stat">
<span class="score">115
<span class="badge">+5</span>
</span>
</div>
<div class="vote inactive">
<i class="fa fa-angle-down"></i>
</div>
</div>
CSS
.wrap{
margin: 100px;
text-align:center;
}
.vote{
font-size:36px;
}
.up{
color:green;
}
.down{
color:red;
}
.inactive{
color:gray;
}
.score{
position: relative;
display:block;
padding: 5px 0;
}
.badge{
background: #eee;
padding:5px 10px;
border-radius: 30px;
position: absolute;
top:0;
margin-left:10px;
}
Try
Demo: http://jsfiddle.net/tamilcselvan/rjv1xxo2/1/
.center-horizontal-inline {
font-size: 2em;
text-align: center;
}
.score {
text-align: center;
}
.score:after {
content:attr(data-badge);
position: absolute;
margin-left: .4em;
font-size: x-small;
min-width: 10px;
padding: 3px 7px;
font-size: 12px;
font-weight: 700;
line-height: 1;
color: #FFF;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
background-color: #777;
border-radius: 10px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-3">
<div class="center-horizontal-inline vote"> <i class="glyphicon glyphicon-chevron-up"></i>
</div>
<div class="score" data-badge="+5">115</div>
<div class="center-horizontal-inline"> <i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>