Center element horizontally css [duplicate] - html

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed last year.
I have a basic type-writing element and I am having trouble centering it within a div. I have tried messing around with the margins, the position, the text alignment, all that. I honestly don't know what I'm doing so any help on this would be greatly appreciated.
This is my first time making a website so bear with me, I know this is probably a frequently asked question. I just couldn't find the answer on my own.
html {
background: #545972;
font-family: 'Roboto', sans-serif;
}
.nav {
float: left;
width: 15%;
height: 100vh;
}
.home {
float: left;
width: 85%;
height: 100vh;
}
section::after {
content: '';
display: table;
clear: both;
}
.nav nav h1 {
font-size: 40px;
margin-left: 30px;
position: relative;
}
.nav nav h1 a {
color: #fff;
text-decoration: none;
}
.nav nav ul {
margin-top: 60px;
line-height: 50px;
margin-left: 60px;
}
.nav nav ul li a {
text-decoration: none;
color: #fff;
font-weight: 700;
padding: 10px;
transition: color 0.5s;
}
.nav nav ul li a:hover {
color: rgb(206, 203, 203);
}
.nav nav ul li i {
color: rgb(27, 27, 31);
}
.vl {
border-left: 1px solid rgba(255, 255, 255, 0.25);
height: 100%;
position: absolute;
margin-left: 15%;
top: 0;
}
.home img {
position: absolute;
width: 128px;
height: 128px;
margin-left: 50%;
}
.home .typewriting {
margin: 0 auto;
}
.home .typewriting h1 a {
text-decoration: none;
color: #fff;
}
<section>
<div class="nav">
<nav>
<h1>Ryan</h1>
<ul class="fa-ul">
<li> <i class="fa-li fa fa-home" aria-hidden="true"></i> Home </li>
<li> <i class="fa-li fa fa-user" aria-hidden="true"></i> About </li>
<li> <i class="fa-li fa fa-briefcase" aria-hidden="true"></i> Services </li>
<li> <i class="fa-li fa fa-graduation-cap" aria-hidden="true"></i> Experience </li>
<li> <i class="fa-li fa fa-code" aria-hidden="true"></i> Projects </li>
<li> <i class="fa-li fa fa-comments" aria-hidden="true"></i> Contact </li>
</ul>
<div class="vl"></div>
</nav>
</div>
<div class="home">
<img src="images/logo.png">
<div class="typewriting">
<h1>
<a href="" class="typewrite" data-period="2000" data-type='[ "Private Ryan" ]'>
<span class="wrap"></span>
</a>
</h1>
</div>
</div>
</section>

Add text-align: center on h1 element. Worked for me.

I believe that you are looking for the css property text-align. This property can position text center, left, right, or justify. For more information click here.

Related

the navigation bar isn't included or aligned with my header for some reason

