Floating an element to the top right of its container - html

I have a HTML file which I cannot edit.
<section>
<h2>Section heading</h2>
<p>Paragraph text</p>
<img src="image.jpg" />
</section>
The design is asking for the photo to be in the top right of the section, which is easy if the image is the top child of section. Unfortunately the image in the supplied HTML is right at the bottom of the section in the HTML, so simply floating right won't work. With a little work I figured out how to absolute position a div while keeping it in the page flow by faking the flow with another floated element.
* {
box-sizing: border-box;
}
section {
position: relative;
outline: 1px solid #CCC;
}
div {
display: inline-block;
width: 140px;
height: 140px;
background-color: green;
border-radius: 100px;
position: absolute;
top: 0;
right: 0;
}
h2::before {
content: "";
display: inline-block;
float: right;
width: 150px;
height: 150px;
outline: 1px solid red;
}
h2 {
outline: 1px solid blue;
}
See https://jsfiddle.net/tnxhy0po/3/
Div - Using it to demonstrate an image in the fiddle.
Red outline - Right floated ::before pesudo element for
the header.
Blue outline - Header outline.
Silver outline - Section
outline.
Now for the problem.
The header contains the words "Meet the Owner, Julie" and Julie is meant to be on a separate line. If I limit the width of the header in order to do this, the floated spacer element gets contained in the width of the header, which means that text below it doesn't flow up to the image.
See https://jsfiddle.net/s7eke1gy/16/
I'm not sure how to make the image float in the top right corner of the section. Placing it there is easy but making it a part of the flow isn't.
Alternatively, the current float + absolute position solution would work if I could find some way to get "Julie" to move to the next line.
Edit: While testing I had set the section to a max width and forgot to remove it. The width of section is dynamic and as such 100%. I've removed the max-width property from the section in here and in the jsfiddles. Sorry for the confusion!

If I understand you correctly, you want to put Julie on a separate line and leave the text floating like on https://jsfiddle.net/tnxhy0po/1/
You can try to combine before and after pseudo elements, use :before to wrap h2
( move Julie to new line ) and :after to wrap paragraph.
h2::before {
content: "";
display: inline-block;
float: right;
width: 150px;
height: 25px;
outline: 1px solid red;
}
h2::after {
content: "";
display: inline-block;
float: right;
width: 10px;
height: 120px;
outline: 1px solid red;
}
You can see result on https://jsfiddle.net/s7eke1gy/13/
Hope I understand you and this can help.

Related

Gap within inline-block elements with different high

I have container with css elements. All of the elements has display: inline-block property. The problem is that one of the element is twice hire than the rest and instead of having two elements on the side I have only one and a lot of white space. This is how it looks:
my css is:
.productBlock {
display: inline-block;
background-color: darkgray;
height: 271px;
width: 161px;
margin: 3px;
}
.productBlock-higher {
background-color: darksalmon;
height: 548px;
width: 161px;
margin: 3px;
display: inline-block;
}
How can I remove the white space and add element another element there?
I would like to add move two elements on the right side of the higher div. It should look like this:
if I understand correctly, you need to set the vertical align top
https://codepen.io/opmasan/pen/vYNvbpZ
.productBlock {
vertical-align: top;
}
I solved it. I added:
.productBlock-higher {
float: left;
}

Displaying three elements inside a <div> in a same line

