https://jsbin.com/dazugonoli/1/edit?html,css,output
How would you guys add border to inline-block div? Imagine I'm doing a calendar, the border seem repeated and not equal on each side of the box.
#parent{
width: 400px;
}
#parent > div{
display: inline-block;
width:50px;
height:50px;
line-height:50px;
background:cyan;
border:1px solid;
text-align:center;
}
<div id="parent">
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
</div>
The simplest solution is to use outline instead of border
#parent > div{
display: inline-block;
width:50px;
height:50px;
line-height:50px;
background:cyan;
outline: 1px solid;
text-align:center;
}
<div id="parent"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
</div>
Add a negative margin to the elements
#parent {
width: 400px;
}
#parent>div {
margin: -1px 0 0 -1px;
display: inline-block;
width: 50px;
height: 50px;
line-height: 50px;
background: cyan;
border: 1px solid;
text-align: center;
}
<div id="parent">
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>
</div>
You could add a negative margin to the top and left (or right and bottom) sides:
margin: -1px 0 0 -1px;
Works if the last line contains the same or less elements than the previous lines. And also when you know the number of elements in one line - you do not use the wrapping.
#parent > div{
display: flex;
}
#parent > div > div{
display: flex;
width:50px;
height:50px;
line-height:50px;
background:cyan;
justify-content:center;
align-items: center;
border-bottom:1px solid;
border-right:1px solid;
}
#parent > div:first-child > div {
border-top: 1px solid;
}
#parent > div > div:first-child {
border-left: 1px solid;
}
<div id="parent">
<div>
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div></div>
<div><div>8</div><div>9</div><div>10</div></div>
</div>
Related
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
Why is btnow moved down? It should be inline with datewrap.
If I remove overflow:hidden from datewrap - it's ok.
But I need overflow:hidden on datewrap.
When you use of overflow:hidden[overflow property evaluating to something other than visible] , the baseline is the bottom edge of the margin-box[insert margin-bottom and see result],so this element for align its baseline with baseline of other element move up a bit.
for fix use of vertical-align: top; like this:
.btnow {
vertical-align: top;
//Other css
}
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
vertical-align: top;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
text-align really shouldn't be used to position elements. There are far better ways to achieve this.
I don't know why overflow is causing it to "teeter-totter", but below is some code to fix this.
.wrap{
display: flex;
justify-content: center;
/* and if you want to make sure the elements are always aligned vertically */
align-items: center;
/* remember: justify-content will always control the same direction as the flex
** box; so, if the flex box is a row, justify-content will control the horizontal
** spacing and align-items will control the vertical spacing, but if the flex box
** is a column justify-content will control the vertical and align-items will
** control the horizontal. */
width: 100%;
position: fixed;
top: 45px;
left: 0;
background: gold;
}
.datewrap, .btnow {
margin: 0 5px;
display: inline-block;
border: 2px solid red;
}
.datewrap{
overflow: hidden;
}
.btnow{
color: white;
background: green;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
position:inherit;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
The vertical-align CSS property specifies the vertical alignment of an inline or table-cell box.
Reference Link:
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
.wrap {
position: fixed;
left: 0;
top: 45px;
width: 100%;
text-align: center;
background: gold;
}
.datewrap, .btnow {
display: inline-block;
margin: 0 5px;
border: 2px solid red;
}
.datewrap {
overflow: hidden;
vertical-align: bottom
}
.btnow {
color: white;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
Note:
If you are writing code in real-time, you need to minimize your CSS.
My HTML
<div id="wrapper">
<div id="div1">The two divs are</div>
<div id="div2">next to each other.</div>
</div>
My CSS
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width:49%;
height:120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width:49%;
height:160px;
border: 1px solid green;
}
A JSFiddle
So, when as you can see the width of each div is 49%, that's the only way I'm getting it to work. If you set the width of each to 50%, the divs aren't displayed next to each other anymore... Why is that?
Because of two things
Border size so you need to change box-sizing to border-box
White space on inline-block elements
* {
box-sizing: border-box;
}
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width: 50%;
height: 120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width: 50%;
height: 160px;
border: 1px solid green;
}
<div id="wrapper">
<div id="div1">The two divs are</div><div id="div2">next to each other.</div>
</div>
You need to remove the line break between the <div> tags and box-sizing:border-box;
The two divs arenext to each other.
Another approach would be to use float
#wrapper {
border: 1px solid blue;box-sizing:border-box;
}
#div1 {
float:left;
width:50%;
height:120px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
#div2 {
float:left;
width:50%;
height:160px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
The other option is to use Flex.
#wrapper {
border: 1px solid blue;
display: flex;
}
#div1 {
width:50%;
height:120px;
border: 1px solid red;
}
#div2 {
width:50%;
height:160px;
border: 1px solid green;
}
This is because you have added a border of 1px to wrapper. Your border with take 2px of total width of your page.
If you wanna keep the border and still keep the width of each div as 50%, you can refer to #NenadVracar 's answer
Another option is to use calc() and calculate the width to be 50% - 2px. I'm just listing that as an option #Nenad Vracar has the right answer
I'm trying to align some span element inside a div to it's left border.
This is currently my code:
.bikoret
{
width:40%;
border:1px solid black;
}
.bikoret > .content
{
width:80%;
padding:0;
word-wrap: break-word;
}
.bikoret > .username
{
text-align:center;
padding-left:1%;
padding-right:1%;
position:relative;
left:0;
border-top:1px inset;
}
<div dir="rtl" style="text-align:center; background-color:White; border-top:1px; border-style:inset; margin-top:4px; padding-left:10px; padding-right:10px;" runat="server" id="takzir">
<center>
<div class='bikoret'>
<div class='content'>
this is centered
</div>
<span class='username'>this is aligned to left</span>
</div>
</center>
</div>
How can I fix it? I tried everything by now..
Here is how I would approach this.
First, I define a class for the parent block, .parent, with your current styling and add text-align: center.
For the child element .bikoret, apply display: inline-block which means that
this element is centered within the parent. Important: set text-align: left.
For the child elements of .bikoret, treat each element separately.
For .content, I would set the width to auto and text-align: center to center your text.
For .username, this is simply an inline element, and because of text-align: left on its parent (.bikoret) it sits to the left edge as you want.
.parent {
text-align: center;
background-color: White;
border-top: 1px;
border-style: inset;
margin-top: 4px;
padding-left: 10px;
padding-right: 10px;
}
.bikoret {
width: 40%;
border: 1px solid black;
display: inline-block;
text-align: left;
}
.bikoret > .content {
text-align: center;
width: auto; /* Why 80%? */
padding: 0;
word-wrap: break-word;
background-color: yellow;
}
.bikoret > .username {
padding-left: 1em; /* % padding won't really work here... */
padding-right: 1em;
border-top: 1px inset;
background-color: beige;
}
<div class="parent">
<div class='bikoret'>
<div class='content'>this is centered</div>
<span class='username'>this is aligned to left</span>
</div>
</div>
Note: The center tag is deprecated and should not be used.
adding a display: block; to your class .username will do the job, but here's another ways of doing it
==========
By giving a position: relative; to your container .bikoret along with a height to avoid the border not being covering the .username and a position absolute to .username as well, will do it:
.bikoret {
width: 40%;
border: 1px solid black;
position: relative;
height: 40px;
}
.bikoret > .username {
padding-left: 1%;
padding-right: 1%;
position: absolute;
left: 0;
border-top: 1px inset;
}
Here's an online example
======== Or if this is all about the divider ========
You can do something like this
<div class='bikoret'>
<div class='content'>this is centered</div>
<div class='divider'></div>
<span class='username'>this is aligned to left</span>
</div>
CSS
.bikoret {
width: 40%;
border: 1px solid black;
}
.divider {
height: 1px;
background: #909090;
width: 60%;
}
.bikoret > .username {
padding-left:1%;
padding-right:1%;
text-align:left;
display:block;
}
Here's the example
To center a block level element: set margin-left:auto; margin-right:auto; to itself.
To center an inline level element: set text-align:center; to its parent.
Updated working demo: http://jsfiddle.net/k2xh3xya/1/
HTML
<div dir="rtl" style="" runat="server" id="takzir">
<center>
<div class='bikoret'>
<div class='content'>this is centered</div>
<span class='username'>this is aligned to left</span>
</div>
</center>
</div>
CSS
#takzir {
background-color:White;
border-top:1px;
border-style:inset;
margin-top:4px;
padding-left:10px;
padding-right:10px;
}
.bikoret {
width:40%;
border:1px solid black;
text-align: left;
}
.bikoret > .content {
width:80%;
padding:0;
word-wrap: break-word;
text-align:center;
margin: auto;
}
.bikoret > .username {
padding-left:1%;
padding-right:1%;
border-top:1px inset;
}
If I understand you correctly you want the "this is aligned to left" to be moved all the way to the left? If so then you could simplify your HTML and CSS.
.textOuter {
width:100%;
float:left;
text-align:center;
background-color:white;
border-top:1px;
border-style:inset;
margin-top:4px;
padding-left:10px;
padding-right:10px;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
box-sizing:border-box;
}
.textInner {
width:40%;
float:left;
position:relative;
left:30%;
border:1px solid black;
}
.textCenter {
width:100%;
float:left;
}
hr {
width:80%;
float:left;
position:relative;
left:10%;
margin:0;
padding:0;
}
.textFull {
float:left;
}
<div class="textOuter">
<div class="textInner">
<div class="textCenter">this is centered</div>
<hr>
<div class="textFull">this is aligned to left</div>
</div>
</div>
I have the following html structure:
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
The parent is positioned absolutely, child1 and child2 are displayed side-by-side using inline-block.
I need this whole thing to be responsive based on the width of the 2 children divs. the problem is, if I increase the width of any of them, the parent's width remains the same. Changing its position to relative fixes this, but I have to have it in absolute.
Is there anyway to get it to be responsive?
EDIT:
I was hoping for this to be simple, but apparently not so much... :(
here's the actual HTML:
<div class="action_container">
<div class="action_inner">
<div class="action_title">Format Text</div>
<div class="action_body">
<div class="action_args_section"></div>
<div class="action_output_section"></div>
</div>
</div>
</div>
And the CSS:
<style>
.action_container {
display: block;
position: absolute;
}
.action_inner {
border: 1px solid black;
}
.action_inner {
min-width: 120px;
min-height: 50px;
background-color: white;
border: 1px solid #666;
border-radius: 5px;
}
.action_title {
font-size: 12px;
font-weight: bold;
text-align: center;
border-bottom: 1px solid #ccc;
padding: 3px;
}
.action_args_section {
display: inline-block;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
}
.action_output_section {
display: inline-block;
width: 50px;
vertical-align: top;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
}
</style>
.parent{
position: absolute;
display: table;
}
.child{
position: relative;
display: table-cell;
}
Use this trick to set children in single line and parent to get width from them. Don't apply floats to nothing. And remember about white-space: nowrap; if You need to keep single line in child elements.
Here is fiddle.
.parent {
position:absolute;
height:50px;
border:1px solid red;
}
.child1 {
width:100px;
height:30px;
border:1px solid green;
}
.child2 {
width:150px;
height:30px;
border:1px solid blue;
}
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
Is this what you're looking for?
JSFiddle
.parent{
position:absolute;
left : 60px;
top : 60px;
width : auto;
height:auto;
border:1px solid black;
}
.parent .child{
display:inline-block;
border:1px solid blue;
}
<div class="parent">
<div class="child">aaaaaassssssssssssss</div>
<div class="child">sssssssccccccccccccccccccc</div>
</div>
Try use a max-width to set a maximum width for the parent div so it doesn't get wider than specified.
I did this easily. Changing the width of the divs changes the parent as well.
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<style>
div{border:1px solid black;}
.parent{
position:absolute;
width:auto;
height:auto;
}
.child1{
display:inline-block;
width:40px;
height:40px;
}
.child2{
display:inline-block;
width:30px;
height:40px;
}
</style>
If you want a responsive design, make sure you're using percentages, and not pixel values because the size of the divs will be calculated by the viewport width.
If you just want the parent to resize based on the absolute sizes of the child divs, add height:auto; width:auto to the parent. Then, change the child divs to display:block; float:left. The parent will resize accordingly.
Updated CodePen Demo
CSS
.action_container {
display: block;
position: absolute;
height:auto;
width:auto;
}
.action_inner {
border: 1px solid black;
}
.action_inner {
min-width: 120px;
min-height: 50px;
background-color: white;
border: 1px solid #666;
border-radius: 5px;
}
.action_title {
font-size: 12px;
font-weight: bold;
text-align: center;
border-bottom: 1px solid #ccc;
padding: 3px;
}
.action_args_section {
display: block;
float:left;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
width:300px;
border: 1px solid red;
}
.action_output_section {
display: block;
float:left;
width: 150px;
vertical-align: top;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
border: 1px solid blue;
}
see the sample solution here in jsfiddle link
using this css:
.parent{
position:fixed;
background-color:blue;
height:auto;
width:auto;
}
.child1{width:200px;background-color:black;height:200px;float:left;}
.child2{width:200px;background-color:red;height:200px; float:left;}
if it is not what you're looking for,you can edit your css here then we can help
.parent{
float: left;
posetion: absolute;
background-color: yellow;
width:auto;
height: auto;
}
.parent div{
float: left;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
<div class="parent">
<div class="child1">this</div>
<div class="child2">this</div>
</div>
Here's The Code You Need :)
See this fiddle
JSFiddle
CSS:
.containers {
width:100%;
height:auto;
padding:10px;
margin-bottom:0px;
}
#id4 {
float:right;
margin-right:0;
display:inline;
border:5px solid red;
}
#id5 {
text-align:center;
border:5px solid red;
}
HTML:
<div class='containers'>
<div id='id4'>
margin-right:10px;
</div>
<div id='id5'>
center-text;
</div>
In this fiddle I want center-text to be center of the page, not at the center between left-border and float element.
The below is one possible option by adding position: absolute; right: 10px; to the id4 div. This will make the div always stay at 10px from the right margin. But it has to be noted that the element is no longer a float element.
Note: The texts would overlap if the result window is shrunk beyond a certain level. I will update the answer if and when I manage to find a fix for that.
.containers {
width: 100%;
height: auto;
padding: 10px;
margin-bottom: 0px;
text-align: center;
box-sizing: border-box;
}
#id4 {
display: inline-block;
position: absolute;
right: 10px;
border: 5px solid red;
}
#id5 {
display: inline-block;
border: 5px solid red;
}
.containers {
width:100%;
height:auto;
padding:10px;
margin-bottom:0px;
text-align:center;
}
#id4 {
float:right;
margin-right:0;
display:inline;
border:5px solid red;
}
#id5 {
margin: 0 auto;
display:inline-block;
border:5px solid red;
}
DEMO