I'm pretty new to using css and html. I don't know why the nav part doesn't have the same background and it's not at the same height as the logo.
I'd really appreciate the help!
body{
font-family: sans-serif;
}
header{
background-color: #14597f;
height: 60px;
}
nav ul{
list-style: none;
text-align: right;
}
nav li{
display: inline-block;
}
nav a{
color: #ffff;
display: block;
text-transform: uppercase;
font-weight: bold;
padding: 10px 10px;
}
nav a:hover{
background-color: #ca0ed1 ;
}
.active{
background-color: #1fb5e9;
border-radius: 4px;
}
<header>
<a href="./index.html" >
<img class="logo" src="assets/img/logo.svg" alt="logo" width="200px">
</a>
<nav>
<ul>
<li><a class="active" href="index.html"> Accueil</a></li>
<li> Produits</li>
<li> Contact</li>
<li><a class="shopping-cart" href="./shopping-cart.html" title="Panier">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x fa-inverse"></i>
<i class="fa fa-shopping-cart fa-stack-1x"></i>
</span>
<span class="count">3</span></a>
</li>
</ul>
</nav>
</header>
What I've done is following:
header {
display: flex;
justify-content: space-between;
align-items:center;
}
and removed display: block; from nav a
Hopefully this makes sense and it doesn't need explanation. If yes, tell me, I will explain it to you.
This way, it's fixed. Full code:
body{
font-family: sans-serif;
}
header{
background-color: #14597f;
height: 60px;
}
header {
display: flex;
justify-content: space-between;
align-items:center;
}
nav ul{
list-style: none;
text-align: right;
}
nav li{
display: inline-block;
}
nav a{
color: #ffff;
text-transform: uppercase;
font-weight: bold;
padding: 10px 10px;
}
nav a:hover{
background-color: #ca0ed1 ;
}
.active{
background-color: #1fb5e9;
border-radius: 4px;
}
<header>
<a href="./index.html" >
<img class="logo" src="assets/img/logo.svg" alt="logo" width="200px">
</a>
<nav>
<ul>
<li><a class="active" href="index.html"> Accueil</a></li>
<li> Produits</li>
<li> Contact</li>
<li><a class="shopping-cart" href="./shopping-cart.html" title="Panier">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x fa-inverse"></i>
<i class="fa fa-shopping-cart fa-stack-1x"></i>
</span>
<span class="count">3</span></a>
</li>
</ul>
</nav>
</header>
you can use flex with space between and align item center.
* {
border: 1px solid red;
}
body{
font-family: sans-serif;
}
header{
background-color: #14597f;
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
}
nav ul{
list-style: none;
text-align: right;
}
nav li{
display: inline-block;
}
nav a{
color: #ffff;
display: block;
text-transform: uppercase;
font-weight: bold;
padding: 10px 10px;
}
nav a:hover{
background-color: #ca0ed1 ;
}
.active{
background-color: #1fb5e9;
border-radius: 4px;
}
<header>
<a href="./index.html" >
<img class="logo" src="assets/img/logo.svg" alt="logo" width="200px">
</a>
<nav>
<ul>
<li><a class="active" href="index.html"> Accueil</a></li>
<li> Produits</li>
<li> Contact</li>
<li><a class="shopping-cart" href="./shopping-cart.html" title="Panier">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x fa-inverse"></i>
<i class="fa fa-shopping-cart fa-stack-1x"></i>
</span>
<span class="count">3</span></a>
</li>
</ul>
</nav>
</header>

Element changes width (?) after hover

