i'm trying to div start in center like float right or left but i didn't do this.
Please help me?
HTML
<div class="conta">
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
</div>
CSS
.conta{width:100%; position:relative; text-align:center}
.conta .box{float:left; width:100px; height:100px; border:1px solid #000; margin:5px;}
I want to start box in center and inline.
there is many possible ways.
One
simplest way this is: margin:0 auto but you need to have fixed width.
DEMO
Two
you can set display:inline-block; to inner element and set text-align:center to the container.
DEMO
HTML
<div>
<div></div>
</div>
CSS
div{
text-align:center;
}
div div{
display:inline-block;
background:gray;
width:200px;
height:100px;
}
Three
set position:relative to the container and position:absolute to the inner element, then set left:50% and margin-left:-100px (margin-left must be half of the width, in this case 100px).
DEMO
CSS
div{
position:relative;
}
div div{
position:absolute;
left:50%;
margin-left:-100px;
background:gray;
width:200px;
height:100px;
}
For block-level items, you can use auto margins to do this (when the item is within the document flow. When you float it, position it other than static, you are taking it out of the document flow.)
div.my-container { margin: 0 auto; }
Will do the trick :)
You can do like this also,
<div id="main" style="text-align:center">
<div id="inner" style="text-align:left;">
I want to be center left.......
</div>
</div>
Now main(outer div) aligned to center and the content div(inner) is aligned left in center. This works fine in IE. You should add margin:0 auto; to make this work in all other browsers.
.myclass{
margin:0px auto;
text-align:center
}
Related
This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 6 years ago.
i have a pink colored container and inside it there are 2 divs, red and green placed side by side with display:inline-block; rule in css.i need both this divs to take 50% width so that they appear as a single div.but when i set the width to 50% the green jumps below red div.when i set width to 49% it jumps to same line but there are gaps in between which is not what i want, some body help.
i need them to stick togather in a line as if they appear as a single div.
i'll put my code pen link here...
http://codepen.io/ShamZ/pen/gLXBow
HTML
<div class="container">
<div class="box">
</div>
<div class="box2">
</div>
</div>
css
.container{
width:800px;
height:800px;
background-color:pink;
}
.box{
display:inline-block;
width:50%;
height:50px;
background-color:red;
}
.box2{
display:inline-block;
width:50%;
height:50px;
background-color:green;
}
the Problem with our Code is that HTML detect whitespace between the box elements in the container - and therefor it seems like theres not enough space in the container for 2 Divs with 50% width. - Set them to 48% or even smaller and u will see that they will fit among a line.
One Solution can be:
.container{
width:800px;
height:800px;
background-color:pink;
display:inline-block;
font-size: 0;
}
.box{
display:inline-block;
width:50%;
height:50px;
background-color:red;
}
.box2{
display:inline-block;
width:50%;
height:50px;
background-color:green;
}
<div class="container">
<div class="box">
</div>
<div class="box2">
</div>
</div>
and then set another font-size in child elements
This is a known issue where white-space between inline-block elements cause margins to appear. Take a look at this example (fixed)
.container{
width:800px;
height:800px;
background-color:pink;
}
.box{
display:inline-block;
width:50%;
height:50px;
background-color:red;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box:nth-of-type(2) {
background-color: green;
}
<div class="container">
<div class="box">
</div><div class="box">
</div>
</div>
Switch to floats, ala http://codepen.io/davewallace/pen/aBVQLN
inline-block can result in odd spacings that may need further workarounds.
When using floats you can achieve the desired effect simply, you may need to investigate the use of 'clearfix' on containers wrapping your floated elements.
Add font-size:0 to parent element .container
.container{
width:800px;
height:800px;
background-color:pink;
font-size:0;
}
.box{
display:inline-block;
width:50%;
height:50px;
background-color:red;
}
.box2{
display:inline-block;
width:50%;
height:50px;
background-color:green;
}
<div class="container">
<div class="box">
</div>
<div class="box2">
</div>
</div>
Im trying to overlap an element with a absolute position .tools outside of its uppermost parent element #canvas_area which has an overflow:scroll, however this doesn't seem to work, but it does work if you remove the overflow:scroll attribute.
HTML:
<div id="canvas_area">
<div class="container">
<div class="blocks">
<div class="tools">
x
</div>
</div>
<div class="blocks">
<div class="tools">
x
</div>
</div>
</div>
</div>
CSS:
#canvas_area{
overflow:scroll; // remove this and it works
height:400px;
width:400px;
background-color:black;
margin: 0 auto
}
.container{
position:relative;
}
.blocks{
overflow:hidden;
width:200px;
height:100px;
background-color:white;
margin-bottom:10px;
}
.tools{
position:absolute;
color:green;
left:-40px;
}
I need #canvas_area to have a scroll, is their a way around this?
here is the jsfiddle: https://jsfiddle.net/2exn6oq5/
remove overflow:scroll from #canvas_area and you will see the green x outside the body, which is what I want it to do.
I had three divs inside a main div with id main_div and has css already as below
<div id="main_div" style="height:10px; line-height:50px; margin-top:1px; position:relative;>
</div>
I just want to insert three divs in the main div as below
<div id="main_div" style="height:10px; line-height:50px; margin-top:1px; position:relative;>
<div>
Div One should be Left(breadcrumb_text)
</div>
<div>
Div Two should be Center(dropdownlist)
</div>
<div>
Div Three should be Right(Pagination)
</div>
</div>
So i want the div format to display the text like
breadcrumb_text dropdownlist Pagination
I tried with different css by using position attribute and various css options but could n't able to align them in a horizontal line with one div as left , one div as center and other div to right.
So can anyone let me know me know the css to place them in an horizontal line ?
This maybe help you Fiddle
#main_div > div {
width: 33%;
float: left;
}
I have modified your code little bit with spacing equally for each div and removed Position in the Main div.
Sometimes position will overlap with other div (position) based on z-index value. so if you really need use position unless not required.
#main_div{
height:10px; line-height:50px; margin-top:1px;
box-sizing:border-box;
}
#main_div > div{
width:31.1%;
float:left;
box-sizing:border-box;
border:1px solid grey;
margin-right: 10px;
display:inline-block;
}
#main_div > div:first-child{
margin-left:10px;}
<div id="main_div">
<div>
Div One should be Left(breadcrumb_text)
</div>
<div>
Div Two should be Center(dropdownlist)
</div>
<div>
Div Three should be Right(Pagination)
</div>
</div>
I think this is what you are asking for
JSFiddle
CSS
body
{
margin:0%;
}
.main_div
{
display:block;
margin:0% 5%;
width:90%;/*Just random, modify as per your requirement*/
height:300px; /*Just random, modify as per your requirement*/
background:#eee;
position:relatve;
}
.left-div, .center-div, .right-div
{
display:inline-block;
height:100%;
position:relative;
float:left;
width:33%;
border:1px solid #000;
text-align:center;
padding-top:5px;
}
HTML
<div class="main_div">
<div class="left-div">
Div One should be Left(breadcrumb_text)
</div>
<div class="center-div">
Div Two should be Center(dropdownlist)
</div>
<div class="right-div">
Div Three should be Right(Pagination)
</div>
</div>
this is my page:
http://bit.ly/Ti6Ptr
I would change this block style from
to
How I have to do in my css?
Thanks in advance.
Just add width: 190px; to you css selector .liRecentPost a
This is how it will look
The easiest way would be for you to set the size of the div elements containing the text. Then the text will automatically flow to the next line when needed.
.imageText
{
width:someWidth;
}
<img ... />
<div class="imageText">
</div>
Without html and css isn't easy but I think that in you text ("li" or "p") you have to set a width and display block and insert into a div to align all image
example:
<div class="block">
<div class="firstCol">
<img src="img/yourimage.jpg" />
</div>
<div class="secondCol">
<p>some text</p>
</div>
</div>
css
.block{
width:600px;
height:auto;
float:left;
}
.firstCol{
width:300px;
height:auto;
float:left;
}
.secondCol{
width:300px;
height:auto;
float:left;
}
.text{
width:300px;
display:block
}
N.B. the dimension is not real is only an example
just add little css to your li
#recent_posts .ulRecentPost .liRecentPost {width:200px; padding:5px;}
I'm building a sort header for a table that's built entirely with divs (ie. no table tags). Inside the sort header, I have two divs, the sort title and the sort direction. I want to have the header horizontally centered. I have a jsfiddle that looks like this:
<div class="SomeHeader">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
<div class="SomeOtherHeader">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>
.SomeHeader{
width:90px;
float:left;
background:red;
color:white;
line-height:20px;}
.SomeOtherHeader{
width:120px;
float:left;
background:black;
color:white;
line-height:20px;}
.HeaderTitle{float:left;}
.HeaderSort{float:left;}
What's the way to do this cleanly? I know I can specify the margin and width of each element to get the desired effect but when the value of these header will change then the alignment will not be centered. I know I can also do this with jQuery by looping over the header and calculating the width of headers and titles and programmatically setting the margins of each header. I'm looking to see if there's a more general CSS-based approach that will center both the HeaderTitle and the HeaderSort inside the SomeHeader classes.
Thanks for your suggestions.
http://jsfiddle.net/7R9WW/13/
Same markup, but display: inline-block on the inner divs and text-align: center on the field containers.
.SomeHeader{
width:90px;
float:left;
background:red;
color:white;
line-height:20px;
text-align:center;}
.SomeOtherHeader{
width:120px;
float:left;
background:black;
color:white;
line-height:20px;
text-align:center;}
.HeaderTitle{display:inline-block;}
.HeaderSort{display:inline-block;}
The HeaderSort div seems unnecessary. Could it not look like this http://jsfiddle.net/7R9WW/7/
Updated answer: http://jsfiddle.net/7R9WW/14/
Use a wrapper!
<div class="wrap">
<div class="SomeHeader">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
<div class="SomeOtherHeader">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>
</div>
And the CSS:
.wrap {width: 220px; margin: auto;}
wrap these two header divs with another div and then giv that div a margin:0 auto.
<div class="SomeHeader">
<div id="wrapper">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
</div>
<div class="SomeOtherHeader">
<div id="wrapper">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>
</div>
CSS:
#wrapper{
width: 100px;
margin: 0 auto;
overflow: hidden;
}
The width should be the sum of the width of the two contained divs , so you'll need to define a width to them.