horizontal align image and paragraph - html

I am trying to align my image with a title and a subtitle in a web page. I would like the image to be at the left and the title with its subtitle centered at the middle.
<header>
<div class="title_div">
<img src="pictures/logo.png" alt="logo"/>
<p>
<h1>Title</h1>
<h2>Subtitle</h2>
</p>
</div>
</header>
I tried to use the float:left attribute but the image goes out from the header... I saw that because I use the following css attributes:
header{
background:#f1f1f1;
border: black solid;
}
I would like to reach something like https://www.ted.com/ but with a subtitle under "Ideas worth spreading".

Using separate div for your logo and your text makes it much cleaner:
<header>
<div class="logo">
<img src="pictures/logo.png" alt="logo"/>
</div>
<div class="title_div">
<h1>Title</h1>
<h2>Subtitle</h2>
</div>
</header>
CSS:
header{
float:left;
}
.logo{
float:left;
margin:20px;
}
.title_div{
float:left;
}
Note: Just a side note that you have very improper use of HTML. P tag is for paragraph, H1 is for headings, so you cant put heading inside a P tag. Re-structure your HTML.

This solution is similar to #user45250's but I'm using a vertical alignment technique for mutli-line text in respect to the image.
<header class="cf">
<img src="http://placehold.it/150x100" alt="logo">
<div class="title">
<h2>Title</h2>
<strong>Subtitle</strong>
</div>
</header>
/* Micro Clearfix - http://nicolasgallagher.com/micro-clearfix-hack/ */
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
header {
background: #f1f1f1;
border: black solid;
}
.title {
height: 100px;
display: table-cell;
text-align: center;
vertical-align: middle;
}
img, .title {
float: left;
}
I have used the micro clearfix as <header> will collapse as it's child elements have been floated.
JSFiddle

you can easily do this with the table display properties:
html clean up and valid ...
header {
display:table;
width:100%;
text-align:center;/* or was it just vertical-align ? */
border:1px solid
}
header>div {
display:table-cell;
vertical-align:middle;/* or was it just text-align ? */
}
.title_div {
width:1%;
}
<header>
<div class="title_div">
<img src="http://dummyimage.com/100x100/&text=logo" alt="logo" />
</div>
<div>
<h1>Title</h1>
<h2>Subtitle</h2>
</div>
</header>

Related

CSS overlap div over another div with a relative position / inline-block?

I have an <h3> element which I need to keep at width:100% so it remains clickable from JavaScript I have, but at the same time, I need the description to be positioned on the right side of the title in one line instead of the next line which usually works fine with display:inline-block, but not while the title keeps 100% width, obviously pushing to a new line.
h3.main.title {
width:100%
}
.main.title {
display:inline-block
}
.description {
display:inline-block
}
<div class="header">
<h3 class="main title">Category</h3>
<div class="description">
<p>Description</p>
</div>
</div>
https://jsfiddle.net/kk5he25q/
Any way I can achieve this?
If you really need to keep h3 at 100%, you can set the position of the description as absolute (don't forget to make the position of the parent div relative). However, keep in mind that h3 and description may overlap. See your updated jsfiddle here: https://jsfiddle.net/kk5he25q/1/
h3.main.title{width:100%; position:relative;}
.main.title {display:block;}
.description{display:block; position:absolute;right:10px; top:7px}
You could use float.
h3.main.title {
width: 100%;
}
.description {
float: right;
margin: 0;
}
<div class="header">
<p class="description">Description</p>
<h3 class="main title">Category</h3>
</div>
Something like this ?
*{
margin:0;padding:0;
}
.header {
display: flex;
}
h3 {
flex: 1;
border: 1px solid;
text-align: center;
padding:15px;
}
.description {
padding:15px;
border: 1px solid;
}
<div class="header">
<h3 class="main title">Category</h3>
<div class="description">
<p>Description</p>
</div>
</div>

Centering an Image

So this is probably an easy answer, but my image which is supposed to be sitting in the middle of the page is slightly off and it's irritating me a lot.
HTML:
</section>
<section class="img_section">
<div>
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
CSS:
img section{
float:center;
text-align:center;
}
I've tried removing float, and taking it out of a section entirely, but it won't budge.
Any help would be amazing
there's no such thing as float: center;
also img section doesn't style image within section.
you could try either:
1:
img {
display: block;
margin: 0 auto;
}
2:
section div {
text-align: center;
}
section div img {
display: inline-block;
}
depending on your other styles
This is simple to answer.
#div1 {
text-align: center;
}
</section>
<section class="img_section">
<div id="div1">
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
The text-align: center; property is going to make all the elements inside the div centered. Also, it is better if you select the div with a specific class or ID. This is going to go on all div elements on the page.
this is much more simple. just include center tag as
<center>
<section>
<div>
<img src="images/menu.jpg" alt="menu" align="center">
</div>
</section>
</center>
try in css:
.img_section img{
text-align:center;
}
html:
<section class="img_section">
<img src="images/menu.jpg" alt="menu" align="center">
</section>
I like to use
margin: 0 auto;
for centering

Why does a container inside a container loses its background color when floated?