So I making a menu for a website, whenever the website loads the menu looks like img#1, the icons on the side are stacked, which I don't want to.
img1
After you hover over the icons they stack properly img#2.
after hovering
Which is how I actually want it.
I am sure it's just something small but I can't figure it out?
nav {
height: 40px;
background-color: var(--main-bg-color);
width: 100%;
margin: 2% 0;
}
.menu-icon {
display: none;
}
.main-nav {
margin-right:auto;
padding-left:4%;
width:fit-content;
}
nav li {
display: inline;
padding-right:5%;
}
nav a {
text-transform: uppercase;
letter-spacing: 1.5px;
text-decoration: none;
color: var(--light-color);
font-family: var(--font-h);
}
.icon-nav{
margin-left:auto;
padding-right:4%;
}
.icon-nav a{
padding: 0 5%;
}
html
<nav>
<div class="menu-icon">
<i class="fa fa-bars" aria-hidden="true"></i>
</div>
<div class="main-nav">
<ul>
<li>
Home
</li>
<li>
About me
</li>
<li>
Skills
</li>
<li>
Portfolio
</li>
<li>
Contact
</li>
</ul>
</div>
<div class="icon-nav">
<a href="" target="_blank">
<i class="fa fa-github" aria-hidden="true"></i>
</a>
<a href="" target="_blank">
<i class="fa fa-twitter" aria-hidden="true"></i>
</a>
<a href="" target="_blank">
<i class="fa fa-linkedin" aria-hidden="true"></i>
</a>
</div>
</nav>
Your problem is likely related to a problem in your layout of .icon-nav and it's padding.
Some ways you can fix this is by:
Adding :not(:last-child) in the .icon-nav a CSS selector making it:
.icon-nav a:not(:last-child) {
padding: 0 5%;
}
OR
Adding:
First, adding width: 15%; to .icon-nav.
Second, sdding width: 75%; to .main-nav.
and adding a .icon-nav::before { content: ""; width: 15%; display: inline-block;}
/******* EXTRA *******/
#screen{
resize:horizontal;
overflow:hidden;
background-image: url('data:image/svg+xml;utf8, <svg width="20" height="20" xmlns="http://www.w3.org/2000/svg" version="1.1" ><rect x="0" y="0" width="10" height="10" fill="lightgrey"/><rect x="10" y="10" width="10" height="10" fill="lightgrey"/></svg>');
}
/*********************/
/********** Things forgotten **********/
html {
--main-bg-color: black;
--light-color: red;
--font-h: /*monospace*/
;
}
ul {
display: inline;
list-style-type: none;
}
#screen div {
display: inline-block;
height: 1em;
}
#screen div * {
border-width: 0;
}
/**************************************/
nav {
height: 40px;
background-color: var(--main-bg-color);
width: 100%;
margin: 2% 0;
}
.menu-icon {
display: none;
}
.main-nav {
margin-right: auto;
padding-left: 4%;
width: fit-content;
/* ADDED { */ width: 70%; /* } */
}
nav li {
display: inline;
padding-right: 5%;
}
nav a {
text-transform: uppercase;
letter-spacing: 1.5px;
text-decoration: none;
color: var(--light-color);
font-family: var(--font-h);
}
/*= ADDED =======================*/
.icon-nav::before {
content: "";
width: 15%;
display: inline-block;
}
/*===============================*/
.icon-nav {
margin-left: auto;
padding-right: 4%;
/* ADDED {*/ width: 15%; /* } */
}
.icon-nav a {
padding: 0 5%;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id="screen">
<nav>
<div class="menu-icon">
<i class="fa fa-bars" aria-hidden="true"></i>
</div>
<div class="main-nav">
<ul>
<li>
Home
</li>
<li>
About me
</li>
<li>
Skills
</li>
<li>
Portfolio
</li>
<li>
Contact
</li>
</ul>
</div>
<div class="icon-nav">
<a href="" target="_blank">
<i class="fa" aria-hidden="true"></i>
</a>
<a href="" target="_blank">
<i class="fa" aria-hidden="true"></i>
</a>
<a href="" target="_blank">
<i class="fa" aria-hidden="true"></i>
</a>
</div>
</nav>
</div>
</body>
</html>

Drawing a triangle over a div in CSS [duplicate]

This question already has an answer here:
How can I draw a left pointing triangle using CSS?
(1 answer)
Closed 6 years ago.
I'm actually trying to implement a design done for me into HTML5 and CSS3 with Twitter Bootstrap.
I've got a sidebar on the left of the screen with a list inside, nothing complex.
<html>
<section class="sidebar">
<ul class="sidebar-menu">
<li class="active">
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
</ul>
</section>
</html>
I would like to arrive to this result :
Any idea on the CSS code to manage to have that triangle on the right side centered in the middle bottom right ?
You can achieve that triangle with a pseudo element ::after, some positioning and transforming:
li {
position: relative;
display: block;
background: #04a4a9;
padding: 1em;
overflow: hidden;
}
li a {
color: white;
text-decoration: none;
}
li::after {
content: '';
position: absolute;
width: 15px;
height: 15px;
background: white;
right: -8px;
top: 50%;
transform: translate(0, -50%) rotate(45deg);
}
<section class="sidebar">
<ul class="sidebar-menu">
<li class="active">
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
</ul>
</section>
You can achieve this using CSS pseudo elements:
To avoid wasting time with the triangle itself, just use a generator.
.btn {
display: flex;
align-items: center;
align-content: center;
justify-content: center;
background: #53BED1;
color: #fff;
width: 400px;
height: 150px;
position: relative;
}
.btn::before {
right: 0;
top: 40%;
position: absolute;
content: '';
width: 0;
height: 0;
border-style: solid;
border-width: 15px 20px 15px 0;
border-color: transparent #ffffff transparent transparent;
}
<div class="btn">My users</div>
You may indeed use a pseudo.
You can also draw background-color of li from that pseudo via a box-shadow, so the triangle is translucide.
You can use color on li to easily change bg-colors since you reset color on <a>, box-shadow color inherits color value(currentcolor) if none set in the rule (so does border-color)..
li {
position: relative;
display: inline-block;
padding: 1em;
overflow: hidden;
color:tomato;
text-shadow:1px 1px black;
}
li:nth-child(even) {
color:turquoise
}
li.active {
color: #04a4a9;
}
li a {
color: white;
text-decoration: none;
position: relative;
z-index: 1;
}
li::after {
content: '';
position: absolute;
width:0px;
height: 15px;
margin: -8px;
right: 0;
top: 50%;
transform: rotate(45deg);
box-shadow: 0 0 0 400px;
}
li.active::after {
width:15px;;
}
/*demo to check if you can see through */
body {
background:linear-gradient(45deg,gray,yellow,pink,blue,gray,yellow,pink,blue,gray,yellow,pink,blue,gray,yellow,pink,blue);
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<section class="sidebar">
<ul class="sidebar-menu">
<li>
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
<li class="active">
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
<li>
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
<li>
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
<li>
<a href="/home">
<i class="fa fa-users active-fa"></i>
<span class="menu-title">MY USERS</span>
</a>
</li>
</ul>
</section>

div breaking the Navigation Menu completely

I have a navigation menu which works perfectly fine on large screen and mobile screen but as soon as i add a div above it, It breaks the navigation menu on large screen and mobile screen. See the fiddle here - https://jsfiddle.net/AwesomeHat/t9q2p2ut/ .
Navigation Menu when alone works perfectly fine see the fiddle here - https://jsfiddle.net/AwesomeHat/dwzhh6L1/.
Please increase & decrease the size of output window to see the menu's responsiveness and see how it is breaking the menu on both screens.
<!--Social Icons-->
<div id="social">
<a href="#" class="icon-button wikipedia"><i class="fa fa-wikipedia-w" aria-hidden="true"></i>
<a href="#" class="icon-button linkedin"><i class="fa fa-linkedin" aria-hidden="true"></i>
<a href="#" class="icon-button google-plus"><i class="fa fa-google-plus" aria-hidden="true"></i>
<a href="#" class="icon-button twitter"><i class="fa fa-twitter" aria-hidden="true"></i>
<a href="#" class="icon-button facebook"><i class="fa fa-facebook" aria-hidden="true"></i>
</div>
<!--Navigation Bar-->
<nav>
<label for="show-menu" class="show-menu">Menu ☰</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li>Home</li>
<li>About us</li>
<li>Whats New
<ul class="hidden">
<li>Just Launched</li>
<li>Launching Soon</li>
<li>Completed Projects</li>
</ul>
</li>
<li>Referral</li>
<li>Buyers Section
<ul class="hidden">
<li>EMI Calculator</li>
<li>Apply For Loan</li>
<li>Make an Enquiry</li>
</ul>
</li>
<li>Careers</li>
<li>Contact</li>
</ul>
</nav>
CSS Code -
/* logo */
.logo {
float: left;
margin-left: 55px;
margin-top: 30px;
}
/* Social Icons */
.icon-button {
display: inline-block;
color: white;
border: 0px;
font-size: 1.0rem;
line-height: 1.7rem;
margin: 1px;
text-align: center;
width: 1.7rem;
margin-top: 60px;
float: right;
overflow: hidden;
}
.facebook {
background-color: #3B5998;
}
.twitter {
background-color: #4099ff;
}
.google-plus {
background-color: #db5a3c;
}
.linkedin {
background-color: #007fb1;
}
.wikipedia {
background-color: white;
overflow: hidden;
color: black;
margin-right: 100px;
}
.icon-button:hover {
background-color: rgba(165,219,89,1);
transition: 1s;
transform: rotate(360deg);
}
/* Navigation Menu */
nav ul {
list-style-type:none;
margin-top: 170px;
padding:0;
position: absolute;
width: 100%;
z-index: 2;
}
nav ul li {
display:inline-block;
float: left;
width: 14.2857%; /* fallback for non-calc() browsers */
width: calc(100% / 7);
}
nav ul li a {
display:block;
min-width:140px;
height: 40px;
text-align: center;
line-height: 40px;
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
color: #fff;
background: #161616;
text-decoration: none;
}
nav ul li:hover a {
color: rgb(165,219,89);
}
nav ul li:hover ul a {
color: #fff;
height: 40px;
line-height: 40px;
}
nav ul li:hover ul a:hover {
color: rgb(165,219,89);
}
nav ul li ul {
margin-top: 0px;
display: none;
}
nav ul li ul li {
display: block;
float: none;
width: 200px;
}
nav ul li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
nav ul li a:hover + .hidden, .hidden:hover {
display: block;
}
.show-menu {
width: 25%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #fff;
background: #161616;
text-align: center;
padding: 10px 0;
display: none;
}
input[type=checkbox]{
display: none;
}
input[type=checkbox]:checked ~ #menu{
display: block;
}
#media screen and (max-width : 760px){
nav ul {
position: relative;
margin-top: 0px;
display: none;
}
nav ul li, li a {
width: 90%;
}
nav ul li ul {
margin-top: 0px;
display: block;
}
nav ul li ul li {
width: 90%;
}
.show-menu {
display:block;
}
}
Syntax error, close anchor tags inside social div
<div id="social">
<i class="fa fa-wikipedia-w" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>
<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-facebook" aria-hidden="true"></i>
</div>
https://jsfiddle.net/Nagasai_Aytha/t9q2p2ut/2/
At 1st div your code will be like this:
<div id="social">
<i class="fa fa-wikipedia-w" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>
<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-facebook" aria-hidden="true"></i>
</div>
You just forget to finish anchorage tag </a>

