Horizontal rule with an image centered - html

I've searched a whole bunch but couldn't find anything that was coming close to it..
I want to have a horizontal line that has an image centered in it..
What's the best way of achieving this with the HR tag or any different way?
This is the image that I want to use: http://www.dylanvanheugten.nl/images/logo.png
Thanks in advance!

This might get you started:
HTML:
<div class="line">
<span class="logo"></span>
</div>
CSS:
.logo {
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
padding: 0 5px;
height: 50px;
width: 50px;
background: #fff url(http://www.dylanvanheugten.nl/images/logo.png) no-repeat 50% 50%;
}
.line {
position: relative;
overflow: visible;
height: 1px;
background-color: #ddd;
border: 1px solid #ddd;
}
Here's a fiddle you can play with: http://jsfiddle.net/4tZLD/1/

You can refer to this article. Maybe you can find a solution that covers all the browsers (or at least the ones you care about):
http://www.sovavsiti.cz/css/hr.html

I think that you want something like this, if I understood right.
http://jsfiddle.net/9yjmU/
HTML:
<div class="image">
<img src="http://www.dylanvanheugten.nl/images/logo.png"/>
</div>
<div class="line">
</div>
CSS:
.image{
text-align: center;
}
.line{
border-top: 1px solid black;
margin-top: -20px;
}
You can see that I used a div with a border-top and a margin-top: -20px; so it's in the center of the image (which looks 40px; height).

HTML:
<div class="line">
<img src="http://www.dylanvanheugten.nl/images/logo.png" class="lineImg">
</div>
CSS:
.line {
border-bottom: 1px solid black;
text-align:center;
height:17px;
margin-bottom:17px;
}
.lineImg {
background-color:white;
padding:0px 5px 0px 5px;
}
see: http://jsfiddle.net/V5wj6/3/
the height and margin-bottom of .line need to be exaclty half the height of img, this way, the image will be vertically centered on the line and the following content wont be directly under the border.
in the .lineImg style the background-color makes it look better by removing the line underneath the img, and the padding gives it some more space, you will have to adjust the background-color to your page

This, hopefully, will finally deliver a simple solution to the never ending quest to centre a horizontally placed graphic and auto locate on resize. The calc() method is supported by most browsers. The below syntax uses a graphic with a 728px width.
Full width = 728px, get 50% = 364px. Then apply the following:
#imagecentre1 {
left : calc(100% / 2 - 364px);
/*rest of syntax */
}
It is important to ensure 'white space' either side of '+' and "-" this to ensure that values, both negative and positive work correctly and for the sake of continuity the practice should apply to '/' and '*'. I'm sure someone will confirm order of execution, from memory it will be +, -, x, /. Calc() has basic features, no 'auto'!!
Expect some limitations. Just give it wirl!

Related

CSS | White spaces in 100% height elements

