2016-02-19 21 views
5

Sto cercando di rendere il testo a sinistra e l'immagine a destra nel div. Ho trovato molti esempi in Stackoverflow in cui l'immagine è rimasta e il testo a destra, quindi ho provato a cambiarli ma non riesco a farlo funzionare. Ho trovato that on stackoverflow ma non ha senso per me.Testo a sinistra e immagine a destra

Ho provato questo JSFIDDLE ma vedrete che non funzionerà.

.main-topic { 
    border: 2px solid green; 
    width: 1541px; 
    margin: 0 auto; 
    clear: both; 
} 

.left-text{ 
    vertical-align:middle; 
} 

.right-picture{ 
    float: right; 
} 

.right-picture > img{ 
    display: block; 
} 

.clear{ 
    clear: both; 
} 

Il mio obiettivo è this (purtroppo non postare un'immagine non ho abbastanza reputazione). Voglio che il testo essere lasciato a fianco sul div delimitata e l'immagine nella parte destra

risposta

2

Basta aggiungere display: flex; a .main-topic per fare il trucco.

E rimuovere i seguenti css. Non ce n'è bisogno ora.

.right-picture{ 
    float: right; 
} 

Working Fiddle

Edit:

Se non necessario per la larghezza e l'altezza statica quindi, rimuovere statica height da section e statico width da .main-topic farà risultato migliore.

Che è width: 1541px; da .main-topic E height: 500px; da section

Updated Fiddle

+0

Ciao ketan, sfortunatamente flex display ha le supporto del browser ss. –

+0

grazie ma c'è spazio tra l'immagine e il bordo div. Voglio che il testo e l'immagine siano direttamente contornati dal bordo –

+0

@LevanTsivadze, perché hai un sacco di larghezza nel genitore e hai dato una larghezza fissa all'immagine. È richiesto 'width: 1541px;' a '.main-topic' – ketan

1

Rimuovere float: right da IMG e mettere float: left in .left testo

html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t border: 0; 
 
\t font-size: 100%; 
 
\t font: inherit; 
 
\t vertical-align: baseline; 
 
} 
 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
\t display: block; 
 
} 
 
body { 
 
\t line-height: 1; 
 
} 
 
ol, ul { 
 
\t list-style: none; 
 
} 
 
blockquote, q { 
 
\t quotes: none; 
 
} 
 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
\t content: ''; 
 
\t content: none; 
 
} 
 
table { 
 
\t border-collapse: collapse; 
 
\t border-spacing: 0; 
 
} 
 

 
ul.topnav { 
 
\t list-style-type: none; 
 
\t /*position: fixed; */ 
 
\t overflow: hidden; 
 
\t margin: 0; 
 
\t padding: 0 0 0 400px; 
 
\t background-color: #110E0C; 
 
    /*width: 100%; 
 
    top: 0; */ 
 
} 
 

 
ul.topnav li { 
 
\t float: left; 
 
} 
 

 
ul.topnav li a { 
 
\t display: block; 
 
\t color: white; 
 
\t text-align: center; 
 
\t text-decoration: none; 
 
\t padding: 14px 30px; 
 
\t text-transform: uppercase; 
 
\t /*background-color: #3F4345;*/ 
 
} 
 

 
ul.topnav li a:hover { 
 
\t background-color: #C8D0D7; 
 
\t color: #D8610C; 
 
\t border-bottom: 1px solid #D8610C; 
 
} 
 

 
ul.topnav li a.active { 
 
\t background-color: #C8D0D7; 
 
\t color: #D8610C; 
 
\t border-bottom: 1px solid #D8610C; 
 
} 
 

 

 
header { 
 
\t width: 100%; 
 
\t padding: 2px; 
 
\t color:black; 
 
\t background-color: #C8D0D7; 
 
\t text-align: center; 
 
\t overflow: hidden; 
 
} 
 

 
header img { 
 
\t 
 
\t height: 150px; 
 
\t width: 150px; 
 
\t margin-left: 900px; 
 
\t float:left; 
 
\t 
 
} 
 
