how to keep floated item centered within container - html

OK, I've gotten the prelim version of my page started, but I'm having a problem with two floated div's that are wrap in header tag. Basically, I want the two rectangles to center within the containing div tag. One of the rectangles overlaps the other. I had to us positioning to be able to expand them within the container other-wise the second would jump below the first.
Here's what I've have so far.
<div id="div1" class="fluid">
<header id="headGraphics">
<div id="headRectangle1">This will be an image.</div>
<div id="headRectangle2">"This will be text adjusted using opacity."
</div>
Here is the css for the page - I have a follow-up question after we get this solved.
.gridContainer.clearfix #headGraphics {
margin-top: 0px;
margin-right: auto;
margin-left: auto;
margin-bottom: 0px;
font-family: "monotype corsiva";
font-size: 20px;
font-weight: 800;
width: 950px;
text-align: center;
}
.gridContainer.clearfix #headGraphics #headRectangle1 {
float: left;
width: 350px;
height: 75px;
position: relative;
border: medium solid #000000;
-webkit-box-shadow: 3px 3px 3px 1px #FF7878;
box-shadow: 3px 3px 3px 1px #FF7878;
margin-left: auto;
margin-right: auto;
display: inline-block;
}
.gridContainer.clearfix #headGraphics #headRectangle2 {
background-color: #FFAAAA;
float: left;
/*margin-right: 50px;*/
width: 350px;
height: 75px;
position: relative;
top: -50px;
right: 0px;
text-align: center;
clear: both;
left: 100px;
margin-left: auto;
margin-right: auto;
display: block;
}
.gridContainer.clearfix #headGraphics:after {
content: " ";
display: table;
clear: both;
}
I can't remove the position tags because they give me the layout that I'm am trying to accomplish.
Let ma know if you need more info. Thank you in advance. And yes, I have searched this page and others to find a solution, but none seem to apply to my particular situation.
let me clear a few things up... and before I go any further, most of my (98%) selectors are in the boiler plate template. That being said, here the computed effects per selector:
.gridContainer.clearfix #headGraphics;
width 950px, margin 0 auto, font-family monotype weight 800px size 20px, text-align center.
.gridContainer.clearfix #headGraphics #headRectangle1;
width 350px, height 75px, display inline-block, margin rt & lft auto, position relative, box-shadow (which isn't working properly)
.gridContainer.clearfix #headGraphics #headRectangle2
width 350px, height 75px, display inline-block, position relative, top -50px, rt 0px, bot 0px, left 100px (this is to bring object up and offset from rectangle), float left, clear both, text-aligh center.

I would suggest removing the float attributes from both, then just setting both items display as inline-block, you will need to specify width and height on both cases, then apply text-align center to the parent, that will allow the child to be centered to the parents available area.
The Display: inline-block will give the two elements the possibility to behave not just like a block element, it will be both, block and inline, so you will be able to use attributes for both at the same time.
If you need an example, I can provide you with one, Just let me know!
EDIT...
Here is a working example
My JSFiddle
http://jsfiddle.net/dq185dw9/
My CSS
#headGraphics {
margin-top: 0px;
margin-right: auto;
margin-left: auto;
margin-bottom: 0px;
font-family: "monotype corsiva";
font-size: 20px;
font-weight: 800;
width: 950px;
text-align: center;
outline: red dashed 1px;
padding: 35px; /* remove or change if needed */
}
#headGraphics [id*="headRectangle"] {
width: 350px;
height: 75px;
position: relative;
border: medium solid #000000;
-webkit-box-shadow: 3px 3px 3px 1px #FF7878;
box-shadow: 3px 3px 3px 1px #FF7878;
display: inline-block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
-khtml-box-sizing: border-box;
box-sizing: border-box;
margin: 0px 25px;
line-height: 75px; /* remove or change if you want to have more than one line of text */
}
My HTML
<header id="headGraphics">
<div id="headRectangle1">This will be an image.</div>
<div id="headRectangle2">"This will be text adjusted using opacity.</div>
</header>

Related

How to indent both sides of a bordered box without changing text