My "my-works" container is set to a light gray background-color. Inside this container, there are two articles and their container are named as "my-blog" and "my-game"
I floated "my-blog" and "my-game" to left and it loses its background-color.
Why does the background color of the second container ("my-blog" and "my-game") inside "my-works" (the parent container ) does not have the same background color that is set to its parent container?
The html code
<section class="my-works">
<h3> MY WORKS </h3>
<article class="my-blog">
<h4>My Blog</h4>
<img src="blog.png" width="45%">
</article>
<article class="my-game">
<h4>A Tambay Inspired Flappy Bird - Yotni Bird</h4>
<img src="flappybird.png" width="45%">
</article>
</section>
The CSS code
.my-works {
width:100%;
background-color: #F0F0F5;
}
.my-works h3 {
text-align:center;
margin-top: 0;
padding: 20px;
}
.my-blog {
width: 40%;
float:left;
}
.my-game {
width: 40%;
float:left;
}
here's the image
The .my-works div will no longer completely wrap the inner divs, because of the floats applied.
You would need to add some sort of clear or clearfix to the .my-works div
clearfix
.my-works:before,
.my-works:after {
content: "";
display: table;
}
.my-works:after {
clear: both;
}
Or you could add a tag with clear:both; to your html.
clear
<section class="my-works">
<h3> MY WORKS </h3>
<article class="my-blog">
<h4>My Blog</h4>
<img src="blog.png" width="45%">
</article>
<article class="my-game">
<h4>A Tambay Inspired Flappy Bird - Yotni Bird</h4>
<img src="flappybird.png" width="45%">
</article>
<br style="clear:both">
</section>

Centering left-aligned text

I want to center left-aligned text in the yellow box (txtbox).
It looks like that now:
and it want it to be looking like this:
HTML:
<div class="content container small">
<div class="txtbox">
<header style="text-align:left;">
<h2>I AIN'T</h2>
<h2>AN ARTIST.<h2>
<h2>I'M A</h2>
<h2>BEAST!</h2>
</header>
</div>
<footer>
More
</footer>
</div>
CSS:
.container.small {
/* width: (containers) * 0.75; */
width: 900px;
}
.txtbox{
width:800px;
margin:0 auto;
background-color:yellow;
text-align:center;
}
.txtbox header h2{
padding-left: 50px;
}
.txtbox header {
display: grid;
grid-template-columns: 1fr;
width: fit-content;
margin: auto;
background-color: yellow;
text-align: left;
align-items: center;
}
This will put your text in a box. Align the text to the left of the box and fitting the box for the text you have.
Then it will center the box.
This also works when you don't know the width or height.
And remove
style="text-align:left;"
out of the header.
Get rid of the inline style in your
<header style="text-align:left;">
it overrides the stylesheet; leave just
<header>
It took me a little bit to figure out what "centered left-aligned" text mean.
If I understand you correctly, You want the text to be left-aligned on a vertical axis, but centered on the page.
If that is what you want than you can do that by centering your inner header tag like so: width: 132px; margin: 0 auto;
Here's a fiddle to demonstrate:
http://jsfiddle.net/u4sj7/
header h2 { position: relative; left : 30% /* percentage of yellow box */; }
<div class="txtbox">
<div class="centerblock">
<header style="text-align:left;">
<h2>I AIN'T</h2>
<h2>AN ARTIST.<h2>
<h2>I'M A</h2>
<h2>BEAST!</h2>
</header>
</div>
</div>
CSS:
.centerblock {
display:inline-block;
margin:auto;
}
HTML:
<div class="content container small">
<div class="txtbox">
<header>
<h2>I AIN'T</h2>
<h2>AN ARTIST.</h2>
<h2>I'M A</h2>
<h2>BEAST!</h2>
</header>
</div>
<footer> More
</footer>
</div>
Add this to CSS:
.txtbox header {
display: block;
margin: 0 auto;
}

Rotated text behaves strange

I'd like to achieve the following:
but it's bleeding from several wounds:
Can't close #div1 to top.
Can't give dinamic size (%) value to #div2, so like h3 and h4
What I can do so far:
http://jsbin.com/ivemal/30/
The html code:
<ul>
<li>
<div id="div1">
<h3> Title </h3>
<h4> SubTitle </h4>
</div>
<div id="div2">
...
</div>
</li>
<li>
...Same...
</li>
...
</ul>
I think the root cause I can't achieve this, is the rotated text. Can someone guide me, how to handle this problem?
Thanks in advance!
HTML
<div id="outer">
<div id="inner"> <div>
<h3>text</h3>
<h4>text2</h4>
</div>
</div>
<div id="inner2">Other text...</div>
</div>
I changed the span to a div because technically you cannot have block-level elements (i.e. headings) within a span.
CSS
html, body {
margin: 0;
}
#outer {
background: green;
}
#inner {
background:#e3e3e3;
height:100px;
width:50px;
float: left;
}
#inner > div {
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
line-height: 0;
}
#inner2 {
margin-left:70px;
background:#e3e3e3;
}
#inner2::after {
display: table;
content: '';
clear: both;
}
See jsFiddle.
I don't know if I understood correctly, but is this what you want to obtain?
http://jsbin.com/ivemal/38/