I'm writing here because I've got a problem with CSS. I have a .container div that contains another div set to position:absolute, top:0, left:0 and width:100%; height:100%. However I keep seeing these kind of white spaces, that when I zoom in the page disappear. Any solution?
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
overflow: hidden;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
top: 0;
left: 36px;
width: calc(100% - 72px);
height: 100%;
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
That is just a draw.
Here you have the screenshot
Well, I'm not totally sure what to do, but the following changes seem to fix the problem for me. I changed:
Set .loop overflow from hidden to visible
Set .goMid top from 0 to -1px
The .goMid height from 100% to calc(100% + 2px)
When I moved the inner div underneath the border using top: -5px I still saw the whitespace until I changed the outer div overflow property to visible. Then if you stretch the inner div a little it seems to solve the problem. It helps that your outer div has a thick border.
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
/* HERE */
overflow: visible;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
/* HERE */
top: -1px;
left: 36px;
width: calc(100% - 72px);
/* HERE */
height: calc(100% + 2px);
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
For what it's worth, I think #MarkP may have a good point. Combining absolute positioning and flexbox does feel like maybe a code smell. But, I don't know what the context is in your code and my flexbox-fu is a little shaky.
ACTUAL GENERATED VIEW:
I added text to all 3 nested divs and got the following centered display.
You can see all your text is bunched together. I am going to review your code and the offer a way forward. You may find you need to re-word your problem to allow us to help you better. My assumption is you are trying to set-up a tool to navigate through some type of media, such as images or pages.
CODE REVIEW
In review of your Code i can see you are trying to use a 3 part display using flexbox. Except you have also included absolute positions which prevents relative display of the divs alongside each other. Now i know you are concerned about white space but i am going to suggest a way to better use flex-box as well as debugging the whitespace, although it would be better to start again with a appropriate flexbox structure.
WHITE SPACE DEGUGGING
My suggestion first would be to remove CSS that could be causing this and then re-introduce the CSS progressively. If you are using Google Chrome you can use the insight tool to adjust the live CSS. Simply right-click on the area you wish to inspect and the CSS being used there will be displayed. You can edit directly in the CSS display and it will change the page behaviour, this is great for debugging and seeing what CSS improves your layout. Once you find the CSS you need you can replicate that in your code.
I would start with removing the following and see how you go:
Remove overflow:hidden;
When you look closer you can see the style code allows for 36px for each div on the left and the right. There may be an image missing from the .goPrev and .goNext divs, where your white space is now. Not sure if you copied your code from somewhere or wrote this from scratch?
TRY STARTING WITH A NEW FLEX-BOX STRUCTURE
I recommend creating your divs from scratch using one of the approaches found here: Common CSS Flexbox Layout Patterns with Example Code . Flexbox is super simple and a great way to build mobile responsive layouts.

CSS resize an image and keep the native ratio

