I'm building a top navigation bar. I'd like the logo & menu items on the right to be vertically centered. To achieve this, I'm looking to set the position of the container of both logo & menu items as relative, and then set the position of logo & menu items as absolute to achieve the effect.
This successfully worked for the logo. However, it's not working for menu items, in fact, all the menu items are gone once the code below is implemented. Not sure what's going on here.
<div class="boxA">
<div class="box1">
<div class="site">
<img src="img/logo.png">
</div>
</div>
<div class="box2">
<nav class="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
<li>menu4</li>
</ul>
</nav>
</div>
</div>
.boxA:after {
content:"";
display: block;
clear: both;
}
.boxA {
height: 100px;
position: relative;
border-bottom: 1px solid #E0DCDC;
}
.boxA img {
position: absolute;
top: 50%;
left: 3%;
transform: translate(0, -50%);
}
.box2 li {
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
.box1 {
float: left;
width: auto;
}
.box2 {
float: right;
width: auto;
}
.box2 ul {
margin: 0;
padding: 0;
list-style: none;
}
.box2 li a {
display: block;
padding: 0px;
color: inherit;
text-decoration: none;
font-size: 12px;
}
.box2 li a:hover {
text-decoration: underline;
}
.box2 ul:after {
content: "";
display: block;
clear: both;
}
.box2 li {
float: left;
width: auto;
}
.box1 img {
position: absolute;
height: 20px;
}
body{margin:0}
.boxA {
display: flex;
flex-direction: row;
background: #b6d6f3;
border-bottom: 2px solid #8ac8ff;
justify-content: space-between;
align-items: center;
padding: 20px 5px;
}
.site img {
width: 100px;
}
.menu ul {
margin: 0;
padding: 0;
list-style-type: none;
}
.menu ul li a {
text-decoration: none;
padding: 2px 5px;
display: inline-block;
}
<div class="boxA">
<div class="box1">
<div class="site">
<img src="https://www.smashingmagazine.com/images/smashing-cat/cat-with-slippers.svg">
</div>
</div>
<div class="box2">
<nav class="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
<li>menu4</li>
</ul>
</nav>
</div>
</div>
Try not to use css float property instead use css grid or css flex box.
EDIT 1 :
If you want to see the same result in IE without using flex property then.
.boxA:after {
content:"";
display: block;
clear: both;
}
.boxA {
height: 100px;
position: relative;
border-bottom: 1px solid #E0DCDC;
}
.boxA img {
position: absolute;
top: 50%;
left: 3%;
transform: translate(0, -50%);
}
.box2 ul {
position: absolute;
top: 50%;
transform: translate(0, -50%);
/* -- NEW -- */
right: 10px;
}
.box1 {
float: left;
/* -- NEW -- */
width: 100PX;
background-color: #cddef7;
height: 100px;
}
.box2 {
float: right;
/* -- NEW -- */
width: 50%;
background-color: #85c3f9;
height: 100px;
}
.box2 ul {
margin: 0;
padding: 0;
list-style: none;
}
.box2 li a {
display: block;
padding: 0px;
color: inherit;
text-decoration: none;
font-size: 12px;
/* --NEW-- */
padding:2px 0;
}
.box2 li a:hover {
text-decoration: underline;
}
.box2 ul:after {
content: "";
display: block;
clear: both;
}
/* DOES NOT REQUIRED--
.box2 li {
float: left;
width: auto;
}
*/
.box1 img {
position: absolute;
/* --NEW-- */
width:80px;
}
<div class="boxA">
<div class="box1">
<div class="site">
<img src="https://www.smashingmagazine.com/images/smashing-cat/cat-with-slippers.svg">
</div>
</div>
<div class="box2">
<nav class="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
<li>menu4</li>
</ul>
</nav>
</div>
</div>
Related
See below code. That i have tried but not getting box-shadow for that triangle shape.
As seen in above image i want to create a triangle shape with shadow using css.(For responsive view also).
I have also add extra div and try using rotate properties but not getting proper solution.
ul {
margin-bottom: 0px;
}
ul li {
list-style: none;
}
.main-header {
background-color: #F16322;
display: inline-block;
vertical-align: top;
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 9;
}
.main-header:before {
content: '';
width: 50%;
height: 190px;
background: #cccccc;
top: 0;
position: fixed;
z-index: 0;
clip-path: polygon(100% 0, 0 0, 0 100%);
}
.main-menu {
float: right;
display: inline-block;
}
.main-menu ul {
float: left;
}
.main-menu li {
float: left;
margin-right: 10px;
}
.main-menu li a {
display: inline-block;
padding: 34px 16px;
color: #ffffff;
font-size: 20px;
}
.main-menu>ul>li:last-child {
margin-right: 0px;
}
.main-menu>ul>li:last-child>a {
padding-right: 0px;
}
.main-menu li a:hover,
.main-menu li.current-menu-item>a {
color: #000000;
}
.header-logo {
float: left;
}
.header-wrap {
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
}
<header class="main-header">
<div class="container">
<div class="header-wrap">
<div class="header-logo">
<img src="images/logo.png" alt="BNPK logo">
</div>
<div class="main-menu">
<ul>
<li>
Features
</li>
<li>
Pricing
</li>
<li>
Contact
</li>
</ul>
</div>
</div>
</div>
</header>
As seen in above image i want to create a triangle shape with shadow using css.(For responsive view also).
I have also add extra div and try using rotate properties but not getting proper solution.
You can not add a box-shadow to an element you are using clip-path on, but i fixed it for you using a workaround ive found once.
<head>
<style>
ul {
margin-bottom: 0px;
}
ul li {
list-style: none;
}
.main-header {
background-color: #F16322;
display: inline-block;
vertical-align: top;
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 9;
}
.triangle {
filter: drop-shadow(3px 1px 1px #b0b0aa);
position: absolute;
width: 50%;
height: 190px;
}
.triangle::before {
position: absolute;
z-index: -1;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #cccccc;
color: #cccccc;
--p: polygon(100% 0, 0 0, 0 100%);
-webkit-clip-path: var(--p);
clip-path: var(--p);
content: '';
}
.main-menu {
float: right;
display: inline-block;
}
.main-menu ul {
float: left;
}
.main-menu li {
float: left;
margin-right: 10px;
}
.main-menu li a {
display: inline-block;
padding: 34px 16px;
color: #ffffff;
font-size: 20px;
}
.main-menu>ul>li:last-child {
margin-right: 0px;
}
</style>
</head>
<body>
<header class="main-header">
<div class="triangle">
</div>
<div class="main-menu">
<ul>
<li>
Features
</li>
<li>
Pricing
</li>
<li>
Contact
</li>
</ul>
</div>
</header>
</body>
</html>
As #Pitzas proposed, a filter would be the solution. Personally, I would prefer to use an SVG for the triangle shape and then apply the shadow to it. Filters are extremely powerful combined with SVGs.
For your shadow, I would use the feGaussianBlur filter:
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feGaussianBlur
Here you have an example using it:
http://xn--dahlstrm-t4a.net/svg/filters/arrow-with-dropshadow.svg
How to put the picture in the left side of my navbar?
I can't seem to understand how it works- the picture is going up and the navbar is outside the div.
body {
margin: 0;
}
#navbar {
background-color: gray;
width: 100%;
height: 160px;
}
#logo {
width: 50%;
height: 50%;
}
ul {
list-style: none;
background-color: red;
margin: 0;
padding: 0;
text-align: center;
overflow: hidden;
}
#navbar li {
display: inline;
}
#navbar li a {
display: inline-block;
padding: 10px;
font-size: 20px;
text-decoration: none;
}
<div class="container">
<div id="navbar"> <img class="logo" src="logo.gif">
<ul>
<li>Home</li>
<li>Game Info</li>
<li>Gameplay</li>
<li>Media</li>
</ul>
</div>
</div>
use this code
#navbar{
position: absolute;
right: 20px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
In the case you want to use flexbox, you may try this:
favorite body {
margin: 0;
}
#navbar {
background-color: gray;
width: 100%;
height: 160px;
/*****************/
display:flex;
justify-content:space-between;
align-items:center;
/*****************/
}
#logo {
width: 50%;
height: 50%;
}
ul {
list-style: none;
background-color: red;
margin: 0;
padding: 0;
text-align: center;
overflow: hidden;
}
#navbar li {
display: inline;
}
#navbar li a {
display: inline-block;
padding: 10px;
font-size: 20px;
text-decoration: none;
}
<div class="container">
<div id="navbar"> <img width="40" class="logo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/apple.png">
<ul>
<li>Home</li>
<li>Game Info</li>
<li>Gameplay</li>
<li>Media</li>
</ul>
</div>
</div>
1-Access class logo by '.'
2-apply positioning to class logo as i applied and set height and width of class
3- set width of also
heres my code
favorite body {
margin: 0;
}
#navbar {
background-color: gray;
width: 100%;
height: 160px;
/*****************/
display:flex;
justify-content:space-between;
align-items:center;
/*****************/
}
.logo {
position :relative;
top: 0px;
width: 10%;
height: 27%;
}
ul {
list-style: none;
background-color: red;
width: 90%;
margin: 0;
padding: 0;
text-align: center;
overflow: hidden;
}
#navbar li {
display: inline;
}
#navbar li a {
display: inline-block;
padding: 10px;
font-size: 20px;
text-decoration: none;
}
<div class="container">
<div id="navbar"> <img width="40" class="logo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/apple.png">
<ul>
<li>Home</li>
<li>Game Info</li>
<li>Gameplay</li>
<li>Media</li>
</ul>
</div>
</div>
down vote favorite body {
margin: 0;
}
#navbar {
background-color: gray;
width: 100%;
height: 160px;
}
#logo {
width: 50%;
height: 50%;
}
ul {
list-style: none;
background-color: red;
margin: 0;
padding: 0;
text-align: center;
overflow: hidden;
}
#navbar li {
display: inline;
}
#navbar li a {
display: inline-block;
padding: 10px;
font-size: 20px;
text-decoration: none;
}
<div class="container">
<div id="navbar"> <img class="logo" src="logo.gif">
<ul>
<li>Home</li>
<li>Game Info</li>
<li>Gameplay</li>
<li>Media</li>
</ul>
</div>
</div>
use this css
ul {
list-style: none;
background-color: red;
margin: 0;
padding: 0;
text-align: center;
overflow: hidden;
display: inline-block;
}
I want to get these two elements to align perfectly vertical side by side using a position value of relative. I'm having trouble as to understanding why these two elements don't want to align.
Can someone explain what I'm doing wrong? and if possible find a solution?
https://jsfiddle.net/kerberonix/qcq68gfg/
HTML
<html>
<body>
<footer>
<div class="test">
<p>Footer Text</p>
</div>
<ul class="social-links">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</footer>
</body>
CSS
footer {
position: absolute;
bottom: 0;
width: 100%;
height: 70px;
padding: 0 20px;
background-color: #262626;
}
.test {
position: relative;
display: inline-block;
top: 50%;
width: 50%;
transform: translateY(-50%);
}
footer p {
font-size: 100%;
color: #888;
}
.social-links {
position: relative;
display: inline-block;
top: 50%;
width: 50%;
transform: translateY(-50%);
}
.social-links li {
display: inline-block;
margin-right: 35px;
text-decoration: none;
}
.social-links li:last-child {
margin: 0;
}
I would remove all of the positioning on the text and social elements, and use display: flex; align-items: center; on the parent.
footer {
position: absolute;
bottom: 0;
height: 70px;
padding: 0 20px;
background-color: #262626;
display: flex;
align-items: center;
box-sizing: border-box;
left: 0; right: 0;
}
.test {
position: relative;
}
footer p {
font-size: 100%;
color: #888;
}
/*--------------------------------------------------------------------------------------------
SOCIAL LINKS
--------------------------------------------------------------------------------------------*/
.social-links {
position: relative;
color: white;
text-align: right;
padding: 0;
}
.test, .social-links {
flex: 1 0 0;
}
.social-links li {
display: inline-block;
margin-left: 35px;
text-decoration: none;
}
.social-links li:last-child {
margin: 0;
}
<html>
<body>
<footer>
<div class="test">
<p>Footer Text</p>
</div>
<ul class="social-links">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</footer>
</body>
</html>
Use position: absolute on the two elements and consider the additional 20px for widths and positions caused by the padding of the footer. Also don't forget to reset the padding for the ul. See the code in my snippet for details:
html,
body {
margin: 0;
}
footer {
box-sizing: border-box;
position: absolute;
bottom: 0;
width: 100%;
height: 70px;
padding: 0 20px;
background-color: #262626;
}
.test {
position: absolute;
display: inline-block;
top: 50%;
width: calc(50% - 20px);
transform: translateY(-50%);
}
footer p {
font-size: 100%;
color: #888;
}
.social-links {
position: absolute;
left: calc(50% - 20px);
top: 50%;
width: calc(50% - 20px);
transform: translateY(-50%);
text-align: right;
margin: 0;
padding: 0;
}
.social-links ul {}
.social-links li {
display: inline-block;
margin-right: 35px;
text-decoration: none;
}
.social-links li:last-child {
margin: 0;
}
<footer>
<div class="test">
<p>Footer Text</p>
</div>
<ul class="social-links">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</footer>
Now its Okay I think you get What you need just replace the code in ssocial links
footer {
position: absolute;
bottom: 0;
width: 100%;
height: 70px;
padding: 0 20px;
background-color: #262626;
}
.test {
position: relative;
display: inline-block;
top: 50%;
width: 50%;
transform: translateY(-50%);
}
footer p {
font-size: 100%;
color: #888;
}
/*--------------------------------------------------------------------------------------------
SOCIAL LINKS
--------------------------------------------------------------------------------------------*/
.social-links {
position: relative;
display: inline-block;
top: 50%;
width: 50%;
transform: translateY(-50%);
text-align: right;
}
.social-links li {
top 150px;
height:20px;
width: 250px;
position: relative;
left: calc(50% – 125px);
}
.social-links li:last-child {
margin: 0;
}
I have two questions:
Can I set the border-right's height?
Can I hide the last li's border-right?
body {
margin: 0;
padding: 0;
}
#banner {
height: 30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul {
list-style: none;
height: 30px;
padding: 0;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align: center;
border-right: 1px solid #aba;
}
<div id="banner">
<ul>
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
Instead of a border you can experiment with ::before and ::after
ul {
display: flex;
}
li {
position: relative;
display: flex;
justify-content: center;
flex: 1;
padding: 10px 0;
background-color: gray;
list-style-type: none;
}
li::after {
content: "";
position: absolute;
top: 5px; /* control the 'height' with top/bottom */
bottom: 5px;
right: 0;
width: 5px; /*same as what your border was */
background-color: black; /* instead of border-color */
}
li:last-child::after { /*hides last 'border' */
display: none;
}
<div>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</div>
Try #banner ul li:last-child { border-right: 0; or border-right-color: hsla(0, 0%, 0%, 0);}
fiddle
https://jsfiddle.net/Hastig/kkx33des/
Hide the last border
This is your border:
li { border-right: 1px solid #aba; }
But you don't want it to appear on the last item. So try this instead:
li + li {
border-left: 1px solid #aba;
}
The new rule applies a left-side border to li elements that come immediately after another li. This will exclude a left border on the first li and a right border on the last li.
Shorter borders
You want the borders to be less than full height. You can achieve the effect with absolutely-positioned pseudo-elements:
li {
position: relative; /* establish the containing block for abspos children */
}
li+li::before {
content: "";
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
height: 50%;
width: 2px;
background-color: red;
}
body {
margin: 0;
padding: 0;
}
#banner {
height: 30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul {
list-style: none;
height: 30px;
padding: 0;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align: center;
/* border-right: 1px solid #aba; */
position: relative;
}
li+li::before {
content: "";
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
height: 50%;
width: 2px;
background-color: red;
}
<div id="banner">
<ul>
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
Try This :
li:not(:last-child):after {
content: "";
display: inline-block;
margin-left: 40px;
border-right: 3px solid orange;
height: 20px;
top: 5px;
position: absolute;
}
Full Code :
body {
margin: 0;
padding:0;
}
#banner {
height:30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
vertical-align: middle;
}
#banner ul{
list-style: none;
height:30px;
padding: 0;
position: relative;
}
#banner ul li {
float: left;
margin: auto 0;
height: 30px;
line-height: 30px;
width: 100px;
text-align:center;
}
li:not(:last-child):after {
content: "";
display: inline-block;
margin-left: 40px;
border-right: 3px solid orange;
height: 20px;
top: 5px;
position: absolute;
}
<div id="banner">
<ul >
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
To remove the border for the last li, use the pseudo-class :last-child
I used display:flex and align-item:center after adding a margin to the top and bottom of the li to center it
body {
margin: 0;
padding:0;
}
#banner {
height:30px;
width: 750px;
margin: 0 auto;
background-color: aquamarine;
}
#banner ul{
list-style: none;
height:30px;
padding: 0;
display: flex;
align-items: center;
}
#banner ul li {
float: left;
width: 100px;
text-align:center;
border-right:1px solid #aba;
margin-top:10px;
margin-bottom: 10px;
}
#banner ul li:last-child {
border-right: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<link rel="stylesheet" href="styles/index.css" type="text/css">
</head>
<body>
<div id="banner">
<ul >
<li>Home</li>
<li>link</li>
<li>product</li>
<li>phone</li>
<li>cat</li>
<li>about</li>
</ul>
</div>
</body>
</html>
U can try pipe symbol as an alternaten using :after selector of each li
You need :last-child pseudo Css selector to hide the border of last li
I have the following: http://jsfiddle.net/5mbK8/2/
The text Link is supposed to function as a link, but it doesn't work because .container .image has a z-index of -1. If I remove the z-index, it works, but then the image isn't behind the yellow shadow.
How can I make the text clickable while keeping the background image behind the yellow box shadow?
HTML:
<div class="container">
<div class="side"></div>
<div class="image" style="background: url('http://i.imgur.com/T0VWLqZ.png');">
<div class="menu">
<ul>
<li><a href="#">Link</li>
</ul>
</div>
</div>
</div>
CSS:
a {
color: black;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.container {
width: 900px;
height: 300px;
}
.container .image {
width: 598px;
height: 300px;
float: right;
position: relative;
z-index: -1;
}
.container .side {
float: left;
width: 302px;
height: 300px;
background: red;
box-shadow: 5px 0 5px -5px yellow;
}
.container .image .menu {
position: absolute;
bottom: 0;
left: -25px;
}
.container .image .menu ul {
list-style: none;
margin: 0;
}
.container .image .menu ul li {
display: inline-block;
text-align: center;
padding: 10px;
background: white;
}
Check out this fiddle: http://jsfiddle.net/5mbK8/5/
A solution would be to position the menu outside the .image div.
And of course you will have to adapt the left: 300px; depending on what width the .side will have.
EDIT (added code):
<div class="container">
<div class="side"></div>
<div class="image" style="background: url('http://i.imgur.com/T0VWLqZ.png');"> </div>
<div class="menu">
<ul>
<li>Link</li>
</ul>
</div>
</div>
CSS:
a {
color: black;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.container {
position: relative;
width: 900px;
height: 300px;
}
.container .image {
width: 598px;
height: 300px;
float: right;
position: relative;
z-index: -1;
}
.container .side {
float: left;
width: 302px;
height: 300px;
background: red;
box-shadow: 5px 0 5px -5px yellow;
}
.container .menu {
position: absolute;
bottom: 0;
left: 300px;
}
.container .menu ul {
list-style: none;
margin: 0;
}
.container .menu ul li {
display: inline-block;
text-align: center;
padding: 10px;
background: white;
}