Mobile slide push nav

I've made my nav and it works fine on desktop fully responsive, but when ever I try to emulate for mobile instead of pushing over the div elements it instead simply shrinks everything. you can see this in action
HTML (inserted via an include statement)
<header id="headers" class="nav-main "> <!-- animated fadeInDown-->
<div class="logo">
<img src="img/logo.png" alt="modren-day-thrones-logo"/>
</div>
<ul>
<li>
Home
</li>
<li>
Chairs
<div class="nav-content">
<div class="nav-sub">
<ul>
<li>
Executive
</li>
<li>
Office
</li>
<li>
Lounge
</li>
</ul>
</div>
</div>
</li>
<li>
About Us
</li>
<?php
if(isset($_SESSION['logged_in'])){ ?>
<li>
Add
</li>
<li>
Log Out
</li>
<?php
}else{ ?>
<li>
Login
</li>
<?php
}
?>
</ul>
</header>
<div class="main-header-mobile-box">
<header class="mobile pushmenu-push">
<nav>
<div class="innerbutton">
<div class="catagoerys">
<!--<div id="cpBtn" class="nav-toggel">-->
<div class="menuebutton group">
<i class="fa fa-bars" id="nav_list"></i>
</div>
</header>
</div>
<body class="pushmenu-push">
<nav class="pushmenu pushmenu-left">
<h3>Menu</h3>
<i class="fa fa-home"></i> Home
<i class="fa fa-folder-open"></i>Chairs
<i class="fa fa-folder"></i> Executive
<i class="fa fa-folder"></i> Office
<i class="fa fa-folder"></i> Lounge
<i class="fa fa-fw fa-list"></i> About Us
<i class="fa fa-sign-in"></i> Login
<?php
if(isset($_SESSION['logged_in'])){ ?>
<i class="fa fa-sign-out"></i> Log Out
<i class="fa fa-plus"></i>Add
<?php
}
?>
<hr>
</nav>
<div class="container">
<div class="main">
<section class="buttonset">
<div id="nav_list">Menu</div>
</section>
end html
body{
text-align:center;
font:1em "Open Sans", sans-serif;
width:70%;
min-width: 349px;
max-width: 1490px;
margin:0 auto;
overflow-x: hidden;
display: block;
}
.pushmenu { /*this is the nav*/
background: #3c3933;
font-family: Arial, Helvetics, sans-serif;
width: 240px;
height: 100%;
top: 0;
z-index: 1000;
position:fixed;
}
.right-login-mobile-nav{
float: right;
display: inline-block;
}
.break-bar{
width:100%;
display: block;
}
.pushmenu h3 {
color: #cbbfad;
font-size: 14px;
font-weight: bold;
padding: 15px 20px;
margin: 0;
background: #282522;
height: 16px;
}
.buttonset{
display: none;
}
.pushmenu a {
display: block; /* drops the nav vertically*/
color: #fff;
font-size: 16px;
font-weight: bold;
text-decoration: none;
border-top: 1px solid #56544e;
border-bottom: 1px solid #312e2a;
padding: 14px;
}
.pushmenu a:hover {
background:#333;
}
.pushmenu a:active {
background: #454f5c;
color: #fff;
}
.pushmenu-left {
left: -240px;
}
.pushmenu-left.pushmenu-open {
left: 0;
}
.pushmenu-push {
overflow-x: hidden;
position: relative;
left: 0;
}
.pushmenu-push-toright {
left: 240px;
}
/*Transition*/
.pushmenu, .pushmenu-push {
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
transition: all 0.3s ease;
}
#nav_list {
background: url(http://www.onlywebpro.com/demo/jquery/icon_nav.png) no-repeat left top;
cursor: pointer;
height: 27px;
width: 33px;
text-indent: -99999em;
}
nav-list.active {
background-position: -33px top;
}
.buttonset {
background: #fff;
height: 16px;
padding: 10px 20px 20px;
}
Mobile specific effects
.mobilebtnmenue{
text-align: left;
margin-top: -22px;
margin-left: 35px;
}
.mobile{
position: relative;
z-index: 50;
background: #222;
font-family: 'Varela Round', sans-serif;
font-size: 17px;
display: block;
}
.pushmenu {
display: block;
}
header{
display:block;
height:100px;
}
body{
width:100%;
min-width: 375px;
}
apparently this code in the header fixes it?
however there seems to be a few hiccups here and there.
<meta name="viewport" content="width=device-width,initial-scale=1.0">