I have this bordered box:
.box {
width: 100%;
height: 100%;
padding: 10px;
border: 2px ridge white;
border: 2px ridge rgb(50, 50, 50);
border-radius: 1em;
/*8px*/
padding-left: 15%;
padding-right: 15%;
padding-top: 5%;
padding-bottom: 5%;
}
<div class="box">Hello World!</div>
If I change width to a smaller percentage (e.g. width: 90%), the right edge moves left, but the left edge does not move, and the text margins also move in. I want both edges to move -- the left edge should move right by the same amount as the right edge moves left -- and I don't want the text margins within the boxes to move at all.
How can I do what I described with a bordered box?
I don't think I understood you well but here are two solutions for you.
Solution 1:
You can use margin but you'll have to specify a width.
.box {
width: 50%; /** setting a width is required **/
margin: auto;
border: 2px ridge rgb(50, 50, 50);
border-radius: 1em;
padding-left: 15%;
padding-right: 15%;
padding-top: 5%;
padding-bottom: 5%;
}
<div class="box">Hello World!</div>
Solution 2:
You can use transform property by the help of translate3d function. You don't have to specify a width. For example, you can declare the display as inline-block to make the width as the content's original width.
You can just use translateX function instead of translate3d and it should work the same way. Also, setting a width will give the same results.
.box {
/** no width is specified but you can set one **/
display: inline-block; /** the width now equals the content width **/
position: relative; /** required to use left property **/
left: 50%;
transform: translate3d(-50%, 0, 0);
border: 2px ridge rgb(50, 50, 50);
border-radius: 1em;
padding-left: 15%;
padding-right: 15%;
padding-top: 5%;
padding-bottom: 5%;
}
<div class="box">Hello World!</div>
Hope I pushed you further.
If you shrink width by 10% just add margin-left:5%; better than that is the following:
.box {
position:absolute;
width: 90%;
left:50%;
margin-left:-45%; /*this must be always 1/2 of the width and negative*/
height: 100%;
padding: 10px;
border: 2px ridge white;
border: 2px ridge rgb(50, 50, 50);
border-radius: 1em;
/*8px*/ padding-left: 15%;
padding-right: 15%;
padding-top: 5%;
padding-bottom: 5%;
}
This way the div will be always centered relatively to its parent element. Hope it helps.
I try set margin: 0 auto; to .box class as lucifer63 said and it worked for equals external borders. And how you do not want the text margins inside the box do not move, set fix padding how padding: 32px. You can also use
display: flex; align-itens: center; justify-content: flex-start; and put a height to .box

Styling progress bar - calculating width

I have the following code:
.mod-prb {
display: block;
width: 250px;
height: 35px;
border: 2px solid #809097;
border-radius: 8px;
padding: 3px;
}
.mod-prb > div {
display: block;
height: 20px;
height: 30px;
border: inherit;
border-radius: 8px;
text-align: right;
padding: 0 10px;
}
<div class="mod mod-prb">
<div class="perc"></div>
</div>
The problem is that the <div class="perc"> can go up to width:95%;. How would I go about calculating pixels so that I can use JS 1%-100%. To clarify: I'm adding width with JS, so that's not an issue.
Why this happens
This issue is happening because you are setting the width to 100%, but the inner box also has a padding of 10px (in left and right) and a border of 2px. That makes it have an actual width of 100% of its parent width + 20px (10px margin on both sides) + 4px (2px border on both sides).
How to fix it
You could fix it in different ways. The easiest one would be to use box-sizing with a value of border-box:
The width and height properties include the padding and border, but not the margin.
The code would look like this (note how the height changes too):
.mod-prb {
display: block;
width: 250px;
height: 35px;
border: 2px solid #809097;
border-radius: 8px;
padding: 3px;
}
.mod-prb > div {
display: block;
height: 35px;
width:100%;
border: inherit;
border-radius: 8px;
text-align: right;
padding: 0 10px;
box-sizing:border-box;
}
<div class="mod mod-prb">
<div class="perc"></div>
</div>

Auto set height of DIV without JQuery

