CSS positioning without the use of float or margin adjustments - css

How can I move my UL element over the the right of the browser without using float, or 'guesstimating' that the element is flush to the right margin through the use of tools such as margin px/% etc?
.nav li {
display: inline;
}
.nav h1 {
background-color: red;
display: inline-block;
}
.nav ul {
display: inline-block;
border: 1px solid black;
}
<div class="nav">
<h1>Resume</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Skills</li>
<li>Experience</li>
<li>Contact</li>
</ul>
</div>

Depending on what browsers you need to support, you can use flexbox.
MDN - Flexible Boxes
Specifically, you want a container with display: flex; and justify-content: space-between;
Something like:
<div class="nav" style="display: inline-flex; justify-content: space-between;">
... child items here ...
</div>
Note that flexbox is only supported on IE11+ and all evergreen browsers (Chrome, Firefox, etc). IE10 has partial support.
See for more details regarding browser support.
If you need to support pre-IE10 browsers, you can try using position: absolute; and right: 0; on your ul.
Flexbox:
.nav {
display: inline-flex; //or just flex
justify-content: space-between;
}
.nav li {
display: inline;
}
.nav h1 {
background-color: red;
display: inline-block;
}
.nav ul {
display: inline-block;
border: 1px solid black;
}
Using position:
.nav li {
display: inline;
}
.nav h1 {
background-color: red;
display: inline-block;
}
.nav ul {
display: inline-block;
border: 1px solid black;
position: absolute;
right: 0;
}

I see from your comment on your original post that you are avoiding floats because your elements are not lining up properly.
.nav li {
display: inline;
}
.nav h1 {
background-color: red;
display: inline-block;
float: left;
}
.nav ul {
display: inline-block;
border: 1px solid black;
float: right;
}
Lines up perfectly when you set the top margins to zero. Your elements are of different height so you need to line them up how you want using margin/padding.

Change the following css properties
.nav li {
display: inline;
}
.nav h1 {
background-color: red;
display: inline-block;
}
.nav ul {
display: inline-block;
float:none;
right:0;
margin-right:10px; // its just an example you can move as much as you want
border: 1px solid black;
}
<h1>Resume</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Skills</li>
<li>Experience</li>
<li>Contact</li>
</ul>

Try position: absolute;, right: 0px;

Related

Vertically aligning an a tag in an li tag with dynamic height

I have a div offers that is dynamically sized, and then inside 6 li tags that each have 16.6% height to fill up the div. Inside each li tag is an a tag, but I want to vertically align the a tag. I have looked at a lot of different solutions and none of them seem to be working for me. An example of my code is;
.offers {
background-color: yellow;
height: 200px;
}
.offers li {
display: table-cell;
float: left;
vertical-align: middle;
border: solid 1px;
box-sizing: border-box;
height: 16.6%;
width: 100%
}
.offers li a {
background-color: red;
}
<div class="offers">
<li>Link1</li>
<li>Link2</li>
<li>Link3</li>
<li>Link4</li>
<li>Link5</li>
<li>Link6</li>
</div>
Here is a codepen - http://codepen.io/anon/pen/dXXJEY
If you want to use display table cell, replace your css in the codepen with the following and it should work:
.offers {
background-color:yellow;
height:200px;
display: table;
width: 100%;
}
.offers li {
display:table-row;
}
.offers li a {
background-color:red;
display: table-cell;
vertical-align:middle;
border:solid 1px black;
}
Instead of tables you can use Flexbox
You can use flex-direction: column on .offers and flex: 1 on li to make each li evenly spaced instead of using %
You can use display: flex and align-items: center on each li to make a vertically centered
.offers {
background-color: yellow;
height: 200px;
display: flex;
flex-direction: column;
padding: 0;
}
.offers li {
border: solid 1px black;
box-sizing: border-box;
display: flex;
align-items: center;
flex: 1;
}
.offers li a {
background-color: red;
}
<ul class="offers">
<li>Link1</li>
<li>Link2</li>
<li>Link3</li>
<li>Link4</li>
<li>Link5</li>
<li>Link6</li>
</ul>
Use display:flex; on the li element. After that put align-items:center; on it.
Use prefixes for crossbrowser compatibillity.
Add the below css.
.offers li:before {
display: inline-block;
vertical-align: middle;
content: '';
height: 100%;
width: 1px;
}
.offers li a {
background-color:red;
display: inline-block;
vertical-align: middle;
}
http://codepen.io/afelixj/pen/ezzVNX
.offers {background-color:yellow;height:200px;}
.offers li {
vertical-align:middle; border:solid 1px;
box-sizing:border-box;
height:33.33px;
width:100%
}
.offers li a {
background-color:red;
line-height: 33.33px;
}
.offers li a span {line-height: 1em;}
<div class="offers">
<li><span>Link1</span></li>
<li><span>Link2</span></li>
<li><span>Link3</span></li>
<li><span>Link4</span></li>
<li><span>Link5</span></li>
<li><span>Link6</span></li>
</div>