What i'm trying to do here, is to crop an image to replace white spaces of my div:
<div id="profilepicture" style='background:url()'>
<img src="utilisateurs/pp/<?php echo $userinfo['photoP']; ?>">
</div>
And here the stylesheet related to this:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
}
#profilepicture img{
width: 100%;
height: auto;
margin: auto;
}
Here is what it gives me :
I just want to crop left and right of the photo and zoom at the center to not have white spaces anymore !
Edit:
If i do
width: auto;
height: 100%;
it gives me this :
Now i just want it to be centered!
Any ideas ?
Using Clip
Use the clip property.
img {
position: absolute;
clip: rect(0, 100px, 100px, 0);
}
One thing to note. clip only works on something position: absolute or position: fixed. I don't know why. This limits its usefulness, but can be dealt with without too much trouble.
The second thing to note is about rect(). Its syntax (like many others in CSS) is rect(top, right, bottom, left);. Now pay attention here because it can be tricky. Both the top and the bottom values define the offset from the top border and the left and right values define the offset from the left border.
So clip: rect(40px, 260px, 150px, 80px); does the following.
Using Background-Image
Probably the easiest option in terms of assembly.
<div id="profilepicture" style='background-image:url('utilisateurs/pp/<?php echo $userinfo['photoP']; ?>')'>
</div>
div {
background-position: center;
}
This, more or less, does what you want, though it does have the side effect of using inline styles.
I suggest a background with "cover" property by moving the image to the background.
html:
<div id="profilepicture" style='background-image:url(utilisateurs/pp/<?php echo $userinfo['photoP']; ?>)'>
</div>
css:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
background-size:cover;
background-position:center;
}
Here is one way of doing it.
Set overflow: hidden on the parent container that contains the image.
You can then use absolute positioning to placed the left edge of the image to the center, and then use translateX to shift it to the left by 50% to center the image.
The net effect if to hide/crop the left and right edges of the image by hiding them (hide the overflow) in the parent container.
#profilepicture {
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
overflow: hidden;
position: relative;
}
#profilepicture img {
width: auto;
height: 100%;
margin: auto;
display: block;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
}
<div id="profilepicture">
<img src="http://placehold.it/400x300">
</div>
Center it, add a negative margin and hide the overflow:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
text-align:center;
overflow:hidden;
}
#profilepicture img{
width: auto;
height: 100%;
margin: 0 -50%;
}
<div id="profilepicture">
<img src="http://lorempixel.com/400/200/">
</div>
Here's what it looks like without the container:
<img src="http://lorempixel.com/400/200/">
OK, I hear you. I have just something very similar to this. There are a few ways to achieve this.
Use the not so well supported CSS property of object-fit - it does exactly what you want but you will need to use a poyfill if you want to support most browsers.
Set your image wrapper div to overflow:hidden as explained in this guide The guide is for circular images, so just omit the border-radius parts.
If you prefer to see an example I have done one for both options. First option is the one that is commented out and the second option is the active one: http://codepen.io/cactusa/pen/qZrobK
Reduce the height of the container (#profilepicture) because you have a square container with equal height and width but the img is a rectangle and doesn't have same height and width increasing the height of the img to 100% will cause distortion and look funny.

Divide two divs by another curved/arched div

I want to create a website layout containing several full width pictures, which are aligned vertically. The pictures shall be seperated by a curved element, which ideally is created with HTML/CSS, as the width could change and the curve shall always fill the 100% width.
I have uploaded a visualization of my problem here:
I have tried some stuff with the border-radius, like this: http://jsfiddle.net/37u4c/34/ but the results are not quite what I want. The height of the element shall remain always 20 px, but with the round border it gets smaller at the edges....
Any tips or ideas are greatly appreciated!
You can achieve this layout using border radius, the point is to make the element with border-radius wider than the viewport :
DEMO
Output :
HTML :
<div>
<img src="http://lorempixel.com/output/people-q-c-640-480-9.jpg" alt="" />
<div class="round">
<img src="http://lorempixel.com/output/people-q-c-640-480-7.jpg" alt="" />
</div>
</div>
CSS :
div{
position:relative;
overflow:hidden;
}
img {
width:100%;
display:block;
margin:0 auto;
}
.round {
position:absolute;
width:200%;
left:-50%; top:50%;
border-top-left-radius:100%;
border-top-right-radius:100%;
border-top:20px solid #fff;
border-right:20px solid #fff;
border-left:20px solid #fff;
}
.round img {
width:60%;
}
The problem with border-radius is that (imho) you can't get custom enough shapes.
A bit of googling got me to this pen.
I guess you could get what you want by creating an svg path element and using it as a separator (lines 36-44 of the html).
PATH REFERENCE
You could achieve this with a border-radius, I've made an example of it for you right here:
http://jsfiddle.net/zvP7s/2/
The CSS looks as following:
.full-width img {
width: 100%;
height: auto;
}
.top-picture {
height: 350px;
overflow: hidden;
}
.bottom-picture {
position: relative;
top: -200px;
overflow: hidden;
border-top: 2px solid white;
-webkit-border-top-left-radius: 50%;
-webkit-border-top-right-radius: 50%;
-moz-border-radius-topleft: 50%;
-moz-border-radius-topright: 50%;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
}
However, it does not look quite as what you want, and that is because I think you should not do this with a border radius. You could create an image of the arc you want and position it between your images.
EDIT
I will post another example of border-radius as there might be another way to do this.
EDIT 2
Nevermind, it looks even worse.

div includes img as content

I would love your help with an issue I have with my html code.
I have the following code:
<img id="logo" src="images/logo.png">
<div id="content">
<h2>header</h2>
<p>text</p>
</div>
and my css code is:
img#logo {
width: 300px;
position:absolute;
right: 10px;
z-index:-1;
}
div#content {
text-align: center;
border: 2px solid;
border-radius: 25px;
margin: 100px 25 0 25px;
}
My problem is that the div includes the image within its borders (so it pulls the image to the margin of 100px from top.
When I include an <br> after the <img> it won't happen but that isn't the best way to solve it.
Does anybody know how to solve this?
this is happening because you have positioned your image absolutely meaning it is taken out of the flow of the page. If you are just wanting to place the image on the right, try using
float:right;
margin-right:10px;
instead of absolute positioning. You can then ensure the content div appears below the image by adding clear:right to it's styles
top position is missing to the absolute logo image. And a typo in right margin (missing px) -
img#logo {
width: 300px;
position:absolute;
right: 10px;
top: 0;
z-index:-1;
}
div#content {
text-align: center;
border: 2px solid;
border-radius: 25px;
margin: 150px 25px 0 25px;
}
Fiddle

