I'm having some trouble creating a (somewhat strange) layout and I can't find an example anywhere of exactly what I'm trying to do.
I would like to layout multiple blocks that look like this:
<div class="rel">
<div class="item">--- a</div>
<div class="item">- b</div>
<div class="item">c</div>
</div>
where all of the .item elements are on top of one another, but the .rel elements layout normally so they are all visible. It's important to note that all the .item elements within a .rel will be of exactly the same length, yet they may be of any length, so they might wrap onto a new line. Here is an image of what I'm trying to do:
I've created this CodePen.
SOLUTION
In case anyone else finds themselves needing this truly strange layout:
CodePen
I am not exactly sure what you are trying to achieve, but it seems that you want <div class="item"> to display inline. you can use float=left or display=inline-block instead of absolute positioning. your divs are on top of each other right now .
Something similar to this
<div class="container">
<div class="rel clearfix">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>
</div>
and
.container {
width: 200px;
}
.rel {
width: 100%;
background: steelblue;
margin: 1em;
padding: 1em;
position: relative;
}
.item {
color: white;
background: gray;
margin: .1em;
float:left;
}
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
.rel-2 {
width: 100%;
background: steelblue;
margin: .2em;
position: relative;
}
check it out here http://codepen.io/anon/pen/vEExvM
Use this on item
display: table-cell;
and this on rel
display: inline-block;
Have a look at this fiddle. http://jsfiddle.net/h8rzw65p/
Total Code:
.container {
width: 200px;
}
.item {
color: white;
background: gray;
margin: .1em;
top: 0px;
left: 0px;
display: table-cell;
}
.rel-2 {
top: 0px;
left: 0px;
width: 100%;
background: steelblue;
margin: .2em;
display: inline-block;
}
Or instead of table-cell you could do this on item:
float: left;
Related
I am trying to add a pseudo before and after vertical line to a textfield for styling purposes. These elements need to be flush to the text -20px left and -20px right.
This works fine when the text is on one line as an inline-block, but as soon as the text spans multiple lines the width expands to that of the parent and the pseudo elements are no longer just 20px from the text.
Is there a way in which I can accomplish this using CSS?
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2 {
position: relative;
display: inline-block;
margin: 0;
padding: 0;
background-color: blue;
color: white;
}
span {
position: relative;
background-color: green;
}
h2::before,
h2::after {
content: '';
position: absolute;
top: 0;
width: 4px;
height: 20px;
background: black;
}
h2::before {
left: -20px;
}
h2::after {
right: -20px;
}
<!-- Single line example works as the black bars are 20px away from the start/end of text-->
<div class="container">
<h2><span>This is a title</span></h2>
</div>
<br> <br>
<!-- double line doesn't work because the h2 is now the full width of the container -->
<div class="container">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
Edit: Here is a working version using tables, but if anyone has a better solution I'd love to hear it: https://codepen.io/anon/pen/MqveLQ
So from what i can see is the issue here is where you are applying the borders with before and after. You need to alter where you apply your borders. Remove them from the h2, and add in a new html element that wraps the h2 and apply there.
eg:
<div class="container">
<div class="headerwrap">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
</div>
<div class="container">
<div class="headerwrap">
<h2><span>This is a title</span></h2>
</div>
</div>
CSS
.headerwrap::before,
.headerwrap::after {
content: '';
position: absolute;
top: 0px;
bottom:0;
margin:auto;
width: 4px;
height: 20px;
background: black;
}
.headerwrap::before {
left: 10px;
}
.headerwrap::after {
right: 10px;
}
Here is a working example: https://codepen.io/FEARtheMoose/pen/VGbJjO?editors=1100#0
Edit: altered example after comments - https://codepen.io/FEARtheMoose/pen/VGbJjO
I have moved your code to this fiddle: https://jsfiddle.net/n2Lr6xy5/13/ and removed position: absolute along with stripping out some of the other styles as they seemed unnecessary and I think I have created what you're after.
Here is the updated CSS:
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2{
display: inline-block;
}
h2:after,
h2:before {
content: "";
width: 4px;
height: 20px;
background: #000000;
display: inline-block;
margin: 0 10px;
}
I am trying to display an outer div with an image on the left and another div with some text to the right on it. I use:
img {
float: left;
}
div.out {
background-color: blue;
padding: 10px;
}
div.in {
background-color: yellow;
padding: 5px;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
</div>
Code works almost fine as shown below but how can I make the first blue div extend to the very end of where the image ends? Also, how can I add a 10px space let's say between the image and the text? (And also, is my way of adding float: left; to the img the best way to display it to the left or is there a better way?)
Thanks a lot
img {
display: inline-block;
}
div.out {
background-color: blue;
padding: 10px;
display: flex;
}
div.in {
display: inline-block;
width: 100%;
background-color: yellow;
padding: 5px;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
</div>
I recommend you should learn how to use flex
You can use the flexbox model for this. Remove the float and on the outer div use display: flex;, and the inner div flex-grow: 1;:
#flexbox_container {
display: flex;
background-color: blue;
padding: 10px;
}
div.flex_item {
background-color: yellow;
padding: 10px;
flex-grow: 1;
}
<div id="flexbox_container">
<img class="flex_item" src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="flex_item item_selected">
Flowers
<br>are
<br>cool</div>
</div>
img {
float: left;
}
div.out {
background-color: blue;
padding: 10px;
}
div.in {
background-color: yellow;
padding: 5px;
}
.clr {
clear: both;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
<div class="clr"></div>
</div>
If you're looking for the blue box to extend to the end of your floated content, your looking for a clear fix. See the minor change above with the added div with class clr.
-- Edit of all edits --
Realized after doing what you asked... it probably wasn't EXACTLY what you meant to ask. But I got the blue box to extend to where the image ends haha.
display:flex don't support in IE9 and earlier, So i use of other way :
.out {
background-color: blue;
padding: 15px;
}
.content {
background-color: yellow;
}
.content:after {
content: '';
display: block;
clear: both;
}
.in {
padding-top: 5px;
}
img {
float: left;
margin-right: 5px;
}
<div class="out">
<div class="content">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">Flowers<br>are<br>cool</div>
</div>
</div>
I'm laying out a product page in HTML. My attempt to make the desc below the photo take up the width of the photo was to use absolute positioning. This worked well until I got a longer desc and I remembered it takes the element out of the flow causing the problem below. How should this be done?
I could not find a question that included the vertical and dynamic aspects of my question. If you are closing this as a dup please provide a link.
More details: At the top it has a div "item details" with two columns. Left column has a photo of variable height/width. Below that I have a text description of variable size that I want exactly as wide as the photo. Right column is "item details" with a max-width div. Below all of that I have more divs which should take the entire width.
Live url: http://www.morphmarket.com/snakes/ball-pythons/14074
HTML
<div class="detail-container">
<div class="item-details">
<div class="image-col">
<div class="image">
...
</div>
<div class="desc">
<b>Description.</b> {{ snake.desc }}
</div>
</div>
<div class="details-col">
...
</div>
</div> <!-- item details -->
<div class="store-details">
...
</div>
</div> <!-- item container -->
<div class="more-from-store">
...
</div>
CSS
.snake-page {
.detail-container {
margin-top: 10px;
//display: table-cell;
width: 100%;
.item-details {
display: table-row;
//display: table;
//width: 1px;
.image, div.desc, .details-col {
.white-box();
margin: 10px 0px;
}
.image-col {
display: inline-block;
position: relative;
.image {
position: relative;
.sold {
size: 10rem;
}
.img-thumbnail {
}
}
div.desc {
max-height: 150px;
padding: 10px;
position: absolute;
overflow:auto;
left: 0px;
right: 0px;
margin: 0 0 10px 0;
}
}
.details-col {
.title {
font-weight: bold;
font-size: 1.4em;
text-align: center;
margin-bottom: 10px;
}
font-size: 1.20em;
max-width: 400px;
padding: 10px;
margin-left: 10px;
display: inline-block;
//display: table-cell;
vertical-align: top;
background: #white;
.details {
.dl-horizontal {
#width: 120px;
dt {
width: #width;
}
dd {
margin-left: #width + 20px;
}
}
.label.trait {
margin-right: 0px;
}
}
.button-col {
div {
margin: 0 auto;
width: 85%;
}
}
}
}
.store-details {
display: table-cell;
width: 1px;
> * {
.white-box();
margin: 0 0 10px 0;
padding: 10px;
}
.store-policy {
}
.about-store {
}
}
} // detail container
}
I couldn't find documentation that says this, but we can see that a table-caption element takes the width of its containing table (as opposed to stretching out based on its own amount of text like a block element does).
Paste this at the bottom of your CSS file to see it in action (I overrode the properties that are in the way. If you adopt this solution you can merge the styles of course).
.snake-page .detail-container .item-details .image-col {
margin-top: 10px;
}
.snake-page .detail-container .item-details .image-col .image {
display: table-row;
}
.snake-page .detail-container .item-details .image-col div.desc {
position: static;
display: table-caption;
caption-side: bottom;
margin-top: 5px;
}
My solution makes both elements table related elements, the description into a table-caption and the image into a table-row, so they basically act as one table. By giving the description caption-side: bottom I moved the description to the bottom of the table.
I'm thinking you can do this by setting rows and cols with Bootstrap and vary the sizes for the cols based on what you need. Like this: http://jsfiddle.net/4d5WR/124/
For example, for the image,
<div class="row">
<div class="col-lg-3">
<div class="image">
<img src="http://www.pet-care-portal.com/images/BallPythonT3Pic1.jpg">
</div>
</div>
</div>
Here is a fiddle of the below:
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
line-height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
What I want is for the #filterSeparator to be aligned with the other divs.
For some reason, all the other divs are below the #filterSeparator.
If I put text inside #filterSeparator, then it works.
Is there a way for me to get it to work without placing any text inside #filterSeparator?
fiddle
For inline / inline-block elements, use the vertical-align property:
.filterDivActual, #filterSeperator {
display: inline-block;
vertical-align: middle ; /* or some other value: */
}
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
I don't know why it does this but you can fix it by using float:left; instead of display:inline-block
Putting content in the empty <div> will fix it.
<div id='filterSeperator'> </div>
#filterSeparator:before {
content: ".";
visibility: hidden;
}
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
#filterSeparator:before {
content: ".";
visibility: hidden;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
I'm trying to make a header bar that looks similar to Bootstrap. If you view this document now the problem is that 'Item 1' is displayed correctly but 'Item 2' is pushed below it, instead of being to the right of it. I thought that by setting "left:80px' to 'Item 2' it would go 80px right of item 1.
Please let me know how to fix this. I was also wondering if I'm doing this in a smart way or if stacking the elements (.items > #item_1) is better. Thanks!
CSS
/* header, logo, and items */
#header {
width: 100%;
height: 45px;
position: absolute;
top: 0px;
left: 0px;
background: #3b5998;
text-align: left;
box-shadow: 0px 1px 0px #888888;
}
.items {
top: 0px;
right: 0px;
width: 200px;
height: 23px;
padding-top: 11px;
padding-bottom: 11px;
position: absolute;
background: #3b5928;
text-align: center;
font-family: 'Calibri';
font-size: 18px;
color: #f7f7f7;
}
#item_1 {
width:80px;
background: fff;
}
#item_2 {
width: 80px;
left: 80px;
background: #3b7328;
}
HTML
<!--Header and Footer-->
<div id="header">
<div class="items">
<div id="item_1"> Item 1 </div>
<div id="item_2"> Item 2 </div>
</div>
</div>
One approach would be to change the display of the elements to inline-block. (example)
.items > div {
display:inline-block;
}
Alternatively, you could float the elements or use a flexbox layout.
It's worth noting that you can't position a static element (i.e., position:static - the default). If you wanted left: 80px to work, you could add position:relative or position:absolute - fixed would work too. (example) As the example demonstrates, this isn't really an effective way to line the elements up though. It would be better to either float them or make them inline.
#item_2 {
width: 80px;
left: 80px;
background: #3b7328;
position: relative;
}
Here is a JS Fiddle, next time you should set one up. It's very useful for other people to see your issue.
<!--Header and Footer-->
<div id="header">
<div class="items">
<div id="item_1"> Item 1 </div>
<div id="item_2"> Item 2 </div>
</div>
</div>
#item_1 {
width:80px;
color: black;
background: #fff;
position: relative;
float: left;
display: inline-block;
}
#item_2 {
float: left;
width: 80px;
background: #3b7328;
position: relative;
display: inline-block;
}
http://jsfiddle.net/ws5ew/