I'm trying to have something like this:
|--------------fixed width---------------|
Title1 .......................... value1
Title2 ................... another value
Another title ........ yet another value
Here is my html example code:
<div class="container">
<span class="left">Title</span>
<span class="center"> </span>
<span class="right">value</span>
</div>
And here my css:
.center {
text-align: center;
border-bottom: 1px dotted blue;
display: inline-block;
outerWidth: 100%;
}
.right {
display: block;
border: 1px dotted red;
float: right;
}
.left {
display: block;
text-align: right;
border: 1px dotted red;
margin-right: 0px;
float: left;
}
.container {
width: 200px;
border: 1px dotted red;
padding: 5px;
}
It's possible to make span "center" expand to fill the space between the other two span elements?
Code on jsfiddle: http://jsfiddle.net/XqHPh/
Thank you!
If you reorder your HTML, you can get a simple solution:
<div class="container">
<span class="left">Title</span>
<span class="right">value</span>
<span class="center"> </span>
</div>
Place the two floated elements ahead of the .center element. The .center element will be in regular content flow and wrap around the left and right content.
The CSS:
.center {
display: block;
border-bottom: 1px dotted blue;
overflow: auto;
position: relative;
top: -4px;
}
.right {
float: right;
margin-left: 10px;
}
.left {
float: left;
margin-right: 10px;
}
.container {
width: 200px;
border: 1px dotted red;
padding: 5px;
}
When you float an element, the display type computes to block, so no need to declare it.
Also, for .center, if you add overflow: auto, you constrain the block so it does not extend beyond the edges of the floated elements. As a result, your bottom border does not underline the title and value text.
Finally, you can add position: relative and move the .center up a few pixels to align the border closer to the baseline of the text.
Fiddle: http://jsfiddle.net/audetwebdesign/DPFYD/
for this you need to change the html structure like this
html
<div class="container">
<span class="left">Title</span>
<span class="right">value</span>
<span class="center"> </span>
</div>
and here is the css for .center span
.center {
text-align: center;
border-bottom: 1px dotted blue;
display:block;
}
jsFiddle File
Meanwhile Flexbox has full browser support, which allows for a more elegant solution without the center element.
.left, .right {
border: 1px dotted red;
}
.container {
display: flex;
justify-content: space-between;
width: 200px;
border: 1px dotted red;
padding: 5px;
}
<div class="container">
<span class="left">Title</span>
<span class="right">value</span>
</div>
Related
My second inner div position is weirdly adjusted when my first inner div have a long link text. How to fix it?
My html code:
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>
My css code:
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
link to the picture of the div:
https://www.dropbox.com/s/9zs4mgj7izuqsp1/question.png?dl=0
The problem is with your CSS. Particularly the .div-wrapper div
You need to change the display setting from inline-block to inline-table to get it inside the cell. You mentioned that you wanted the box inside the larger box, but you need to clarify how exactly you want the inner boxes to be placed inside the larger box (ex: small gap between the boxes, both perfectly fit inside the large box with equal sizes)
Just changed inline-block to inline-flex for your inner div and looks fine.
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-flex;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>
Just have to fix this, I don't think any solution here explains why the problem exists. Just to add up, the problem with this is because vertical-align is set to baseline by default.
What you have to do is set the vertical-align to top
Insert it in your CSS:
.div-wrapper div {
vertical-align: top;
}
Link to solution: https://jsfiddle.net/Lnvgkfz3/
Small changes in CSS
.div-wrapper {
border: 1px solid black;
width: auto;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 190px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
The next image is currently what I have.
And this is what should be:
As you can see, the dots on the third column are not aligned. It should meet the next requirement:
As you can imagine, I might use two divs because of the two borders.
This next code is what I have tried all day long, I cannot achieve to position the dots in the middle with the background-color stretched (considering the two border colors). What am I wrong? Should I remove everything and change it by a flexbox? I'll appreciate your help.
Html code:
You have 4 items in your cart
<article class="cart-item">
<div class="left">
<img src="images/item1.jpg"></img>
</div>
<div class="center">
<h4 class="title">Dexter Men's Max Bowling Shoes (Right Handed Only)</h4>
<span class="description">Shipping 3-day with UPS</span>
<span class="description">Color: Gray</span>
<span class="description">Size: 28.5</span>
<span class="price">$60.00</span>
</div>
<div class="right">
<div class="grouped-dots">
<span></span>
<span></span>
<span></span>
</div>
</div>
</article>
Css code
.cart-item
{
border-bottom: 1px solid #CCCCCC;
border-top: 1px solid #CCCCCC;
display: table;
height: 100%;
overflow: hidden;
}
.cart-item>div
{
display: table-cell;
}
.left,.center
{
padding-bottom: 10px;
padding-top: 10px;
}
.left
{
padding-left: 15px;
padding-right: 15px;
width: 33.33%;
}
.left img
{
max-width: 100%;
}
.center
{
padding-right: 15px;
text-align: left;
vertical-align: top;
width: auto;
}
.right
{
border-left: 1px solid #CCCCCC;
border-right: 1px solid #CCCCCC;
vertical-align: middle;
width: 15px;
}
.right .grouped-dots
{
background-color: #F5F5F5;
border-left: 1px solid #FFFFFF;
border-right: 1px solid #FFFFFF;
display: block;
height: 100%;
}
.cart-item .grouped-dots span::after
{
color: #CCCCCC;
content: '.';
font-family: 'Open Sans',sans-serif;
font-size: 40px;
line-height: 0;
}
This approach is using table and table-cells as display values. If you think I'm in the wrong path, please let me know.
It's because of this style:
.right .grouped-dots {
height: 100%;
}
Since its as tall as its parent, there's no room for it to move vertically to the "middle."
Remove that style, and move its background color to .right:
.right {
background-color: #F5F5F5;
}
Fiddle
http://s4.postimg.org/mbrpxn2d9/Untitled.png
Edit: Not a duplicate. The other question doesn't contain information about divs being automatically adjusted to the words on the inside.
I have 4 divs. I have 3 divs inside another div, and I'm trying to float one to the left, one to the center, and one to the right. I'm also trying to make the width and height of the divs on the inside to be automatically adjusted to the width and height of the words on the inside of the divs. I also want the divs on the inside to stack up on top of each other, instead of being on the same line. So far, I got the left div to float to the left, and the right div to float to the right, but I just cannot get the middle div to be centered, nor get it to adjust to the width and height of the word inside of it. Please take a look at my code:
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
border: 1px solid red;
float: left;
}
#innerMiddle {
border: 1px solid red;
margin: auto;
}
#innerRight {
border: 1px solid red;
float: right;
}
<div id='outer'>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'>Middle</div>
<div id='innerRight'>Right</div>
</div>
Depending on the output of the image, I think flexbox solution would be a good way to go.
Let the container have a flexible layout with column wrapping.
Align each item based on position in the container i.e. flex-start, center and flex-end
#outer {
display: flex;
display: -ms-flex;
flex-flow: column wrap; /* Wrap the items column wise */
justify-content: flex-start; /* Items to start from the top of the container */
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
align-self: flex-start; /* Equivalent to float: left of your code */
border: 1px solid red;
}
#innerMiddle {
align-self: center; /* Equivalent to margin: auto */
border: 1px solid red;
}
#innerRight {
align-self: flex-end; /* Equivalent to float: right */
border: 1px solid red;
}
<div id='outer'>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'>Middle</div>
<div id='innerRight'>Right</div>
</div>
If changing your HTML just a bit is an option, you can add span elements in your divs which will give you want, and it will work in all browsers:
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
text-align:left;
}
#innerMiddle {
text-align:center;
}
#innerRight {
text-align:right;
}
div > div > span {
border: 1px solid red;
}
<div id='outer'>
<div id='innerLeft'><span>Left</span></div>
<div id='innerMiddle'><span>Middle</span></div>
<div id='innerRight'><span>Right</span></div>
</div>
This is what you mean?? I had Edited
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
border: 1px solid red;
/* width: 30%; */
float: left;
}
#innerMiddle {
border: 1px solid red;
float: left;
margin: 0 5px;
}
#innerRight {
border: 1px solid red;
float: right;
}
<div id='outer'>
<div id='innerLeft'>LeftLeftLeftLeft</div> <br>
<div id='innerMiddle'>MiddleMiddleMiddleMiddle</div> <br>
<div id='innerRight'>RightRightRightRight</div>
</div>
write your html tags like this hope it help!
<div id='outer'>
<div id='innerRight'>Right</div>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'></div>
</div>
I'm creating a widget with a tabular structure and I'm not seeing why the two rows on the right are not expanding to full width (or why they don't have a red border around them).
My goal is for the black horizontal rule to run between rows 1 and 2 and reach the far right end of the entire table.
Demo Fiddle
.table {
display: table;
width: 100%;
white-space: nowrap;
border: 1px solid blue;
}
#cell1 {
display: table-cell;
padding-left: 30px;
border: 1px dashed orange;
}
#cell1 > #line1 {
display: inline-block;
font-size: 16px;
padding-top: 5px;
border: 1px solid red;
}
#cell1 > #line2 {
display: inline-block;
font-size: 30px;
border: 1px solid red;
}
#cell1 > #line3 {
display: inline-block;
padding-right: 20px;
border: 1px solid red;
}
#cell2 {
display: block;
width: 100%;
border: 1px dashed orange;
}
#cell2 > #topRow {
display: table-row;
width: 100%;
border: 1px solid red;
padding-top: 10px;
padding-bottom: 10px;
border-bottom: 1px solid black; // horizontal rule
}
#cell2 > #bottomRow {
display: table-row;
width: 100%;
border: 1px solid red;
padding-top: 10px;
padding-bottom: 10px;
}
<div class="table">
<div id="cell1">
<span id="line1">line 1</span>
<br>
<span id="line2">line 2</span>
<br>
<span id="line3">line 3</span>
</div>
<div id="cell2">
<div id="topRow">
This is the top row
</div>
<div id="bottomRow">
This is the bottom row
</div>
</div>
</div>
Remove display:table-row to make the bottom border visible
or use:
<div id="topRow">
This is the top row
</div>
<hr />
<div id="bottomRow">
This is the bottom row
</div>
What you have to do is create one full width row with your <hr> within it.
<tr><!-- table row 100% width -->
<hr></hr>
</tr>
Your defining a new full width table row with only the horizontal rule running through it.
Actually it appears you aren't even using tables, so this is kind of an odd question - but, in your case just make a full width div with the <hr> within it, or position the hr absolute with a higher z-index by adding a selector directly to hr element.
To Put it simple, I would like a header with two elements floating to each side and vertically centered:
I started out with doing this with non-floating elements and managed to make this example.
But once I add the float:left or float:right the vertical centering is lost (I understand why, because it's not part of the flow anymore)
I wonder what is the best method to achieve this. Complete CSS redesign is happily accepted.
Thanks in Advance!
Vertical centering can be painful, especially when you are not dealing with inline elements. In this case, I would recommend taking advantage of display:table-cell.
HTML
<div id="wrapper">
<div class="cell">
<div class="content">
Content Goes here
</div>
</div>
<div class="cell">
<div class="content2">
<div class="redbox">
</div>
</div>
</div>
</div>
CSS
#wrapper {
color: white;
display: table;
border: 1px solid darkblue;
background: blue;
width: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
height: 200px;
}
.content {
float: left;
}
.content2{
float: right;
}
.redbox {
border: 2px solid darkred;
background: red;
height: 75px;
width: 75px;
}
Example: http://jsfiddle.net/YBAfF/
Add text-align:right to parent div, it makes child elements to align right side. Now add float:left to #text
#parent {
border: 1px solid black;
display: block;
line-height: 400px;
height: 400px; text-align:right
}
#text {
display: inline-block;
border: 1px dashed black;
height: 100%; text-align:left; float:left
}
#logo {
border: 1px dashed black;
height: 90%;
line-height: 90%;
vertical-align: middle;
display: inline-block;
}
#logo img {
border: 1px dashed red;
height: 100%;
}
DEMO
Here's a sample jsfiddle and the same code below. When you set the height of an element, you can set the same line-height to nested elements and they'll expand to the height. Vertically centering the content.
HTML
<div id="wrapper">
<div id="left">left</div>
<div id="right">right</div>
</div>
CSS
#wrapper{
margin:0 auto;
width 960px;
background: #eee;
height:50px;
}
#left{
float:left;
background:#ccc;
line-height:50px;
}
#right{
float:right;
background:#ddd;
line-height:50px;
}
You should add a wrapper around the elements you want to center and float them inside the wrapper. Something like that:
HTML
<div class="center">
<p class="left">Some text goes here</p>
<img src="/path/toimage" alt="My image" class="right">
</div>
CSS
.center {
margin:0 auto;
width: 400px;
}
.right {
float: right;
}
.right {
float: left;
}
Of course, this is a very simple example. You can change the values and CSS according to your needs.