I have two div elements. Second is inside in first element.
In second I display some text. For second div I set height to auto and when I put more text in div height is greater. Also I set height for first div to auto, but first div has always same height.
How I can set height of DIV to be dependable of number of text rows?
<div class="first-div">
<div class="second-div">
</div>
</div>
.first-div {
background-color: #f5f5f5;
margin-top: 5px;
margin-left: 5px;
margin-right: 5px;
border-radius: 6px;
border: 1px solid #b8b8b8;
text-align: justify;
padding: 4px 4px 4px 4px;
word-wrap: break-word;
height: auto;
min-height: 75px;
}
.second-div {
width: 30%;
float: right;
font-size: 9px;
height: auto;
}
Add overflow:hidden to .first-div.
You may want to check out this question: How does CSS 'overflow:hidden' work to force an element (containing floated elements) to wrap around floated elements?
Demo 1
add overflow: auto to outer div (.first-div)
css
.first-div {
background-color: #f5f5f5;
margin-top: 5px;
margin-left: 5px;
margin-right: 5px;
border-radius: 6px;
border: 1px solid #b8b8b8;
text-align: justify;
padding: 4px 4px 4px 4px;
word-wrap: break-word;
height: 100%;
min-height: 75px;
overflow:auto; /* added */
}
.second-div {
width: 30%;
float: right;
font-size: 9px;
height: auto;
}
Demo 2
or you can add div to the html and set its style as clear: both
css
.clear {
clear: both;
}
html
<div class="first-div">
<div class="second-div"></div>
<div class="clear"></div>
</div>
You can remove the min-height from your .first-div and apply overflow: hidden check out the fiddle, I think this is what you want.
In the .second-div you can change the height with min-height. In the fiddle I have it at 300px.
http://jsfiddle.net/wcnbq9xc/

Inner DIV seems to have bigger bottom margin

I do not understand why in this simple code my .slot or .card classes seems to have a bigger margin/distance to their border at the bottom than at the top.
Thanks in advance,
JsFiddle: http://jsfiddle.net/Tighttempo/LgeAf/
<div id="hand">
<div class="card" id="card1"></div>
<div class="card" id="card2"></div>
<div class="card" id="card3"></div>
<div class="card" id="card4"></div>
</div>
<div id="playfield">
<div class="slot" id="slot1"></div>
<div class="slot" id="slot2"></div>
<div class="slot" id="slot3"></div>
<div class="slot" id="slot4"></div>
</div>
The CSS:
#hand{
text-align: center;
width: 320px;
border: solid black 3px;
padding: 5px;
}
.card{
display: inline-block;
width: 60px;
height: 90px;
border-radius: 5%;
background: teal;
margin: 0px 5px 0px 5px;
}
#playfield{
width: 320px;
text-align: center;
border: solid black 3px;
padding: 5px;
}
.slot{
display: inline-block;
width: 60px;
height: 90px;
border-radius: 5%;
border: dashed grey 2px;
margin: 0px 5px 0px 5px;
}
Thanks in advance!
If you are not comfortable with making the font-size:0 then here is a solution that i personally prefer.
Display:inline-block is tricky and has strange issues with margins. What i personally do is, i use float instead of inline-block. See this :
.card{
width: 60px;
height: 90px;
border-radius: 5%;
background: teal;
margin: 0px 10px;
float:left;
}
.slot{
width: 60px;
height: 90px;
border-radius: 5%;
border: dashed grey 2px;
margin: 0px 8px;
float:left;
}
What i did is, i added float:left to your .slot and .card and then created a new class .cls(clear:both) and applied that in the div structure. See if this helps.
http://jsfiddle.net/LgeAf/3/
Inline-block elements are tricky - because they are not treated as block elements when it comes to positioning them in the document flow. Their positions and spacings are influenced by CSS properties that control text, like line-height, word-spacing, letter-spacing and font-sizes.
If you set font-size in the parent containers, #card and #playfield, to 0, you will remove the extra bottom margin. See fiddle - http://jsfiddle.net/teddyrised/GwqcV/
#hand, #playfield {
font-size: 0;
}
The drawback of this method is that you will have to redeclare the font-size in the child elements if you are using relative font sizes, like ems.

Floating two 50% width divs with a 10px margin between

