How to add a border next to an image? - html

I have two divs within a larger div. The first one has an image, the second has text.
In the first div, there is a border below the image currently.
http://jsfiddle.net/8f3arq2y/
#newsfeed_header {
padding: 10px 0px;
width: 98%;
position: relative;
}
#nf_image:after {
content: " ";
border-bottom: solid 1px #000000;
display: block;
}
#nf_text {
text-transform: uppercase;
font-size: 14px;
font-weight: bold;
font-family: Oswald;
}
<div id="newsfeed_header">
<div id="nf_image">
<img src="//dummyimage.com/35x35" />
</div>
<div id="nf_text">Read the latest</div>
<br/>
</div>
However I'd like the border to be next to the image, how can I do that?
|<img>__________________|
|text |

Here is how you can do it, also works with dynamic image size.
#nf_image {
display: table;
width: 100%;
margin-bottom: 10px;
}
#nf_image img {
display: table-cell;
vertical-align: top;
}
#nf_image:after {
display: table-cell;
content: "";
width: 100%;
border-bottom: solid 1px black;
}
UPDATED DEMO
#newsfeed_header {
padding: 10px 0px;
width: 98%;
position: relative;
}
#nf_image {
display: table;
width: 100%;
margin-bottom: 10px;
}
#nf_image img {
display: table-cell;
vertical-align: top;
}
#nf_image:after {
display: table-cell;
content: "";
width: 100%;
border-bottom: solid 1px black;
}
#nf_text {
text-transform: uppercase;
font-size: 14px;
font-weight: bold;
font-family: Oswald;
}
<div id="newsfeed_header">
<div id="nf_image">
<img src="//dummyimage.com/35x35" />
</div>
<div id="nf_text">Read the latest</div>
<br/>
</div>

You could use an empty div below the image to do this:
#newsfeedheader{
padding: 10px 0px;
width: 98%;
position: relative;
}
#border {
margin-top: -8px;
margin-bottom: 10px;
border-top: 2px solid red;
z-index: -10;
}
#nf_text{
text-transform: uppercase;
font-size: 14px;
font-weight: bold;
font-family: Oswald;
}
<div id="newsfeed_header">
<div id="nf_image"><img src="http://www.pixelsquish.com/wp-content/gallery/random-impulses/the-arch-jumpers.jpg" height="105" /></div>
<div id="border"></div>
<div id="nf_text">Read the latest</div>
<br/>
</div>
You give the div a negative top margin and a negative z-index so that it appears behind the image. That way the border you put on the div appears to be next to the image.

GOT IT!
I added a margin-left equal to the width of the image to the :after block. The bottom border now only starts immediately after the image.

Related

I am trying to make a responsive rectangle with an image to the left inside and text centered

