I'm new to html and css and I'm trying to create a website, part of the code is here:
HTML:
<div class="row">
<div class="circle"></div>
</div>
<div class="row">
<div class="circle"></div>
<div class="circle"></div>
</div>
<div class="row">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
<div class="row">
<div class="circle"></div>
<div class="circle"></div>
</div>
<div class="row">
<div class="circle"></div>
</div>
CSS:
.circle
{
border-style: solid;
border-color: red;
width: 70px;
border-radius: 40px;
float:left;
margin: 2px;
}
.row
{
border-style: solid;
border-color: black;
height: 100px;
width: 700px;
margin: 10px;
}
http://jsfiddle.net/ubd9W/
I'm trying to centre red circles (horizontally and vertically) within the black boxes but I can't seem to manage it. I've tried using 'text-align' and setting the left and right margin to auto but that doesn't work. I also can't use 'absolute' positioning because I have a fixed menu bar at the top of the page and that gets ruined if you scroll.
Any help will be greatly appreciated. Thanks
very simple to understand with the same code you provide you just need to give the parent element a text-align:center; and a position:relative;
.row{
border:4px solid black;
height: 100px;
width: 700px;
margin: 10px;
text-align:center;
position:relative;
}
then set the children margin:10px auto; and display:inline-block;
.circle{
border:4px solid red;
height: 70px;
width: 70px;
border-radius: 40px;
position:relative;
margin:10px auto;
display:inline-block;
}
or if you want more margin between them change margin:10px auto; to margin: 10px 40px;
demo: http://jsfiddle.net/ubd9W/14/
I don't think you can achieve this only with CSS, without hardcoding values.
You can use flexbox http://coding.smashingmagazine.com/2013/05/22/centering-elements-with-flexbox/ (not so good browser support) or a JavaScript solution.
EDIT:
I'm using jQuery.
for three circles:
var rowWidth = jQuery('.row').width();
var circleWidth = jQuery('.circle').width();
var equalSpace = rowWidth - (3*circleWidth);
jQ('.row').css("padding-left", equalSpace + "px").css("padding-right", equalSpace + "px");
for a dynamic number of circles:
var rowWidth = jQuery('.row').width();
var circleWidth = jQuery('.circle').width();
jQuery('.row').each(function(){
var circNumber = jQuery(this).children('.row').length; //this will give you the number of circles inside the current row.
var thisWidth = rowWidth - (circNumber * circleWidth);
jQ(this).css('padding-left', thisWidth + "px").css('padding-right', thisWidth + "px")
})
We iterate through all the row and see how many circles we have in them and multiply theyr number to a circle's width so we can substract the left/right padding.
using flexbox it's by far the best option, but it's now supported by ie<10 http://caniuse.com/#feat=flexbox
If you need it to work on browsers that doesn't support flexbox, the horizontal alignment is easy, you can do it adding a the attributes display: inline on .circle and text-align: center on .row.
http://jsfiddle.net/BTh2t/2/
.circle
{
border-style: solid;
border-color: red;
height: 70px;
width: 70px;
border-radius: 40px;
display: inline-block;
margin: 2px;
}
.row
{
border-style: solid;
border-color: black;
height: 100px;
width: 700px;
margin: 10px;
text-align: center;
}
For the vertical alignment i could make it work using percentages for the height of the circle and i change the box-sizing property and the top and bottom margin, so the percentage assigned make sense and assign position relative to the circle class so we it can use the top property using the half of the remaining percentage ex:
circle height = 70%,
circle top = 15%
http://jsfiddle.net/BTh2t/3/
.circle
{
border-style: solid;
border-color: red;
height: 70%;
width: 70px;
border-radius: 40px;
display: inline-block;
margin-left: 2px;
margin-right: 2px;
position: relative;
top: 15%;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.row
{
border-style: solid;
border-color: black;
height: 100px;
width: 700px;
margin: 10px;
text-align: center;
}
keep in mind that with this approach if you increase the height of the .row class the height of the circle will increase automatically.
I hope it helps!
Another simple solution with the display:table property as well:
HTML
<div class="row">
<div class="wrapper">
<div class="circle"></div>
</div>
</div>
<div class="row">
<div class="wrapper">
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
<div class="row">
<div class="wrapper">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
<div class="row">
<div class="wrapper">
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
<div class="row">
<div class="wrapper">
<div class="circle"></div>
</div>
</div>
CSS to add:
.wrapper {
display: table;
margin: auto;
}
Link to Fiddle
For horizontal aligning: use text-align: center; + display:inline-block;
For vertical aligning: use line-height + vertical-align: middle;
FIDDLE
CSS
.circle
{
border-style: solid;
border-color: red;
height: 70px;
width: 70px;
border-radius: 40px;
margin: 2px;
display:inline-block; /* for horizontal alignment */
vertical-align: middle; /* for vertical alignment */
}
.row
{
border-style: solid;
border-color: black;
height: 100px;
line-height: 100px; /* for vertical alignment */
width: 700px;
margin: 10px;
text-align: center; /* for horizontal alignment */
}
Related
I'm trying to shrink elements smaller than their paddings.
But I cannot.
I want box to be 5px. But it is 80px because I have padding: 40px
How can I make box 5px without removing paddings?
I tried to make it flex. But it didn't help.
.wrapper {
width: 100%;
white-space: nowrap;
/* display: flex; */
}
.box {
/* flex: 1; */
width: 5px;
height: 50px;
background-color: red;
padding: 40px;
display: inline-block;
border: 1px solid green;
box-sizing: border-box;
max-width: 5px;
}
<div class="wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
If i am not wrong and you want to make your .box div to smaller size use below css.
.box {
width: 5px;
height: 50px;
background-color: red;
padding: 40px;
display: inline-block;
border: 1px solid green;
box-sizing: border-box;
min-width: 10px;
transform: scale(0.5); //this will make you box smaller without removing the padding
}
This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 6 years ago.
Why is there a margin between divs? I tried to remove it by different methods but nothing worked. I had to reduce their width to stack them in rows.
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
display: inline-block;
margin: 0px;
background-color: #f1f1f1;
width: 248px;
height: 250px;
border: 0 !important;
font-size: 0;
}
<div class="wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
Make the width of .box 250px and add an attribute of 'float: left' to .box
.box{
display: inline-block;
margin: 0px;
padding: 0;
background-color: #ff9900;
width: 250px;
height: 250px;
float: left;
}
Fiddle
Due to your display: inline-blocks, the white spaces appear in between your block elements.
There are many resolutions to the same, refer to David Walsh's blog
What I would prefer to do here is use float instead of display: inline-block.
Refer code:
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
float: left;
margin: 0px;
background-color: #f1f1f1;
width: 248px;
height: 250px;
}
<div class="wrapper">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
The problem is that there are spaces between the div's. Two possible solutions:
1:
<div class="wrapper">
<div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div>
</div>
-
.box { display: block; } // not multiple elements in one line, if you want this
2:
.wrapper { font-size: 0px; }
.box { display: block; } // not multiple elements in one line, if you want this
Its not margin what is causing space between two div its because of display:inline-block which you have added to box class, just add float: left; to same and it will go away.
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
display: inline-block;
margin: 0px !important;
background-color: #f1f1f1;
width: 248px;
height: 250px;
border: 0 !important;
float: left;
}
<div class="wrapper">
<div class="box" style="background: rebeccapurple;">
</div>
<div class="box" style="background: orange;">
</div>
<div class="box" style="background: orange;">
</div>
<div class="box" style="background: rebeccapurple;">
</div>
</div>
Try setting border: 0 !important on all divs affected, once I had a similar problem and found that the divs were inheriting a 1px border that was breaking the width.
You are displaying them as inline blocks, so the white space between them in the formatting of your code is still being displayed just as it would had they been any other inline element.
You need to reformat your code, or set the wrapper to have a zero font size so they do not get rendered.
Try using
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: auto;
padding: 0;
display: block;
background: green;
}
.box{
display: block;
margin: 0px;
width: 248px;
height: 250px;
background: red;
padding: 0;
float: left;
}
Display: inline-block creating that margin.
Or may be you could try
.wrapper{font-size: 0;}
.box{ display:inline-block;}
I have an outer DIV (4) and three inner DIVs (1-3). I don't care about width here. It's all about height and vertical centering. I want the outer DIV (4) to get the height of the highest inner DIV (2 in row A). More importantly I want the other inner DIVs (1 and 3 in row A) to get centered vertically (in relation to the height of the outer DIV that has the same height as the highest inner DIV).
The contents of the DIVs are dynamic (compare row A and B) therefore I don't know which inner DIV will be the highest. Until now I used a jQuery solution that set the margin-top of the smaller DIVs (red marks) but I would like to solve it in plain CSS now.
This is easy using a flexbox - the property align-items: center produces the desired result - see a demo below:
.wrapper {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid red;
}
.wrapper > div {
border: 1px solid;
}
<div class="wrapper">
<div class="one">Some text here</div>
<div class="two">
There is a lot of text here
<br>There is a lot of text here
<br>There is a lot of text here
<br>
</div>
<div class="three">Some
<br>text</div>
</div>
.outera {
border:solid 1px #333;
}
.outera div {
width:20px;
border-radius: 16px;
background-color:#212121;
margin:10px;
display:inline-block;
vertical-align: middle;
}
.outera .a1 {
height:20px;
}
.outera .a2 {
height:80px;
}
.outera .a3 {
height:50px;
}
<div class='outera'>
<div class='a1'></div>
<div class='a2'></div>
<div class='a3'></div>
</div>
You can use CSS Flexbox.
In the below snippet I've used display: inline-flex;. Have a look at the snippet below:
body {
padding: 20px;
}
.outer {
display: inline-flex;
align-items: center;
justify-content: center;
border: 1px solid #000;
}
.inner {}
.a .element {
width: 20px;
height: 20px;
border-radius: 50%;
background: red;
}
.b .element {
width: 20px;
height: 50px;
border-radius: 50%;
background: green;
}
.c .element {
width: 20px;
height: 30px;
border-radius: 50%;
background: blue;
}
<div class="outer">
<div class="inner a">
<div class="element"></div>
</div>
<div class="inner b">
<div class="element"></div>
</div>
<div class="inner c">
<div class="element"></div>
</div>
</div>
Hope this helps!
I have the following structure and as you will see in this JSfiddle, the height of the .parent elements is not auto-expanding to the total height of their content.
What am I doing wrong ??
CSS
.container {
float: left;
width: 100%;
height: auto;
}
.parent{
position: relative;
margin: 0 auto;
max-width: 200px;
height: auto;
border: 10px solid transparent;
border-top-width: 50px;
border-bottom-width: 50px;
width: 100%;
box-sizing:border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
}
.float{
float: left;
width: 100%;
position: relative;
height: 70px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
background-color: blue;
}
.float:nth-child(2){
background-color: red;
}
HTML
<div class="parent">
<div class="float"></div>
<div class="float"></div>
</div>
<div class="parent">
<div class="float"></div>
<div class="float"></div>
</div>
<div class="parent">
<div class="float"></div>
<div class="float"></div>
</div>
If you add an
overflow: hidden;
to your class parent, this will start a new "block formatting context". Then the parent will span the complete height of all children elements.
http://colinaarts.com/articles/the-magic-of-overflow-hidden/
use
min-height
instead of
height
in . float class
I have 3 divs but the 3rd div comes down when i resize the browser.
How can i still display them inline when browser resize?
I can do it by changing the width of my container
but i want it to be 100%
This is my Code:
.box{
float :left;
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container{
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Thanks :)
You need to use the CSS3 flexible box layout so that the elements do not wrap to next line and resize accordingly. The default values of flex-direction is row and flex-wrap is nowrap. So you need not set the values here.
.box {
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container {
width: 100%;
display: flex; /* Added */
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
You can use like this -
.box{float:left;
max-width: 250px;
min-width: 20%;
/*max-width: 250px;*/
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.container{
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Instead of specifying .box {width: 250px} make it in percentage, remove margin, add box-sizing for padding issue:
.box {
float: left;
width: 33.33%;
/* Instead of old 250px; */
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
box-sizing: border-box;
/* To include padding to width */
/*margin-right: 26px; Can't use margin for responsive gaps*/
}
.container {
width: 100%;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
Added an inner div
bodY{
margin: 0;
}
.box{
float :left;
width: 250px;
background: #f6f6f6;
border: 1px solid #e0e0e0;
min-height: 150px;
position: relative;
padding: 20px;
margin-right: 26px;
}
.box:nth-child(3){
margin-right: 0;
}
.container{
overflow-x: hidden;
width: 100%;
}
.inner{
margin-right: -400px;
width: 928px;
}
<div class="container">
<div class="inner">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br style="clear: left;">
</div>
</div>
I think it is easy if you try bootstrap.
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-4 col-sm-4 col-xs-4">
//first div contents
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
//second div contents
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
// third div contents
</div>
Note you should include required bootstrap files to use this classes