I want to make an image in the left and the text on the right.
This is a jsfiddle
http://jsfiddle.net/VnnZ2/
I put the image in a div with class imageClass and I put the text in a div with class information and I gave the imageClass left float and the information right float.
I also tried to make this:
display: inline-block;
for both classes but still the result as you see in the jsfiddle.
I tried the same css with label and input and it works but I don't know why it is not working with div and div.
Plus, I already give the imageClass a width 200 and the informaton class a width 300 and the ul width 500px so I tried all what I could
Update 1
Sorry I gave you the wrong jsfiddle by mistake, this is the correct one http://jsfiddle.net/VnnZ2/9/
Check this updated code
HTML:
div class="allRestaurants">
<ul>
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">
nameres </div>
</li>
</ul>
<ul>
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">
Zuma 2 </div>
</li>
</ul>
</div>
CSS:
.allRestaurants{
background-color: #376b66;
width:100%;
float:left;
}
.allRestaurants ul{
margin: 10px;
padding: 0;
width: 600px;
background-color: #ffffff;
margin-left: 10px;
width:90%;
overflow:auto;
}
.allRestaurants ul li{
margin-bottom: 10px;
list-style: none;
box-sizing: border-box;
border-radius: 10px;
-webkit-border-radius: 10px;
}
.allRestaurants ul li .imageClass{
float: left;
}
.allRestaurants ul li .imageClass img{
width: 200px;
height: 200px;
}
.allRestaurants ul li .information{
width: 250px;
}
.allRestaurants ul li{
float:left;
clear:both;
}
Updated JS FIDDLE CODE
You need to clear your floats to stop the parent element collapsing. There are a number of ways. This is probably the simplest to understand:
WORKING DEMO
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">nameres</div>
<div class="clearfix"></div> /* Add an empty element with class .clearfix */
</li>
In your CSS:
.clearfix {
clear: both;
}
Or this method:
WORKING DEMO
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
Where the clearfix class would be applied to the parent element(li)
Add overflow: auto; to .allRestaurants ul li, that way the li will know that there are elements inside it that float.
Also, check this updated Fiddle.
Related
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;
}
I've got an issue that I can't resolve.
I have a list of lists, like this one:
<ul id="nav">
<li>
<div class="tabquad">
Item 1
</div>
<ul>
<li>
<a class="item" href="index.jsp?quad=1">
<div class="tabquad">
1.1
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=2">
<div class="tabquad">
1.2
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=3">
<div class="tabquad">
1.3
</div>
</a>
</li>
</ul>
</li>
<li>
<div class="tabquad">
Item 2
</div>
<ul>
<li>
<a class="item" href="index.jsp?quad=7">
<div class="tabquad">
2.1
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=8">
<div class="tabquad">
2.2
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=9">
<div class="tabquad">
2.3
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=10">
<div class="tabquad">
2.4
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=11">
<div class="tabquad">
2.5
</div>
</a>
</li>
</ul>
</li>
</ul>
with this CSS:
#nav, #nav ul {
padding: 0;
margin: 0 auto;
list-style: none;
}
#nav {
width: 45em;
margin: 0 auto;
}
#nav a {
display: block;
width: 15em;
}
#nav li {
float: left;
width: 15em;
}
#nav li ul {
position: absolute;
width: 15em;
left: -500em;
}
#nav li:hover ul {
left: auto;
}
#nav li:hover ul, #nav li.sfhover ul {
left: auto;
}
.item{
color:#0E4063;
text-decoration:none;
}
.tabquad{
color:white;
margin:auto;
position:relative;
border:1px solid #000;
border-color:rgb(82,115,154);
width:200px;
height:30px;
text-align:center;
padding-top:10px;
top:25px;
background-color:rgb(0,56,130);
}
.tabquad:hover{
background-color:rgb(49,87,132);
cursor: hand;
}
My problem is: Item 1 list and Item 2 list are not in the center of my page, they are left-oriented.
I've tried with the center parameter and with the float, without success.
Any suggestion?
You can find a fiddle here: Link
you have to set the width of the #nav to the right value (corresponding to the width of the content). change your example to the following value and the two menus will be centered on the page.
#nav {
width: 30em;
margin: 0 auto;
}
updated jsFiddle: http://jsfiddle.net/F9Wrb/17/
and just to increase cross-browser compatibility, add the following CSS to the parent node of your #nav:
body {
text-align: center;
}
You might want to read about the basics of CSS page centering and page layouts.
Normally, you'd have a "container" <div> that contains the entire page and defines the layout and boundaries of the page. You can call it anything (not necessarily container)
Now, you need to decide what layout fits you best, most commonly used layout sizes can vary anywhere between 960px to 1200px.
general styling for your container would be,
.container{
width:960px; //<-- this can be anything, based on your layout.
margin:0 auto;
}
This ensures your page is centered almost always. You would want to use some kind of a CSS Reset to ensure you reset browser defaults
Here is your solution. I have just changed these two styles. FYI, unless you are using html5, you cannot put div inside <a> tag; it's invalid.
#nav, #nav ul {
padding: 0;
margin: 0 auto;
list-style: none;
text-align:center
}
#nav li {
width: 15em;
display:inline-block;
}
Three possible solutions:
Using absolute positioning and a negative left margin:
#nav {
position: absolute;
left: 50%;
margin: 0 0 0 -15em;
}
Demo 1
Using auto margins with ul width specified:
#nav {
width: 30em;
margin: 0 auto;
}
Demo 2
If you want to use the auto-margin solution and don't want to specify the ul width, declare the LIs as display: inline-block (instead of floating them). This works because the LIs will now be in the page flow instead of out of it, as they are when floated. Answer courtesy of Kashif Raza.
#nav {
margin: 0 auto;
}
#nav > li {
display: inline-block;
width: 15em;
}
Demo 3
#nav a {
display: block;
width: 15em;
margin:auto;
}
#nav>li {
width:50% !important;
}
#nav li:hover ul {
position:relative;
left: auto;
}
Make these changes in your CSS and lists will be in the center of your page.
Just put a width on the surrounding div, then change that to margin:0 auto, which will center it in the page.
.center{
width:400px;
margin:0 auto;
}
And the html, rather than float="center"
<div class="center">
http://jsfiddle.net/F9Wrb/12/
As mentioned, there is no such thing as float="center", or for that matter, float:center;! For advanced positioning of elements, I'd recommend using a CSS framework such as zurb foundation or Gridpak.com
Put this where you want to center:
div align="center"
When inspecting my elements, i noticed my height on the first li is very low.
I'm trying to add a background to the whole li after selecting the "city (2)".
The background fills only City(2) an half of New York. I tried without floating, but then i can't get them lined up.
I've added 2 photos. One where i have floating, and one without.
<ul class="specs">
<li>
<div class="trigger">› City (2)</div>
<div class="cityByLocation">
<ul>
<li class="cityInfo">
<div class="cityName" style="float: left;">
› New York
</div>
<div class="cityDistance" style="float: left;">
430 miles
</div>
<div class="btn-take"></div>
</li>
<li class="cityInfo">
<div class="cityName" style="float: left;">
› Chicago
</div>
<div class="cityDistance" style="float: left;">
430 miles
</div>
<div class="btn-take"></div>
</li>
</ul>
</div>
</li>
<ul>
css:
.cityName{
float: left;
width: 45%;
}
.cityDistance {
float: left;
width: 20%;
}
.specs {
padding: 0;
}
.specs li {
padding: 0;
padding-top: 10px;
list-style: none;
}
.specs ul {
list-style: none;
padding: 0;
}
You need to clear your floats as Eric said. The simplest way to achieve that is by applying overflow: hidden to the container, so:
.cityByLocation {
overflow: hidden;
}
http://jsfiddle.net/a3GLG/
I think this will solve your problem.
The problem happens when a floated element is within a container box, that element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow.
You can use 2 methods to fix it:
{clear:both} or .clearfix
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
source
You can also fix it by setting overflow: hidden to li element. It will clear and auto calculate height.
-- update --
thanks #mickmackusa
https://jsfiddle.net/e94pzbbn/1/
My code is here on jsfiddle
What I can't accomplish is: if you narow the screen size by axis x, my boxes containing the image of santa will "fall" out of the container box below the first container, which is OK (it's ok that the go below), but I would also like the that the div which contains these images "streches" down as well.
I bet I'm missing something in my css part of the code - can you please advise?
Whenever you float some elements the parent of those elements will not take the actual height
so for this you apply a clear fix class for container div of floated elements.
In Css
.clearfix:after {
clear:both;
content:".";
display: block;
height:0;
visibility:hidden;
}
.clearfix {display:inline-block;}
* html .clearfix {height:1%;}
.clearfix {display:block;}
In Html
<div class="clearfix" >
<div class="okvir">
<img src="http://cdn1.iconfinder.com/data/icons/silent_night_icons/128/santa.png" /><br/>CTA 2
</div>
<div class="okvir">
<img src="http://cdn1.iconfinder.com/data/icons/silent_night_icons/128/santa.png" /><br/>CTA 2
</div>
<div class="okvir">
<img src="http://cdn1.iconfinder.com/data/icons/silent_night_icons/128/santa.png" /><br/>CTA 2
</div>
</div>
Here is a css only solution: http://jsfiddle.net/surendraVsingh/Prjd8/6/
CSS
.accContent{
background-color: #F5F5F5;
padding: 5px;
min-height: 160px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
-moz-border-bottom-right-radius:5px;
-moz-border-bottom-left-radius:5px;
-webkit-border-bottom-right-radius:5px;
-webkit-border-bottom-left-radius:5px;
-khtml-border-bottom-right-radius:5px;
-khtml-border-bottom-left-radius:5px;
overflow:hidden; /* Add This */
}
.okvir{
width: 210px;
height: 150px;
text-align: center;
background-color: #CCC;
margin:0 0 20px 20px;/* Modify this to make it look good when resized */
float: left;
border-radius: 5px;
}
<div class="pull_down">
<ul class="zvuc">
<li class="prod">
<div class="conutine">
<div class="prod_name">Asus 15.6-Inch Versati...</div>
<img src="../images/test/lotr.jpg">
<div class="timer"></div>
</div>
</li>
</ul>
</div>
CSS:
div.pull_down
{
padding: 20px 20px 20px 30px;
}
li.prod
{
background: url("../images/test/bg-horizontal.png") no-repeat;
height: 286px;
width: 170px;
text-align: center;
padding-top: 0;
display: list-item;
width: 170px;
min-height: 286px;
float: left;
}
ul
{
list-style: none outside none;
margin: 0;
padding: 0;
text-align: left;
}
When i mouse over the division with firebug, it doesn't contain anything. Why is this?
Your are floating your LI's so you most likely need a "clearfix" on the parent DIV.
A "clearfix" will force an element to self-clear it's children.
http://css-tricks.com/snippets/css/clear-fix/
Demo: http://jsfiddle.net/wdm954/Dxkpx/
You can just add the .group classes to your CSS file and add class="group" to any parent that you need to clearfix.
This is a semantic and scale-able solution. You should avoid adding empty DIVs to your markup or extra CSS to parents (such as floats). That can produce undesirable results and become confusing in the long run.
Hi you need to add <div style="clear:both"></div> after the </ul> to close the float
or to add float: left; to the class .div.pull_down