How to make a short horizontal line over an image? - html

I am trying to draw a short horizontal line over an image. Like a quote followed by horizontal line followed by author name. An example of what I'm trying to do is below.
How can I achieve this in html css?

You can use either an hr or a div with a border. I made a simple example, hope it helps.
html, body{
height: 100%
}
body{
background: lightslategray;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.quote {
background: url('http://lorempixel.com/g/600/200/abstract/?random=true');
color: white;
background-size: cover;
padding: 2rem;
text-align: center;
}
hr {
width: 40%;
}
<div class="quote">
<h1>A dream doesn't become reality through magic<br/> it takes sweat, determination and hard work.</h1>
<hr />
<h2>Colin Powell</h2>
</div>

you can use the horizontal line in html <hr>

You can do it in several way. How you go about it will depend on what you are trying to accomplish. Here are two ways; one using br element and the other using a div (it could be a label too). If you want to use a different color and add more style to it, you want to go with the div
.motto, .author{
display: block;
text-align: center;
}
.motto{
font-size: 1.85em;
}
.br{
width: 50%;
height: 2px;
margin: 25px auto;
border-radius: 2px;
background-color: #0088ee;
}
<i class="motto">It is better to be feared than loved if you cannot be both</i>
<div class="br"></div>
<!-- <hr/> -->
<strong class="author">Niccolo Machiavelli</strong>
Comment out <div class="br"></div> and uncomment <hr/> to see the two results.

Related

Line Break between text + button isn't working in h1

I'm trying to move the button underneath the text (which currently says "test?") but the line break(s) is doing nothing. I've googled everything I can think of and can't find anything about this so any help is appreciated!
Html and the CSS for the button if that's relevant:
.custom-button,
.custom-button:visited {
display: flex;
justify-content: center;
align-items: center;
padding: 5px 10px;
border: 2px solid #FFF;
}
.custom-button:hover {
background: #000;
color: #FFF;
}
<h1>
Test? <br>
<a class="custom-button" href="#scroll">Click Me</a>
</h1>
EDIT: Here's the code for the header as well, it seems like the display: flex part is causing the problem but how do I centre the text vertically without that? This is all new to me, so thank you guy for the help!
h1 {
font-size: 62px;
font-family: "Sofia", sans-serif;
color: cyan;
text-align: center;
vertical-align: middle;
background-color: grey;
background-repeat: no-repeat;
background-position: center;
margin: -10px;
height: 110%;
display: flex;
justify-content: center;
align-items: center;
}
This is caused by the combination of display: flex and justify-content: center. Flexboxes are typically used to center things, so if you don't want it centered, there is little reason to use one.
Simply remove display: flex if you want it to set underneath the text. You can also remove justify-content and align-items when you do, as neither of those will affect anything if you remove display: flex:
.custom-button,
.custom-button:visited {
padding: 5px 10px;
border: 2px solid #FFF;
}
.custom-button:hover {
background: #000;
color: #FFF;
}
<h1>
Test? <br>
<a class="custom-button" href="#scroll">Click Me</a>
</h1>
Alternatively, if you want them both centered, you will need to apply your flex rules to both elements, along with flex-direction: column to ensure that the flexbox is vertical as opposed to horizontal:
.custom-button,
.custom-button:visited {
padding: 5px 10px;
border: 2px solid #FFF;
}
.custom-button:hover {
background: #000;
color: #FFF;
}
h1, a {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
<h1>
Test? <br>
<a class="custom-button" href="#scroll">Click Me</a>
</h1>
I think there is no need for the line break,
you need just to wrap the text into < p > tag
<h1>
<p>Test?</p>
<a class="custom-button" href="#scroll">Click Me</a>
</h1>
You can use a regular div instead of h1, like this:
<div>
<div>Test?</div>
<div><a class="custom-button" href="#scroll">Click Me</a></div>
</div>
So, divs will behave as line breaks by default (i.e. display: block).
Also, you might want to add some additional styling, as well.

How to get a box like this on a webpage?

am looking for a way to get a box like this on a webpage Image here
This is actually reddit's theme and I would like to understand how to create a box like this with a different color and then another box sticked with it in white. I couldn't come up with a term to google for this, nonetheless, I still googled things like ''box with css'', ''box table in css'' etc. but no luck.
What this is called and how to exactly do this?
The height/width are probably going to be off for what your ultimate code will need, but with this snippet you can get the idea of adding border-radius to an outer card element, and then border-top-right-radius and border-top-left-radius to the inner card-header element to get the desired effect.
The display: flex on the card-header element allows you to use justify-content: space-between to push the title and the ellipsis away from each other with minimal code.
I added some other styling to just give the same sense of colors, font style, etc.
.body {
display: block;
background: #FAFAFA;
width: 1000px:
height: 1000px;
padding: 25px;
}
.card {
background: #fff;
border-radius: 5px;
width: 300px;
height: 400px;
}
.card .card-header {
display: flex;
justify-content: space-between;
color: #fff;
font-family: sans-serif;
font-size: 12px;
font-weight: bold;
letter-spacing: 0.5px;
background: #4BA2AF;
padding: 10px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
<div class="body">
<div class="card">
<div class="card-header">
<span class="title">COMMUNITY DETAILS</span>
<span class="ellipsis">…</span>
</div>
</div>
</div>

Separating two divs

I'm creating a page, at the top of which there is a button (aligned to the right), followed by the main page content in a div.
I've encountered an issue when trying to separate the button and the main content div. The two divs are currently overlapping. I don't imagine this to be a huge issue, but I'd like to clarify what the most accepted way of separating these would be, rather than just messing about with margins etc.
.view-all-container {
display: block;
float: right;
margin-bottom: 40px;
}
.view-all {
background-color: #808080;
color: #fff;
padding: 10px;
}
.main-section {
height: 400px;
background-color: #ebebeb;
}
<div class="view-all-container">
<a class="view-all">View our range of holiday homes</a>
</div>
<div class="main-section">
</div>
I've found that when I add a margin-top: 50px to .main-section the button travels with it, as if it's contained within the same div.
If you are looking for best practices then consider the following:
1) Avoid using float. There are many better ways to get elements where you want them without needing to revert to a complicated process. The biggest problem with float is that it removes your element from the normal DOM flow. https://designshack.net/articles/css/farewell-floats-the-future-of-css-layout/, https://www.webdesignerdepot.com/2014/07/the-secret-to-designing-website-layouts-without-css-floats/
2) If you are navigating, then use the <a> tag. If you are doing something on the same page use a <button> or <input type='button'/> https://davidwalsh.name/html5-buttons
Here is a simple fix for what you want:
.view-all-container {
margin-bottom: 10px;
text-align: right;
}
.view-all {
background-color: #808080;
border: none;
color: #fff;
padding: 10px;
text-align: middle;
}
.main-section {
height: 400px;
background-color: #ebebeb;
padding: 5px;
}
<div class="view-all-container">
<button class="view-all">View our range of holiday homes</button>
</div>
<div class="main-section">
Stuff in the main section
</div>
I removed the float and changed to text-align. The <div> is already display: block so I removed that.
I assumed that your button at the top was to make changes on the active page so I changed the html from an <a> tag to a <button>.
If you don't want to use text-align then try flex-box:
.view-all-container {
display: flex;
justify-content: flex-end;
margin-bottom: 10px;
}
.view-all {
background-color: #808080;
border: none;
color: #fff;
padding: 10px;
}
.main-section {
height: 400px;
background-color: #ebebeb;
padding: 5px;
}
<div class="view-all-container">
<button class="view-all">View our range of holiday homes</button>
</div>
<div class="main-section">
Stuff in the main section
</div>
One of my favorite quotes about using float comes from this article: https://www.sitepoint.com/give-floats-the-flick-in-css-layouts/
If you’re new to CSS layouts, you’d be forgiven for thinking that using CSS floats in imaginative ways is the height of skill. If you have consumed as many CSS layout tutorials as you can find, you might suppose that mastering floats is a rite of passage. You’ll be dazzled by the ingenuity, astounded by the complexity, and you’ll gain a sense of achievement when you finally understand how floats work.
Don’t be fooled. You’re being brainwashed.
You just need to clear the float with clear:right on .main-section
.view-all-container {
display: block;
float: right;
margin-bottom: 40px;
}
.view-all {
background-color: #808080;
color: #fff;
padding: 10px;
}
.main-section {
height: 400px;
background-color: #ebebeb;
clear: right;
}
<div class="view-all-container">
<a class="view-all">View our range of holiday homes</a>
</div>
<div class="main-section">
</div>

How to TRULY vertically center text with flexbox?

So vertically centering text seemed simple enough with justify-content and align-items center but when I looked closely I can see that the text isn't truly centered. It has less spacing at the top of the character. I tried to investigate further by searching online and I found this https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align but there must be a simpler solution to this.
Example
https://jsfiddle.net/z7cy487o/
html,
body {
height: 100%;
}
.box {
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
<div class="box">
C
</div>
The way you perceive that depends on which characters you are using. I copied your example twice to show different situations:
In the second version I only used the letter "y", which has a descender, i.e. a part that extends below the baseline, to the lower border of the area which is defined as line-height. On the other hand, it doesn't go up the whole way, so it seems exactly the opposite of the first version (letter "C") concerning vertical alignment.
In the third version I used both of those letters combined in a word. Here you can see that the different characters/letters together indeed do extend across the whole width, so the vertical centering is correct as it is.
Line-height (and in relation to that, vertical alignment of letters) does not depend on which letters are used - it always applies to all possible letters/characters, even if they are not used in that particular situation.
html, body { height: 100%; }
.box
{
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
<div class="box">
C
</div>
<div class="box">
y
</div>
<div class="box">
Cyborg
</div>
This solution based off a modified version of Center text character ☢ vertically and horizontally within a circle (CSS)
It seems to work with dynamic heights, but as Johannes mentions in the comment of his answer. I believe the solution will only work well with my situation.
html,
body {
height: 100%;
}
.box {
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
.char {
line-height: 1px;
font-family: sans-serif;
font-weight: 500;
position: relative;
top: 0.05em;
}
<div class="box">
<span class="char">C</span>
</div>

Line segment won't stay under h2 tag

So, I've just started learning HTML/CSS and I've been trying to figure out how to 'stick' a line under an h2 tag. What I mean by this is that in the HTML there is an h2 tag called Instructions followed by a div tag that contains 3 other divs that make up a line segment. By default the line is on the left side (naturally), but what I want to do is have the line stuck under the h2 tag so when the browser is extended or shrunk the line stays directly under the h2 tag instead of moving across the screen by itself.
I came across this site: http://www.barelyfitz.com/screencast/html-training/css/positioning/ and I was using it to try and see if absolute/relative positioning would help here. I guess I'm doing it horribly wrong since it doesn't seem to help.
I'm providing HTML/CSS and a jsfiddle below (The jsfiddle doesn't show how the line moves around when the browser is extended/shrunk, though so I'm hoping you get what I mean). If you can help guide me or give me some resources to understand what I need to do better that would be great :D
I'm sure this is trivial, but I'm trying to do my due diligence in learning it. There were a lot of different methods (I think) I found, but they seemed kinda complex.
HTML
<div id="instructions_box">
<h2>Instructions</h2>
<div class="line_divider">
<div class="blue_line"></div>
<div class="yellow_line"></div>
<div class="blue_line"></div>
</div>
</div>
CSS
#instructions_box{
display: inline-block;
//position: relative;
}
.line_divider{
background-color: aqua;
//position: absolute;
//bottom: 0;
//right: 2rem;
}
.blue_line{
height: 2px;
width: 50px;
background-color: rgb(0,0,139);
float: left;
}
.yellow_line{
height: 2px;
width: 90px;
background-color: yellow;
float: left;
}
#instructions_box h2{
text-align: center;
}
https://jsfiddle.net/10szzwvs/1/
Thanks
The wrapping you're seeing is, I think, due to the fixed widths you're using. Change your line width to percentages and it wont wrap on any size screen. Note you'll need to add your visual spacing elsewhere, e.g. on the h2 itself.
#instructions_box{
display: inline-block;
}
#instructions_box h2{
text-align: center;
padding: 0 25px 0; /* visual spacing */
margin: 0;
}
.line_divider{
background-color: aqua;
}
.blue_line{
height: 2px;
width: 30%; /* dynamic width here */
background-color: rgb(0,0,139);
float: left;
}
.yellow_line{
height: 2px;
width: 40%; /* dynamic width here */
background-color: yellow;
float: left;
}
<div id="instructions_box">
<h2>Instructions</h2>
<div class="line_divider">
<div class="blue_line"></div>
<div class="yellow_line"></div>
<div class="blue_line"></div>
</div>
</div>
You can always use CSS3 Flexbox. You've got to have the div of lines and the h2 in the same container as you already do. And then.
#instructions_box{
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}