i am pulling data from database that prints in a ul li format. since data is different we may have 2 li items or we may have 10 who knows.
what is the best approach to center a ul or a div that will change in width? i know to center a div or ul i would use following:
#name, ul {
width:200px; /*set certain width */
margin:0 auto; /* center div */
display:block /* play friendly with the existing layout */
}
my problem is with the data the div may be 200px or beyond 200px, and as a result it would not truly be centered, since the div center alignment is based on width.
any help would be appreciated.
Here, is a way you can center a ul that has dynamic content length.
You can wrap the ul in a div and set the CSS property on the DIV as
.wrapper{
display:table;
margin: 0 auto;
}
This will center align the ul inside the div irrespective of the width of the div, you can see an example below:
Codepen Link http://codepen.io/nadirlaskar/pen/RKbBpM
.wrapper{
display:table;
margin: 0 auto;
background-color:yellow;
}
ul{
background-color:red;
}
li{
background-color:blue;
}
<div class="wrapper">
<ul>
<li>Item 1</li>
<li>Item 2 is now large</li>
</ul>
</div>
Related
I kept searching on the Internet, but the only thing I get is how to avoid/prevent the div elements on wrapping/floating when resizing the browser. My problem is exactly the opposite: I have an horizontal menu and I'm trying to wrap down the elements (eg. Home, Contact etc.) once the browser is shrinking and then, to return to its initial state when the browser is maximized.
Here is the HTML document:
<div id="menu">
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
</ul>
And the CSS is something like this:
#menu
{border:1px;
height: 40px;
width: 400px;
clear: both;
float: left;
position:relative;
top:20px;}
#menu ul
{list-style-type:none;
margin:0;
padding:0;
overflow:hidden;}
#menu li
{float:left;}
#menu a:link,a:visited
{display:block;
width:100px;
font-weight:bold;
text-align:center;}
I tried to change the height and width to auto, remove/shift the clear, float, overflow and position tag, I even changed the float:left in "#menu li", to float:none, but then the menu elements get fixed one after another and it stays that way, even if I resize the browser.
I also, divided each of the menu elements with div (is not in the code below), but without any succes.
Do I have to change the CSS code entirely or to do this with js, jquery etc.?
If so, how?
DEMO
This can be achieved simply by removing the fixed height widths.
Elements with unspecified widths are set to width:auto which is usually the size of the elements contents. Floated elements will wrap if they cannot fit on the page adjacently.
CSS
#menu{
border:1px;
clear: both;
/*height: 40px; Remove*/
/*width: 400px; Remove*/
float: left;
position:relative;
top:20px;
background:#58c;
}
#menu ul{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
#menu li{
float:left;
}
#menu a:link,a:visited{
display:block;
width:100px;
font-weight:bold;
text-align:center;
}
You are setting the width of #menu to 400px which fixes that width, no matter how wide the browser window is. Use this one:
width:100%;
max-width:400px;
What simple CSS mistake am I making?
I have a number of images setup in a ul as part of a gallery, each with a hover over text box
For everything except mobile breakpoints, I'd like them (the thumbnail versions) to line up 4 across.
By setting the width to 25%, I thought the following would handle that, but it's breaking it into 3 across with a bunch of space on the right side of the container - as if there was padding on the container or margin on the images forcing a new row.
There also appears to be a small amount of margin or padding on all sides of the image. I've double checked each element through the browser and there is no margin or padding being applied from some other rogue style that I may have missed.
JSFiddle Example Here
UPDATE: On my fiddle example, the hover-over text box is clearly going beyond the right edge of the image, but the css sets it's width at 100%. Do I just have my positioning screwed up?
Link to live page at www.deckdoctors.net/bay-area-deck-ideas.html
Abridged HTML:
<div id="gallery">
<ul class="nivo">
<li>
<a href="full-size-image.jpg"><img src="thumbnail-image.jpg">
<span>Some overlay text</span></a>
</li>
<li>
<a href="full-size-image.jpg"><img src="thumbnail-image.jpg">
<span>Some overlay text</span></a>
</li>
<li>
<a href="full-size-image.jpg"><img src="thumbnail-image.jpg">
<span>Some overlay text</span></a>
</li>
<li>
<a href="full-size-image.jpg"><img src="thumbnail-image.jpg">
<span>Some overlay text</span></a>
</li>
</ul>
</div>
Abridged CSS:
#gallery {
width:100%;
padding:0;
}
#gallery ul {
list-style:none;
margin:0;
padding:0;
}
#gallery ul li {
display:inline;
position:relative;
margin:0;
padding:0;
}
#gallery ul li span {
visibility:hidden;
position:absolute;
width:100%;
left:0;
bottom:0px;
z-index:3;
background:rgba(0,0,0,.6);
color:#FFF;
text-align:center;
}
#gallery ul li:hover span {
visibility:visible;
}
#gallery ul li a img {
width:25%;
margin:0;
padding:0;
}
Here's the issue:
Inline elements will have spaces after them which makes the last item on the row drop to the next line, so if you set a width of 25%, you'd need to counter for the spaces produced after each <li>.
A better way to do this would be to use float: left (which makes elements 'block-level') on the <li> and set it's width to 25%.
eg.
ul li {float: left; width: 25%}
See this fiddle: http://jsfiddle.net/pavkr/685m0b5z/
Also see this screenshot of it in action on your website:
Here's my fix: http://jsfiddle.net/3rmzz59m/1/
I believe the problem is related to the inline lis; sizing is a bit weird when it comes to inline elements. By changing them to inline-block and setting the width on the li elements instead, the extra spacing around them goes away.
Make the width of images 24% instead of 25%
#gallery ul li a img {
width:24%;
margin:0;
padding:0;
}
also set text-align:center for Gallery:
#gallery {
width:100%;
padding:0;
text-align: center;
}
Checkout this DEMO: http://jsfiddle.net/3rmzz59m/2/
#headermenu ul li {
width:195px;
height:45px;
float:left;
left:0;
text-decoration:none;
text-align:center;
font-family:century gothic;
background-color:#fef3e2;
}
this is my li css ive been trying to make the width and height to a certain percent but apparently its not working..my question is, is there a way or is it possible to set this width and height to percent i dont want to use table here because if i do the submenu when hover in the menu will be distorted meaning they will be side by side not on top of each other(i want them to be on top of each other)..any idea is appreciated..
UPDATE:
http://jsfiddle.net/guradio/gvmRX/
in this fidde you can see that i change the li size to 100 to make it fit in the this is the reason why i want to make it a percent width if possible to avoid over sized width
To be able to give a height and width in a percentage you need to give the parent the width and height.
HTML:
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
CSS:
ul li {
width: 33%;
height: 33%;
float:left;
left:0;
text-decoration:none;
text-align:center;
font-family:century gothic;
background-color:#fef3e2;
}
In this demo we have the <li> you can see it is using a width as percentage as the parent has already a set width (<ul> is a block element) but the height is not set already so the percentage height it not working.
Demo Here
Now lets set the parents height and see if it changes anything.
Added CSS:
html, body {
height: 100%;
}
ul {
height: 100%;
}
so here we are setting the html, body height to use 100% and then the the parent of <li> needs to also be set (again <ul> is a block element, so it has no set height). This allows us to use percentages for the height.
Demo Here
Any question let me know.
I have the following layout:
<ul>
<li>Text
<div class="task"></div>
</li>
</ul>
See fiddle. I am having problems aligning the text Text. Now it is on the same line as the div. I would like to align (vertically) the Text to the middle of the div. I have tried nesting the Text inside a span and giving the span margin-bottom, padding-bottom, but nothing seems to work.
Beware of spacing issues with inline-block - http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/
http://jsfiddle.net/6AxbH/1/
.task {
display:inline-block;
vertical-align: middle;
}
.task {
height:20px;
width:20px;
background-color:red;
display:inline-block;
text-align:center;
float:none;
}
ul li {
text-align:center;
width:50px;
display:block;
list-style:none;
}
I just set a width on the LI to match what the size of the box was (then reduced the size of the box to make sure it centered) and applied display:block;text-align:center; to center everything.
My HTML looks like this:
<body>
<div class="nav"><ul>...</ul></div>
<div class="view">this text won't wrap if I resize browser</div>
</body>
and my CSS looks like this:
.nav {
width:200px;
float:left;
font-weight:bold;
margin-right:46px;
}
.nav a {
font-weight:normal;
}
.nav ul {
padding:0;
margin:0;
list-style-type:none;
text-align:right;
}
.nav ul li {
margin-bottom:7px;
}
.view {
float:left;
}
If I resize the browser to be skinnier, then it won't word wrap the text in the view div at all. It will just un-float the view div and put it below the nav div.
How can I make the text in the view div word wrap instead of un-float to under the nav div?
You want your .nav div to be 200 pixels wide and I assume you want 46 pixels between the .nav div and the .view div, at least that's what I understand from the margin-right:46px on the .nav div. You don't need to float the .view div. In fact, you can't because if it's floated, the only way to get it next to the .nav div is to set a width (otherwise it will default to 100% of it's parent). But you can't set the width because you want it to grow and shrink with the size of the browser.
So floating is not an option but also not necessary. The .nav div is floated and because of this the .view div will appear underneath the .nav div (because floated div's are taken out of the flow). To make the .view div appear next to the .nav div you simply set a margin-left of 246 pixels (200px width of .nav + 46px margin).
.nav {
width:200px;
float:left;
}
.view {
margin-left:246px;
}
You need to set a width on floated elements, otherwise they will take UP TO 100% width of their containers.
also, you should clear your float containers.