So I created a drop-down menu using only CSS and I've positioned them on my header and footer, my problem is how can I make my menu display above my button instead of under it(only for footer button). Any ideas?
CSS
.d_button {
color: #FFFFFF;
background-color: #222222;
border: none;
cursor: pointer;
font-size: 80%;
font-weight: bolder;
line-height: 50%;
padding: 8px;
}
.drop_top {
position: inherit;
display: inline-block;
background-color: #222222;
float: right;
}
.drop_bot {
position: inherit;
display: inline-block;
background-color: #222222;
float: left;
}
.drop_content {
display: none;
position: absolute;
right: 14px;
background-color: #222222;
min-width: 80px;
}
.drop_content a {
color: #FFFFFF;
padding: 5px;
text-decoration: none;
display: block;
}
.drop_content a:hover {
color: #03A9F4;
}
.drop_top:hover .drop_content {
display: block;
}
.drop_bot:hover .drop_content {
display: block;
}
<div id="header_container">
<div id="header">
Header Content
<div class="drop_top">
<button class="d_button">
<div id="nav_icon" class="top">☰</div>
</button>
<div class="drop_content">
A
B
C
</div>
</div>
</div>
</div>
<div id="container">
<div id="content">
</div>
</div>
<div id="footer_container">
<div id="footer">
Footer Content
<div class="drop_bot">
<button class="d_button">
<div id="nav_icon" class="bottom">☰</div>
</button>
<div class="drop_content">
A
B
C
</div>
</div>
</div>
</div>
Thanks
Ok so first things first is that this dropdown that you have will more than likely not work because your dropdown menu will only show when you hover on the button and not when you hover on the actual dropdown menu. So your dropdown menu will appear but when you go to try and click on the links once you remove the cursor from the dropdown button the menu disappears. That being said to create a css dropdown menu you will want to wrap both the dropdown button and the dropdown menu in a container and then position this container as relative. Then you can position the dropdown menu as absolute and you can control the position of this absolute positioned div inside the relative positioned div with top, right, left, and bottom in your css. Hopefully that makes sense. So I have created a fiddle for you to view the proper technique for createing a dropdown menu.
Here is the fiddle Fiddle
First you will start with the following markup for your dropdown menu
<div class="dropdown">
<button class="dropdown-button">☰</button>
<div class="dropdown-menu">
Link
Link
Link
</div>
</div>
Then the following Css:
.dropdown{
position:relative;
}
.dropdown-menu{
position:absolute;
top:100%;right:0;
min-width:80px;
background:#000;
display:none;
}
footer .dropdown-menu{
bottom:100%;top:auto;left:0;right:auto;
}
.dropdown:hover .dropdown-menu{
display:block;
}
.dropdown-menu a{
display:block;
color:#fff;
padding:5px;
text-decoration:none;
}
.dropdown-button{
border:none;
background:#222;
color:#fff;
outline:0;
cursor:pointer;
padding:10px;
}
header .dropdown{float:right}
footer .dropdown{float:left;}
Then you can see in this css I have positioned the footers dropdown menu to have the following css bottom:100%;top:auto;left:0;right:auto; so you can control the placement of an absolute positioned div inside of a relative positioned div accordingly.
Related
I would like to place an image on a div which I use for a navigation bar, but when I resize the image to 50px or above, the padding on the div gets bigger as well. I don't like this since the image will either be too small to see or the navigation bar will be too big to look pleasing, any ideas on how to fix this?
.navbar{
background-color:green;
padding:20px;
}
.navbar #image1{
width:40px;
margin-left:950px;
padding:0px;
}
<div class='navbar'>
<a href='home.html'>Home</a>
<a href='1.html'>Profile</a>
<a href='2.html'>Transactions</a>
<a href='3.html'><p>Settings</p></a>
<img src='https://picsum.photos/200/300' id='image1'/>
</div>
You should first start by removing
margin-left:950px
as the margin will exclude your item from your navbar
then apply flex properties to your navbar
.navbar {display : flex}
so your navbar items become in the same line
I recommend checking out this flex-box guide as well flexbox properties
can you please share the image's link so we can help you?
also you most edit margin-left:950px; to margin-left: auto; if you want to center it
and this is an example navbar code (press run to see what is it)
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.navbar{
padding: 10px 20px;
background: black;
color: white;
display: flex;
align-items: center;
}
.navbar li{
list-style: none;
display: inline-block;
padding: 0 20px;
cursor: pointer;
}
.navbar li a{
color: white;
text-decoration: none;
}
.navbar li:hover,
.navbar li a:hover{
color: #666;
}
.navbar img{
width: 50px;
height: 50px; /*or :auto ; */
}
<ul class="navbar">
<li>home</li>
<li>project</li>
<li>contact</li>
<li><a>settings</a></li>
<img src="https://www.calliaweb.co.uk/wp-content/uploads/2015/10/600x600.jpg"/>
</ul>
<br><br><br>
<p>or</p>
<br><br><br>
<ul class="navbar">
<img src="https://www.calliaweb.co.uk/wp-content/uploads/2015/10/600x600.jpg"/>
<li>home</li>
<li>project</li>
<li>contact</li>
<li><a>settings</a></li>
</ul>
I think you need to learn the basics before start doing websites
I'm trying to set up a nav bar with a png link to the top left corner, and a text element "menu" to the top right corner. I haven't been able to get this to work with "float: right;"
I've included the code that shows I used float: right; for the .topnav elements. I'm not sure if .logo is interfering with this. I needed the png logo to be aligned with the text element which was not possible without putting them in separate divs.
.container {
position: absolute;
margin: 20px;
width: auto;
}
.topnav {
overflow: hidden;
float: right;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 10px 10px;
text-decoration: none;
font-size: 17px;
}
.topnav_right {
float: right;
}
.logo {
float: left;
}
<div class="container">
<div class="logo">
<img src="####.png" style="max-width:100%;">
</div>
<div class="topnav">
<div class="topnav_right">
Menu
</div>
</div>
</div>
The text still remain next to the logo, when it should be in the opposite corner to the right.
In the container class, instead of having position: absolute , do position: flex . It will fix the problem.
Since you want a Navbar with left-aligned png-link and right-aligned text, it can be achieved in a much simpler way using flex-box, with the need of nesting them.
It also handles alignment easily
You can read more about flexbox from csstricks
<style>
.topnav {
display:flex;
justify-content:space-between;
align-items:center;
}
</style>
<div class="topnav">
<div class="logo">
<img src="###.png">
</div>
<div class="text">
Menu
</div>
</div>
I have a menu in the top of my website with this css:
.menu {
width: 100%;
display: block;
float: left;
}
inside of it, I have few divs:
.menu .menu-item {
position: relative;
width: 260px;
float: left;
height: 430px;
}
This is all good, but when I try to add a small div underneath the menu, with this HTML structure:
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="menu-bar">
</div>
and this css:
.menu-save {
position: relative;
display: block;
margin: 0 auto;
width: 100%
height: 20px;
}
With this CSS my expected output is that the menu-bar div goes underneath the whole menu, but what I'm currently getting is that menu-bar sits inside of menu, at the top of it. What CSS am I missing?
I think
use clear: both CSS property to avoid the floating problem
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="clear"></div>
<div class="menu-bar">
In Css add this one
.clear{
clear:both;
}
The elements of the web page I'm building move around when I want them to stay in place. If the line beginning "This is a Beta version" changes length, the 4 menu items (translucent gray rectangles) shift: If I shorten the bottom line, the menu items move to the right; if I lengthen it, the menu items move left.
How can I arrange things so that the width of one element won't change the horizontal position of other elements that are stacked vertically with it?
You can view the page at http://apdamien.info/nfair/GH/demo/mainmenu.html
Here is what I think is the relevant sections of the code:
CSS:
#mainmenu {
width: 350px;
padding-bottom: 14px;
margin-right: 60px;
}
#maindiv {
background: url(imgs/smalltown.jpg) no-repeat;
}
.menu-entry {
display: block;
cursor: default;
color: #fff;
width: 100%;
height: 39px;
padding-top: 13px;
font-size: 20px;
font-weight: bold;
font-family: Univers,sans-serif;
text-transform: uppercase;
margin-bottom: 19px;
margin-left: 8em;
border: 2px solid black;
text-align: center;
background: rgba(96,96,96,0.65);
text-decoration: none;
text-transform: uppercase;
}
.menu-entry:hover {
background: rgba(0,0,0,0.5);
}
And the relevant chunk of HTML:
<body>
<div id="maindiv">
<div id="titleauth">
<div id="title"><img alt="Demo Game" src="imgs/title.svg"/></div>
<div id="author"><a href="http://www.apdamien.info"
target="_blank"><img alt="A. P. Damien" src="imgs/author.svg"/></a>
</div>
</div>
<div id="lowerleft">
<div id="mainmenu">
<a class="menu-entry" href="game.html">New Game</a>
<a class="menu-entry" href="helpmain.html">How to Play</a>
<a class="menu-entry" href="restore.html">Restore Saved Game</a>
<a class="menu-entry" href="credits.html">Credits</a>
</div>
<div id="bottom-line">
<img alt="Beta version warning" src="imgs/beta.svg" />
</div>
</div>
</div>
</body>
Both #mainmenu and #bottom-line are positioned relative to the left-hand side of #lowerleft. If you want to have them positioned relative to the right-hand side, you'll need to float all children to the right. You'll also want to use clear: both, so that the children won't sit next to each other.
#lowerleft * {
float: right;
clear: both;
}
Using the above on your live site allows me to adjust the width of #bottom-line without affecting the postion of #mainmenu, so this should hopefully work for you :)
Note that this shifts the menu slightly to the right. If you want to move the main menu back to its original position, you can increase its margin-right value. It would have had no effect on the menu previously, though the addition of float: right also fixes that ;)
Why wont this show my sprite image as the background of the yellow bordered box?
There is an example here:
If anyone could come with some examples on what i should change, that would be great.
Here is my code:
Html:
<div class="repeat">
<div style='width:100%;'>
<div style='float:left;margin-top:107px;margin-left:50px;width:80px;height:100px;border:1px solid yellow;'>
<div class="menu">
<ul id='nav'>
<li class='front'><a href='?p=front'>Front</a></li>
</ul>
</div>
</div>
<div style='float:left;margin-top:107px;margin-left:10px;width:80px;height:100px;border:1px solid yellow;'></div>
<div style='clear:both;'></div>
</div>
</div>
Css:
.repeat {
float:left;
width: 884px;
height: auto;
min-height:700px;
background: url(images/repeat.png) repeat-y;
z-index:2;
}
.menu {
width:80px;
height:100px;
position:relative;
}
ul#nav {
list-style: none inside;
}
ul#nav li {
display: inline;
}
ul#nav li a {
display: block;
height: 40px;
float: left;
}
ul#nav li.front a{
width:80px;
background: url(images/menu/p1.png) top center no-repeat;
}
ul#nav li a:hover {
background-position: bottom center;
}
ul#nav li.front2 a{
width:80px;
background: url(images/menu/p1.png) bottom center no-repeat;
}
Your Sprite is showing just fine... Just that it's not visible in the overall setting.
Try adding height: 80px to your "ul#nav li.front a" selector and you will see where you are going wrong.
Try on yourself from there.
Hint: Use your browser developer tools (I prefer Chrome) and hover over each element to see what area they occupy. In your case check the 'div menu' and 'li a' elements.
The background-image shows up fine, except that part of the spritemap (center top) is empty, and it's set to no-repeat. Also, you have not reset your list, which makes the child elements oddly offset.
Revise:
ul#nav { padding: 0; }
ul#nav li.front a {
background: url("images/menu/p1.png") repeat-y center bottom transparent;
width: 80px;
height: 87px;
}
This will probably not solve all your problems. It is not evident from your markup and CSS what you are trying to achieve. You might have better luch with some absolute positioning of your anchor tag ... or simply do some cleanup.
This should do it - there was no rule that targeted the boxes you want styled.
And if you don't want a single red image that spans the gap between the two, you need to remove the rule for the "ul#nav li.front a" selector.
.yellowBox
{
float: left;
margin-top: 107px;
width: 80px;
height: 100px;
border: 1px solid yellow;
background-image: url(images/menu/p1.png);
}
#box1
{
margin-left: 50px;
}
#box2
{
margin-left: 10px;
}
<div class="repeat">
<div style="width:100%;">
<div class='yellowBox' id='box1'>
<div class="menu">
<ul id="nav">
<li class="front">Front</li>
</ul>
</div>
</div>
<div class='yellowBox' id='box2'></div>
<div style="clear:both;"></div>
</div>
</div>