Im trying to position 3 div block horizontally but the 3 block positions itself on a new line.
Also when resizing I would like them not to move.
.left1 {
padding: 9px;
border: 1px solid #e7e7e7;
float: left;
margin-right: 40px;
text-align: center;
}
.left2 {
padding: 9px;
border: 1px solid #e7e7e7;
float: left;
margin-right: 40px;
}
.right1 {
padding: 9px;
border: 1px solid #e7e7e7;
float: left;
margin-right: 40px;
}
Rest of the code
http://jsfiddle.net/EWuR8/
the boxes are too wide for the container. i recreated here using only the 3 blocks and it works fine. just reduce the container width.
http://jsfiddle.net/zy4cN/
.block1,.block2,.block3{
float:left;
padding: 9px;
border: 1px solid #e7e7e7;
float: left;
margin-right: 40px;
}
.nomarg{margin-right:0!important;}
Provide some percentage widths. There is a catch here, in that you have to calculate other things into your widths. For example if you have padding, borders or margin, the width or thickness of those have to be considered as well.
For instance consider the following situation:
You have one containing box which is 200PX wide. You have two smaller boxes that you want to distribute evenly inside that 200 px, which means each of the two smaller boxes should be 100PX wide since 100PX + 100PX = 200PX the situation works fine.
Now let's assume you want a 1PX border around those boxes and a 10PX margin space between them. If you use 100PX for your box width they fail to sit next to each other. Why? If you consider 1PX of border on the left and 1PX of border on the right, of both boxes, thats 4PX of total border width. Then 10PX of margin space. If you use 100PX for the width of the boxes then you have 100+100+4+10 = 214PX Since 214PX is more than 200PX the floats break.
To make the above work, you must adjust the width of your boxes to be 93PX each. Re-calculating it per box... 93PX + 2PX + 5PX = 100PX per box at 2 boxes 100PX * 2 = 200PX.
Conceptual proof aside...
Each of your 3 boxes have an image inside them which is set to 500PX wide. In order to fit all 3 you would need at least 1500PX worth of space not including paddings, margins or borders.
By simply changing the width="500px" to width="100PX" makes them small enough that they stack next to each other fine. See this fiddle: http://jsfiddle.net/EWuR8/2/
Note however that if you shrink the jsfiddle window to be narrow enough, the boxes will break again.
Related
I have a simple grid with 2 columns with border and two columns Fiddle
The problem is I want to adjust the border to the content and the content has left padding, so I want to remove that free space.
I tried to use box-sizing: border-box; but it causes no effect
HTML:
<div class="totalContainer totalContainer__space" *ngIf="selectedMenuItem === menu[3]">
<div class="totalContainer__text">
<label><strong>Annual</strong> Test Test </label>
</div>
<div class="totalContainer__text totalContainer__result">
<label><strong>80</strong></label>
</div>
</div>
SCSS:
.totalContainer {
display: grid;
grid-template-columns: 2fr 1fr;
margin-top: 30px;
border: 1px solid rgba(72, 82, 93, 0.8);
border-radius: 12px;
box-sizing: border-box;
&__row {
background-color: #E5E5E5;
}
&__space {
padding: 10px 0 10px 140px;
}
&__text {
font-size: 13px;
}
&__result {
text-align: right;
}
}
Using margin-left instead of padding:
&__space {
padding: 10px 0 10px 10px;
margin-left: 140px;
}
I think you are missing the big picture here.
Making up a block box in CSS we have the:
Content box: The area where your content is displayed, which can be
sized using properties like width and height. Padding box: The padding
sits around the content as white space; its size can be controlled
using padding and related properties.
Border box: The border box wraps
the content and any padding. Its size and style can be controlled
using border and related properties.
Margin box: The margin is the
outermost layer, wrapping the content, padding, and border as
whitespace between this box and other elements. Its size can be
controlled using margin and related properties.
So in short the border includes the padding (it wraps the content as well as the padding), while the margin lays outside of it (pushing the content with border and padding included). I would recomand to check the box model docs.
https://jsfiddle.net/zarch/btrhuxj9/1/
I have this text area and coloured box that looks okay on a full width screen.
But when I reduce the width the text falls out of the box at the bottom. (you can replicate this by minimising the jsfiddle screen)
How can I make the box dynamic height so that it scales?
All pretty new to this, so apologies if this is a newbie question.
<label for="battery-output"></label>
<output id="battery-output" for="battery-size battery-usable" name="output">70% of ( 90% of 3.6kWh ) = 2.27kWh per day</output>
<br>
<output id="battery-output" for="battery-size battery-usable" name="output2">This will take 1.9 x 30 min charge slots (so we round up to 2 slots)</output>
</div>
.rcorners {
border-radius: 5px;
border: 1px solid #ffb347;
background-color: #ffb347;
padding: 5px;
width: 100%;
height: 60px;
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}```
Try to stay away from hard-coded heights. You can see that using a hard-coded height paints you into a pixelated corner at different screen dimensions. Instead, use padding and let the content dictate the height of the element.
Demo
.rcorners {
border-radius: 5px;
border: 1px solid #ffb347;
background-color: #ffb347;
padding: 10px 5px;
/* ~~~~
Box has 10px padding top/bottom added to element's
natural (intrinsic) content height
*/
width: 100%;
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
<div class="rcorners">
<label for="battery-output"></label>
<output id="battery-output" for="battery-size battery-usable" name="output">70% of ( 90% of 3.6kWh ) = 2.27kWh per day</output>
<br>
<output id="battery-output" for="battery-size battery-usable" name="output2">This will take 1.9 x 30 min charge slots (so we round up to 2 slots)</output>
</div>
You may wonder when to ever use hard-coded, extrinsic sizing. One solid use-case is for specifying a maximum container width which houses all of your webpage content. Without specifying an overall container width, the container's content will eventually grow to fill the entire screen.
.container {
max-width: 960px;
}
Further reading
How Big Is That Box? Understanding Sizing In CSS Layout
I am trying to stick my pop up to top and make it a bit smaller to fit the screen.
Here is my landing page URL - yogavoga.com/2weekdiet
Any help will be appreciated.
.modal-content {
margin: 5px auto;
background-color: #fefefe;
border: 1px solid #888;border-width:3px;
width: 90%;
}
I'm not sure if this solves your question in full, because your sample code is a bit short and it doesn't show the element itself. I tried visiting your website, but can't find the element. So it is very difficult for us to say what you actually want.
margin is the space around your div element, in this case your modal. With your code you say your browser to put your element at the top, (0 margin at the top), and do the rest automatically. It does that and will center your element based on the width of your element.
You can scale your element with width. Make it smaller by reducing the percentage.
.modal-content {
margin: 0 auto; // 0 from top, left, bottom and right auto.
background-color: #fefefe;
border: 1px solid #888;
border-width: 3px;
width: 60%; // Width of your element.
}
TIP: remove the margin and padding presets from your body to have your element at the absolute browser border.
I'm trying to implement a baseline/vertical rhythm on a responsive site. Top and bottom borders on table cells are causing me trouble. I have a minimal version of the problem here:
http://codepen.io/bakert/pen/akZWXE
If you set the border on th/td to 0 the vertical grid is respected from top to bottom and the text in both columns lines up. If you set the border to 0.0625em/1px/anything and scroll down below the table you will see that the columns are now out of whack. The text in the table itself is also no longer respecting the vertical grid.
The problem is that line-height does not account for border-width. The two things are additive in determining the height of the tds. Ideally I'm looking for something like box-sizing: border-box but for line-height + border.
I could solve this by explicitly giving th.bordered, td.bordered a fractionally smaller line-height than p but that sounds pretty awful. Should I be setting my vertical rhythm using something other than line-height? A combination of line-height and margin and I then reduce the margin by the width of the border? I'm hoping there's something simpler than that!
table {
width: 100%;
box-shadow: inset -1px -1px 0 0 #ccc;
}
td {
padding-right: 1em;
width: 50%;
box-shadow: inset 1px 1px 0 0 #ccc;
}
EDIT - September 4th
Fix for background-color.
You can use border for element and margin should move element below 1px up. So border on table would not create another line-height problem. But this can not be used on elements.
table {
width: 100%;
margin-bottom: -1px;
border: 0 1px 1px 0;
border-style: solid;
border-color: #ccc;
}
In the "CSS The definitive Guide", the author said "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". But In the following, the child element is wider than the parent.
//html
<div class="wrapper">
<div class="content-main">
<div class="main">This is main</div>
</div>
</div>
// style
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
.main {
width: 500px;
border: 1px solid #f00;
}
So I have two quesions:
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
Why in my example, the element will stick out the parent.
in the edit version, the seven properties add up to the width of the element' containing block seems work well. Why the equation not apply to my example?
EDIT VERSION
p.wide width is 438px, the author calculate as following
10px(left margin) + 1(left border) + 0 + 438px + 0 + 1(right border) – 50px(right margin) = 400px(parent width)
// HTML
<div>
<p class="wide">A paragraph</p>
<p>Another paragraph</p>
</div>
// CSS
div {width: 400px; border: 3px solid black;}
p.wide {
margin-left: 10px; width: auto; margin-right: -50px;
border: 1px solid #f00;
}
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
He is teaching you CSS Box Model, here, you are using div elements which are block level in nature, block level means they take up entire horizontal space by default, unlike span or i or b tags, which are inline elements.
So when you use padding or border they are added outside of the element and not inside. So for example you have an element of say 100x100 in dimension, and you add a padding like
element {
width: 100px;
height: 100px;
padding: 10px;
}
So in the above case, your element will be 120x120 in total, because it will add up 10px of padding on all four size of your element.
Explaining padding syntax
You have two different padding syntax, which are as follows...
padding: 30px 0; in .wrapper and padding: 0 20px; in .content-main so these aren't the same.
Both the above syntax are nothing but short hand syntax of padding ... The complete version looks like...
padding: 5px 10px 15px 20px; /*Nothing to do with your code, this is just a demo */
So in the above example, you have to go clock wise, so 5px is nothing but padding-top: 5px;, then comes 10px which is right, next is bottom and the last 20px is padding-left.
So what when it's just two parameters defined, that means...
padding: 0 20px;
--^---^---
top bottom/left right
So, top and bottom are set to 0 here and right and left to 20px respectively...
Explaining the CSS
Note: None of the element has the height set by you, so the screens
you see ahead which I've attached are computed. So ignore height in them
completely.
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
Here, your element is 502px wide, so why? As I said that border will add on all four sides of the element, and hence it will add 1px on all four sides but your padding is applied to top and bottom only. It's better to use tools like Firebug which will show you graphical presentation of what's going on behind the scenes.
Coming to the second snippet which has the following syntax
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
Here, it is now adding 2px border to your element but, the padding is now applied to left and right and nothing for top and bottom so now the computation will be
Coming to the last snippet which is
.main {
width: 500px;
border: 1px solid #f00;
}
Here, just border is applied, but why it goes out? In technical words, why it overflows? Because you have width defined. So since you have padding set for the parent element, which is padding: 0 20px;, so it will nudge the child element by 20px from the left side. I'll attach a screen of Firebug to show you why it is nudged....
Why in my example, the element will stick out the parent.
Because you are defining width of 500px to your .main div
Demo (What happens when you take out the width)
The default box model is known as content-box
This can be altered by defining a new CSS3 introduced property called box-sizing set to border-box which will alter your box-model in such a way that it will count the padding and border inside the element instead of outside