I am trying to make a responsive tweet button with the twitter bird floated left, the text next to it and centered.
My code is:
.flex-rectangle {
float: left;
margin: 0 5px;
max-width: 500px;
text-align: center;
width: 200%;
background: #FFFFFF;
border: 7px solid #00A5EF;
}
/* Styles Twitter Bird png */
.image-wrapper {
padding-top: 10%;
position: relative;
width: 100%;
padding-bottom: 10%;
}
img .tweet {
float: left;
}
/* Tweet This: */
.span-content {
display: block;
color: #00A5EF;
}
.span {
display: block;
text-align: center;
font-family: OpenSans;
font-size: 36px;
color: #00A5EF;
}
<div class="flex-rectangle">
<div class="image-wrapper">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet" />
</div>
</div>
<div id="buttons">
<div class="span-content">
<span>Tweet This</span>
</div>
</div>
CSS
I've tried pretty much everything under the sun.
I can't seem to get the rectangle to shrink and widen when I resize the page or go into Dev Tools and use the mobile device pane.
I understand CSS less than I do JavaScript at this point. Not sure if I should use flexbox in this instance or how I would do that.
Here is the CodePen
you can use quotes using pseudo element ::before and a::after
Thank you. This works for the most part. However I can't get the
twitter bird to float left and the text to be beside it. Any
suggestions?
I used flexbox the text will be next to the twitter button on desktop view, and below on mobile view.
#import url(https://fonts.googleapis.com/css?family=Open+Sans|Satisfy);
/*Styles for whole page */
img {
max-width: 100%;
border: 7px solid #00a5ef;
}
#page-wrap {
display: flex;
flex-wrap: wrap;
justify-content: center
}
h1 {
font-weight: bold;
text-transform: uppercase;
font-size: 30px;
margin-top: 50px;
width: 300px;
line-height: 1;
font-family: 'Open Sans', sans-serif;
color: #1485C7;
text-align: center;
letter-spacing: 0;
}
/* On: */
h1 .center {
text-transform: capitalize;
font-weight: normal;
font-family: "Satisfy";
vertical-align: text-bottom;
line-height: 10px;
color: #1485C7;
}
h1 .bigger {
font-size: 46px;
color: #1485C7;
display: block
}
/* Rectangle 1: */
.flex-rectangle {
background: #fff none repeat scroll 0 0;
flex: 1 15%;
margin: 0 15%;
max-width: 300px;
padding: 10px;
position: relative;
quotes: "\201C""\201D";
text-align: center;
top: 0;
}
.flex-rectangle::before {
color: #00a5ef;
content: open-quote;
font-family: Georgia;
font-size: 25vw;
left: -15vw;
position: absolute;
top: 50%;
}
.flex-rectangle::after {
color: #00a5ef;
content: close-quote;
font-family: Georgia;
font-size: 25vw;
position: absolute;
right: -15vw;
top: 50%;
}
.text {
align-self: flex-end
}
.span-content {
display: inline-block;
color: #00A5EF;
margin: 0 auto;
padding: 5px;
border: 3px solid #00A5EF;
}
<div id="page-wrap">
<div class="flex-rectangle">
<div class="heading">
<h1>Random Quotes<span class="center">On</span><span class="bigger">Design</span></h1>
</div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet" />
<div id="buttons">
<div class="span-content">
Tweet This
</div>
</div>
</div>
<div class="text">
<h1>Random Quotes</h1>
</div>
</div>
you have to place the bird and the text to one div and code for the image element in order to code for the image part you have to call first the first parent div and other div in one code where the image element is located .flex-rectangle .image-wrapper imgto edit the code for image. and also you have to insert the html code for <span>Tweet This</span> inside the .image-wrapper to make the image go left and your text go center.
CSS CODE :
.flex-rectangle {
float: left;
margin: 0 5px;
max-width: 500px;
text-align:center;
width: 200%;
background: #FFFFFF;
border: 7px solid #00A5EF;
}
/* Styles Twitter Bird png */
.image-wrapper {
padding-top: 10%;
position: relative;
margin: auto;
max-width: 125;
max-height: 50px;
width: 100%;
padding-bottom: 15%;
}
.flex-rectangle .image-wrapper img {
float: left;
max-width: 50px;
max-height: 50px;
width: 100%;
}
/* Tweet This: */
.span-content {
display: block;
text-align: center;
color: #00A5EF;
}
.span {
display: block;
text-align: center;
font-family: OpenSans;
font-size: 36px;
color: #00A5EF;
}
HTML Code:
<div class="flex-rectangle">
<div class="image-wrapper">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet"/>
<div id="buttons">
<div class="span-content">
<span>Tweet This</span>
</div>
</div>
</div>
</div>

Why inline-block element position has wrong vertical position in a line