header p { 
 
\t padding: 40px; 
 
} 
 

 
section { 
 
\t background-color: #333333; 
 
\t height: 500px; 
 
} 
 

 
section img { 
 
\t border: 1px solid white; 
 
\t height: 400px; 
 
\t width: 500px; 
 
} 
 

 
section h3 { 
 
\t color: white; 
 
} 
 

 
section p { 
 
\t color: white; 
 
\t height: 170px; 
 
\t width: 600px; 
 
\t padding: 0; 
 
\t margin: 0; 
 
\t line-height: 20px; 
 
\t vertical-align: top; 
 
} 
 

 
.main-topic { 
 
\t border: 2px solid green; 
 
\t width: 1541px; 
 
\t margin: 0 auto; 
 
\t clear: both; 
 
} 
 

 
.left-text{ 
 
\t vertical-align:middle; 
 
    float:left; 
 
} 
 

 
.right-picture{ 
 

 
} 
 

 
.right-picture > img{ 
 
\t display: block; 
 
} 
 

 
.clear{ 
 
\t clear: both; 
 
}
\t <section> 
 
\t \t <div class="main-topic"> 
 
\t \t \t <div class="left-text"> 
 
\t \t \t \t <h3>Deadpool Fans Petition 'SNL' for Superhero to Host</h3> 
 
\t \t \t \t <p> 
 
\t \t \t \t \t Deadpool fans want its superhero to host Saturday Night Live — Deadpool, that is, not Ryan Reynolds. Fans of the Merc With a Mouth, who led the Marvel film to a history-making debut at the box office, have launched a Change.org petition calling for the antihero to host an upcoming episode of the NBC sketch comedy show. "We've all seen the trailers, the magazine covers, the viral videos, and the billboards by now, so what's left for Deadpool (Ryan Reynolds) to do?" creator Andrew Stege asks in the petition, which is directed to SNL, creator Lorne Michaels, parent. 
 
\t \t \t \t </p> 
 
\t \t \t </div> 
 
\t \t \t <div class="right-picture"> 
 
\t \t \t \t <img src="http://cdn3.whatculture.com/wp-content/uploads/2015/10/f5S3dvUb.jpg"> 
 
\t \t \t </div> 
 
\t \t \t <div class="clear"></div> 
 
\t \t <div> 
 
\t </section>

+0

grazie ma c'è spazio tra l'immagine e il bordo div –

0

ne dite di questo: http://jsbin.com/yenuxajode

solo galleggiare div testo a sinistra con la larghezza del 50% (o quello che volete) e sposta l'immagine div verso destra con la larghezza rimanente. Fai in modo che img abbia larghezza massima: 100%; per mantenere tutto sulla pagina.

0

Si prega di provare a questo, penso che sarà il lavoro

html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t border: 0; 
 
\t font-size: 100%; 
 
\t font: inherit; 
 
\t vertical-align: baseline; 
 
} 
 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
\t display: block; 
 
} 
 
body { 
 
\t line-height: 1; 
 
} 
 
ol, ul { 
 
\t list-style: none; 
 
} 
 
blockquote, q { 
 
\t quotes: none; 
 
} 
 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
\t content: ''; 
 
\t content: none; 
 
} 
 
