So I have a box on my website, which has a title and an image to the right. The issue is that when the width of the screen is too small the image gets pushed down. Instead what I want is that the title breaks to a new line.
I've tried using maxwidth on the h2, and various other options with display, width and position but I can't get it to work. I feel like there should be a trivial solution, but I haven't been able to find it.
HTML
<div class="projects-container">
<h2>Title that gets too long</h2><img src="img/ddicon.jpg" width="30px">
<div id="website">
<p>I like to build websites, I would like to be better at it. So then this happened.</p>
</div>
</div>
CSS
.projects-container {
border: 5px solid black;
border-radius: 20px;
padding: 10px;
margin-bottom: 20px;
}
.projects-container > h2 {
margin: 5px 0px;
display: inline;
}
.projects-container > img {
float: right;
}
You will need to make 2 simple changes.
1- Place <img> first in HTML.
<div class="projects-container">
<img src="img/ddicon.jpg" width="30px">
<h2>Title that gets too long</h2>
</div>
2- Remove display: inline and add overflow: hidden:
.projects-container > h2 {
margin: 5px 0;
overflow: hidden;
}
As you are using float on child elements, so don't forget to set layout of parent element. There are many ways of setting layout. For example:
.projects-container {
overflow: hidden;
}
Or
.projects-container:after {
display: block;
clear: both;
content: '';
}
.projects-container {
border: 5px solid black;
border-radius: 20px;
padding: 10px;
overflow: hidden;
margin-bottom: 20px;
}
.projects-container > h2 {
margin: 5px 0px;
overflow: hidden;
}
.projects-container > img {
float: right;
}
<div class="projects-container">
<img src="img/ddicon.jpg" width="30px">
<h2>Title that gets too long</h2>
<div id="website">
<p>I like to build websites, I would like to be better at it. So then this happened.</p>
</div>
</div>
width add with calc function
.projects-container > h2 {
margin: 5px 0px;
display: inline-block;
width:calc(100% - 35px);
}
https://jsfiddle.net/sjmxd9f4/
Related
I'm trying to implement some bootrap css - I don't want the entire css so i've just copied some css for certain components such as alerts and the media component.
However the media-right css doesn't seem to work. I have the following:
<style>
.media,
.media-body {
overflow: hidden;
zoom: 1;
}
.media, .media .media {
margin-bottom: 15px;
position: relative;
padding: 20px;
border: 1px solid #ddd;
}
.media-body,
.media-left,
.media-right {
display: table-cell;
vertical-align: top;
}
.media-middle {
vertical-align: middle;
}
.media-left,
.media > .pull-left {
padding-right: 25px;
}
.media-right,
.media > .pull-right {
padding-left: 25px;
}
.media-object {
display: block;
}
.media-heading {
margin-top: 0;
margin-bottom: 5px;
}
.media-body p {
margin: 10px 0px 10px 0px;
}
<div class="media">
<div class="media-body">
<h4 class="media-heading">
Football
</h4>
<p>xyz</p>
</div>
<div class="media-right" style="width:200px">
<img class="media-object" src="xyx" alt="">
</div>
</div>
The image is to the right of the media-body but when there isn't enough content the image looks like it's in the middle.
Is there some css I'm missing or how do i ensure the image is to the right even when there is no content?
You need to import the CSS from the #media too, there are some pull-right and pull-left classes there. It should fix your problem for different size of screens.
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>
the following is my CSS code:
.portrait
{
width: 400px;
position: relative;
display: inline-block;
background-color: #4E5555;
}
.portrait img
{
width: 150px;
float: left;
padding-right: 20px;
}
.portrait h4
{
text-align: left;
margin: 0px 0px 0px 0px;
color: #fff;
}
And the following is my relevant html code:
<div class="portrait">
<img src="images\filmmakers\Aboui, Julian\JulianAboui-web.jpg">
<h4>Julian Aboui</h4>
</div>
<div class="portrait">
<img src="images\filmmakers\Alter, Aaron\AaronAlter-web.jpg">
<h4>Aaron Alter</h4>
</div>
<div class="portrait">
<img src="images\filmmakers\Abrahams, Pia\PiaAbrahams-web.jpg">
<h4>Pia Abrahams</h4>
<h4>STUFF STUFF STUFF STUFF STUFF STUFF STUFF STUFF STUFF STUFF STUFF</h4>
</div>
<div class="portrait">
<img src="images\filmmakers\Asnani, Shailen\ShailenAsnani-web.jpg">
<h4>Shailen Asnani</h4>
</div>
My output is the following:
http://i.imgur.com/YRkJvmn.png
I think I know what the problem is, but I'm not sure how to fix it. The last container element (on the bottom right) is placed further down because it thinks it is under the text. Is that correct? I'm unsure how to fix that.
Any help is appreciated, thank you!
Divs are finicky. I would recommend using ul with display:block inline. Just look at the page source of a web site where you can see something like that working. A snippet from my css where it works (I have a div inside the li withe text and multiple images)
div.list_holder {margin-bottom: 10px; clear: both; font-style:normal;}
ul.user_list {display:block; margin: 0px auto;}
li.list_item {list-style: outside none none; margin-right: -100%;
position: relative; padding: 0px; clear: none;
margin-bottom: 10px !important;
border: 2px solid !important; min-height: 325px;
max-width: 206px; float: left; margin-right: -100%;
width: 23.5%;border-radius: 2px;}
Not sure but a simple solution would be to display it as a table
.row {
display:table-row;
}
.portrait
{
width: 400px;
position: relative;
display: table-cell;
background-color: #4E5555;
}
.portrait img
{
width: 150px;
float: left;
padding-right: 20px;
}
.portrait h4
{
text-align: left;
margin: 0px 0px 0px 0px;
color: #fff;
}
And wrap the two rows you want in <div class='row'>
In my opinion for positioning these kind of containers you don't need to sue postion: relative if you have a bigger contaner/wrapper, which wraps the portraits and it is positioned via margins. In that case you can use margin to position your container. Also It is a good idea to use figure tag for the images if you want to style them little bit better and make them display: block if you want the text to be under the image.
I'm trying to line up these image links in a row but having some difficulty with it. If I add some CSS parameters like float left and float right, it ends up positioning it in weird spots on the page.
This is what I got:
.preview {
position: relative;
width: 300px;
height: 300px;
overflow: hidden;
}
.preview img {
width: 100%;
height: 100%;
padding: 0;
}
.preview > div {
background-color: rgba(0,0,0,0.75);
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
-webkit-transition: opacity 0.3s linear;
text-shadow: #000 1px 1px 0;
color: #ccc;
text-align: center;
}
.preview:hover > div {
display: block;
opacity: 1.0;
}
.preview > div div {
padding: 20px 40px;
}
.preview h2 {
font-size: 1.2em;
letter-spacing: 0.2em;
text-transform: uppercase;
margin-bottom: 10px;
}
.preview p {
margin-bottom: 10px;
}
<a href="http://www.page.com/album">
<div class="preview">
<img src="http://upload.wikimedia.org/wikipedia/commons/e/ec/Record-Album-02.jpg" title="Photo Albums" alt="" />
<div>
<div>
<h2>Photo Albums</h2>
</div>
</div>
</div>
</a>
<a href="http://www.page.com/storybook">
<div class="preview">
<img src="http://www.clipartbest.com/cliparts/RiA/6a5/RiA6a5eoT.jpeg" title="Digital Story Books" alt="" />
<div>
<div>
<h2>Digital Story Books</h2>
</div>
</div>
</div>
</a>
How can I line them up so that there is a half an inch of space in between?
Here is a preview of what I have:
http://jsfiddle.net/FZ2rZ/
This should do it:
.preview {
display: inline-block;
margin: 0 40px 0 0;
}
Inline-block for the div containing the image will display the images in a row as you'd like. The margin applies 40px to the right side of the images.
By default images are block elements, meaning they will take up the entire width of the page. Inline elements (like an or tag) do not collapse and will be on the same row. Learn more about display CSS property.
*edited to correct margin instead of padding.
Avoid float if you can. Floating is for a limited scenario. Usually it is appropriate to use display:inline-block;. That said, when you do, any space you have inbetween tags will also be displayed (you're making it "inline" and spaces are inline, too.)
You can take what you have an add this CSS:
a {
display:inline-block;
}
a:not(:first-child) {
margin-left:.5in;
}
Then, delete the space here
</a>
<a href="http://www.page.com/storybook">
to get
</a><a href="http://www.page.com/storybook">
Here's your modified fiddle.
I need a div to be positioned at the top inside its containing div, and leave unused space below itself. The default behavior seems to be the opposite, e.g. the contained div falls down to the floor of its containing div and leaves unused space above itself.
I assume that's quite a trivial thing to do, but I don't even know how to search for the solution on Google (tried "div float top", "div gravity" and some other meaningless searches...)
Here is my html code:
<div class="bonus">
<div class="bonusbookmakerlogo">
<a rel="nofollow" href="http://..." target="_blank"><img src="/img/box.png" alt="blah" title="blah"/></a>
</div>
<div class="bonustext">
<span>Bonus description.</span>
</div>
<div class="bonusdivider"></div>
</div>
And relevant css:
.bonus {
font-size: 90%;
text-align: justify;
margin: 1em 2em;
}
.bonusdivider {
margin: 1em 0 1em 0;
border: none;
height: 1px;
color: #999999;
background-color: #999999;
}
.bonusbookmakerlogo {
display: inline-block;
width: 20%;
}
.bonustext {
display: inline-block;
width: 70%;
}
The resulting layout is ok except the logo div (the one containing the img tag) that occupies the lower part of its containing div free space, while I need it to "fight" gravity and stay with its top edge hooked to the container top edge.
Thanks in advance for any help.
Here is a slight modification using float instead of inline-block.
Seems to work OK:
<div class="bonus">
<div class="bonusbookmakerlogo">
<a rel="nofollow" href="http://..." target="_blank"><img src="/img/box.png" alt="blah" title="blah"/></a>
</div>
<div class="bonustext">
<span>Bonus description.</span>
</div>
<div class="bonusdivider"></div>
</div>
And CSS:
.bonus {
font-size: 90%;
text-align: justify;
margin: 1em 2em;
height: 100px;
border: 10px solid red; /* test */
}
.bonusdivider {
margin: 1em 0 1em 0;
border: none;
height: 1px;
color: #999999;
background-color: #999999;
clear: both;
}
.bonusbookmakerlogo {
float: left;
width: 20%;
}
.bonustext {
float: left;
width: 70%;
}
The answer by #Marius George works and I think it is the cleanest possible solution, but here his a different one I've found meanwhile:
.bonusbookmakerlogo {
display: inline-block;
width: 20%;
vertical-align: top;
}