A better way to write this HTML/CSS? - html

I have a float left image with a title and a small text on it's right. My question is if there is a better way of writing it. I made a jsFiddle for this at http://jsfiddle.net/GjKTG/1/
this is the code without css
<div style="float: left;">
<div id="image">
<img src="http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png">
</div>
<p class="title">Here goes a title</p>
<div id="text">here goes two or more lines for content
</div>
</div>
Thank you

You'll probably get some varying answers. What you have isn't terrible, but I made an attempt at cleaning it up some: http://jsfiddle.net/GjKTG/18/. Some of this boils down to personal preference.
HTML
<div class="imageholder">
<img src="http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png">
<h3>Here goes a title</h3>
<p>here goes two or more lines for content</p>
</div>
CSS
.imageholder {float:left;}
.imageholder img { float: left; width: 50px; }
.imageholder h3 {color:#2D101F;font-family: georgia;}
.imageholder p { font-size: 11px; line-height: 13px;}
Changes:
Made the title into an h3 to give it some context. If something is a title, treat it like so.
Gave it one class and removed extra classes. Handling the rest with inheritance
Took out the inline styling
Got rid of the image wrapper div and handled that with CSS.
You'd possibly have some issues with the floats if you have multiples of these after another, but that's hard to say.

how about this:
html:
<div class="box">
<h1 class="title">Here goes a title</h1>
<div class="text">here goes two or more lines for content
</div>
</div>
css:
.title {
margin: 0;
color: #2D101F;
font-family: Georgia;
padding-bottom: 20px;
}
.text {
font-size: 11px;
line-height: 13px;
margin-top: -17px;
}
.box {
background: url("http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png") no-repeat top left;
padding-left: 40px; /* the width of the image */
min-height: 80px; /* the height of the image */
}
http://jsfiddle.net/ZYY9q/
and also remember, use ids as css selector is not a good idea actually.

Something like this perhaps? http://jsfiddle.net/SHFEb/
<div class="side-content">
<img src="http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png" />
<h2>Here goes a title</h2>
<p>here goes two or more lines for content</p>
</div>
.side-content{float:left}
.side-content img{float:left}
.side-content h2{color:#2D101F;font-family:georgia}
.side-content p{padding-bottom:20px;font-size:11px;line-height:13px}

Something more semantic:
<article>
<img src="http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png" alt="">
<h1>Here goes a title</h1>
<p>here goes two or more lines for content</p>
</article>
Which would be appropriate if you're using HTML5, since you didn't provide more context I assumed <article> would be the tag to use, but that depends on what your html-code represents in context.
updated jsFiddle: http://jsfiddle.net/nils_r/GjKTG/8/ … but you propably have to adjust it to suite more your needs.

If you wanted your markup to be a bit more semantic you could drop some of the divs as they're not really needed and use a header tag instead of a p tag for the title.
<div style="float: left;">
<img src="http://b.dryicons.com/images/icon_sets/shine_icon_set/png/48x48/light_bulb.png">
<h2>Here goes a title</h2>
<p>here goes two or more lines for content</p>
</div>
You can then apply styles directly these elements:
div img {float:left;width:50px;}
div h2 {
color:#2D101F;
font-family: georgia;
}
div p {
font-size: 11px;
line-height: 13px;
}

Related

Set background image using css

Here i upload the picture, i want to put my image to the left side of Food and Travel text
.block-title h3 {
color: #151515;
font-family: 'Montserrat', sans-serif;
font-size: 36px;
font-weight: 700;
line-height: 1.4;
letter-spacing: -0.9px;
margin-bottom: 24px;
margin-top: 50px;
padding-bottom: 13px;
position: relative;
text-align: center;
}
<div class="col-lg-12">
<p>
<center><img src="#">
<div class="block-title">
<h3>Food & Travel</h3>
</div>
</center>
</p>
</div>
You just need to add that line of CSS
div.block-title { display: inline-block; }
<div class="col-lg-12">
<p>
<center><img src="#">
<div class="block-title">
<h3>Food & Travel</h3>
</div>
</center>
</p>
</div>
I would change your HTML a little bit:
<div class="col-lg-12">
<div class="block-title">
<img class="image" src="https://image.flaticon.com/icons/png/512/45/45260.png">
<h3 class="title">Food & Travel</h3>
</div>
</div>
Some observations about your HTML:
Since the creation of CSS, it is considered a bad practice to use styling elements inside HTML, like center. HTML should hold only content and CSS styles. center in HTML can be, in most cases, easily replaced by text-align: center in CSS;
Avoid giving styles to a tag (as you did with H3). It is always better to give a class for each individual element you want to style. For example, you can give a class to your image and to your header, as I did on the example above.
Float, as mentioned by some users here, is barely a good option. I would not recommend it.
I'd go for using Flexbox on the container (block-title). It is the better option and the most accurate.
Your container would be something like
.block-title {
display: flex;
justify-content: center;
align-items: center;
}
... and the magic is done!
Here is an example using flexbox:
https://codepen.io/annabranco/pen/mzEXGv
Another option if you are not comfortable with using Flebox yet, it's to give the H3 a display: inline. By default, all header force a line break (they have display: block). If you change it to display: inline you force the other elements to be displayed in the same line as your header.
In this case you would need to play around with vertical-align to find the exact spot where your text would be centered to the image.
.title {
display: inline;
(..)
}
.image {
vertical-align: -25px; //negative values go up and positive down.
}
Here is an another example, using inline:
https://codepen.io/annabranco/pen/yRJvQa

CSS - Floating Two Divs Next To Eachother, Instead They Go Underneath Eachother

I'm trying to position two divs next to eachother, and keeping the mobile visitors in mind.
The problem: Instead of floating next to eachother, when there's a good amount of text used in the div, it goes underneath.
Here's an example:
.codeblock {
width:500px;
}
.left{
float: left;
}
<div class="codeblock">
<img src="https://placehold.it/307x322" class="left" style="width:125px">
<div class="left">
<h3>Some title</h3>
<p>Some text with a long explanation of how buildings don't make the Earth any heavier because all the materials were already on it.</p>
</div>
</div>
Why is this happening? Is there a solution, without using fixed values (excluding the image style width)?
Float only the image
.codeblock {
width:500px;
}
.left{
float: left;
margin-right:10px;
}
<div class="codeblock">
<img src="https://placehold.it/307x322" class="left" style="width:125px">
<div >
<h3>Some title</h3>
<p>Some text with a long explanation of how buildings don't make the Earth any heavier because all the materials were already on it.</p>
</div>
</div>
Another option would be to use flexbox instead of float. It will be a little bit more work, but it is a new feature and always good to try new things.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
UPDATE
Like this: no class. You inform the main class that it is flexbox and its son will have a padding do separate them.
.codeblock {
display: flex;
width:500px;
}
.codeblock > * {
padding: 0 10px;
}
<div class="codeblock">
<img src="https://placehold.it/307x322">
<div >
<h3>Some title</h3>
<p>Some text with a long explanation of how buildings don't make the Earth any heavier because all the materials were already on it.</p>
</div>
</div>
Considering mobile users I would do this that way with flex-wrap and min values for content
.codeblock {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
max-width:500px;
}
.codeblock>img {
flex: 0 0 125px;
width: 125px;
height: auto;
margin-right: 20px;
}
.codeblock>div {
flex: 1 1 200px;
min-width: 200px;
}
<div class="codeblock">
<img src="https://placehold.it/307x322">
<div>
<h3>Some title</h3>
<p>Some text with a long explanation of how buildings don't make the Earth any heavier because all the materials were already on it.</p>
</div>
</div>

How to put h1 and text in headers on the same line without inline HTML

I just have started with HTML and just basically trying around. So I have found some (Quite alot) stackoverflow where people are wondering the same and I just found out that alot of people using inline and my thought was if there is a way to maybe do it without a inline and I haven't come to any answers and here iam!
I have tried to do so far:
.right {
float: right;
}
<header>
<h1>Left Text</h1>
<span class="right">Right Text</span>
</header>
and it ends up pretty bad I would say
Picture of how it looks like
Anyone has any idea?
<h1> tag in HTML is display: BLOCK element by default, So it comes with a new line always. If you want <span> tag to be displayed on the same line just change the <h1> tag display to inline.
<h1 style="display: inline;"> Left Text </h1>
I think this is what you are looking for.
<h1>Left Text
<span style="float:right">Right Text</span>
</h1>
* {
margin: 0;
padding: 0;
}
header {
display: flex;
align-items: center;
}
.left {
float: left;
margin-right: 20px;
}
.Right {
float: right;
}
<header>
<h1 class="left">Left Text </h1>
<span class="Right">Right Text</span>
</header>

How to set "text-align" to its default value (disable "text-align" inheritance)

In a specific part on my document (a novel) I have a block of text that most be centered but not its content. For this, I used:
<div class="center">
<div class="inline-block">
<p class="left">Some text</p>
<p class="left">More text</p>
<p class="left">Even more text</p>
</div>
</div>
<style>
.center {
text-align: center;
}
.inline-block {
display: inline-block;
width: /* sometimes I specify it, others I let the biggest paragraph do it by itself */;
}
.left {
text-align: left;
}
</style>
Overall, I am satisfied with this solution. But, as you can see, I need to use the class “.left” to avoid paragraphs to inherit the centered value of their parent div. The thing is that I don’t want to set the justification (justified/left) of my document; I want the user (or the default browser/app/epub reader/etc.) to set it instead. The problem is that if I don’t add this class “.left”, the paragraphs will inherit the centered value.
Is there a way to make this paragraphs have a “default” value (either justified or left)? ¿Can I achieve the same overall design using other elements/styles? Can I disable the inheritance for this paragraphs by any means?
I have tried things that included “transform: translateX(-50%)”, “display: table”, setting the paragraphs to “value: initial;”, mixing pseudo-classes/attribute selectors… Basically, everything I could to see if I can trick my way through. So far nothing works. Any ideas?
You can also use initial
.center {
text-align: center;
}
.rtl {
direction: rtl;
}
.inline-block {
display: inline-block;
width: /* sometimes I specify it, others I let the biggest paragraph do it by itself */
;
border: solid;
}
.left {
text-align: initial;
}
<div class="center">
<div class="inline-block">
<p class="left">Some text</p>
<p class="left">More text</p>
<p class="left">Even more text</p>
</div>
</div>
<hr/>Test when direction is the otherway round...
<div class="center rtl">
<div class="inline-block">
<p class="left">Some text</p>
<p class="left">More text</p>
<p class="left">Even more text</p>
</div>
</div>
https://developer.mozilla.org/en-US/docs/Web/CSS/initial
The initial CSS keyword applies the initial value of a property to an element. It can be applied to any CSS property, including the CSS shorthand all.
Try This:
.inline-block p {
text-align: initial;
}
.center {
text-align: center;
}
.inline-block p {
text-align: initial;
}
<div class="center">Center
<div class="inline-block">
<p class="left">Some text</p>
<p class="left">More text</p>
<p class="left">Even more text</p>
</div>
</div>

Bootstrap H1 Aligning Problems

I need help getting my H1s to align. My code is below. An image of it is also below. I would like for the Spencer Hiltbrand bit at the top to be all the way to the right. The Beautiful Websites, Inspiring Photography part the same it is now. I am using Bootstrap.
Homepage:
.intro {
color: white;
font-weight: 600;
text-align: left;
font-size: 80px;
margin-left: 45px !important;
padding-top: 380px;
}
.name {
text-align: right !important;
}
<!-- Intro Section -->
<section id="intro" class="intro-section">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="name"><span class="red">Spencer</span> Hiltbrand</h4>
<h1 class="intro"><span class="red">Beautiful</span> Websites, <br>and <span class="red">Inspiring</span> Photography</h1>
</div>
</div>
</div>
</section>
The following line of HTMl you have is invalid:
<h1 class="name"><span class="red">Spencer</span> Hiltbrand</h4>
You're starting with an h1 tag but never closing it because you try to close it with an h4 tag. This may be causing part of your issue if styles aren't working.
To answer your actual question, an easy way to get your top brand/text to the right is simply to use float: right instead of text-align: right.
If you need to align the heading all to the right, then you need to get rid of container, row and col-lg-12 because they introduce padding and margins. The heading's margin-top has been changed little from the top. Please have a look at the HTML, CSS and its working demo.
HTML
<!-- Intro Section -->
<section id="intro" class="intro-section">
<div>
<h1 class="name"><span class="red">Spencer</span> Hiltbrand</h1>
<h1 class="intro"><span class="red">Beautiful</span> Websites, <br>and <span class="red">Inspiring</span> Photography</h1>
</div>
</section>
CSS
.intro {
color: white;
font-weight: 600;
text-align: left;
font-size: 80px;
margin-left: 45px !important;
padding-top: 380px;
}
.name {
text-align: right;
margin: 5px 0 0 0;
}
Still if you have to make use of bootstrap's container, row and col-*, then you need to modify the existing html.