table { 
 
\t border-collapse: collapse; 
 
\t border-spacing: 0; 
 
} 
 

 
ul.topnav { 
 
\t list-style-type: none; 
 
\t /*position: fixed; */ 
 
\t overflow: hidden; 
 
\t margin: 0; 
 
\t padding: 0 0 0 400px; 
 
\t background-color: #110E0C; 
 
    /*width: 100%; 
 
    top: 0; */ 
 
} 
 

 
ul.topnav li { 
 
\t float: left; 
 
} 
 

 
ul.topnav li a { 
 
\t display: block; 
 
\t color: white; 
 
\t text-align: center; 
 
\t text-decoration: none; 
 
\t padding: 14px 30px; 
 
\t text-transform: uppercase; 
 
\t /*background-color: #3F4345;*/ 
 
} 
 

 
ul.topnav li a:hover { 
 
\t background-color: #C8D0D7; 
 
\t color: #D8610C; 
 
\t border-bottom: 1px solid #D8610C; 
 
} 
 

 
ul.topnav li a.active { 
 
\t background-color: #C8D0D7; 
 
\t color: #D8610C; 
 
\t border-bottom: 1px solid #D8610C; 
 
} 
 

 

 
header { 
 
\t width: 100%; 
 
\t padding: 2px; 
 
\t color:black; 
 
\t background-color: #C8D0D7; 
 
\t text-align: center; 
 
\t overflow: hidden; 
 
} 
 

 
header img { 
 
\t 
 
\t height: 150px; 
 
\t width: 150px; 
 
\t margin-left: 900px; 
 
\t float:left; 
 
\t 
 
} 
 
header p { 
 
\t padding: 40px; 
 
} 
 

 
section { 
 
\t background-color: #333333; 
 
\t height: 500px; 
 
} 
 

 
section img { 
 
\t border: 1px solid white; 
 
\t height: 400px; 
 
\t width: 500px; 
 
} 
 

 
section h3 { 
 
\t color: white; 
 
} 
 

 
section p { 
 
\t color: white; 
 
\t height: 170px; 
 
\t width: 600px; 
 
\t padding: 0; 
 
\t margin: 0; 
 
\t line-height: 20px; 
 
\t vertical-align: top; 
 
} 
 

 
.main-topic { 
 
\t border: 2px solid green; 
 
\t width: 1102px; 
 
\t margin: 0 auto; 
 
\t clear: both; 
 
} 
 

 
.left-text{ 
 
\t vertical-align:middle; 
 
    float:left; 
 
} 
 

 
.right-picture{ 
 

 
} 
 

 
.right-picture > img{ 
 
\t display: block; 
 
} 
 

 
.clear{ 
 
\t clear: both; 
 
}
<section> 
 
\t \t <div class="main-topic"> 
 
\t \t \t <div class="left-text"> 
 
\t \t \t \t <h3>Deadpool Fans Petition 'SNL' for Superhero to Host</h3> 
 
\t \t \t \t <p> 
 
\t \t \t \t \t Deadpool fans want its superhero to host Saturday Night Live — Deadpool, that is, not Ryan Reynolds. Fans of the Merc With a Mouth, who led the Marvel film to a history-making debut at the box office, have launched a Change.org petition calling for the antihero to host an upcoming episode of the NBC sketch comedy show. "We've all seen the trailers, the magazine covers, the viral videos, and the billboards by now, so what's left for Deadpool (Ryan Reynolds) to do?" creator Andrew Stege asks in the petition, which is directed to SNL, creator Lorne Michaels, parent. 
 
\t \t \t \t </p> 
 
\t \t \t </div> 
 
\t \t \t <div class="right-picture"> 
 
\t \t \t \t <img src="http://cdn3.whatculture.com/wp-content/uploads/2015/10/f5S3dvUb.jpg"> 
 
\t \t \t </div> 
 
\t \t \t <div class="clear"></div> 
 
\t \t <div> 
 
\t </section>

+1

Funziona Perfetto per me Grazie Uomo –

0

I updated your fiddle, rimosso alcuni/impostazioni di altezza e larghezza rettificato le seguenti regole CSS, così ora si guarda bene

.main-topic { 
    display: table; 
    border: 2px solid green; 
    margin: 0 auto; 
} 

.left-text{ 
    display: table-cell; 
    vertical-align:middle; 
} 

.right-picture{ 
    display: table-cell; 
} 
Problemi correlati