I want to place some text to the right of blue ribbon so that it would be on the same line with already existing elements. All my attempts to try doing that causes text occuring on the next line.
What can you recommend?
Here is the code: (JSFiddle)
HTML
<div id='ribbon'>
<ul id='topMenu'>
<li>Thing one</li>
<li>Thing two</li>
<li>Thing three</li>
</ul>
</div>
CSS
#topMenu {
height: 35px;
margin: 0; padding: 0;
}
#topMenu a {
color: black;
}
#topMenu li {
padding: 0 10px 0 10px;
float: left;
list-style-type: none;
}
#topMenu li:hover {
color: white;
background-color: #00D0FF;
}
UPD: But if I need that text not to be <li> element? I mean, not to use general <li> stylesheet.
You can use nth-of-type pseudo and float the elements to the right, for example
#topMenu li:nth-of-type(2), #topMenu li:nth-of-type(3) {
float: right;
}
Demo
Or you can simply use
#topMenu li:last-child {
float: right;
}
To shift the last li to the right. Also make sure you clear your floating elements nested inside ul, you can use a self clearing class like -
.clear_self:after {
content: "";
display: table;
clear: both;
}
So that it doesn't mess up your document flow. Just call that class on the ul element, i.e container element holding floated elements.
Also, you can alternative call classes on the li element which you want to float to the right of the ribbon, but incase if you don't want to increase your markup, you can use pseudo here..
As per your update
Demo 2
Explanation: Float your entire ul element to the left, and this will create an empty space on the right, so that the next div will shift to right cuz we are using float: right;, also make sure you clear your floating elements, else, the next div will sit right in the empty space, for more info on clearing floats, you can read my answer here..
Check out my fiddle. You needed to add float right to the last li. I separated it with a class of .last
#topMenu li.last{float:right;}
http://jsfiddle.net/9GGLE/2/
If you are trying to float another element, sibling of #ribbon, you can check out this jsFiddle
.floated { float:right; margin-top:-30px; /* this is to overcome height+padding of the ribbon */ }
Related
I created a very simple HTML page which has a list and a paragraph. The problem is that the paragraph appears to the right of the list. If I create another paragraph, it is placed correctly. I would like the first one to show in the same manner.
<body>
<ul>
<li>(...)
<li>(...)
<li>(...)
<li>(...)
</ul>
<p>This text is to the right of the list.</p>
<p>This text is in a new line and is left-aligned.</p>
</body>
I don't refer to paragraphs anywhere in my CSS file and this is the part relating lists:
ul {
list-style: none;
margin 0;
}
li {
float: left;
}
Add this
ul:after{
content:'';
display:table;
clear:both;
}
DEMO
This has to be done whenever you use floats. Best practice is to make a class like this -->
.clearfix:after{
content:'';
display:table;
clear:both;
}
and add this class to the parent block if the child is floated .Always try to follow this step.
For more detailed explanation refer this link -- > http://www.impressivewebs.com/clearing-floats-why-necessary/
Don't use float: *; if you don't know what the floating an element does. float takes an element out of the normal flow. However, you want to show the li on a single line without loosing their display: block; properties. That's exactly what display:inline-block; is for (demo):
ul {
list-style: none;
margin 0;
}
li {
display: inline-block;
}
See it in action HERE
The code
HTML
<body>
<ul>
<li>(...)</li>
<li>(...)</li>
<li>(...)</li>
</ul>
<p>This text is to the right of the list.</p>
<p>This text is in a new line and is left-aligned.</p>
</body>
CSS
ul {
list-style: none;
margin 0;
padding: 0;
}
li {
float: left;
margin-right:10px;
}
li:last-child{
margin-right:0;
}
p{
clear: both;
}
Explanation:
you need to add clear:both to your paragraph <p></p> elements.
in ul{...} we set the margin & padding to 0
in li{...} we give some space between the li items by giving margin of 10px to the right.
in li:last-child{...} we removed the margin-right:10px & set it to 0 because it is the last item in the li list.
I have following html for menu
<ul>
<li id="btnHome">Link One</li>
<li id="btnAbout">Link Two</li>
<li id="btnContact">Link Three</li>
<li id="btnLinks">Link Four</li>
</ul>
and following is my css for it
ul {
margin: 0;
padding: 0;
}
ul li {
list-style-type: none;
}
#nav {
background: #999;
padding: 2%;
}
#nav ul li {
float: left;
margin-right: 2%;
}
I use above for IE6 and 7 in order to display links in a single row. float: left displays menu items in a row but it also changes the style for #nav div and menu items do not appear inside #nav div anymore.
How can I fix this issue for IE6 and 7?
Note: I am using display: inline-block for modern browser and this works fine.
You could use a CSS declaration like zoom: 1; for #nav element to trigger hasLayout on IE 6-7.
#nav {
background: #999;
padding: 2%;
*zoom: 1;
}
Note: The star/asterisk prefix is a CSS hack for targeting IE 6/7.
Other options
Using overflow: hidden; for the #nav element to create a new block formatting context.
Creating an element with clear: both; CSS declaration as the last child of the #nav element.
You might want to take a look at Nicolas Gallagher's micro clearfix hack.
Not sure without the rest of the document but you could try adding a
<div style="clear:both;"></div>
right after your close your ul element, that should grow the size of your containing #nav to the place your floated content occupies in it.
I'm trying to create an unordered list beside a paragraph being used as a subtitle, however the list is squashed into the size of one item and items are then stacked on top of each other.
I would also like them to both be side by side on the same level, but both aligned to the left and right sides of the header element, respectively.
Here's what I've got so far. You can see the stacked list as well as an attempt to have them side by side, however I'm sure there's a better way to do this.
This is what I've tried to have the subtitle and list on the same level (also look at the jsfiddle):
.subtitle {
..
margin-top: -40px;
..
}
Thanks.
You should not be floating just the li. You have to inline your p and float nav to the right http://jsfiddle.net/Zz6Xs/12/
nav {
float: right;
}
.subtitle {
display: inline;
...
nav ul {
margin-top: 0;
}
There are default padding and margin settings that are applied to most HTML elements and they differ from browser to browser. Many people use a CSS reset system to get a consistent look and feel across browsers. See this example, just like what you had, but with having to reset margins yourself
What's even better than using floats is the flex box model if you can use HTML 5. In the following example, we tell the nav to take up all remaining space while p is only wide as it needs to be
CSS
section {
display: box;
box-orient: horizontal;
}
nav {
box-flex: 1;
text-align: end;
}
HTML
<section>
<p class="subtitle">Subtitle </p>
<nav><ul>
<li class="menu">Item 1</li>
<li class="menu">Item 2</li>
<li class="menu">Itemmm</li>
</ul></nav>
</section>
Also try dropping the negative margins and zero out the padding and margins on the elements.
header {
width: 50% ;
margin:0 auto;
}
h1 {
font-size: 50px;
color: #33B5E5;
}
h1,p,nav,ul{
padding:0;
margin:0;
}
.subtitle {
color: #33B5E5;
float:left;
width:20px
}
ul{
float:right;
}
.menu {
display: inline;
width: 20%;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Navigation hyperlinks only work when mouse is on the text
Can you set a link to the whole width of an < li > instead of just where the text is?
This is what I mean, I want the user to be able to click on anywhere on the button and go to the link and not just the text: http://jsfiddle.net/b7S4L/
One of the problems is that I cannot use display: block; because I have a number after the < a > link for example (1)
Don't style the LI at all, (other than float:left and clearing padding, marging and list-style-type) if needed. Put all styling on the A (and use display:block).
I don't want the number on the right to be on a seperate line that's
the problem, it should be on the right of the Text
I think I understand what you're trying to do here. Though, I'm not sure because your question has been quite confusing..
First, do set display: block on the a. That is the right thing to do here.
Then, move the number inside the a, and add a span inside:
<li class="cat-item cat-item-147">
<a href="http://test.vps.graenseguiden.dk/newscat/food/" title="Vis alle indlæg i kategorien Food">
<span>Food</span> (4)
</a>
</li>
Then, some extra CSS is needed. You should merge the new CSS with what you already have - for the demo, I've added it within the HTML pane for simplicity (marked with <!--new css right here-->):
http://jsfiddle.net/thirtydot/b7S4L/3/
div.gg_newscats li a {
display: block;
padding: 16px 0;
color: #333
}
div.gg_newscats ul li {
padding-top: 0;
padding-bottom: 0
}
div.gg_newscats li a span {
color: #cc0014
}
div.gg_newscats li a:hover {
text-decoration: none
}
div.gg_newscats li a:hover span {
text-decoration: underline
}
The messing around with span and :hover is to keep the colour and underline exactly as you had it.
Anchor tags by default are inline boxes, which means that they don't fill their parent entirely (they don't take all the space) and they shrink only to fit their content. Thus you should use this CSS to make'em fill the space of li element:
li a
{
display: block;
height: 100%;
}
Also keep in mind that you should remove any padding from the li elements and remove margins of a elements. This way, border of anchor tags meet borders of li tags. For an example, look at links of Thought Results.
One solution I tend to use is to make the <a /> element within a <li /> element blocklevel with
display: block;
After that removing any padding you specified on the <li /> element and add it on the <a /> element instead and you should get the same visual output, but with the entire <li /> as a link
While you can manage this with jQuery, you can also use simple CSS for most browsers:
<style>
ul { width: 200px; background: #ccc; }
li { line-height: 3em; }
a { display: block; width: 100%; height: 100%; padding: 5px; }
</style>
<ul>
<li>This is a link</li>
</ul>
Add display:block; to the style and you're all set!
EDIT
Eh, didn't see the jsFiddle example. If you remove the top/bottom padding from the LIs and put it on the As, plus put the count in a SPAN within the As, these rules will achieve the desired result:
div.gg_newscats a {
display: block;
height: 100%;
padding: 10px;
}
div.gg_newscats a span {
color: black;
}
div.gg_newscats ul li {
float: left;
font-size: 13px;
margin-left: 2%;
margin-top: 2px;
text-align: center;
width: 30%;
padding: 2px;
}
Sample HTML:
<li class="cat-item cat-item-148">
<a title="Vis alle indlæg i kategorien Electrical" href="http://test.vps.graenseguiden.dk/newscat/electrical/">
Electrical
<br>
<span>(1)</span>
</a>
</li>
Edit 2
new code... a lot simpler... only thing that didn't go the way I liked was that the text-decoration of the link had to go.
.cat-item
{
padding: 0px;
}
.cat-item a
{
padding: 13px 0px 13px 0px;
}
.cat-item span
{
margin-left: 5px;
color: black;
}
.cat-item a:hover
{
text-decoration:none;
}
I had to change the markup just a little (put the numbers in a span) but other than that it wasn't too much
demo here: http://jsfiddle.net/ZW6uV/1
had to tack on !important because of a conflicting imported style sheet.
Edit
Readers Digest version: Don't put your padding on the <li> ... ever. Put padding on the <a> within the <li> and then it will fill the empty space and have the same effect but be able to handle the click also. -snip-
Yes just remove any padding from the LI element and push out the padding as needed on the anchor tag
<li class="link-wrapper">
<a href="http://this.com" >Go Here</a>
</li>
CSS
.link-wrapper{
margin: 0;
padding: 0;
}
.link-wrapper a{
display: block;
padding: 3px 5px;
}
Since you are using jQuery, you can do it this way:
$("li.cat-item").click(function () {
$("a", this).click();
return false;
});
I have a pagination output which is show within . An example is posted here: http://jsfiddle.net/6N4bD/1/
The ul is wrapped around 2 divs
1st div is 550 width
2nd div called "paginationDiv" basically wraps the ul around
The ul has list-style: none. What I am trying to do is make it float right, but it keeps appearing as a block. I have tried quite a few things but nothing seems to work. If I add a width to the paginationDiv then it works but it's not accurate because it will never be fixed width
Here is what it looks like:
<div class="parentDiv">
<div class="paginationDiv">
<ul id="paginationLinks">
<li>Page 1</li>
<li>Page 2</li>
</ul>
</div>
</div>
That's the html code
Here is the css:
div.parentDiv {
width: 550px;
}
div.paginationDiv {
float: right;
}
#paginationLinks ul {
border: 0;
padding: 0;
margin: 0;
}
#paginationLinks li {
list-style: none;
}
I have posted an example here: http://jsfiddle.net/6N4bD/1/
What seems to work is adding display: inline; to the li elements.
http://jsfiddle.net/XFdqT/ has a demo of this.
give float:left to #paginationLinks li and then see the result