CSS stop drop down menu from disappearing when hovering on items

HTML:
<ul>
<li class="nav-item">
<a>Hello</a>
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</li>
</ul>
CSS:
*{
list-style: none;
text-decoration: none;
padding: 0;
margin: 0;
}
.nav-item{
position: relative;
margin-left: 50px;
margin-top: 20px;
}
.nav-item >a{
background: gray;
width: 100px;
text-align: center;
display: inline-block;
}
.nav-item > ul{
position: relative;
display: none;
width: 100px;
}
.nav-item > ul > li{
background: yellow;
opacity: 0.4;
display: block;
text-align: center;
}
.nav-item > a:hover + ul{
display: block;
}
.nav-item > ul :hover {
display: block;
}
The problem is that when i hover on drop down menu it closes,how do i prevent it?I tried using
.nav-item > ul :hover {
display: block;
}
to not hide it when hovering on it,but it doesn't fix the problem.I tried to google but i cant find any solutions,please help me.Thank you for your help.
Removing the space between ul and :hover:
.nav-item > ul:hover {
display: block;
}
See fiddle here
As per my understanding:
With the below code you are saying make the UL to display:block (within the nav-item) when you hover over it, but in fact it's hidden .nav-item > ul {display : none} so how can you hover over it?
.nav-item > ul :hover {
display: block;
}
If you remove the UL from this and keep it as below, even this works.
.nav-item :hover {
display: block;
}
Note: If you want to modify an element based on other element, you should use jquery.

css anchor fill entire list element

I have this menu that I have been working on for a while. I am using the CSS table displays to accomplish it. When the text inside of my links take up two lines, the ones that are only one line will not fill the parent li on hover. Is there any way I am missing that can accomplish this?
http://jsfiddle.net/g7jmh567/
css
.menu {
background-color: #687c9e;
display: table;
}
.menu-list {
display: table-row;
}
.menu-list > li {
display: table-cell;
height: 100%;
overflow: hidden;
line-height: 1.125rem;
overflow: auto;
}
.menu-list > li > a {
display: block;
color: #fff;
text-decoration: none;
padding: 1.25rem 1.25rem 1.25rem 0;
box-sizing: border-box;
height: 100%;
min-height: 2.25rem;
}
.menu-list > li > a:hover {
background-color: #7889a8;
}
.dropdown-list {
display: none;
}
.hidden {
display: none;
}
html
<nav class="content menu">
<ul class="menu-list">
<li>Home</li>
<li>A really long</li>
<li>Some really long word</li>
<li>Special Events</li>
<li>Newsletter</li>
<li>Photo Gallery</li>
</ul>
</nav>
Simply remove the padding from your li, and add it to your menu-list, check out the link below;
Nav
the reason why it didn't fill the entire li 'coz you're just filling the anchor
hover the li instead of the anchor
.menu-list > li:hover {
background-color: #7889a8;
}
JSFIDDLE DEMO
See This link: this may help you: https://jsfiddle.net/guruWork/8fwo0r06/2/
<nav class="content menu">
<ul class="menu-list">
<li><span>Home</span></li>
<li><span>A really long</span></li>
<li><span>Some really long word</span></li>
<li><span>Special Events</span></li>
<li><span>Newsletter</span></li>
<li><span>Photo Gallery</span></li>
</ul>
</nav>
And CSS
.menu {
background-color: #687c9e;
}
.menu-list {
display: table;padding:0; margin:0;width:100%;
}
.menu-list > li {
display: table-cell;
height: 100%;
overflow: hidden; vertical-align: top;
overflow: auto;
}
.menu-list > li > a {
display: table;
color: #fff;
text-decoration: none;
padding: 0;
box-sizing: border-box;
height: 100%;
min-height:53px; text-align:center;
}
.menu-list > li > a span{display: table-cell;padding: 5% .5rem;vertical-align: middle;}
.menu-list > li > a:hover {
background-color: #7889a8;
}
.dropdown-list {
display: none;
}
.hidden {
display: none;
}