When i put my inline-block element (14x14px) in single-line row (height and line-height = 20px), it takes place not in the middle of it's parent (vertical). Line-height problem picture
Here's a Сodepen example
HTML
<div class="status status_success"> Success</div>
<div class="status status_busy"> Busy</div>
<div class="status status_missed"> Missed</div>
CSS
body {
font-size: 16px;
line-height: 20px;
}
.status {
position: relative;
display: block;
white-space: nowrap;
border: 1px solid #000; // block border for helping test
margin: 0 0 20px;
&:before {
content: '';
display: inline-block;
vertical-align: middle;
width: 14px;
height: 14px;
background-color: #d6d6d6;
border-radius: 50%;
}
}
Tell me, please, why is it happening?
The vertical-align: middle aligns the middle of the element with the middle of lowercase letters in the parent, which simply means the vertical alignment is not a 100% precise way to put an element in the exact middle of its parent.
Src: https://css-tricks.com/almanac/properties/v/vertical-align/
In below samples I added a wrapper (and span's in 2:nd sample, with font size matching the pseudo's size) to show how they interact and how you can do to make the outcome look better.
Note: As suggested by "Vangel Tzo", flex is one way that does the job better.
.wrap {
padding: 20px;
font-size: 16px;
font-family: "helveticaneuecyr", Arial, sans-serif;
line-height: 20px;
}
.status {
position: relative;
white-space: nowrap;
border: 1px solid #000;
margin: 0 0 20px;
}
.status:before {
content: '';
display: inline-block;
vertical-align: middle;
width: 14px;
height: 14px;
background-color: #d6d6d6;
border-radius: 50%;
}
.status_success:before {
background-color: #3ad994;
}
.status_missed:before {
background-color: #e83e3e;
}
.status_busy:before {
background-color: #f5be48;
}
.status span {
display: inline-block;
vertical-align: middle;
font-size: 14px;
}
<div class="wrap">
<div class="status status_success"> Success</div>
<div class="status status_busy"> Busy</div>
<div class="status status_missed"> Missed</div>
</div>
<div class="wrap">
<div class="status status_success"> <span>Success</span></div>
<div class="status status_busy"> <span>Busy</span></div>
<div class="status status_missed"> <span>Missed</span></div>
</div>
You could use display: flex for parent element (.status) and the align-self: center property to center it vertically.
.status {
position: relative;
white-space: nowrap;
border: 1px solid #000;
margin: 0 0 20px;
display: flex;
}
.status:before {
content: '';
display: inline-block;
width: 14px;
height: 14px;
align-self: center;
background-color: #d6d6d6;
border-radius: 50%;
}
An example: http://codepen.io/srekoble/pen/BKWJgx
As #LGSon explaination, the vertical-align is not a magical css, and its behaviour is never trusted by me. So I suggest an alternative way to align your elements in the way you want.
Because you already put position:relative in the .status, I suggest to use position:absolute to style for your generated content and it is more consistent between each browsers.
A codepen example: http://codepen.io/thovo/pen/MypQbW
try below code for horizontaly center code.
body { font-size: 16px; line-height: 20px; text-align:center;}
.status { float:none; position: relative; display:inline-block; white-space: nowrap; border: 1px solid #000; // block border for helping test margin: 0 0 20px;}
try below code for verticaly center code.
.status { display:table: width:100%; float:none; position: relative; display:inline-block; white-space: nowrap; border: 1px solid #000; // block border for helping test margin: 0 0 20px;}
.status:before { display:table-cell; vertical-align: middle;}

Can't align text next to image

I have the following code, and I can't get the text in the header-loggedout div to display centered within the borders. If I adjust the height, vertical margins, or padding of the div it always ends up moving the bottom border down for some reason. The image and text just won't align properly. How can I keep the text and image in (at least roughly) the same position but vertically align both to the middle between the top/bottom borders?
Here's a fiddle.
.header-lower {
position: relative;
display: table;
z-index: 0;
padding: 10px 0px;
width: 100%;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
.header-logo {
display: table-cell;
text-align: left;
margin: 0 0 20px;
vertical-align: inherit !important;
}
.header-logo a {
display: inline-block;
float: left;
max-width: 100%;
line-height: 0;
}
.header-loggedout {
font-size: 26px;
vertical-align: middle;
}
<div class="header-lower">
<div class="header-logo">
<a href="#">
<img title="" alt="alt" src="http://placehold.it/310x39" />
</a>
</div>
<div class="header-loggedout">
Test Text
</div>
</div>
You can set display of .header-loggedout as table-cell:
.header-loggedout {
font-size: 26px;
vertical-align: middle;
display: table-cell;
}
Fiddle Here
replace this class
.header-lower {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
display: table;
padding: 10px 0;
position: relative;
text-align: center;
vertical-align: middle;
width: 100%;
z-index: 0;
}
You can use absolute positioning
Fiddle here
.header-loggedout {
font-size: 26px;
position:absolute;
top:50%;
right: 20px;
transform: translateY(-50%);
}

CSS style heading - background must cover only text with a bottom border

I am trying to style a heading just like this:
But I need some help as I cannot get it correct.
I was planning on using gradient, but I must have only the text of the heading (with some padding) with a background color and gradient changes colour at a specific point in the table cell. The heading can be different words - some long & some short - so the background color cannot be stopped at a specific point. I also need the width of the cell to be underlined (border-bottom).
I am unsure if my HTML needs changing or my CSS needs changing or both or if this can be done.
My HTML code:
<div class="wrapper">
<div class="row">
<div class="heading">Heading</div>
</div>
<div class="row">
<div class="stuff">Just some stuff.</div>
</div>
</div>
My CSS code:
.wrapper {
border-spacing: 0px 0px;
display: table;
width: 100%;
}
.row {
display: table-row;
}
.heading {
border-bottom: 2px solid red;
background-color: lime;
color: blue;
direction: ltr;
display: table-cell;
font-weight: bold;
min-height: 25px;
overflow: hidden;
padding: 2px;
padding-left: 30px;
padding-right: 30px;
text-align: left;
text-transform: uppercase;
vertical-align: top;
}
.stuff {
width: 100%;
display: table-cell;
}
You should adjust the width of your heading to occupy that of only its contents. A common hack is to simply use display: inline-block. I changed a few of your styles and markup to create the lower border effect. Your wrapper and stuff classes were redundant so I removed them.
JSFiddle: http://jsfiddle.net/mkmeLzjL/11/
HTML
<div class="row">
<div class="heading">Profile</div>
</div>
<br>
<div>
<div>Just some stuff.</div>
</div>
CSS
#import url(http://fonts.googleapis.com/css?family=Open+Sans);
* {
font-family: Open Sans;
}
.row {
border-bottom: 2px solid black;
}
.heading {
background-color: black;
color: white;
display: inline-block;
font-weight: bold;
padding: 7px 30px 7px 15px;
font-size: 20px;
text-transform: uppercase;
}
Here's a solution using a pseudo-element: http://jsfiddle.net/npeqzm32/.
HTML:
<div class="wrapper">
<div class="row">
<h1>Heading</h1>
</div>
<div class="row">
<div class="stuff">Just some stuff.</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
border: 0;
}
body {
padding: 10px;
}
.row {
position: relative;
}
.row h1 {
font: bold 20px/2 Sans-Serif;
background-color: #222;
color: white;
display: inline-block;
padding: 0 10px;
text-transform: uppercase;
letter-spacing: 1px;
}
.row h1:before {
content: "";
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 3px;
background-color: #222;
}
I have made some changes to output somewhat similar what you want.
DEMO HERE
HTML
<div class="wrapper">
<div class="row">
<div class="heading">Heading</div>
</div>
</div>
CSS
.row {
border-bottom:4px solid black;
}
.row .heading
{
display:inline-block;
background-color:#111;
color:#fff;
font-size:24px;
font-family:calibri;
font-weight:bold;
padding:10px 20px;
}

My </p> tag is affecting the position of my div. Why is this?

I have this div wedged between two bars(other divs), though when I add text into the equation, the div gets repositioned down. It works as intended without the p element and its children. Here's a fiddle to demonstrate the issue: http://jsfiddle.net/57uSQ/
this is the HTML that is causing the problem:
<p>
<span class="name">DOLCE & GABBANA</span>
</br>
<span class="title">THE ONE</span>
</p>
And the correlating CSS:
.videoDesc {
display: inline-block;
border: 1px solid #000000;
border-right: 0px;
height: 200px;
width: 500px;
}
.videoDesc p {
display: inline-block;
margin: 0;
padding: 0;
}
.videoDesc .name {
display: inline-block;
padding: 0px;
}
.videoDesc .title {
display: inline-block;
padding: 0px;
}
.title {
font-family: Lekton;
font-size: 1.25em;
}
.name {
font-family: Oswald;
font-weight: lighter;
font-size: 2.5em;
letter-spacing: 10px;
padding-left: 5px;
}
You need to add vertical-align:top to .videoDesc:
.videoDesc {
display: inline-block;
border: 1px solid #000000;
border-right: 0px;
height: 200px;
width: 500px;
vertical-align:top;
}
jsFiddle example
The default vertical alignment is baseline, which is causing the behavior you see.