This is the html code:
<div class="produto_title">
<h2 th:text="${produto.name}"></h2>
Baixar
Comprar <span th:text="${produto.preco}"></span>
</div>
Could anyone give me a hint how to place the three items inside .produto_title in a same line (h2 floating at left and the two a floating at right).
Also, h2 has a border around item and the a is displayed like a button; I want add a line behind crossing all the "line" formed by this three elements, like this:
jsfiddle: https://jsfiddle.net/klebermo/sf7a6fnj/5/
ps.: also, how let the content of tag <span> inside the button, like the text?
An hr is a block element that's essentially just a line with a border.
I'd recommend sticking one of those at the top of the container and giving it a negative margin that vertically centers it in the parent. position: absolute is more trouble than it's worth.
https://jsfiddle.net/JackHasaKeyboard/0juqg4j7/
As for aligning the elements to the left and the right, I'll let you figure that out. There's many ways to accomplish it, the simplest way being with float.
I would look at twitter's bootstrap, specifically the row and col components.
You could do
<div class="row">
<div class="col-md-4">
// something here
</div>
<div class="col-md-4">
// something here
</div>
<div class="col-md-4">
// something here
</div>
</div>
This will all be displayed on the same line, splitting the row into equal thirds
btns{
height: auto; //Fix the span not being in the element
margin-top: 20px; //line everything up with the top of the heading element.
}
As for the line you can make a div and give it a absolute position (remember to give parent a relative position) and then position it accordingly.
.parent{
position: relative;
width: 100%;
}
.line{
height: 4px;
background-color: #000;
position: absolute;
z-index: -1;
top: 50%;
width: 100%;
}
This is a very bare-bones answer but it will be a start for you to go off.
For the first question, you can do that easily by manipulating margin or vertical-align properties. For example, if you put margin: 30px 5px; on your btn elements, it would be on the same line-ish.
Secondly, the <span> problem: if you set fixed width: 60px; of element (in your case .btn_comprar), text would either overflow from button to the right or bottom. Try setting width: 90px; or more on button elements, or height: auto; if you need it to be fixed.
Updated fiddle
First of all, you can't set a fixed width on a button if you want the text to not wrap. I recommend leaving the buttons at a width: auto and using padding to control the spacing around the text. I'd also bundle the styles for both button selectors, as they're exactly the same
Secondly, the only way (I know of) to get items to vertically align while they're float: right is by manually pushing them down, so I recommend making your buttons position: relative and setting a top: 25px;
/* Bundled both buttons together as they share the same styles */
.btn_free,
.btn_comprar {
float: right;
/* width: 60px; Removing this to allow the text to set the button width */
/* height: 20px; Removing this to let the line-height set the button height */
background: silver;
border-radius: 5px;
padding: 1px 15px;
box-shadow: 1px 1px 1px #000;
/* display: block; Removing this as floats are automatically display: block; */
/* text-align: center; Removing this since the text is already setting width */
background-image: linear-gradient(to bottom, #f4f5f5, #dfdddd);
font-family: arial;
font-size: 12px;
line-height:20px;
position: relative; /* Pushing buttons down a bit */
top: 25px;
margin: 0 10px; /* Spacing buttons out */
}
.btn_free:hover,
.btn_comprar:hover{
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
}
Thirdly, remember to use a clearfix so the .produto_title container maintains height!
.produto_title:after {
content: "";
display: table;
clear: both;
}
Lastly, rather than using another div to make the line, I'd use the :before psuedo-element on .produto-title (can't use :after if you're also doing a clearfix).
.produto_title:before {
content: '';
height: 1px;
background: #000;
width: 100%;
position: absolute;
left: 0;
top: 50%;
display: block;
}
Here's a working demo:
https://jsfiddle.net/zcqLbg4h/1/

Horizontally center span within another span in making a tooltip?

For a given word wrapped in a span element, I am trying to make a tooltip appear on hover using plain CSS only (without the various tooltip functions, the reason being that I need to have LaTeX displayed within the tooltip). The tooltip itself is a span within its parent span. Currently I'm getting a tooltip that is off center (left) while I'm trying to achieve the result on the right:
JSFiddle here. I've tried various combination of display (inline, table, block) with auto 0 margin etc. unsuccessfully.
Try adding the following css property and let me know how that works for you:
.link-cont:hover .tooltip{
display: table;
left: 10%;
}
You ought to be able to adjust to suit your needs - 10% seemed to do the trick with this particular example.
(I've updated your JS fiddle for you with the above)
It's because your tool tip is position absolute. You can't use margin auto on absolute.
Try having your container to place the tool tip above the text absolute and then have another child element inside that is relative. You can the give that element margin auto.
A fairly easy way to center the tool-tip is to create a wrapper .tip-wrap that is
a child element of the link text block .link-cont. tip-wrap has the same width
as its parent and has text-align: center to center the inline-block .tooltip.
.tip-wrap is absolutely positioned just above the top border of .link-cont.
Since you specifiec a height of 20px, and the decorative triangle is 5px, the
top offset is 25px.
Note: If your tool tip is longer than the link text, then you need to make some
adjustments to the positioning of the .tip-wrap. Specify a sufficiently long
width (the exact length does not matter), and then apply a negative margin equal
to half of the specified width.
.tip-wrap {
position: absolute;
left: 50%;
margin-left: -100px;
bottom: 25px;
width: 200px;
display: none;
text-align: center;
}
.link-cont {
display: inline-block;
cursor: default;
position: relative;
background: grey;
width: auto;
text-align: center;
white-space: nowrap;
overflow: visible;
}
.tooltip {
background: #000;
color: #fff;
text-decoration: none;
padding: 15px;
border-radius: 10px;
display: inline-block;
height: 20px;
height: auto;
text-align: center;
}
.link-cont:hover .tip-wrap {
display: block;
}
.tooltip .tooltip-triangle {
display: block;
position: absolute;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
content: "";
}
.tooltip .tooltip-triangle {
border-top: 5px solid #000;
bottom: -5px;
left: calc(50% - 5px); /* slightly more accurate */
}
<p>Text.
<br>
<br>
<br>This is a <span class="link-cont">loooooooooooooooooooooooooong
<span class="tip-wrap"><span class="tooltip ">the content<span class="tooltip-triangle"></span></span>
</span>
</span> word.</p>
<p>This is a sentence containing a <span class="link-cont">short
<span class="tip-wrap"><span class="tooltip ">the short word content<span class="tooltip-triangle"></span></span>
</span>
</span> word and a few extra at the end.</p>

Having trouble positioning text inside a box

I am having an issue with positioning text inside a div. I want the image on the right top corner (which I was able to do) and the text kind of center the bottom text in the box.
This is an example of what I want to do: http://jsfiddle.net/Lucky500/Nq769/
I created a div .bottom_box and added:
.bottom_box {
position: relative;
bottom: -50px;
left: 50px;
}
Is there an easier or more correct way to do this?
Alright -
Added text-align:center to your and elements.
Set your outer_box position to relative.
Set the img value to absolute and positioned with 0.25 em top and right instead of margin.
http://jsfiddle.net/mr_mayers/Nq769/2/
.outer_box {
border: solid #6ac5ac 3px;
display: inline-block;
width: 250px;
height: 200px;
margin: .5em;
Position: relative;
}
.bottom_box {
position: relative;
bottom: -50px;
}
p {
color: blue;
text-align: center;
}
img {
position: absolute;
padding: 3px;
top: 0.25em;
right: 0.25em;
}
h1 {
text-align: center;
color: red;
}
You can achieve your layout as follows:
For this HTML:
<div class="outer_box">
<img src="http://placehold.it/100x50">
<div class="bottom_box">
<h1>$25 OFF</h1>
<p>$25 off your first cleaning!</p>
</div>
</div>
Try the following CSS:
.outer_box {
border: solid #6ac5ac 3px;
display: inline-block;
width: 250px;
height: 200px;
margin: 0.5em;
}
.bottom_box {
clear: both;
border: 1px dotted gray; /* for demo only, optional */
}
img {
float: right;
padding: 3px;
margin: 0 0 1em 1em;
}
p {
color: blue;
margin-left: 50px;
}
h1 {
color: red;
margin-left: 50px;
}
Since your image is floated, simply clear the .bottom-box.
Use margin-left on the child elements to get any white space.
See sample: http://jsfiddle.net/audetwebdesign/3SjRG/
You can use text-align: center if you are centering the p and h1 content, but I was not sure if you wanted ragged left or ragged right alignment on the text block;
You'd be better off using text-align:center and position: absolute
See example
There are some solutions.
An other way is to make the box relative and positioning the text and image inside absolute.
I would create a container div with a border for your box, then set the inner divs (one with your image and one with your text) to position absolute. then you can use top:0; right:0; for the picture on the right corner. then bottom:xx; and left:yy; for positioning the text div.
This is just a different method than you used. If it works, doesn't break in any situation, and is simple, then it's correct. Many ways to skin a cat in programming.

Correctly aligning image captions

How can I achieve a layout like this?
Right now I'm using this HTML:
<div class="image">
<img>
<div class="caption">
Caption Text
</div>
</div>
And this CSS:
.image {
background-color: #2A2A2A;
}
img {
max-width: 590px;
}
But the .image box is too big (since it expands to fit its parent):
The key is to not set a width for the img element, or the parent container. If the parent, .image is simply floated or in any other way adapted so that it shrinks to the size of its contents, this should work.
I used float to achieve the shrink-wrap aspect, but position: absolute; would do the same, as would display: inline-block;.
There's a demo over at JS Bin, which uses some jQuery to swap the images around, but it does nothing to the width of any elements. The CSS is reproduced below:
.image {
float: left; // for the shrink wrap
padding: 1em; // To achieve the bordered effect
background-color: #666; // just for contrast
-moz-border-radius: 2em; // for that web 2.0 goodness...
-webkit-border-radius: 2em;
border-radius: 2em;
}
.image img {
-moz-border-radius: 2em; // no width, anywhere. Presumably width: auto, would
-webkit-border-radius: 2em; // work, but that's redundant, since it's the default
border-radius: 2em;
}
.image img + .caption {
width: 100%; // forcing the .caption to take up 100% of the width
background-color: #ffa; // of its parent, except for the padding, so that it's
} // the same width as the image above.
As #Kyle said, block elements adjust their width to fit their parent's.
Setting a block element as inline though, is not the correct approach: what you need to do, is to set the .image div as a floating element, thus achieving a similar result, while keeping the features of a block element. The css to do the trick should be:
.image {
float: left;
display: inline; /* needed to fix the (IE <= 6) "3 pixels out of nowhere bug" */
/* whatever code you may find appropriate in order to render the rounded border */
}
.image .caption {
clear: left;
}
I left to you any further style improvement you may feel needed.
If you set the width of the .image box to the same width as the image, then apply padding to the .image box, you will get the border you are looking for because when you specify width, padding gets added to it.
So basically, you would need the following CSS:
.image {
padding: 10px;
width: 300px; /* assuming the picture is 300px */
}
Try the following:
.image {
position: relative;
width: 250px;
}
img {
border: 15px solid #777777;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
width: 100%;
}
.caption {
border-left: 15px solid #777777;
border-right: 15px solid #777777;
border-bottom: 15px solid #777777;
position: absolute;
width: 100%;
bottom: 0px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
<div class="image">
<img src="yourImage" height="150px" />
<div class="caption">
Caption TextCaption TextCaption TextCaption TextCaption Text
</div>
</div>
Now the reason I have applied 3 borders to the caption div is because you do not know the width of the image without the border, but you do know the width of the border for the image. Applying the same border to the caption will give the caption the same width. Of course you will need to adjust the width of .image and the height of the img tag (this can be done through css), but the rest will be done for you. Also the caption div will resize for larger captions.
Regards,
Richard
PS this code has been tried and tested in Chrome - it works fine.
Since divs are block-level elements, they expand to fit their parent.
It may not be the best solution, but if you don't know the size of the image ahead of time, you could do the below:
.image
{
padding: 10px;
max-width: 590px;
disply: inline;
}
.caption
{
background-color: #2A2A2A;
disply: inline;
}
The above will cause the img div to be rendered as an inline element which will shrink it to fit the content rather than its parent, and the padding will add the border.
I have come up with another solution. I dont believe David Thomas' answer makes the caption appear within the image (by all means correct me if I am wrong), so try the code below (I have used a combination of my code and Davids).
.image {
position: relative;
float: left;
border: 15px solid #777777;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
.caption {
position: absolute;
bottom: 5px;
left: 5px;
}
.image-container {
position: relative;
}
<div class="image">
<img src="/Images/header1.png" />
<div class="caption">
Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text
</div>
</div>