HTML CSS Navigation Menu positioning text and images - html

I am trying to create a I am trying to create aI am trying to create aI am trying to create aI am trying to create aI am trying to create aI am trying to create aI am trying to create aI am trying to create a
HTML
<div id="pageTop">
<nav>
<ul>
<li> <img src="images/search.png" alt="" class="headerCon" /> <navText> Home </navText> </li>
</ul>
</nav>
</div>
CSS
nav > ul > li > a {
color: #aaa;
}
nav > ul > li > div ul > li {
display: block;
}

You can make the <img> elements block elements and center them using an auto margin.
ul img {
display : block;
margin : 0 auto;
}
Another would be to use background images along with a background position:
ul li.home {
background-image : url(...);
background-position : center;
}
You will probably also want to remove the spaces between the <li> elements. I like doing this by omitting the optional closing tags so you would have:
<ul>
<li><a href=...><img src=... />Item 1
<li><a href=...><img src=... />Item 2
...
</ul>
Other options for removing spaces can be found here: How to remove the space between list items
Edit: JSFiddle added with a stripped down version to show how this could work.

just put a line break in the <li>
<li><a href="..."> <img src="..." class="headerCon" /> <br/>
<navText> Home </navText> </a>
</li>
I would take out the <navText> too, that's not valid HTML
<li><a href="..."> <img src="..." class="headerCon" /> <br/>
Home </a>
</li>

Related

Is it Possible to Open a Dropdown Menu by Hovering Over a Sibling Element?

I have to make a few changes to the menu of my company's website, including a dropdown menu. The thing is, our website is made with Prestashop, which kind of messed up the hierarchy of the HTML. It pretty much looks like this:
<ul class ="menu">
<li>
<a href="" class="sf-with-menu">
<ul class ="submenu">
<li>
<a href="">
</li>
<li>
<a href="">
</li>
</ul>
</li>
</ul>
I want to have the ul with the class .submenu open when I hover on .sf-with-menu, which are both at the same level in the page's hierarchy. Is that possible only with CSS? I'm very limited with Prestashop regarding the HTML, since I've read changing the HTML would basically break everything when we update our modules, so I'd like to stick to CSS only if possible.
I suppose you could use the + selector, which selects "elements that [are] placed immediately after (not inside) the first specified element," like this:
<ul class ="menu">
<li>
Anchor
<ul class ="submenu">
<li>
<a href="">One
</li>
<li>
<a href="">Two
</li>
</ul>
</li>
</ul>
<style>
.submenu {
display: none;
}
.sf-with-menu:hover + .submenu {
display: initial;
}
</style>
This applies the styles to every instance of .submenu placed immediately after .sf-with-menu, when hovering over .sf-with-menu.
It will only highlight the first instance though, as you can see in this demo, so if you have the structure:
.menu
li
.sf-with-menu
.submenu
// stuff in menu
.submenu
// stuff in menu
...then only the first submenu will be opened on hover.
See here for more CSS info.

Control space between in-line list

Trying to create a navigation menu at the top of the page, but the images are spaced too far away from each other and then they wrap. How do I bring them closer together?
CSS:
.weddingMenuIcon{
width:30%;
}
HTML:
<ul class="list-inline">
<li>
<img src="~/images/home.svg" class="img-responsive weddingMenuIcon" />
</li>
<li>
<img src="~/images/home.svg" class="img-responsive weddingMenuIcon" />
</li>
<li>
<img src="~/images/home.svg" class="img-responsive weddingMenuIcon" />
</li>
</ul>
If I were building this as a centered navigation I would apply inline: block to the list items and then use a margin to define the spacing between them.
See this jsFiddle
.list-inline li {
display: inline-block;
margin: 0 40px; /* Items have 40px spacing between */
}
The fix was simply changing the width from using percentage to using fixed pixel width.

two lines / line break in li element

I have a <ul> with a couple of <li>. In my css with li { display: inline; } I put the li elements in a horizontal order. The li elements contain a picutre and text. But now the picture and the text are also in horizontal order, but I want them to be under neeth each other. How can I do that?
<ul>
<li>
<img src="img/a.png" />
A
</li>
<li>
<img src="img/b.png" />
B
</li>
<li>
<img src="img/c.png"/>
C
</li>
</ul>
You will need to change your CSS as follows:
li {
display: inline-block;
}
li img {
display: block;
}
Here is a quick demo: http://codepen.io/anon/pen/VLLoEZ
This is not a bug but a normal behaviour. <img> tag is by default inline. You could solve this non-issue by either wrapping either your image or, better, your text into a block element. For example, a <p>tag for your text :
<ul>
<li>
<img src="http://placehold.it/140x100" />
<p>Your text here</p>
</li>
<li>
<img src="http://placehold.it/140x100" />
<p>Your text here</p>
</li>
<li>
<img src="http://placehold.it/140x100" />
<p>Your text here</p>
</li>
</ul>
jsFiddle
Note I use display:inline-block on li elements, taking advantage of both inline (putting things side-by-side, alignment,...) and block (fixed size, top/bottom margins) properties. Although it has a strange but easilly fixed "feature/issue", this is most of the time the best way to put elements side-by-side. display: inline or floating elements are also used but come with some other issues sometimes a bit trickier to be fixed.

CSS to move text to new line on horizontal menu

I have a simple horizontal menu. On some of the menu headings, i would like to split them so the text starts on a new line.
HTML:
<ul class="tabs">
<li>
<div class="home">
Home
</div>
</li>
<li>
<div class="contact">
How to
Contact Us
</div>
</li>
<li>
<div class="products">
About Our
Products
</div>
</li>
</ul>
CSS:
.tabs ul {
list-style: none;
}
.tabs li {
display: inline-block;
}
So, what i am trying to do is, rather than appear as About Our Products.. Instead:
About Our
Products
Here's my fiddle: http://jsfiddle.net/oampz/dWbx5/
Ideally without using br
So like I said in the comment, you can maybe use max-width
HERE is a fiddle using it.
EDIT: I added it to the inline CSS, but you can move it into your CSS page.
Simply use a break <br />?
<div class="products">
About Our<br />Products
</div>
I also prefer to use a float: left; on the li instead of inline-block, it's easier (and more logic) to work with:
http://jsfiddle.net/2EDbr/
<div >
About Our
</div>
<div>
Products
</div>
similarly u can use for all
http://jsfiddle.net/dWbx5/4/

Make menu and image appear on the same line

I want to make my menu and image appear on the same line but sadly that doesn't seem to be happening. Could anyone tell me why and how I would solve my problem? I've got the following image and menu...
HTML
<div id="header">
<img alt="" height="67" src="Aidanlogo.png" width="400" />
<div id="myslidemenu" class="jqueryslidemenu">
<ul>
<li>Home</li>
<li>Blog</li>
<li>About</li>
<li>Contact</li>
</ul>
<br style="clear: left" />
</div>
</div>
Menu CSS: http://pastebin.com/drMD7gwg
Header CSS
#header {
width: 700px;
margin-left: auto ;
margin-right: auto ;
}
try this
#menu ul li { display: inline; }
Divs are block elements, so by default it will always appear on a separate line.
You could make the image a block and float it left (display:block; float:left;)
You could make your div display:inline-block, or float:right; it, assuming there's room in the parent (700px).
DIV is a block element, so it won't display on the same line as anything else unless you change it's inline property:
#myslidemenu { display:inline; }
Also note that you'll have to modify the <ul> styles to display the <li> tags on a single line. See this link for more on that part.
edit I'm not sure what the jQuery slide menu does to the <div> or <ul> styles - you might have a look in Firebug after it's rendered.