making two divs line up side by side without gap

I have seen people ask questions about how to get two divs to line up side by side. I can get mine to do that just fine.
My problem is that they will not smash up against each other. There always seems to be a gap.
For example, I have a wrapper div with a width of 500px. Inside that div I have two other divs with widths of 250px.
They will not line up next to each other because there is not enough space for each other.
When I set the width to 248px they do line up but with a 4px gap between each other.
I have an example of this code located here:
https://c9.io/riotgear66/day1/workspace/sams/html/index.html
Please feel free to take a look at it and try adjusting it with your browser's element inspector.
The layout problem is the result of applying display: inline-block to the div elements.
Any white space between those div elements are taken into account when laying out the content.
You could remove the white space (linefeed or carriage return) between the div's if you don't mind how your source code looks.
Since your parent container has specific dimensions (500px x 300px), I would use absolute positioning to place the child elements. This would make it easier to position your logo motif over the other images.
You could also use floats as stated in other responses, not any easier or harder.
In this application, the layout is fixed so there are no design considerations for a responsive or flexible design, so either approach is valid.
Demo
You can see how this might work in the following demo: http://jsfiddle.net/audetwebdesign/hZ5dB/
The HTML:
<div class="container">
<div class="panel ul"></div>
<div class="panel ur"></div>
<div class="panel ll"></div>
<div class="panel lr"></div>
<div class="overlay"><span>Cats</span></div>
</div>
and the CSS:
.container {
border: 1px dotted blue;
width: 500px;
height: 300px;
position: relative;
margin: 0 auto;
}
.panel {
width: 250px;
height: 150px;
position: absolute;
}
.ul {
background: red url("http://placekitten.com/400/400") -50px -20px no-repeat;
top: 0; left: 0;
}
.ur {
background: red url("http://placekitten.com/300/300") 0px -30px no-repeat;
top: 0; right: 0;
}
.ll {
background: red url("http://placekitten.com/350/250") -20px -20px no-repeat;
bottom: 0; left: 0;
}
.lr {
background: red url("http://placekitten.com/300/200") 0px -30px no-repeat;
bottom: 0; right: 0;
}
.overlay {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
width: 100px;
height: 100px;
background-color: red;
border-radius: 50%;
}
.overlay span {
display: block;
background-color: gray;
border-radius: 50%;
text-align: center;
vertical-align: middle;
width: 80%;
height: 80%;
margin: 10%;
line-height: 80px;
}
I also show how you can create the circular motif without having to modify the original background images, saves a bit of work with PhotoShop or similar.
You shouldn't be using
display: inline-block;
Make them:
float: left;
Here is a jsfiddle sample of how it should be.
http://jsfiddle.net/Tqdqa/
The problem lies in the white space in your HTML. When using display: inline-block, white space after elements is taken into account like Marc Audet said.
To fix it without changing your current method, you must remove that white space. The easiest way I've found to do so while still maintaining readability of the HTML is by commenting it out, or using <!-- after each element and --> before the next element. This prevents having to change the whole structure and you can make each one 250px again as well
You could also move the closing > to the next line, move everything after the opening <div> to the next line, or use margin-left:-4px; for each element after the first. Or use a method described by others here, floating it or using FlexBox
Here is the CSS Tricks page that references this situation and provides more detail