how do i make this responsive nav bar work properly?

i am trying to make a responsive navigation bar with these 4 elements. however, if i drag the browserwindow to a certrain point it starts placing one of the 4 below the rest. but i dont know what i'm doing wrong.
The HTML:
<div id="nav">
<ul>
<li class="blue">Home</li>
<li class="blue">Trailer</li>
<li class="red">Gallery</li>
<li clas="red">Contact Us</li>
</ul>
</div>
The CSS:
#nav {
width: 100%;
background-color:transparant;
}
#nav ul {
width: 85%;
max-width: 1200px;
padding: 0;
margin: 0 auto;
list-style: none;
text-align: center;
}
#nav ul li {
display: inline-table;
width: 24%;
padding: 4px;
background-color:#242424;
border-radius: 5px;
}
#nav ul li a {
color:white;
text-decoration: none;
width: 100%;
padding-top: 10px;
padding-bottom: 10px;
display: inline-block;
border-radius: 5px;
}
You are maybe having problems with your padding.
For that, add the box-sizing property in your lis and the padding will be included in the width of the element, like:
#nav ul li {
display: inline-table;
width: 24%;
padding: 4px;
background-color:#242424;
border-radius: 5px;
box-sizing: border-box;
}
Fiddle: http://jsfiddle.net/begyu5v3/
Info: http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
And there's those undesirable white spaces between your lis. In this link, there are some ways to avoid this problem, like font-size:
#nav ul {
font-size: 0;
}
#nav ul li {
font-size: 16px;
}
Give it a try and let me know if it helps!

Auto width when add list

I have a simple menu (centered with margin:0 auto) with some list items.
Now, I'm trying to keep the menu on the same centered position when I add an additional list items.
Here is the fiddle play with it
ul{
list-style-type: none;
background: red;
margin:0 auto;
width: 56%;
max-width:600px
}
ul li{
display: inline-block;
width: 100px;
background-color: #000;
color: #fff;
}
I want to an additional li's to the ul to wrap it and still be centered.
I don't want to use flexbox because IE doesn't support it :D
The problem is solved. Giving the ul {display:table} Thank you all,especially Coop !
Not sure if this is exactly what you're after but I've often had issues centering nav menus and came up with this solution:
ul{
display: table;
margin: 0 auto;
list-style-type: none;
background: red; }
ul li {
float: left;
color: #fff;
background-color: #000; }
Note the li's are floated so you also need to clear them on the ul. I'd suggest a clearfix solution to do that. For example the full code could be:
ul {
display: table;
margin: 0 auto;
list-style-type: none;
background: red; }
ul li {
float: left;
color: #fff;
background-color: #000; }
.clear:before, .clear:after { content: " "; display: table; line-height: 0; }
.clear:after { clear: both; }
.clear { *zoom: 1; }
...
<ul class="clear">
<li>First item here</li>
<li>Second item here</li>
<li>Third item here</li>
</ul>

Resources