I want to float a pair of fluid divs across my page, each taking up half of their container's width, but with a margin of 10px between them. I've done this JSFiddle http://jsfiddle.net/andfinally/sa53B/5/, and here's the HTML:
<div class="clearfix">
<a class="prev" href="#">Previous</a>
<a class="next" href="#">Next</a>
</div>
And CSS:
.prev {
background: black;
color: white;
font-size: 16px;
margin-bottom: 10px;
display: block;
float: left;
box-sizing: border-box;
width: 50%;
margin-right: 5px;
}
.next {
background: black;
color: white;
font-size: 16px;
margin-bottom: 10px;
display: block;
float: right;
box-sizing: border-box;
width: 50%;
margin-left: 5px;
}
The box-sizing rule isn't enough to make this work - the divs are more than 50% wide. In one of the answers to this question somebody suggested using CSS display-table. Can anyone explain how to make that work in this situation?
Thanks!
You can either a) lower 50% to 48% and make the margin 2% or b) use CSS3 calc, which is not supported everywhere, but should be an option in the near future. (Specifically, when IE8 is off the table) (See http://caniuse.com/#feat=calc for compatability)
Example using percentages: http://jsfiddle.net/sa53B/9/
.prev {
background: black;
color: white;
font-size: 16px;
display: block;
float: left;
box-sizing: border-box;
width: 48%;
margin: 0 2% 10px 0
}
.next {
background: black;
color: white;
font-size: 16px;
display: block;
float: right;
box-sizing: border-box;
width: 48%;
margin: 0 0 10px 2%
}
Example using calc: http://jsfiddle.net/sa53B/6/
.prev {
background: black;
color: white;
font-size: 16px;
display: block;
float: left;
box-sizing: border-box;
width: 47%;
width: -webkit-calc(50% - 5px);
width: calc(50% - 5px);
margin: 0 5px 10px 0;
}
.next {
background: black;
color: white;
font-size: 16px;
display: block;
float: right;
box-sizing: border-box;
width: 47%;
width: -webkit-calc(50% - 5px);
margin: 0 0 10px 5px;
}
Margin will add to your containers width. If you are using a width that is based on a percentage you should set your margin value a percentage as well.
For example, if you want two 50% divs. You need to account for the margin too. In order to do so, you need to subtract the margin from the total width. If you want 1% margin left and right, thats a total of 2% you need to remove from the total width.
div {
float: left;
width: 48%;
margin: 0 1%;
}
your updated fiddle: http://jsfiddle.net/sa53B/8/
Recently in my task i need two float columns with 8px fixed margin between them.
So, i used border and box-sizing properties without any calc magic.
So, the solition is:
.wrapper__col {
width: 50%;
box-sizing: border-box; // used to change box-model
overfow: hidden; // clearfix hack
}
.wrapper__col:nth-child(odd) {
float: left;
border-left: 4px solid transparent;
}
.wrapper__col:nth-child(even) {
float: right;
border-right: 4px solid transparent;
}
<div class="wrapper">
<div class="wrapper__col">1</div>
<div class="wrapper__col">2</div>
</div>
So, that's all ;)
It's late but someone might be interested in this way to fix :
Wrap the elements you wish to display in columns with divs :
<div class="left"><a ....></a></div>
<div class="right"><a ....></a></div>
And just set those styles :
.left {
float:left;
width:50%;
}
.right {
float:right;
width:50%;
}
Whatever the margin of divs contents it won't affect the 50% width.
I used to proceed this way before hearing of usefull css calc.
See JSFiddle
What you are trying to do does not work with fixed margins. You need to calculate using percentage margins.
Box sizing only affects padding and border space, not margin space. So 50% + 50% =100% +5px+5px > 100%.
Use % margins and your problem is solved.
Here's how I do it, but it uses variable gap between:
css:
.left {
float:left;
width:59%;
margin-right: 1%;
background-color:red;
}
.right {
float:left;
width:39%;
margin-left: 1%;
background-color:blue;
}
html:
<div>
<div class="left">left</div>
<div class="right">right</div>
</div>
jsfiddle:
http://jsfiddle.net/gkmjLfgx/
Sometimes you want the same 10px spacing horizontally and vertically and still have equally sized columns.
You can do this by adding a "border-left: 10px solid white" to an extra DIV inside each column and add a "margin-left: -10px" to the columns container to eat up the border of the left column.
fiddle 128psahu